FormHelperTest.php 327 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Collection\Collection;
  17. use Cake\Core\Configure;
  18. use Cake\Form\Form;
  19. use Cake\Http\ServerRequest;
  20. use Cake\ORM\Entity;
  21. use Cake\ORM\Table;
  22. use Cake\Routing\Router;
  23. use Cake\TestSuite\TestCase;
  24. use Cake\Utility\Security;
  25. use Cake\Validation\Validator;
  26. use Cake\View\Form\EntityContext;
  27. use Cake\View\Helper\FormHelper;
  28. use Cake\View\View;
  29. use Cake\View\Widget\WidgetLocator;
  30. /**
  31. * Test stub.
  32. */
  33. class Article extends Entity
  34. {
  35. }
  36. /**
  37. * Contact class
  38. */
  39. class ContactsTable extends Table
  40. {
  41. /**
  42. * Default schema
  43. *
  44. * @var array
  45. */
  46. protected $_schema = [
  47. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  48. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  49. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  50. 'phone' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  51. 'password' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  52. 'published' => ['type' => 'date', 'null' => true, 'default' => null, 'length' => null],
  53. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  54. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  55. 'age' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => null],
  56. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  57. ];
  58. /**
  59. * Initializes the schema
  60. *
  61. * @param array $config
  62. * @return void
  63. */
  64. public function initialize(array $config)
  65. {
  66. $this->setSchema($this->_schema);
  67. }
  68. }
  69. /**
  70. * ValidateUser class
  71. */
  72. class ValidateUsersTable extends Table
  73. {
  74. /**
  75. * schema method
  76. *
  77. * @var array
  78. */
  79. protected $_schema = [
  80. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  81. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  82. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  83. 'balance' => ['type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2],
  84. 'cost_decimal' => ['type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3],
  85. 'null_decimal' => ['type' => 'decimal', 'null' => false, 'length' => null, 'precision' => null],
  86. 'ratio' => ['type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6],
  87. 'population' => ['type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0],
  88. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  89. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  90. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  91. ];
  92. /**
  93. * Initializes the schema
  94. *
  95. * @param array $config
  96. * @return void
  97. */
  98. public function initialize(array $config)
  99. {
  100. $this->setSchema($this->_schema);
  101. }
  102. }
  103. /**
  104. * FormHelperTest class
  105. *
  106. * @property \Cake\View\Helper\FormHelper $Form
  107. * @property \Cake\View\View $View
  108. */
  109. class FormHelperTest extends TestCase
  110. {
  111. /**
  112. * Fixtures to be used
  113. *
  114. * @var array
  115. */
  116. public $fixtures = ['core.Articles', 'core.Comments'];
  117. /**
  118. * Do not load the fixtures by default
  119. *
  120. * @var bool
  121. */
  122. public $autoFixtures = false;
  123. /**
  124. * @var array
  125. */
  126. protected $article = [];
  127. /**
  128. * setUp method
  129. *
  130. * @return void
  131. */
  132. public function setUp()
  133. {
  134. parent::setUp();
  135. Configure::write('Config.language', 'eng');
  136. Configure::write('App.base', '');
  137. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  138. $request = new ServerRequest([
  139. 'webroot' => '',
  140. 'base' => '',
  141. 'url' => '/articles/add',
  142. 'params' => [
  143. 'controller' => 'articles',
  144. 'action' => 'add',
  145. ],
  146. ]);
  147. $this->View = new View($request);
  148. $this->Form = new FormHelper($this->View);
  149. $this->dateRegex = [
  150. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  151. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  152. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  153. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  154. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  155. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  156. ];
  157. $this->article = [
  158. 'schema' => [
  159. 'id' => ['type' => 'integer'],
  160. 'author_id' => ['type' => 'integer', 'null' => true],
  161. 'title' => ['type' => 'string', 'null' => true],
  162. 'body' => 'text',
  163. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  164. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  165. ],
  166. 'required' => [
  167. 'author_id' => true,
  168. 'title' => true,
  169. ],
  170. ];
  171. Security::setSalt('foo!');
  172. Router::connect('/:controller', ['action' => 'index']);
  173. Router::connect('/:controller/:action/*');
  174. }
  175. /**
  176. * tearDown method
  177. *
  178. * @return void
  179. */
  180. public function tearDown()
  181. {
  182. parent::tearDown();
  183. unset($this->Form, $this->Controller, $this->View);
  184. }
  185. /**
  186. * Test construct() with the templates option.
  187. *
  188. * @return void
  189. */
  190. public function testConstructTemplatesFile()
  191. {
  192. $helper = new FormHelper($this->View, [
  193. 'templates' => 'htmlhelper_tags',
  194. ]);
  195. $result = $helper->control('name');
  196. $this->assertContains('<input', $result);
  197. }
  198. /**
  199. * Test that when specifying custom widgets the config array for that widget
  200. * is overwritten instead of merged.
  201. *
  202. * @return void
  203. */
  204. public function testConstructWithWidgets()
  205. {
  206. $config = [
  207. 'widgets' => [
  208. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select'],
  209. ],
  210. ];
  211. $helper = new FormHelper($this->View, $config);
  212. $locator = $helper->getWidgetLocator();
  213. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  214. }
  215. /**
  216. * Test that when specifying custom widgets config file and it should be
  217. * added to widgets array. WidgetLocator will load widgets in constructor.
  218. *
  219. * @return void
  220. */
  221. public function testConstructWithWidgetsConfig()
  222. {
  223. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  224. $locator = $helper->getWidgetLocator();
  225. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  226. }
  227. /**
  228. * Test setting the widget locator
  229. *
  230. * @return void
  231. */
  232. public function testSetAndGetWidgetLocator()
  233. {
  234. $helper = new FormHelper($this->View);
  235. $locator = new WidgetLocator($helper->templater(), $this->View);
  236. $helper->setWidgetLocator($locator);
  237. $this->assertSame($locator, $helper->getWidgetLocator());
  238. }
  239. /**
  240. * Test overridding grouped input types which controls generation of "for"
  241. * attribute of labels.
  242. *
  243. * @return void
  244. */
  245. public function testConstructWithGroupedInputTypes()
  246. {
  247. $helper = new FormHelper($this->View, [
  248. 'groupedInputTypes' => ['radio'],
  249. ]);
  250. $result = $helper->control('when', ['type' => 'datetime']);
  251. $this->assertContains('<label for="when">When</label>', $result);
  252. }
  253. /**
  254. * Test registering a new widget class and rendering it.
  255. *
  256. * @return void
  257. */
  258. public function testAddWidgetAndRenderWidget()
  259. {
  260. $data = [
  261. 'val' => 1,
  262. ];
  263. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  264. $this->Form->addWidget('test', $mock);
  265. $mock->expects($this->once())
  266. ->method('render')
  267. ->with($data)
  268. ->will($this->returnValue('HTML'));
  269. $result = $this->Form->widget('test', $data);
  270. $this->assertEquals('HTML', $result);
  271. }
  272. /**
  273. * Test that secureFields() of widget is called after calling render(),
  274. * not before.
  275. *
  276. * @return void
  277. */
  278. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  279. {
  280. $data = [
  281. 'val' => 1,
  282. 'name' => 'test',
  283. ];
  284. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  285. $this->Form->addWidget('test', $mock);
  286. $mock->expects($this->at(0))
  287. ->method('render')
  288. ->with($data)
  289. ->will($this->returnValue('HTML'));
  290. $mock->expects($this->at(1))
  291. ->method('secureFields')
  292. ->with($data)
  293. ->will($this->returnValue(['test']));
  294. $result = $this->Form->widget('test', $data + ['secure' => true]);
  295. $this->assertEquals('HTML', $result);
  296. }
  297. /**
  298. * Test that empty string is not added to secure fields list when
  299. * rendering input widget without name.
  300. *
  301. * @return void
  302. */
  303. public function testRenderingWidgetWithEmptyName()
  304. {
  305. $this->assertEquals([], $this->Form->fields);
  306. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  307. $this->assertEquals('<select name=""></select>', $result);
  308. $this->assertEquals([], $this->Form->fields);
  309. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  310. $this->assertEquals('<select name="0"></select>', $result);
  311. $this->assertEquals(['0'], $this->Form->fields);
  312. }
  313. /**
  314. * Test registering an invalid widget class.
  315. *
  316. * @return void
  317. */
  318. public function testAddWidgetInvalid()
  319. {
  320. $this->expectException(\RuntimeException::class);
  321. $mock = new \StdClass();
  322. $this->Form->addWidget('test', $mock);
  323. $this->Form->widget('test');
  324. }
  325. /**
  326. * Test adding a new context class.
  327. *
  328. * @return void
  329. */
  330. public function testAddContextProvider()
  331. {
  332. $context = null;
  333. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  334. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  335. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  336. $this->assertSame($context, $data['entity']);
  337. return $stub;
  338. });
  339. $this->Form->create($context);
  340. $result = $this->Form->context();
  341. $this->assertSame($stub, $result);
  342. }
  343. /**
  344. * Test replacing a context class.
  345. *
  346. * @return void
  347. */
  348. public function testAddContextProviderReplace()
  349. {
  350. $entity = new Article();
  351. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  352. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  353. return $stub;
  354. });
  355. $this->Form->create($entity);
  356. $result = $this->Form->context();
  357. $this->assertSame($stub, $result);
  358. }
  359. /**
  360. * Test overriding a context class.
  361. *
  362. * @return void
  363. */
  364. public function testAddContextProviderAdd()
  365. {
  366. $entity = new Article();
  367. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  368. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  369. if ($data['entity'] instanceof Entity) {
  370. return $stub;
  371. }
  372. });
  373. $this->Form->create($entity);
  374. $result = $this->Form->context();
  375. $this->assertSame($stub, $result);
  376. }
  377. /**
  378. * Test adding an invalid context class.
  379. *
  380. * @return void
  381. */
  382. public function testAddContextProviderInvalid()
  383. {
  384. $this->expectException(\RuntimeException::class);
  385. $this->expectExceptionMessage('Context providers must return object implementing Cake\View\Form\ContextInterface. Got "stdClass" instead.');
  386. $context = null;
  387. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  388. return new \StdClass();
  389. });
  390. $this->Form->create($context);
  391. }
  392. /**
  393. * Provides context options for create().
  394. *
  395. * @return array
  396. */
  397. public function contextSelectionProvider()
  398. {
  399. $entity = new Article();
  400. $collection = new Collection([$entity]);
  401. $emptyCollection = new Collection([]);
  402. $arrayObject = new \ArrayObject([]);
  403. $data = [
  404. 'schema' => [
  405. 'title' => ['type' => 'string'],
  406. ],
  407. ];
  408. $form = new Form();
  409. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  410. return [
  411. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  412. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  413. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  414. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  415. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  416. 'form' => [$form, 'Cake\View\Form\FormContext'],
  417. 'none' => [null, 'Cake\View\Form\NullContext'],
  418. 'custom' => [$custom, get_class($custom)],
  419. ];
  420. }
  421. /**
  422. * Test default context selection in create()
  423. *
  424. * @dataProvider contextSelectionProvider
  425. * @param mixed $data
  426. * @param string $class
  427. * @return void
  428. */
  429. public function testCreateContextSelectionBuiltIn($data, $class)
  430. {
  431. $this->loadFixtures('Articles');
  432. $this->Form->create($data);
  433. $this->assertInstanceOf($class, $this->Form->context());
  434. }
  435. /**
  436. * Test deprecated default context selection in create()
  437. *
  438. * @group deprecated
  439. * @return void
  440. */
  441. public function testCreateContextSelectionBuiltInDeprecated()
  442. {
  443. $this->loadFixtures('Articles');
  444. $class = 'Cake\View\Form\NullContext';
  445. $this->deprecated(function () {
  446. $this->Form->create(false);
  447. });
  448. $this->assertInstanceOf($class, $this->Form->context());
  449. $this->deprecated(function () {
  450. $this->Form->create('');
  451. });
  452. $this->assertInstanceOf($class, $this->Form->context());
  453. }
  454. /**
  455. * Data provider for type option.
  456. *
  457. * @return array
  458. */
  459. public static function requestTypeProvider()
  460. {
  461. return [
  462. // type, method, override
  463. ['post', 'post', 'POST'],
  464. ['put', 'post', 'PUT'],
  465. ['patch', 'post', 'PATCH'],
  466. ['delete', 'post', 'DELETE'],
  467. ];
  468. }
  469. /**
  470. * Test creating file forms.
  471. *
  472. * @return void
  473. */
  474. public function testCreateFile()
  475. {
  476. $encoding = strtolower(Configure::read('App.encoding'));
  477. $result = $this->Form->create(null, ['type' => 'file']);
  478. $expected = [
  479. 'form' => [
  480. 'method' => 'post', 'action' => '/articles/add',
  481. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data',
  482. ],
  483. 'div' => ['style' => 'display:none;'],
  484. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  485. '/div',
  486. ];
  487. $this->assertHtml($expected, $result);
  488. }
  489. /**
  490. * Test creating GET forms.
  491. *
  492. * @return void
  493. */
  494. public function testCreateGet()
  495. {
  496. $encoding = strtolower(Configure::read('App.encoding'));
  497. $result = $this->Form->create(null, ['type' => 'get']);
  498. $expected = ['form' => [
  499. 'method' => 'get', 'action' => '/articles/add',
  500. 'accept-charset' => $encoding,
  501. ]];
  502. $this->assertHtml($expected, $result);
  503. }
  504. /**
  505. * Test explicit method/enctype options.
  506. *
  507. * Explicit method overwrites inferred method from 'type'
  508. *
  509. * @return void
  510. */
  511. public function testCreateExplicitMethodEnctype()
  512. {
  513. $encoding = strtolower(Configure::read('App.encoding'));
  514. $result = $this->Form->create(null, [
  515. 'type' => 'get',
  516. 'method' => 'put',
  517. 'enctype' => 'multipart/form-data',
  518. ]);
  519. $expected = ['form' => [
  520. 'method' => 'put',
  521. 'action' => '/articles/add',
  522. 'enctype' => 'multipart/form-data',
  523. 'accept-charset' => $encoding,
  524. ]];
  525. $this->assertHtml($expected, $result);
  526. }
  527. /**
  528. * Test create() with the templates option.
  529. *
  530. * @return void
  531. */
  532. public function testCreateTemplatesArray()
  533. {
  534. $result = $this->Form->create($this->article, [
  535. 'templates' => [
  536. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  537. ],
  538. ]);
  539. $expected = [
  540. 'form' => [
  541. 'class' => 'form-horizontal',
  542. 'method' => 'post',
  543. 'action' => '/articles/add',
  544. 'accept-charset' => 'utf-8',
  545. ],
  546. ];
  547. $this->assertHtml($expected, $result);
  548. }
  549. /**
  550. * Test create() with the templates option.
  551. *
  552. * @return void
  553. */
  554. public function testCreateTemplatesFile()
  555. {
  556. $result = $this->Form->create($this->article, [
  557. 'templates' => 'htmlhelper_tags',
  558. ]);
  559. $expected = [
  560. 'start form',
  561. 'div' => ['class' => 'hidden'],
  562. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  563. '/div',
  564. ];
  565. $this->assertHtml($expected, $result);
  566. }
  567. /**
  568. * Test that create() and end() restore templates.
  569. *
  570. * @return void
  571. */
  572. public function testCreateEndRestoreTemplates()
  573. {
  574. $this->Form->create($this->article, [
  575. 'templates' => ['input' => 'custom input element'],
  576. ]);
  577. $this->Form->end();
  578. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  579. }
  580. /**
  581. * Test using template vars in various templates used by control() method.
  582. *
  583. * @return void
  584. */
  585. public function testControlTemplateVars()
  586. {
  587. $result = $this->Form->control('text', [
  588. 'templates' => [
  589. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  590. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  591. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  592. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  593. ],
  594. 'templateVars' => [
  595. 'forinput' => 'in-input',
  596. 'forlabel' => 'in-label',
  597. 'forgroup' => 'in-group',
  598. 'forcontainer' => 'in-container',
  599. ],
  600. ]);
  601. $expected = [
  602. 'div' => ['class'],
  603. 'label' => ['for'],
  604. 'Text in-label',
  605. '/label',
  606. 'in-group',
  607. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  608. 'in-container',
  609. '/div',
  610. ];
  611. $this->assertHtml($expected, $result);
  612. }
  613. /**
  614. * Test ensuring template variables work in template files loaded
  615. * during control().
  616. *
  617. * @return void
  618. */
  619. public function testControlTemplatesFromFile()
  620. {
  621. $result = $this->Form->control('title', [
  622. 'templates' => 'test_templates',
  623. 'templateVars' => [
  624. 'forcontainer' => 'container-data',
  625. ],
  626. ]);
  627. $expected = [
  628. 'div' => ['class'],
  629. 'label' => ['for'],
  630. 'Title',
  631. '/label',
  632. 'input' => ['name', 'type' => 'text', 'id'],
  633. 'container-data',
  634. '/div',
  635. ];
  636. $this->assertHtml($expected, $result);
  637. }
  638. /**
  639. * Test using template vars in inputSubmit and submitContainer template.
  640. *
  641. * @return void
  642. */
  643. public function testSubmitTemplateVars()
  644. {
  645. $this->Form->setTemplates([
  646. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  647. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>',
  648. ]);
  649. $result = $this->Form->submit('Submit', [
  650. 'templateVars' => [
  651. 'forinput' => 'in-input',
  652. 'forcontainer' => 'in-container',
  653. ],
  654. ]);
  655. $expected = [
  656. 'div' => ['class'],
  657. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  658. 'in-container',
  659. '/div',
  660. ];
  661. $this->assertHtml($expected, $result);
  662. }
  663. /**
  664. * test the create() method
  665. *
  666. * @dataProvider requestTypeProvider
  667. * @return void
  668. */
  669. public function testCreateTypeOptions($type, $method, $override)
  670. {
  671. $encoding = strtolower(Configure::read('App.encoding'));
  672. $result = $this->Form->create(null, ['type' => $type]);
  673. $expected = [
  674. 'form' => [
  675. 'method' => $method, 'action' => '/articles/add',
  676. 'accept-charset' => $encoding,
  677. ],
  678. 'div' => ['style' => 'display:none;'],
  679. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  680. '/div',
  681. ];
  682. $this->assertHtml($expected, $result);
  683. }
  684. /**
  685. * Test using template vars in Create (formStart template)
  686. *
  687. * @return void
  688. */
  689. public function testCreateTemplateVars()
  690. {
  691. $result = $this->Form->create($this->article, [
  692. 'templates' => [
  693. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  694. ],
  695. 'templateVars' => ['header' => 'headertext'],
  696. ]);
  697. $expected = [
  698. 'h4' => ['class'],
  699. 'headertext',
  700. '/h4',
  701. 'form' => [
  702. 'method' => 'post',
  703. 'action' => '/articles/add',
  704. 'accept-charset' => 'utf-8',
  705. ],
  706. ];
  707. $this->assertHtml($expected, $result);
  708. }
  709. /**
  710. * Test opening a form for an update operation.
  711. *
  712. * @return void
  713. */
  714. public function testCreateUpdateForm()
  715. {
  716. $encoding = strtolower(Configure::read('App.encoding'));
  717. $this->View->setRequest($this->View->getRequest()
  718. ->withRequestTarget('/articles/edit/1')
  719. ->withParam('action', 'edit'));
  720. $this->article['defaults']['id'] = 1;
  721. $result = $this->Form->create($this->article);
  722. $expected = [
  723. 'form' => [
  724. 'method' => 'post',
  725. 'action' => '/articles/edit/1',
  726. 'accept-charset' => $encoding,
  727. ],
  728. 'div' => ['style' => 'display:none;'],
  729. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  730. '/div',
  731. ];
  732. $this->assertHtml($expected, $result);
  733. }
  734. /**
  735. * test create() with automatic url generation
  736. *
  737. * @return void
  738. */
  739. public function testCreateAutoUrl()
  740. {
  741. $encoding = strtolower(Configure::read('App.encoding'));
  742. $this->View->setRequest($this->View->getRequest()
  743. ->withRequestTarget('/articles/delete/10')
  744. ->withParam('action', 'delete'));
  745. $result = $this->Form->create($this->article);
  746. $expected = [
  747. 'form' => [
  748. 'method' => 'post', 'action' => '/articles/delete/10',
  749. 'accept-charset' => $encoding,
  750. ],
  751. 'div' => ['style' => 'display:none;'],
  752. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  753. '/div',
  754. ];
  755. $this->assertHtml($expected, $result);
  756. $this->article['defaults'] = ['id' => 1];
  757. $this->View->setRequest($this->View->getRequest()
  758. ->withRequestTarget('/articles/edit/1')
  759. ->withParam('action', 'delete'));
  760. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  761. $expected = [
  762. 'form' => [
  763. 'method' => 'post',
  764. 'action' => '/articles/edit/1',
  765. 'accept-charset' => $encoding,
  766. ],
  767. 'div' => ['style' => 'display:none;'],
  768. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  769. '/div',
  770. ];
  771. $this->assertHtml($expected, $result);
  772. $this->View->setRequest($this->View->getRequest()
  773. ->withParam('action', 'add'));
  774. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  775. $expected = [
  776. 'form' => [
  777. 'method' => 'post',
  778. 'action' => '/articles/publish/1',
  779. 'accept-charset' => $encoding,
  780. ],
  781. 'div' => ['style' => 'display:none;'],
  782. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  783. '/div',
  784. ];
  785. $this->assertHtml($expected, $result);
  786. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  787. $expected = [
  788. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  789. 'div' => ['style' => 'display:none;'],
  790. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  791. '/div',
  792. ];
  793. $this->assertHtml($expected, $result);
  794. $this->View->setRequest($this->View->getRequest()
  795. ->withParam('controller', 'Pages'));
  796. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  797. $expected = [
  798. 'form' => [
  799. 'method' => 'post', 'action' => '/Pages/signup/1',
  800. 'accept-charset' => $encoding,
  801. ],
  802. 'div' => ['style' => 'display:none;'],
  803. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  804. '/div',
  805. ];
  806. $this->assertHtml($expected, $result);
  807. }
  808. /**
  809. * Test create() with no URL (no "action" attribute for <form> tag)
  810. *
  811. * @return void
  812. */
  813. public function testCreateNoUrl()
  814. {
  815. $result = $this->Form->create(null, ['url' => false]);
  816. $expected = [
  817. 'form' => [
  818. 'method' => 'post',
  819. 'accept-charset' => strtolower(Configure::read('App.encoding')),
  820. ],
  821. 'div' => ['style' => 'display:none;'],
  822. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  823. '/div',
  824. ];
  825. $this->assertHtml($expected, $result);
  826. }
  827. /**
  828. * test create() with a custom route
  829. *
  830. * @return void
  831. */
  832. public function testCreateCustomRoute()
  833. {
  834. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  835. $encoding = strtolower(Configure::read('App.encoding'));
  836. $this->View->setRequest($this->View->getRequest()
  837. ->withParam('controller', 'users'));
  838. $result = $this->Form->create(null, ['url' => ['action' => 'login']]);
  839. $expected = [
  840. 'form' => [
  841. 'method' => 'post', 'action' => '/login',
  842. 'accept-charset' => $encoding,
  843. ],
  844. 'div' => ['style' => 'display:none;'],
  845. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  846. '/div',
  847. ];
  848. $this->assertHtml($expected, $result);
  849. Router::connect(
  850. '/new-article',
  851. ['controller' => 'articles', 'action' => 'myaction'],
  852. ['_name' => 'my-route']
  853. );
  854. $result = $this->Form->create(null, ['url' => ['_name' => 'my-route']]);
  855. $expected = [
  856. 'form' => [
  857. 'method' => 'post', 'action' => '/new-article',
  858. 'accept-charset' => $encoding,
  859. ],
  860. 'div' => ['style' => 'display:none;'],
  861. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  862. '/div',
  863. ];
  864. $this->assertHtml($expected, $result);
  865. }
  866. /**
  867. * test automatic accept-charset overriding
  868. *
  869. * @return void
  870. */
  871. public function testCreateWithAcceptCharset()
  872. {
  873. $result = $this->Form->create(
  874. $this->article,
  875. [
  876. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1',
  877. ]
  878. );
  879. $expected = [
  880. 'form' => [
  881. 'method' => 'post', 'action' => '/articles',
  882. 'accept-charset' => 'iso-8859-1',
  883. ],
  884. 'div' => ['style' => 'display:none;'],
  885. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  886. '/div',
  887. ];
  888. $this->assertHtml($expected, $result);
  889. }
  890. /**
  891. * Test base form URL when url param is passed with multiple parameters (&)
  892. */
  893. public function testCreateQueryStringRequest()
  894. {
  895. $encoding = strtolower(Configure::read('App.encoding'));
  896. $result = $this->Form->create($this->article, [
  897. 'type' => 'post',
  898. 'escape' => false,
  899. 'url' => [
  900. 'controller' => 'controller',
  901. 'action' => 'action',
  902. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  903. ],
  904. ]);
  905. $expected = [
  906. 'form' => [
  907. 'method' => 'post',
  908. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  909. 'accept-charset' => $encoding,
  910. ],
  911. 'div' => ['style' => 'display:none;'],
  912. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  913. '/div',
  914. ];
  915. $this->assertHtml($expected, $result);
  916. $result = $this->Form->create($this->article, [
  917. 'type' => 'post',
  918. 'url' => [
  919. 'controller' => 'controller',
  920. 'action' => 'action',
  921. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  922. ],
  923. ]);
  924. $this->assertHtml($expected, $result);
  925. }
  926. /**
  927. * test that create() doesn't cause errors by multiple id's being in the primary key
  928. * as could happen with multiple select or checkboxes.
  929. *
  930. * @return void
  931. */
  932. public function testCreateWithMultipleIdInData()
  933. {
  934. $encoding = strtolower(Configure::read('App.encoding'));
  935. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  936. $result = $this->Form->create($this->article);
  937. $expected = [
  938. 'form' => [
  939. 'method' => 'post',
  940. 'action' => '/articles/add',
  941. 'accept-charset' => $encoding,
  942. ],
  943. 'div' => ['style' => 'display:none;'],
  944. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  945. '/div',
  946. ];
  947. $this->assertHtml($expected, $result);
  948. }
  949. /**
  950. * test that create() doesn't add in extra passed params.
  951. *
  952. * @return void
  953. */
  954. public function testCreatePassedArgs()
  955. {
  956. $encoding = strtolower(Configure::read('App.encoding'));
  957. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  958. $result = $this->Form->create($this->article, [
  959. 'type' => 'post',
  960. 'escape' => false,
  961. 'url' => [
  962. 'action' => 'edit',
  963. 'myparam',
  964. ],
  965. ]);
  966. $expected = [
  967. 'form' => [
  968. 'method' => 'post',
  969. 'action' => '/articles/edit/myparam',
  970. 'accept-charset' => $encoding,
  971. ],
  972. 'div' => ['style' => 'display:none;'],
  973. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  974. '/div',
  975. ];
  976. $this->assertHtml($expected, $result);
  977. }
  978. /**
  979. * test creating a get form, and get form inputs.
  980. *
  981. * @return void
  982. */
  983. public function testGetFormCreate()
  984. {
  985. $encoding = strtolower(Configure::read('App.encoding'));
  986. $result = $this->Form->create($this->article, ['type' => 'get']);
  987. $expected = ['form' => [
  988. 'method' => 'get', 'action' => '/articles/add',
  989. 'accept-charset' => $encoding,
  990. ]];
  991. $this->assertHtml($expected, $result);
  992. $result = $this->Form->text('title');
  993. $expected = ['input' => [
  994. 'name' => 'title', 'type' => 'text', 'required' => 'required',
  995. ]];
  996. $this->assertHtml($expected, $result);
  997. $result = $this->Form->password('password');
  998. $expected = ['input' => [
  999. 'name' => 'password', 'type' => 'password',
  1000. ]];
  1001. $this->assertHtml($expected, $result);
  1002. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  1003. $result = $this->Form->text('user_form');
  1004. $expected = ['input' => [
  1005. 'name' => 'user_form', 'type' => 'text',
  1006. ]];
  1007. $this->assertHtml($expected, $result);
  1008. }
  1009. /**
  1010. * test get form, and inputs when the model param is false
  1011. *
  1012. * @return void
  1013. */
  1014. public function testGetFormWithFalseModel()
  1015. {
  1016. $encoding = strtolower(Configure::read('App.encoding'));
  1017. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'contact_test'));
  1018. $result = $this->Form->create(null, [
  1019. 'type' => 'get', 'url' => ['controller' => 'contact_test'],
  1020. ]);
  1021. $expected = ['form' => [
  1022. 'method' => 'get', 'action' => '/contact_test/add',
  1023. 'accept-charset' => $encoding,
  1024. ]];
  1025. $this->assertHtml($expected, $result);
  1026. $result = $this->Form->text('reason');
  1027. $expected = [
  1028. 'input' => ['type' => 'text', 'name' => 'reason'],
  1029. ];
  1030. $this->assertHtml($expected, $result);
  1031. }
  1032. /**
  1033. * testFormCreateWithSecurity method
  1034. *
  1035. * Test form->create() with security key.
  1036. *
  1037. * @return void
  1038. */
  1039. public function testCreateWithSecurity()
  1040. {
  1041. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  1042. $encoding = strtolower(Configure::read('App.encoding'));
  1043. $result = $this->Form->create($this->article, [
  1044. 'url' => '/articles/publish',
  1045. ]);
  1046. $expected = [
  1047. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  1048. 'div' => ['style' => 'display:none;'],
  1049. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1050. ['input' => [
  1051. 'type' => 'hidden',
  1052. 'name' => '_csrfToken',
  1053. 'value' => 'testKey',
  1054. 'autocomplete' => 'off',
  1055. ]],
  1056. '/div',
  1057. ];
  1058. $this->assertHtml($expected, $result);
  1059. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1060. $expected['form']['id'] = 'MyForm';
  1061. $this->assertHtml($expected, $result);
  1062. }
  1063. /**
  1064. * testFormCreateGetNoSecurity method
  1065. *
  1066. * Test form->create() with no security key as its a get form
  1067. *
  1068. * @return void
  1069. */
  1070. public function testCreateEndGetNoSecurity()
  1071. {
  1072. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  1073. $article = new Article();
  1074. $result = $this->Form->create($article, [
  1075. 'type' => 'get',
  1076. 'url' => '/contacts/add',
  1077. ]);
  1078. $this->assertNotContains('testKey', $result);
  1079. $result = $this->Form->end();
  1080. $this->assertNotContains('testKey', $result);
  1081. }
  1082. /**
  1083. * test that create() clears the fields property so it starts fresh
  1084. *
  1085. * @return void
  1086. */
  1087. public function testCreateClearingFields()
  1088. {
  1089. $this->Form->fields = ['model_id'];
  1090. $this->Form->create($this->article);
  1091. $this->assertEquals([], $this->Form->fields);
  1092. }
  1093. /**
  1094. * Tests form hash generation with model-less data
  1095. *
  1096. * @return void
  1097. */
  1098. public function testValidateHashNoModel()
  1099. {
  1100. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1101. $fields = ['anything'];
  1102. $result = $this->Form->secure($fields);
  1103. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1104. $this->assertContains($hash, $result);
  1105. }
  1106. /**
  1107. * Tests that hidden fields generated for checkboxes don't get locked
  1108. *
  1109. * @return void
  1110. */
  1111. public function testNoCheckboxLocking()
  1112. {
  1113. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1114. $this->assertSame([], $this->Form->fields);
  1115. $this->Form->checkbox('check', ['value' => '1']);
  1116. $this->assertSame($this->Form->fields, ['check']);
  1117. }
  1118. /**
  1119. * testFormSecurityFields method
  1120. *
  1121. * Test generation of secure form hash generation.
  1122. *
  1123. * @return void
  1124. */
  1125. public function testFormSecurityFields()
  1126. {
  1127. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1128. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1129. $result = $this->Form->secure($fields);
  1130. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1131. $hash .= ':' . 'Model.valid';
  1132. $hash = urlencode($hash);
  1133. $tokenDebug = urlencode(json_encode([
  1134. '',
  1135. $fields,
  1136. [],
  1137. ]));
  1138. $expected = [
  1139. 'div' => ['style' => 'display:none;'],
  1140. ['input' => [
  1141. 'type' => 'hidden',
  1142. 'name' => '_Token[fields]',
  1143. 'value' => $hash,
  1144. 'autocomplete' => 'off',
  1145. ]],
  1146. ['input' => [
  1147. 'type' => 'hidden',
  1148. 'name' => '_Token[unlocked]',
  1149. 'value' => '',
  1150. 'autocomplete' => 'off',
  1151. ]],
  1152. ['input' => [
  1153. 'type' => 'hidden',
  1154. 'name' => '_Token[debug]',
  1155. 'value' => $tokenDebug,
  1156. 'autocomplete' => 'off',
  1157. ]],
  1158. '/div',
  1159. ];
  1160. $this->assertHtml($expected, $result);
  1161. }
  1162. /**
  1163. * testFormSecurityFields method
  1164. *
  1165. * Test debug token is not generated if debug is false
  1166. *
  1167. * @return void
  1168. */
  1169. public function testFormSecurityFieldsNoDebugMode()
  1170. {
  1171. Configure::write('debug', false);
  1172. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1173. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1174. $result = $this->Form->secure($fields);
  1175. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1176. $hash .= ':' . 'Model.valid';
  1177. $hash = urlencode($hash);
  1178. $expected = [
  1179. 'div' => ['style' => 'display:none;'],
  1180. ['input' => [
  1181. 'type' => 'hidden',
  1182. 'name' => '_Token[fields]',
  1183. 'autocomplete' => 'off',
  1184. 'value' => $hash,
  1185. ]],
  1186. ['input' => [
  1187. 'type' => 'hidden',
  1188. 'name' => '_Token[unlocked]',
  1189. 'autocomplete' => 'off',
  1190. 'value' => '',
  1191. ]],
  1192. '/div',
  1193. ];
  1194. $this->assertHtml($expected, $result);
  1195. }
  1196. /**
  1197. * Tests correct generation of number fields for smallint
  1198. *
  1199. * @return void
  1200. */
  1201. public function testTextFieldGenerationForSmallint()
  1202. {
  1203. $this->article['schema'] = [
  1204. 'foo' => [
  1205. 'type' => 'smallinteger',
  1206. 'null' => false,
  1207. 'default' => null,
  1208. 'length' => 10,
  1209. ],
  1210. ];
  1211. $this->Form->create($this->article);
  1212. $result = $this->Form->control('foo');
  1213. $this->assertContains('class="input number"', $result);
  1214. $this->assertContains('type="number"', $result);
  1215. }
  1216. /**
  1217. * Tests correct generation of number fields for tinyint
  1218. *
  1219. * @return void
  1220. */
  1221. public function testTextFieldGenerationForTinyint()
  1222. {
  1223. $this->article['schema'] = [
  1224. 'foo' => [
  1225. 'type' => 'tinyinteger',
  1226. 'null' => false,
  1227. 'default' => null,
  1228. 'length' => 10,
  1229. ],
  1230. ];
  1231. $this->Form->create($this->article);
  1232. $result = $this->Form->control('foo');
  1233. $this->assertContains('class="input number"', $result);
  1234. $this->assertContains('type="number"', $result);
  1235. }
  1236. /**
  1237. * Tests correct generation of number fields for double and float fields
  1238. *
  1239. * @return void
  1240. */
  1241. public function testTextFieldGenerationForFloats()
  1242. {
  1243. $this->article['schema'] = [
  1244. 'foo' => [
  1245. 'type' => 'float',
  1246. 'null' => false,
  1247. 'default' => null,
  1248. 'length' => 10,
  1249. ],
  1250. ];
  1251. $this->Form->create($this->article);
  1252. $result = $this->Form->control('foo');
  1253. $expected = [
  1254. 'div' => ['class' => 'input number'],
  1255. 'label' => ['for' => 'foo'],
  1256. 'Foo',
  1257. '/label',
  1258. ['input' => [
  1259. 'type' => 'number',
  1260. 'name' => 'foo',
  1261. 'id' => 'foo',
  1262. 'step' => 'any',
  1263. ]],
  1264. '/div',
  1265. ];
  1266. $this->assertHtml($expected, $result);
  1267. $result = $this->Form->control('foo', ['step' => 0.5]);
  1268. $expected = [
  1269. 'div' => ['class' => 'input number'],
  1270. 'label' => ['for' => 'foo'],
  1271. 'Foo',
  1272. '/label',
  1273. ['input' => [
  1274. 'type' => 'number',
  1275. 'name' => 'foo',
  1276. 'id' => 'foo',
  1277. 'step' => '0.5',
  1278. ]],
  1279. '/div',
  1280. ];
  1281. $this->assertHtml($expected, $result);
  1282. }
  1283. /**
  1284. * Tests correct generation of number fields for integer fields
  1285. *
  1286. * @return void
  1287. */
  1288. public function testTextFieldTypeNumberGenerationForIntegers()
  1289. {
  1290. $this->getTableLocator()->get('Contacts', [
  1291. 'className' => __NAMESPACE__ . '\ContactsTable',
  1292. ]);
  1293. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1294. $result = $this->Form->control('age');
  1295. $expected = [
  1296. 'div' => ['class' => 'input number'],
  1297. 'label' => ['for' => 'age'],
  1298. 'Age',
  1299. '/label',
  1300. ['input' => [
  1301. 'type' => 'number', 'name' => 'age',
  1302. 'id' => 'age',
  1303. ]],
  1304. '/div',
  1305. ];
  1306. $this->assertHtml($expected, $result);
  1307. }
  1308. /**
  1309. * Tests correct generation of file upload fields for binary fields
  1310. *
  1311. * @return void
  1312. */
  1313. public function testFileUploadFieldTypeGenerationForBinaries()
  1314. {
  1315. $table = $this->getTableLocator()->get('Contacts', [
  1316. 'className' => __NAMESPACE__ . '\ContactsTable',
  1317. ]);
  1318. $table->setSchema(['foo' => [
  1319. 'type' => 'binary',
  1320. 'null' => false,
  1321. 'default' => null,
  1322. 'length' => 1024,
  1323. ]]);
  1324. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1325. $result = $this->Form->control('foo');
  1326. $expected = [
  1327. 'div' => ['class' => 'input file'],
  1328. 'label' => ['for' => 'foo'],
  1329. 'Foo',
  1330. '/label',
  1331. ['input' => [
  1332. 'type' => 'file', 'name' => 'foo',
  1333. 'id' => 'foo',
  1334. ]],
  1335. '/div',
  1336. ];
  1337. $this->assertHtml($expected, $result);
  1338. }
  1339. /**
  1340. * testFormSecurityMultipleFields method
  1341. *
  1342. * Test secure() with multiple row form. Ensure hash is correct.
  1343. *
  1344. * @return void
  1345. */
  1346. public function testFormSecurityMultipleFields()
  1347. {
  1348. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1349. $fields = [
  1350. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1351. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1352. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0',
  1353. ];
  1354. $result = $this->Form->secure($fields);
  1355. $hash = '8670192c3f040bf58680479060b4755b7a5c3596' .
  1356. '%3AModel.0.hidden%7CModel.0.valid%7CModel.1.hidden%7CModel.1.valid';
  1357. $tokenDebug = urlencode(json_encode([
  1358. '',
  1359. $fields,
  1360. [],
  1361. ]));
  1362. $expected = [
  1363. 'div' => ['style' => 'display:none;'],
  1364. ['input' => [
  1365. 'type' => 'hidden',
  1366. 'name' => '_Token[fields]',
  1367. 'value' => $hash,
  1368. 'autocomplete' => 'off',
  1369. ]],
  1370. ['input' => [
  1371. 'type' => 'hidden',
  1372. 'name' => '_Token[unlocked]',
  1373. 'autocomplete' => 'off',
  1374. 'value' => '',
  1375. ]],
  1376. ['input' => [
  1377. 'type' => 'hidden',
  1378. 'name' => '_Token[debug]',
  1379. 'value' => $tokenDebug,
  1380. 'autocomplete' => 'off',
  1381. ]],
  1382. '/div',
  1383. ];
  1384. $this->assertHtml($expected, $result);
  1385. }
  1386. /**
  1387. * testFormSecurityMultipleSubmitButtons
  1388. *
  1389. * test form submit generation and ensure that _Token is only created on end()
  1390. *
  1391. * @return void
  1392. */
  1393. public function testFormSecurityMultipleSubmitButtons()
  1394. {
  1395. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1396. $this->Form->create($this->article);
  1397. $this->Form->text('Address.title');
  1398. $this->Form->text('Address.first_name');
  1399. $result = $this->Form->submit('Save', ['name' => 'save']);
  1400. $expected = [
  1401. 'div' => ['class' => 'submit'],
  1402. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1403. '/div',
  1404. ];
  1405. $this->assertHtml($expected, $result);
  1406. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1407. $expected = [
  1408. 'div' => ['class' => 'submit'],
  1409. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1410. '/div',
  1411. ];
  1412. $this->assertHtml($expected, $result);
  1413. $result = $this->Form->end();
  1414. $tokenDebug = urlencode(json_encode([
  1415. '/articles/add',
  1416. [
  1417. 'Address.title',
  1418. 'Address.first_name',
  1419. ],
  1420. ['save', 'cancel'],
  1421. ]));
  1422. $expected = [
  1423. 'div' => ['style' => 'display:none;'],
  1424. ['input' => [
  1425. 'type' => 'hidden',
  1426. 'name' => '_Token[fields]',
  1427. 'autocomplete',
  1428. 'value',
  1429. ]],
  1430. ['input' => [
  1431. 'type' => 'hidden',
  1432. 'name' => '_Token[unlocked]',
  1433. 'value' => 'cancel%7Csave',
  1434. 'autocomplete' => 'off',
  1435. ]],
  1436. ['input' => [
  1437. 'type' => 'hidden',
  1438. 'name' => '_Token[debug]',
  1439. 'value' => $tokenDebug,
  1440. 'autocomplete' => 'off',
  1441. ]],
  1442. '/div',
  1443. ];
  1444. $this->assertHtml($expected, $result);
  1445. }
  1446. /**
  1447. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1448. *
  1449. * @return void
  1450. */
  1451. public function testSecurityButtonNestedNamed()
  1452. {
  1453. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  1454. $this->Form->create();
  1455. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1456. $result = $this->Form->unlockField();
  1457. $this->assertEquals(['Address.button'], $result);
  1458. }
  1459. /**
  1460. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1461. *
  1462. * @return void
  1463. */
  1464. public function testSecuritySubmitNestedNamed()
  1465. {
  1466. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1467. $this->Form->create($this->article);
  1468. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1469. $result = $this->Form->unlockField();
  1470. $this->assertEquals(['Address.button'], $result);
  1471. }
  1472. /**
  1473. * Test that the correct fields are unlocked for image submits with no names.
  1474. *
  1475. * @return void
  1476. */
  1477. public function testSecuritySubmitImageNoName()
  1478. {
  1479. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1480. $this->Form->create();
  1481. $result = $this->Form->submit('save.png');
  1482. $expected = [
  1483. 'div' => ['class' => 'submit'],
  1484. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1485. '/div',
  1486. ];
  1487. $this->assertHtml($expected, $result);
  1488. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1489. }
  1490. /**
  1491. * Test that the correct fields are unlocked for image submits with names.
  1492. *
  1493. * @return void
  1494. */
  1495. public function testSecuritySubmitImageName()
  1496. {
  1497. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1498. $this->Form->create(null);
  1499. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1500. $expected = [
  1501. 'div' => ['class' => 'submit'],
  1502. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1503. '/div',
  1504. ];
  1505. $this->assertHtml($expected, $result);
  1506. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1507. }
  1508. /**
  1509. * testFormSecurityMultipleControlFields method
  1510. *
  1511. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1512. *
  1513. * @return void
  1514. */
  1515. public function testFormSecurityMultipleControlFields()
  1516. {
  1517. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1518. $this->Form->create();
  1519. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1520. $this->Form->control('Addresses.0.title');
  1521. $this->Form->control('Addresses.0.first_name');
  1522. $this->Form->control('Addresses.0.last_name');
  1523. $this->Form->control('Addresses.0.address');
  1524. $this->Form->control('Addresses.0.city');
  1525. $this->Form->control('Addresses.0.phone');
  1526. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1527. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1528. $this->Form->control('Addresses.1.title');
  1529. $this->Form->control('Addresses.1.first_name');
  1530. $this->Form->control('Addresses.1.last_name');
  1531. $this->Form->control('Addresses.1.address');
  1532. $this->Form->control('Addresses.1.city');
  1533. $this->Form->control('Addresses.1.phone');
  1534. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1535. $result = $this->Form->secure($this->Form->fields);
  1536. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1537. $tokenDebug = urlencode(json_encode([
  1538. '/articles/add',
  1539. [
  1540. 'Addresses.0.id' => '123456',
  1541. 'Addresses.0.title',
  1542. 'Addresses.0.first_name',
  1543. 'Addresses.0.last_name',
  1544. 'Addresses.0.address',
  1545. 'Addresses.0.city',
  1546. 'Addresses.0.phone',
  1547. 'Addresses.0.primary',
  1548. 'Addresses.1.id' => '654321',
  1549. 'Addresses.1.title',
  1550. 'Addresses.1.first_name',
  1551. 'Addresses.1.last_name',
  1552. 'Addresses.1.address',
  1553. 'Addresses.1.city',
  1554. 'Addresses.1.phone',
  1555. 'Addresses.1.primary',
  1556. ],
  1557. [],
  1558. ]));
  1559. $expected = [
  1560. 'div' => ['style' => 'display:none;'],
  1561. ['input' => [
  1562. 'type' => 'hidden',
  1563. 'name' => '_Token[fields]',
  1564. 'value' => $hash,
  1565. 'autocomplete' => 'off',
  1566. ]],
  1567. ['input' => [
  1568. 'type' => 'hidden',
  1569. 'name' => '_Token[unlocked]',
  1570. 'autocomplete' => 'off',
  1571. 'value' => '',
  1572. ]],
  1573. ['input' => [
  1574. 'type' => 'hidden',
  1575. 'name' => '_Token[debug]',
  1576. 'value' => $tokenDebug,
  1577. 'autocomplete' => 'off',
  1578. ]],
  1579. '/div',
  1580. ];
  1581. $this->assertHtml($expected, $result);
  1582. }
  1583. /**
  1584. * testFormSecurityArrayFields method
  1585. *
  1586. * Test form security with Model.field.0 style inputs.
  1587. *
  1588. * @return void
  1589. */
  1590. public function testFormSecurityArrayFields()
  1591. {
  1592. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1593. $this->Form->create();
  1594. $this->Form->text('Address.primary.1');
  1595. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1596. $this->Form->text('Address.secondary.1.0');
  1597. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1598. }
  1599. /**
  1600. * testFormSecurityMultipleControlDisabledFields method
  1601. *
  1602. * Test secure form generation with multiple records and disabled fields.
  1603. *
  1604. * @return void
  1605. */
  1606. public function testFormSecurityMultipleControlDisabledFields()
  1607. {
  1608. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1609. 'unlockedFields' => ['first_name', 'address'],
  1610. ]));
  1611. $this->Form->create();
  1612. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1613. $this->Form->text('Addresses.0.title');
  1614. $this->Form->text('Addresses.0.first_name');
  1615. $this->Form->text('Addresses.0.last_name');
  1616. $this->Form->text('Addresses.0.address');
  1617. $this->Form->text('Addresses.0.city');
  1618. $this->Form->text('Addresses.0.phone');
  1619. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1620. $this->Form->text('Addresses.1.title');
  1621. $this->Form->text('Addresses.1.first_name');
  1622. $this->Form->text('Addresses.1.last_name');
  1623. $this->Form->text('Addresses.1.address');
  1624. $this->Form->text('Addresses.1.city');
  1625. $this->Form->text('Addresses.1.phone');
  1626. $result = $this->Form->secure($this->Form->fields);
  1627. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1628. $tokenDebug = urlencode(json_encode([
  1629. '/articles/add',
  1630. [
  1631. 'Addresses.0.id' => '123456',
  1632. 'Addresses.0.title',
  1633. 'Addresses.0.last_name',
  1634. 'Addresses.0.city',
  1635. 'Addresses.0.phone',
  1636. 'Addresses.1.id' => '654321',
  1637. 'Addresses.1.title',
  1638. 'Addresses.1.last_name',
  1639. 'Addresses.1.city',
  1640. 'Addresses.1.phone',
  1641. ],
  1642. [
  1643. 'first_name',
  1644. 'address',
  1645. ],
  1646. ]));
  1647. $expected = [
  1648. 'div' => ['style' => 'display:none;'],
  1649. ['input' => [
  1650. 'type' => 'hidden',
  1651. 'name' => '_Token[fields]',
  1652. 'autocomplete' => 'off',
  1653. 'value' => $hash,
  1654. ]],
  1655. ['input' => [
  1656. 'type' => 'hidden',
  1657. 'name' => '_Token[unlocked]',
  1658. 'autocomplete' => 'off',
  1659. 'value' => 'address%7Cfirst_name',
  1660. ]],
  1661. ['input' => [
  1662. 'type' => 'hidden',
  1663. 'name' => '_Token[debug]',
  1664. 'autocomplete' => 'off',
  1665. 'value' => $tokenDebug,
  1666. ]],
  1667. '/div',
  1668. ];
  1669. $this->assertHtml($expected, $result);
  1670. }
  1671. /**
  1672. * testFormSecurityControlDisabledFields method
  1673. *
  1674. * Test single record form with disabled fields.
  1675. *
  1676. * @return void
  1677. */
  1678. public function testFormSecurityControlUnlockedFields()
  1679. {
  1680. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1681. 'unlockedFields' => ['first_name', 'address'],
  1682. ]));
  1683. $this->Form->create();
  1684. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1685. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1686. $this->Form->text('Addresses.title');
  1687. $this->Form->text('Addresses.first_name');
  1688. $this->Form->text('Addresses.last_name');
  1689. $this->Form->text('Addresses.address');
  1690. $this->Form->text('Addresses.city');
  1691. $this->Form->text('Addresses.phone');
  1692. $result = $this->Form->fields;
  1693. $expected = [
  1694. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1695. 'Addresses.city', 'Addresses.phone',
  1696. ];
  1697. $this->assertEquals($expected, $result);
  1698. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1699. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1700. $tokenDebug = urlencode(json_encode([
  1701. '/articles/add',
  1702. [
  1703. 'Addresses.id' => '123456',
  1704. 'Addresses.title',
  1705. 'Addresses.last_name',
  1706. 'Addresses.city',
  1707. 'Addresses.phone',
  1708. ],
  1709. [
  1710. 'first_name',
  1711. 'address',
  1712. ],
  1713. ]));
  1714. $expected = [
  1715. 'div' => ['style' => 'display:none;'],
  1716. ['input' => [
  1717. 'type' => 'hidden',
  1718. 'name' => '_Token[fields]',
  1719. 'value' => $hash,
  1720. 'autocomplete' => 'off',
  1721. 'data-foo' => 'bar',
  1722. ]],
  1723. ['input' => [
  1724. 'type' => 'hidden',
  1725. 'name' => '_Token[unlocked]',
  1726. 'value' => 'address%7Cfirst_name',
  1727. 'autocomplete' => 'off',
  1728. 'data-foo' => 'bar',
  1729. ]],
  1730. ['input' => [
  1731. 'type' => 'hidden', 'name' => '_Token[debug]',
  1732. 'value' => $tokenDebug,
  1733. 'autocomplete' => 'off',
  1734. 'data-foo' => 'bar',
  1735. ]],
  1736. '/div',
  1737. ];
  1738. $this->assertHtml($expected, $result);
  1739. }
  1740. /**
  1741. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1742. *
  1743. * Test single record form with debugSecurity param.
  1744. *
  1745. * @return void
  1746. */
  1747. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1748. {
  1749. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1750. 'unlockedFields' => ['first_name', 'address'],
  1751. ]));
  1752. $this->Form->create();
  1753. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1754. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1755. $this->Form->text('Addresses.title');
  1756. $this->Form->text('Addresses.first_name');
  1757. $this->Form->text('Addresses.last_name');
  1758. $this->Form->text('Addresses.address');
  1759. $this->Form->text('Addresses.city');
  1760. $this->Form->text('Addresses.phone');
  1761. $result = $this->Form->fields;
  1762. $expected = [
  1763. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1764. 'Addresses.city', 'Addresses.phone',
  1765. ];
  1766. $this->assertEquals($expected, $result);
  1767. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1768. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1769. $tokenDebug = urlencode(json_encode([
  1770. '/articles/add',
  1771. [
  1772. 'Addresses.id' => '123456',
  1773. 'Addresses.title',
  1774. 'Addresses.last_name',
  1775. 'Addresses.city',
  1776. 'Addresses.phone',
  1777. ],
  1778. [
  1779. 'first_name',
  1780. 'address',
  1781. ],
  1782. ]));
  1783. $expected = [
  1784. 'div' => ['style' => 'display:none;'],
  1785. ['input' => [
  1786. 'type' => 'hidden',
  1787. 'name' => '_Token[fields]',
  1788. 'value' => $hash,
  1789. 'autocomplete' => 'off',
  1790. 'data-foo' => 'bar',
  1791. ]],
  1792. ['input' => [
  1793. 'type' => 'hidden',
  1794. 'name' => '_Token[unlocked]',
  1795. 'value' => 'address%7Cfirst_name',
  1796. 'autocomplete' => 'off',
  1797. 'data-foo' => 'bar',
  1798. ]],
  1799. ['input' => [
  1800. 'type' => 'hidden', 'name' => '_Token[debug]',
  1801. 'value' => $tokenDebug,
  1802. 'autocomplete' => 'off',
  1803. 'data-foo' => 'bar',
  1804. ]],
  1805. '/div',
  1806. ];
  1807. $this->assertHtml($expected, $result);
  1808. }
  1809. /**
  1810. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1811. *
  1812. * Debug is false, debugSecurity is true -> no debug
  1813. *
  1814. * @return void
  1815. */
  1816. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1817. {
  1818. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1819. 'unlockedFields' => ['first_name', 'address'],
  1820. ]));
  1821. $this->Form->create();
  1822. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1823. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1824. $this->Form->text('Addresses.title');
  1825. $this->Form->text('Addresses.first_name');
  1826. $this->Form->text('Addresses.last_name');
  1827. $this->Form->text('Addresses.address');
  1828. $this->Form->text('Addresses.city');
  1829. $this->Form->text('Addresses.phone');
  1830. $result = $this->Form->fields;
  1831. $expected = [
  1832. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1833. 'Addresses.city', 'Addresses.phone',
  1834. ];
  1835. $this->assertEquals($expected, $result);
  1836. Configure::write('debug', false);
  1837. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1838. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1839. $expected = [
  1840. 'div' => ['style' => 'display:none;'],
  1841. ['input' => [
  1842. 'type' => 'hidden',
  1843. 'name' => '_Token[fields]',
  1844. 'value' => $hash,
  1845. 'autocomplete' => 'off',
  1846. 'data-foo' => 'bar',
  1847. ]],
  1848. ['input' => [
  1849. 'type' => 'hidden',
  1850. 'name' => '_Token[unlocked]',
  1851. 'value' => 'address%7Cfirst_name',
  1852. 'autocomplete' => 'off',
  1853. 'data-foo' => 'bar',
  1854. ]],
  1855. '/div',
  1856. ];
  1857. $this->assertHtml($expected, $result);
  1858. }
  1859. /**
  1860. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1861. *
  1862. * Test single record form with debugSecurity param.
  1863. *
  1864. * @return void
  1865. */
  1866. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1867. {
  1868. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1869. 'unlockedFields' => ['first_name', 'address'],
  1870. ]));
  1871. $this->Form->create();
  1872. $this->assertEquals(
  1873. $this->View->getRequest()->getParam('_Token.unlockedFields'),
  1874. $this->Form->unlockField()
  1875. );
  1876. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1877. $this->Form->text('Addresses.title');
  1878. $this->Form->text('Addresses.first_name');
  1879. $this->Form->text('Addresses.last_name');
  1880. $this->Form->text('Addresses.address');
  1881. $this->Form->text('Addresses.city');
  1882. $this->Form->text('Addresses.phone');
  1883. $result = $this->Form->fields;
  1884. $expected = [
  1885. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1886. 'Addresses.city', 'Addresses.phone',
  1887. ];
  1888. $this->assertEquals($expected, $result);
  1889. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1890. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1891. $expected = [
  1892. 'div' => ['style' => 'display:none;'],
  1893. ['input' => [
  1894. 'type' => 'hidden',
  1895. 'name' => '_Token[fields]',
  1896. 'value' => $hash,
  1897. 'autocomplete' => 'off',
  1898. 'data-foo' => 'bar',
  1899. ]],
  1900. ['input' => [
  1901. 'type' => 'hidden',
  1902. 'name' => '_Token[unlocked]',
  1903. 'value' => 'address%7Cfirst_name',
  1904. 'autocomplete' => 'off',
  1905. 'data-foo' => 'bar',
  1906. ]],
  1907. '/div',
  1908. ];
  1909. $this->assertHtml($expected, $result);
  1910. }
  1911. /**
  1912. * testFormSecureWithCustomNameAttribute method
  1913. *
  1914. * Test securing inputs with custom name attributes.
  1915. *
  1916. * @return void
  1917. */
  1918. public function testFormSecureWithCustomNameAttribute()
  1919. {
  1920. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1921. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1922. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1923. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1924. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1925. }
  1926. /**
  1927. * testFormSecuredControl method
  1928. *
  1929. * Test generation of entire secure form, assertions made on control() output.
  1930. *
  1931. * @return void
  1932. */
  1933. public function testFormSecuredControl()
  1934. {
  1935. $this->View->setRequest($this->View->getRequest()
  1936. ->withParam('_Token', 'stuff')
  1937. ->withParam('_csrfToken', 'testKey'));
  1938. $this->article['schema'] = [
  1939. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1940. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1941. ];
  1942. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1943. $encoding = strtolower(Configure::read('App.encoding'));
  1944. $expected = [
  1945. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1946. 'div' => ['style' => 'display:none;'],
  1947. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1948. ['input' => [
  1949. 'type' => 'hidden',
  1950. 'name' => '_csrfToken',
  1951. 'value' => 'testKey',
  1952. 'autocomplete' => 'off',
  1953. ]],
  1954. '/div',
  1955. ];
  1956. $this->assertHtml($expected, $result);
  1957. $result = $this->Form->control('ratio');
  1958. $expected = [
  1959. 'div' => ['class'],
  1960. 'label' => ['for'],
  1961. 'Ratio',
  1962. '/label',
  1963. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1964. '/div',
  1965. ];
  1966. $this->assertHtml($expected, $result);
  1967. $result = $this->Form->control('population');
  1968. $expected = [
  1969. 'div' => ['class'],
  1970. 'label' => ['for'],
  1971. 'Population',
  1972. '/label',
  1973. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1974. '/div',
  1975. ];
  1976. $this->assertHtml($expected, $result);
  1977. $result = $this->Form->control('published', ['type' => 'text']);
  1978. $expected = [
  1979. 'div' => ['class' => 'input text'],
  1980. 'label' => ['for' => 'published'],
  1981. 'Published',
  1982. '/label',
  1983. ['input' => [
  1984. 'type' => 'text',
  1985. 'name' => 'published',
  1986. 'id' => 'published',
  1987. ]],
  1988. '/div',
  1989. ];
  1990. $this->assertHtml($expected, $result);
  1991. $result = $this->Form->control('other', ['type' => 'text']);
  1992. $expected = [
  1993. 'div' => ['class' => 'input text'],
  1994. 'label' => ['for' => 'other'],
  1995. 'Other',
  1996. '/label',
  1997. ['input' => [
  1998. 'type' => 'text',
  1999. 'name' => 'other',
  2000. 'id',
  2001. ]],
  2002. '/div',
  2003. ];
  2004. $this->assertHtml($expected, $result);
  2005. $result = $this->Form->hidden('stuff');
  2006. $expected = [
  2007. 'input' => [
  2008. 'type' => 'hidden',
  2009. 'name' => 'stuff',
  2010. ],
  2011. ];
  2012. $this->assertHtml($expected, $result);
  2013. $result = $this->Form->hidden('hidden', ['value' => '0']);
  2014. $expected = ['input' => [
  2015. 'type' => 'hidden',
  2016. 'name' => 'hidden',
  2017. 'value' => '0',
  2018. ]];
  2019. $this->assertHtml($expected, $result);
  2020. $result = $this->Form->control('something', ['type' => 'checkbox']);
  2021. $expected = [
  2022. 'div' => ['class' => 'input checkbox'],
  2023. ['input' => [
  2024. 'type' => 'hidden',
  2025. 'name' => 'something',
  2026. 'value' => '0',
  2027. ]],
  2028. 'label' => ['for' => 'something'],
  2029. ['input' => [
  2030. 'type' => 'checkbox',
  2031. 'name' => 'something',
  2032. 'value' => '1',
  2033. 'id' => 'something',
  2034. ]],
  2035. 'Something',
  2036. '/label',
  2037. '/div',
  2038. ];
  2039. $this->assertHtml($expected, $result);
  2040. $result = $this->Form->fields;
  2041. $expectedFields = [
  2042. 'ratio',
  2043. 'population',
  2044. 'published',
  2045. 'other',
  2046. 'stuff' => '',
  2047. 'hidden' => '0',
  2048. 'something',
  2049. ];
  2050. $this->assertEquals($expectedFields, $result);
  2051. $result = $this->Form->secure($this->Form->fields);
  2052. $tokenDebug = urlencode(json_encode([
  2053. '/articles/add',
  2054. $expectedFields,
  2055. [],
  2056. ]));
  2057. $expected = [
  2058. 'div' => ['style' => 'display:none;'],
  2059. ['input' => [
  2060. 'type' => 'hidden',
  2061. 'name' => '_Token[fields]',
  2062. 'value',
  2063. 'autocomplete',
  2064. ]],
  2065. ['input' => [
  2066. 'type' => 'hidden',
  2067. 'name' => '_Token[unlocked]',
  2068. 'value' => '',
  2069. 'autocomplete' => 'off',
  2070. ]],
  2071. ['input' => [
  2072. 'type' => 'hidden', 'name' => '_Token[debug]',
  2073. 'value' => $tokenDebug,
  2074. 'autocomplete' => 'off',
  2075. ]],
  2076. '/div',
  2077. ];
  2078. $this->assertHtml($expected, $result);
  2079. }
  2080. /**
  2081. * testSecuredControlCustomName method
  2082. *
  2083. * Test secured inputs with custom names.
  2084. *
  2085. * @return void
  2086. */
  2087. public function testSecuredControlCustomName()
  2088. {
  2089. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2090. $this->assertEquals([], $this->Form->fields);
  2091. $this->Form->text('text_input', [
  2092. 'name' => 'Option[General.default_role]',
  2093. ]);
  2094. $expected = ['Option.General.default_role'];
  2095. $this->assertEquals($expected, $this->Form->fields);
  2096. $this->Form->select('select_box', [1, 2], [
  2097. 'name' => 'Option[General.select_role]',
  2098. ]);
  2099. $expected[] = 'Option.General.select_role';
  2100. $this->assertEquals($expected, $this->Form->fields);
  2101. $this->Form->text('other.things[]');
  2102. $expected[] = 'other.things';
  2103. $this->assertEquals($expected, $this->Form->fields);
  2104. }
  2105. /**
  2106. * testSecuredControlDuplicate method
  2107. *
  2108. * Test that a hidden field followed by a visible field
  2109. * undoes the hidden field locking.
  2110. *
  2111. * @return void
  2112. */
  2113. public function testSecuredControlDuplicate()
  2114. {
  2115. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2116. $this->assertEquals([], $this->Form->fields);
  2117. $this->Form->control('text_val', [
  2118. 'type' => 'hidden',
  2119. 'value' => 'some text',
  2120. ]);
  2121. $expected = ['text_val' => 'some text'];
  2122. $this->assertEquals($expected, $this->Form->fields);
  2123. $this->Form->control('text_val', [
  2124. 'type' => 'text',
  2125. ]);
  2126. $expected = ['text_val'];
  2127. $this->assertEquals($expected, $this->Form->fields);
  2128. }
  2129. /**
  2130. * testFormSecuredFileControl method
  2131. *
  2132. * Tests that the correct keys are added to the field hash index.
  2133. *
  2134. * @return void
  2135. */
  2136. public function testFormSecuredFileControl()
  2137. {
  2138. $this->assertEquals([], $this->Form->fields);
  2139. $this->Form->file('Attachment.file');
  2140. $expected = [
  2141. 'Attachment.file.name', 'Attachment.file.type',
  2142. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2143. 'Attachment.file.size',
  2144. ];
  2145. $this->assertEquals($expected, $this->Form->fields);
  2146. }
  2147. /**
  2148. * testFormSecuredMultipleSelect method
  2149. *
  2150. * Test that multiple selects keys are added to field hash.
  2151. *
  2152. * @return void
  2153. */
  2154. public function testFormSecuredMultipleSelect()
  2155. {
  2156. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  2157. $this->assertEquals([], $this->Form->fields);
  2158. $options = ['1' => 'one', '2' => 'two'];
  2159. $this->Form->select('Model.select', $options);
  2160. $expected = ['Model.select'];
  2161. $this->assertEquals($expected, $this->Form->fields);
  2162. $this->Form->fields = [];
  2163. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2164. $this->assertEquals($expected, $this->Form->fields);
  2165. }
  2166. /**
  2167. * testFormSecuredRadio method
  2168. *
  2169. * @return void
  2170. */
  2171. public function testFormSecuredRadio()
  2172. {
  2173. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2174. $this->assertEquals([], $this->Form->fields);
  2175. $options = ['1' => 'option1', '2' => 'option2'];
  2176. $this->Form->radio('Test.test', $options);
  2177. $expected = ['Test.test'];
  2178. $this->assertEquals($expected, $this->Form->fields);
  2179. $this->Form->radio('Test.all', $options, [
  2180. 'disabled' => ['option1', 'option2'],
  2181. ]);
  2182. $expected = ['Test.test', 'Test.all' => ''];
  2183. $this->assertEquals($expected, $this->Form->fields);
  2184. $this->Form->radio('Test.some', $options, [
  2185. 'disabled' => ['option1'],
  2186. ]);
  2187. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2188. $this->assertEquals($expected, $this->Form->fields);
  2189. }
  2190. /**
  2191. * testFormSecuredAndDisabledNotAssoc method
  2192. *
  2193. * Test that when disabled is in a list based attribute array it works.
  2194. *
  2195. * @return void
  2196. */
  2197. public function testFormSecuredAndDisabledNotAssoc()
  2198. {
  2199. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2200. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2201. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2202. $this->Form->text('Model.text', ['disabled']);
  2203. $this->Form->textarea('Model.textarea', ['disabled']);
  2204. $this->Form->password('Model.password', ['disabled']);
  2205. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2206. $expected = [
  2207. 'Model.radio' => '',
  2208. ];
  2209. $this->assertEquals($expected, $this->Form->fields);
  2210. }
  2211. /**
  2212. * testFormSecuredAndDisabled method
  2213. *
  2214. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2215. * hashing.
  2216. *
  2217. * @return void
  2218. */
  2219. public function testFormSecuredAndDisabled()
  2220. {
  2221. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2222. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2223. $this->Form->text('Model.text', ['disabled' => true]);
  2224. $this->Form->password('Model.text', ['disabled' => true]);
  2225. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2226. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2227. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2228. $this->Form->year('Model.year', ['disabled' => true]);
  2229. $this->Form->month('Model.month', ['disabled' => true]);
  2230. $this->Form->day('Model.day', ['disabled' => true]);
  2231. $this->Form->hour('Model.hour', ['disabled' => true]);
  2232. $this->Form->minute('Model.minute', ['disabled' => true]);
  2233. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2234. $expected = [
  2235. 'Model.radio' => '',
  2236. ];
  2237. $this->assertEquals($expected, $this->Form->fields);
  2238. }
  2239. /**
  2240. * testDisableSecurityUsingForm method
  2241. *
  2242. * @return void
  2243. */
  2244. public function testDisableSecurityUsingForm()
  2245. {
  2246. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2247. 'disabledFields' => [],
  2248. ]));
  2249. $this->Form->create();
  2250. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2251. $this->Form->text('Addresses.title');
  2252. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2253. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2254. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2255. $result = $this->Form->fields;
  2256. $expected = [
  2257. 'Addresses.id' => '123456', 'Addresses.title',
  2258. ];
  2259. $this->assertEquals($expected, $result);
  2260. }
  2261. /**
  2262. * testUnlockFieldAddsToList method
  2263. *
  2264. * Test disableField.
  2265. *
  2266. * @return void
  2267. */
  2268. public function testUnlockFieldAddsToList()
  2269. {
  2270. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2271. 'unlockedFields' => [],
  2272. ]));
  2273. $this->Form->unlockField('Contact.name');
  2274. $this->Form->text('Contact.name');
  2275. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2276. $this->assertEquals([], $this->Form->fields);
  2277. }
  2278. /**
  2279. * testUnlockFieldRemovingFromFields method
  2280. *
  2281. * Test unlockField removing from fields array.
  2282. *
  2283. * @return void
  2284. */
  2285. public function testUnlockFieldRemovingFromFields()
  2286. {
  2287. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2288. 'unlockedFields' => [],
  2289. ]));
  2290. $this->Form->create($this->article);
  2291. $this->Form->hidden('Article.id', ['value' => 1]);
  2292. $this->Form->text('Article.title');
  2293. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2294. $this->assertContains('Article.title', $this->Form->fields, 'Field should be secured.');
  2295. $this->Form->unlockField('Article.title');
  2296. $this->Form->unlockField('Article.id');
  2297. $this->assertEquals([], $this->Form->fields);
  2298. }
  2299. /**
  2300. * testResetUnlockFields method
  2301. *
  2302. * Test reset unlockFields, when create new form.
  2303. *
  2304. * @return void
  2305. */
  2306. public function testResetUnlockFields()
  2307. {
  2308. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2309. 'key' => 'testKey',
  2310. 'unlockedFields' => [],
  2311. ]));
  2312. $this->Form->unlockField('Contact.id');
  2313. $this->Form->create();
  2314. $this->Form->hidden('Contact.id', ['value' => 1]);
  2315. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2316. $this->Form->end();
  2317. $this->Form->create();
  2318. $this->Form->hidden('Contact.id', ['value' => 1]);
  2319. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2320. }
  2321. /**
  2322. * testSecuredFormUrlIgnoresHost method
  2323. *
  2324. * Test that only the path + query elements of a form's URL show up in their hash.
  2325. *
  2326. * @return void
  2327. */
  2328. public function testSecuredFormUrlIgnoresHost()
  2329. {
  2330. $this->View->setRequest($this->View->getRequest()->withParam('_Token', ['key' => 'testKey']));
  2331. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2332. $this->Form->create($this->article, [
  2333. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]],
  2334. ]);
  2335. $result = $this->Form->secure();
  2336. $this->assertContains($expected, $result);
  2337. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2338. $result = $this->Form->secure();
  2339. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2340. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2341. $result = $this->Form->secure();
  2342. $this->assertContains($expected, $result, 'URL path + query should work.');
  2343. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2344. $result = $this->Form->secure();
  2345. $this->assertNotContains($expected, $result, 'URL is different');
  2346. }
  2347. /**
  2348. * testSecuredFormUrlHasHtmlAndIdentifier method
  2349. *
  2350. * Test that URL, HTML and identifier show up in their hashes.
  2351. *
  2352. * @return void
  2353. */
  2354. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2355. {
  2356. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2357. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2358. $this->Form->create($this->article, [
  2359. 'url' => [
  2360. 'controller' => 'articles',
  2361. 'action' => 'view',
  2362. '?' => [
  2363. 'page' => 1,
  2364. 'limit' => 10,
  2365. 'html' => '<>"',
  2366. ],
  2367. '#' => 'result',
  2368. ],
  2369. ]);
  2370. $result = $this->Form->secure();
  2371. $this->assertContains($expected, $result);
  2372. $this->Form->create($this->article, [
  2373. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2374. ]);
  2375. $result = $this->Form->secure();
  2376. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2377. $this->Form->create($this->article, [
  2378. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2379. ]);
  2380. $result = $this->Form->secure();
  2381. $this->assertContains($expected, $result, 'URL path + query should work.');
  2382. }
  2383. /**
  2384. * testErrorMessageDisplay method
  2385. *
  2386. * Test error message display.
  2387. *
  2388. * @return void
  2389. */
  2390. public function testErrorMessageDisplay()
  2391. {
  2392. $this->article['errors'] = [
  2393. 'Article' => [
  2394. 'title' => 'error message',
  2395. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2396. ],
  2397. ];
  2398. $this->Form->create($this->article);
  2399. $result = $this->Form->control('Article.title');
  2400. $expected = [
  2401. 'div' => ['class' => 'input text error'],
  2402. 'label' => ['for' => 'article-title'],
  2403. 'Title',
  2404. '/label',
  2405. 'input' => [
  2406. 'type' => 'text', 'name' => 'Article[title]',
  2407. 'id' => 'article-title', 'class' => 'form-error',
  2408. ],
  2409. ['div' => ['class' => 'error-message']],
  2410. 'error message',
  2411. '/div',
  2412. '/div',
  2413. ];
  2414. $this->assertHtml($expected, $result);
  2415. $result = $this->Form->control('Article.title', [
  2416. 'templates' => [
  2417. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>',
  2418. ],
  2419. ]);
  2420. $expected = [
  2421. 'div' => ['class' => 'input text error'],
  2422. 'label' => ['for' => 'article-title'],
  2423. 'Title',
  2424. '/label',
  2425. 'input' => [
  2426. 'type' => 'text', 'name' => 'Article[title]',
  2427. 'id' => 'article-title', 'class' => 'form-error',
  2428. ],
  2429. '/div',
  2430. ];
  2431. $this->assertHtml($expected, $result);
  2432. $result = $this->Form->control('Article.content');
  2433. $expected = [
  2434. 'div' => ['class' => 'input text error'],
  2435. 'label' => ['for' => 'article-content'],
  2436. 'Content',
  2437. '/label',
  2438. 'input' => [
  2439. 'type' => 'text', 'name' => 'Article[content]',
  2440. 'id' => 'article-content', 'class' => 'form-error',
  2441. ],
  2442. ['div' => ['class' => 'error-message']],
  2443. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2444. '/div',
  2445. '/div',
  2446. ];
  2447. $this->assertHtml($expected, $result);
  2448. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2449. $expected = [
  2450. 'div' => ['class' => 'input text error'],
  2451. 'label' => ['for' => 'article-content'],
  2452. 'Content',
  2453. '/label',
  2454. 'input' => [
  2455. 'type' => 'text', 'name' => 'Article[content]',
  2456. 'id' => 'article-content', 'class' => 'form-error',
  2457. ],
  2458. ['div' => ['class' => 'error-message']],
  2459. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2460. '/div',
  2461. '/div',
  2462. ];
  2463. $this->assertHtml($expected, $result);
  2464. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2465. $expected = [
  2466. 'div' => ['class' => 'input text error'],
  2467. 'label' => ['for' => 'article-content'],
  2468. 'Content',
  2469. '/label',
  2470. 'input' => [
  2471. 'type' => 'text', 'name' => 'Article[content]',
  2472. 'id' => 'article-content', 'class' => 'form-error',
  2473. ],
  2474. ['div' => ['class' => 'error-message']],
  2475. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2476. '/div',
  2477. '/div',
  2478. ];
  2479. $this->assertHtml($expected, $result);
  2480. }
  2481. /**
  2482. * testEmptyErrorValidation method
  2483. *
  2484. * Test validation errors, when validation message is an empty string.
  2485. *
  2486. * @return void
  2487. */
  2488. public function testEmptyErrorValidation()
  2489. {
  2490. $this->article['errors'] = [
  2491. 'Article' => ['title' => ''],
  2492. ];
  2493. $this->Form->create($this->article);
  2494. $result = $this->Form->control('Article.title');
  2495. $expected = [
  2496. 'div' => ['class' => 'input text error'],
  2497. 'label' => ['for' => 'article-title'],
  2498. 'Title',
  2499. '/label',
  2500. 'input' => [
  2501. 'type' => 'text', 'name' => 'Article[title]',
  2502. 'id' => 'article-title', 'class' => 'form-error',
  2503. ],
  2504. ['div' => ['class' => 'error-message']],
  2505. [],
  2506. '/div',
  2507. '/div',
  2508. ];
  2509. $this->assertHtml($expected, $result);
  2510. }
  2511. /**
  2512. * testEmptyControlErrorValidation method
  2513. *
  2514. * Test validation errors, when calling control() overriding validation message by an empty string.
  2515. *
  2516. * @return void
  2517. */
  2518. public function testEmptyControlErrorValidation()
  2519. {
  2520. $this->article['errors'] = [
  2521. 'Article' => ['title' => 'error message'],
  2522. ];
  2523. $this->Form->create($this->article);
  2524. $result = $this->Form->control('Article.title', ['error' => '']);
  2525. $expected = [
  2526. 'div' => ['class' => 'input text error'],
  2527. 'label' => ['for' => 'article-title'],
  2528. 'Title',
  2529. '/label',
  2530. 'input' => [
  2531. 'type' => 'text', 'name' => 'Article[title]',
  2532. 'id' => 'article-title', 'class' => 'form-error',
  2533. ],
  2534. ['div' => ['class' => 'error-message']],
  2535. [],
  2536. '/div',
  2537. '/div',
  2538. ];
  2539. $this->assertHtml($expected, $result);
  2540. }
  2541. /**
  2542. * testControlErrorMessage method
  2543. *
  2544. * Test validation errors, when calling control() overriding validation messages.
  2545. *
  2546. * @return void
  2547. */
  2548. public function testControlErrorMessage()
  2549. {
  2550. $this->article['errors'] = [
  2551. 'title' => ['error message'],
  2552. ];
  2553. $this->Form->create($this->article);
  2554. $result = $this->Form->control('title', [
  2555. 'error' => 'Custom error!',
  2556. ]);
  2557. $expected = [
  2558. 'div' => ['class' => 'input text required error'],
  2559. 'label' => ['for' => 'title'],
  2560. 'Title',
  2561. '/label',
  2562. 'input' => [
  2563. 'type' => 'text', 'name' => 'title',
  2564. 'id' => 'title', 'class' => 'form-error',
  2565. 'required' => 'required',
  2566. ],
  2567. ['div' => ['class' => 'error-message']],
  2568. 'Custom error!',
  2569. '/div',
  2570. '/div',
  2571. ];
  2572. $this->assertHtml($expected, $result);
  2573. $result = $this->Form->control('title', [
  2574. 'error' => ['error message' => 'Custom error!'],
  2575. ]);
  2576. $expected = [
  2577. 'div' => ['class' => 'input text required error'],
  2578. 'label' => ['for' => 'title'],
  2579. 'Title',
  2580. '/label',
  2581. 'input' => [
  2582. 'type' => 'text',
  2583. 'name' => 'title',
  2584. 'id' => 'title',
  2585. 'class' => 'form-error',
  2586. 'required' => 'required',
  2587. ],
  2588. ['div' => ['class' => 'error-message']],
  2589. 'Custom error!',
  2590. '/div',
  2591. '/div',
  2592. ];
  2593. $this->assertHtml($expected, $result);
  2594. }
  2595. /**
  2596. * testFormValidationAssociated method
  2597. *
  2598. * Tests displaying errors for nested entities.
  2599. *
  2600. * @return void
  2601. */
  2602. public function testFormValidationAssociated()
  2603. {
  2604. $nested = new Entity(['foo' => 'bar']);
  2605. $nested->setError('foo', ['not a valid bar']);
  2606. $entity = new Entity(['nested' => $nested]);
  2607. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2608. $result = $this->Form->error('nested.foo');
  2609. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2610. }
  2611. /**
  2612. * testFormValidationAssociatedSecondLevel method
  2613. *
  2614. * Test form error display with associated model.
  2615. *
  2616. * @return void
  2617. */
  2618. public function testFormValidationAssociatedSecondLevel()
  2619. {
  2620. $inner = new Entity(['bar' => 'baz']);
  2621. $nested = new Entity(['foo' => $inner]);
  2622. $entity = new Entity(['nested' => $nested]);
  2623. $inner->setError('bar', ['not a valid one']);
  2624. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2625. $result = $this->Form->error('nested.foo.bar');
  2626. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2627. }
  2628. /**
  2629. * testFormValidationMultiRecord method
  2630. *
  2631. * Test form error display with multiple records.
  2632. *
  2633. * @return void
  2634. */
  2635. public function testFormValidationMultiRecord()
  2636. {
  2637. $one = new Entity();
  2638. $two = new Entity();
  2639. $this->getTableLocator()->get('Contacts', [
  2640. 'className' => __NAMESPACE__ . '\ContactsTable',
  2641. ]);
  2642. $one->set('email', '');
  2643. $one->setError('email', ['invalid email']);
  2644. $two->set('name', '');
  2645. $two->setError('name', ['This is wrong']);
  2646. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2647. $result = $this->Form->control('0.email');
  2648. $expected = [
  2649. 'div' => ['class' => 'input email error'],
  2650. 'label' => ['for' => '0-email'],
  2651. 'Email',
  2652. '/label',
  2653. 'input' => [
  2654. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2655. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2656. ],
  2657. ['div' => ['class' => 'error-message']],
  2658. 'invalid email',
  2659. '/div',
  2660. '/div',
  2661. ];
  2662. $this->assertHtml($expected, $result);
  2663. $result = $this->Form->control('1.name');
  2664. $expected = [
  2665. 'div' => ['class' => 'input text error'],
  2666. 'label' => ['for' => '1-name'],
  2667. 'Name',
  2668. '/label',
  2669. 'input' => [
  2670. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2671. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2672. ],
  2673. ['div' => ['class' => 'error-message']],
  2674. 'This is wrong',
  2675. '/div',
  2676. '/div',
  2677. ];
  2678. $this->assertHtml($expected, $result);
  2679. }
  2680. /**
  2681. * testControl method
  2682. *
  2683. * Test various incarnations of control().
  2684. *
  2685. * @return void
  2686. */
  2687. public function testControl()
  2688. {
  2689. $this->getTableLocator()->get('ValidateUsers', [
  2690. 'className' => __NAMESPACE__ . '\ValidateUsersTable',
  2691. ]);
  2692. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2693. $result = $this->Form->control('ValidateUsers.balance');
  2694. $expected = [
  2695. 'div' => ['class'],
  2696. 'label' => ['for'],
  2697. 'Balance',
  2698. '/label',
  2699. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2700. '/div',
  2701. ];
  2702. $this->assertHtml($expected, $result);
  2703. $result = $this->Form->control('ValidateUser.cost_decimal');
  2704. $expected = [
  2705. 'div' => ['class'],
  2706. 'label' => ['for'],
  2707. 'Cost Decimal',
  2708. '/label',
  2709. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2710. '/div',
  2711. ];
  2712. $this->assertHtml($expected, $result);
  2713. $result = $this->Form->control('ValidateUser.null_decimal');
  2714. $expected = [
  2715. 'div' => ['class'],
  2716. 'label' => ['for'],
  2717. 'Null Decimal',
  2718. '/label',
  2719. 'input' => ['name', 'type' => 'number', 'id'],
  2720. '/div',
  2721. ];
  2722. $this->assertHtml($expected, $result);
  2723. }
  2724. /**
  2725. * testControlCustomization method
  2726. *
  2727. * Tests the input method and passing custom options.
  2728. *
  2729. * @return void
  2730. */
  2731. public function testControlCustomization()
  2732. {
  2733. $this->getTableLocator()->get('Contacts', [
  2734. 'className' => __NAMESPACE__ . '\ContactsTable',
  2735. ]);
  2736. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2737. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2738. $expected = [
  2739. 'div' => ['class' => 'input email'],
  2740. 'label' => ['for' => 'custom'],
  2741. 'Email',
  2742. '/label',
  2743. ['input' => [
  2744. 'type' => 'email', 'name' => 'Contact[email]',
  2745. 'id' => 'custom', 'maxlength' => 255,
  2746. ]],
  2747. '/div',
  2748. ];
  2749. $this->assertHtml($expected, $result);
  2750. $result = $this->Form->control('Contact.email', [
  2751. 'templates' => ['inputContainer' => '<div>{{content}}</div>'],
  2752. ]);
  2753. $expected = [
  2754. '<div',
  2755. 'label' => ['for' => 'contact-email'],
  2756. 'Email',
  2757. '/label',
  2758. ['input' => [
  2759. 'type' => 'email', 'name' => 'Contact[email]',
  2760. 'id' => 'contact-email', 'maxlength' => 255,
  2761. ]],
  2762. '/div',
  2763. ];
  2764. $this->assertHtml($expected, $result);
  2765. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2766. $expected = [
  2767. 'div' => ['class' => 'input text'],
  2768. 'label' => ['for' => 'contact-email'],
  2769. 'Email',
  2770. '/label',
  2771. ['input' => [
  2772. 'type' => 'text', 'name' => 'Contact[email]',
  2773. 'id' => 'contact-email', 'maxlength' => '255',
  2774. ]],
  2775. '/div',
  2776. ];
  2777. $this->assertHtml($expected, $result);
  2778. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2779. $expected = [
  2780. 'div' => ['class' => 'input text'],
  2781. 'label' => ['for' => 'contact-5-email'],
  2782. 'Email',
  2783. '/label',
  2784. ['input' => [
  2785. 'type' => 'text', 'name' => 'Contact[5][email]',
  2786. 'id' => 'contact-5-email', 'maxlength' => '255',
  2787. ]],
  2788. '/div',
  2789. ];
  2790. $this->assertHtml($expected, $result);
  2791. $result = $this->Form->control('Contact.password');
  2792. $expected = [
  2793. 'div' => ['class' => 'input password'],
  2794. 'label' => ['for' => 'contact-password'],
  2795. 'Password',
  2796. '/label',
  2797. ['input' => [
  2798. 'type' => 'password', 'name' => 'Contact[password]',
  2799. 'id' => 'contact-password',
  2800. ]],
  2801. '/div',
  2802. ];
  2803. $this->assertHtml($expected, $result);
  2804. $result = $this->Form->control('Contact.email', [
  2805. 'type' => 'file', 'class' => 'textbox',
  2806. ]);
  2807. $expected = [
  2808. 'div' => ['class' => 'input file'],
  2809. 'label' => ['for' => 'contact-email'],
  2810. 'Email',
  2811. '/label',
  2812. ['input' => [
  2813. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2814. 'id' => 'contact-email',
  2815. ]],
  2816. '/div',
  2817. ];
  2818. $this->assertHtml($expected, $result);
  2819. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2820. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2821. $result = $this->Form->control('phone');
  2822. $expected = [
  2823. 'div' => ['class' => 'input tel'],
  2824. 'label' => ['for' => 'phone'],
  2825. 'Phone',
  2826. '/label',
  2827. ['input' => [
  2828. 'type' => 'tel', 'name' => 'phone',
  2829. 'value' => 'Hello &amp; World &gt; weird chars',
  2830. 'id' => 'phone', 'maxlength' => 255,
  2831. ]],
  2832. '/div',
  2833. ];
  2834. $this->assertHtml($expected, $result);
  2835. $this->View->setRequest(
  2836. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2837. );
  2838. $this->Form->create();
  2839. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2840. $expected = [
  2841. 'div' => ['class' => 'input text'],
  2842. 'label' => ['for' => 'myId'],
  2843. 'Field',
  2844. '/label',
  2845. 'input' => [
  2846. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2847. 'value' => 'My value', 'id' => 'myId',
  2848. ],
  2849. '/div',
  2850. ];
  2851. $this->assertHtml($expected, $result);
  2852. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2853. $this->Form->create();
  2854. $entity->setError('field', 'Badness!');
  2855. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2856. $result = $this->Form->control('field');
  2857. $expected = [
  2858. 'div' => ['class' => 'input text error'],
  2859. 'label' => ['for' => 'field'],
  2860. 'Field',
  2861. '/label',
  2862. 'input' => [
  2863. 'type' => 'text', 'name' => 'field',
  2864. 'id' => 'field', 'class' => 'form-error',
  2865. ],
  2866. ['div' => ['class' => 'error-message']],
  2867. 'Badness!',
  2868. '/div',
  2869. '/div',
  2870. ];
  2871. $this->assertHtml($expected, $result);
  2872. $result = $this->Form->control('field', [
  2873. 'templates' => [
  2874. 'inputContainerError' => '{{content}}{{error}}',
  2875. 'error' => '<span class="error-message">{{content}}</span>',
  2876. ],
  2877. ]);
  2878. $expected = [
  2879. 'label' => ['for' => 'field'],
  2880. 'Field',
  2881. '/label',
  2882. 'input' => [
  2883. 'type' => 'text', 'name' => 'field',
  2884. 'id' => 'field', 'class' => 'form-error',
  2885. ],
  2886. ['span' => ['class' => 'error-message']],
  2887. 'Badness!',
  2888. '/span',
  2889. ];
  2890. $this->assertHtml($expected, $result);
  2891. $entity->setError('field', ['minLength'], true);
  2892. $result = $this->Form->control('field', [
  2893. 'error' => [
  2894. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2895. 'maxLength' => 'login too large',
  2896. ],
  2897. ]);
  2898. $expected = [
  2899. 'div' => ['class' => 'input text error'],
  2900. 'label' => ['for' => 'field'],
  2901. 'Field',
  2902. '/label',
  2903. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2904. ['div' => ['class' => 'error-message']],
  2905. 'Le login doit contenir au moins 2 caractères',
  2906. '/div',
  2907. '/div',
  2908. ];
  2909. $this->assertHtml($expected, $result);
  2910. $entity->setError('field', ['maxLength'], true);
  2911. $result = $this->Form->control('field', [
  2912. 'error' => [
  2913. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2914. 'maxLength' => 'login too large',
  2915. ],
  2916. ]);
  2917. $expected = [
  2918. 'div' => ['class' => 'input text error'],
  2919. 'label' => ['for' => 'field'],
  2920. 'Field',
  2921. '/label',
  2922. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2923. ['div' => ['class' => 'error-message']],
  2924. 'login too large',
  2925. '/div',
  2926. '/div',
  2927. ];
  2928. $this->assertHtml($expected, $result);
  2929. }
  2930. /**
  2931. * testControlWithTemplateFile method
  2932. *
  2933. * Test that control() accepts a template file.
  2934. *
  2935. * @return void
  2936. */
  2937. public function testControlWithTemplateFile()
  2938. {
  2939. $result = $this->Form->control('field', [
  2940. 'templates' => 'htmlhelper_tags',
  2941. ]);
  2942. $expected = [
  2943. 'label' => ['for' => 'field'],
  2944. 'Field',
  2945. '/label',
  2946. 'input' => [
  2947. 'type' => 'text', 'name' => 'field',
  2948. 'id' => 'field',
  2949. ],
  2950. ];
  2951. $this->assertHtml($expected, $result);
  2952. }
  2953. /**
  2954. * testNestedControlsEndWithBrackets method
  2955. *
  2956. * Test that nested inputs end with brackets.
  2957. *
  2958. * @return void
  2959. */
  2960. public function testNestedControlsEndWithBrackets()
  2961. {
  2962. $result = $this->Form->text('nested.text[]');
  2963. $expected = [
  2964. 'input' => [
  2965. 'type' => 'text', 'name' => 'nested[text][]',
  2966. ],
  2967. ];
  2968. $this->assertHtml($expected, $result);
  2969. $result = $this->Form->file('nested.file[]');
  2970. $expected = [
  2971. 'input' => [
  2972. 'type' => 'file', 'name' => 'nested[file][]',
  2973. ],
  2974. ];
  2975. $this->assertHtml($expected, $result);
  2976. }
  2977. /**
  2978. * testCreateIdPrefix method
  2979. *
  2980. * Test id prefix.
  2981. *
  2982. * @return void
  2983. */
  2984. public function testCreateIdPrefix()
  2985. {
  2986. $this->Form->create(null, ['idPrefix' => 'prefix']);
  2987. $result = $this->Form->control('field');
  2988. $expected = [
  2989. 'div' => ['class' => 'input text'],
  2990. 'label' => ['for' => 'prefix-field'],
  2991. 'Field',
  2992. '/label',
  2993. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2994. '/div',
  2995. ];
  2996. $this->assertHtml($expected, $result);
  2997. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2998. $expected = [
  2999. 'div' => ['class' => 'input text'],
  3000. 'label' => ['for' => 'custom-id'],
  3001. 'Field',
  3002. '/label',
  3003. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  3004. '/div',
  3005. ];
  3006. $this->assertHtml($expected, $result);
  3007. $result = $this->Form->radio('Model.field', ['option A']);
  3008. $expected = [
  3009. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  3010. 'label' => ['for' => 'prefix-model-field-0'],
  3011. ['input' => [
  3012. 'type' => 'radio',
  3013. 'name' => 'Model[field]',
  3014. 'value' => '0',
  3015. 'id' => 'prefix-model-field-0',
  3016. ]],
  3017. 'option A',
  3018. '/label',
  3019. ];
  3020. $this->assertHtml($expected, $result);
  3021. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  3022. $expected = [
  3023. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  3024. 'label' => ['for' => 'prefix-model-field-0'],
  3025. ['input' => [
  3026. 'type' => 'radio',
  3027. 'name' => 'Model[field]',
  3028. 'value' => '0',
  3029. 'id' => 'prefix-model-field-0',
  3030. ]],
  3031. 'option A',
  3032. '/label',
  3033. ];
  3034. $this->assertHtml($expected, $result);
  3035. $result = $this->Form->select(
  3036. 'Model.multi_field',
  3037. ['first'],
  3038. ['multiple' => 'checkbox']
  3039. );
  3040. $expected = [
  3041. 'input' => [
  3042. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  3043. ],
  3044. ['div' => ['class' => 'checkbox']],
  3045. ['label' => ['for' => 'prefix-model-multi-field-0']],
  3046. ['input' => [
  3047. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3048. 'value' => '0', 'id' => 'prefix-model-multi-field-0',
  3049. ]],
  3050. 'first',
  3051. '/label',
  3052. '/div',
  3053. ];
  3054. $this->assertHtml($expected, $result);
  3055. $this->Form->end();
  3056. $result = $this->Form->control('field');
  3057. $expected = [
  3058. 'div' => ['class' => 'input text'],
  3059. 'label' => ['for' => 'field'],
  3060. 'Field',
  3061. '/label',
  3062. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  3063. '/div',
  3064. ];
  3065. $this->assertHtml($expected, $result);
  3066. }
  3067. /**
  3068. * testControlZero method
  3069. *
  3070. * Test that inputs with 0 can be created.
  3071. *
  3072. * @return void
  3073. */
  3074. public function testControlZero()
  3075. {
  3076. $this->getTableLocator()->get('Contacts', [
  3077. 'className' => __NAMESPACE__ . '\ContactsTable',
  3078. ]);
  3079. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  3080. $result = $this->Form->control('0');
  3081. $expected = [
  3082. 'div' => ['class' => 'input text'],
  3083. 'label' => ['for' => '0'], '/label',
  3084. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  3085. '/div',
  3086. ];
  3087. $this->assertHtml($expected, $result);
  3088. }
  3089. /**
  3090. * testControlCheckbox method
  3091. *
  3092. * Test control() with checkbox creation.
  3093. *
  3094. * @return void
  3095. */
  3096. public function testControlCheckbox()
  3097. {
  3098. $articles = $this->getTableLocator()->get('Articles');
  3099. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3100. $article = $articles->newEntity();
  3101. $this->Form->create($article);
  3102. $result = $this->Form->control('Articles.active');
  3103. $expected = [
  3104. 'div' => ['class' => 'input checkbox'],
  3105. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3106. 'label' => ['for' => 'articles-active'],
  3107. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3108. 'Active',
  3109. '/label',
  3110. '/div',
  3111. ];
  3112. $this->assertHtml($expected, $result);
  3113. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3114. $expected = [
  3115. 'div' => ['class' => 'input checkbox'],
  3116. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3117. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3118. '/div',
  3119. ];
  3120. $this->assertHtml($expected, $result);
  3121. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3122. $expected = [
  3123. 'div' => ['class' => 'input checkbox'],
  3124. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3125. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3126. '/div',
  3127. ];
  3128. $this->assertHtml($expected, $result);
  3129. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3130. $expected = [
  3131. 'div' => ['class' => 'input checkbox'],
  3132. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3133. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3134. '/div',
  3135. ];
  3136. $this->assertHtml($expected, $result);
  3137. $result = $this->Form->control('Articles.disabled', [
  3138. 'label' => 'Disabled',
  3139. 'type' => 'checkbox',
  3140. 'data-foo' => 'disabled',
  3141. ]);
  3142. $expected = [
  3143. 'div' => ['class' => 'input checkbox'],
  3144. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3145. 'label' => ['for' => 'articles-disabled'],
  3146. ['input' => [
  3147. 'type' => 'checkbox',
  3148. 'name' => 'Articles[disabled]',
  3149. 'value' => '1',
  3150. 'id' => 'articles-disabled',
  3151. 'data-foo' => 'disabled',
  3152. ]],
  3153. 'Disabled',
  3154. '/label',
  3155. '/div',
  3156. ];
  3157. $this->assertHtml($expected, $result);
  3158. $result = $this->Form->control('Articles.confirm', [
  3159. 'label' => 'Confirm <b>me</b>!',
  3160. 'type' => 'checkbox',
  3161. 'escape' => false,
  3162. ]);
  3163. $expected = [
  3164. 'div' => ['class' => 'input checkbox'],
  3165. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3166. 'label' => ['for' => 'articles-confirm'],
  3167. ['input' => [
  3168. 'type' => 'checkbox',
  3169. 'name' => 'Articles[confirm]',
  3170. 'value' => '1',
  3171. 'id' => 'articles-confirm',
  3172. ]],
  3173. 'Confirm <b>me</b>!',
  3174. '/label',
  3175. '/div',
  3176. ];
  3177. $this->assertHtml($expected, $result);
  3178. }
  3179. /**
  3180. * testControlHidden method
  3181. *
  3182. * Test that control() does not create wrapping div and label tag for hidden fields.
  3183. *
  3184. * @return void
  3185. */
  3186. public function testControlHidden()
  3187. {
  3188. $this->getTableLocator()->get('ValidateUsers', [
  3189. 'className' => __NAMESPACE__ . '\ValidateUsersTable',
  3190. ]);
  3191. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3192. $result = $this->Form->control('ValidateUser.id');
  3193. $expected = [
  3194. 'input' => ['name', 'type' => 'hidden', 'id'],
  3195. ];
  3196. $this->assertHtml($expected, $result);
  3197. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3198. $expected = [
  3199. 'input' => ['name', 'type' => 'hidden', 'id'],
  3200. ];
  3201. $this->assertHtml($expected, $result);
  3202. }
  3203. /**
  3204. * testControlDatetime method
  3205. *
  3206. * Test form->control() with datetime.
  3207. *
  3208. * @return void
  3209. */
  3210. public function testControlDatetime()
  3211. {
  3212. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3213. ->setMethods(['datetime'])
  3214. ->setConstructorArgs([new View()])
  3215. ->getMock();
  3216. $this->Form->expects($this->once())->method('datetime')
  3217. ->with('prueba', [
  3218. 'type' => 'datetime',
  3219. 'timeFormat' => 24,
  3220. 'minYear' => 2008,
  3221. 'maxYear' => 2011,
  3222. 'interval' => 15,
  3223. 'options' => null,
  3224. 'empty' => false,
  3225. 'id' => 'prueba',
  3226. 'required' => false,
  3227. 'templateVars' => [],
  3228. ])
  3229. ->will($this->returnValue('This is it!'));
  3230. $result = $this->Form->control('prueba', [
  3231. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3232. 'maxYear' => 2011, 'interval' => 15,
  3233. ]);
  3234. $expected = [
  3235. 'div' => ['class' => 'input datetime'],
  3236. '<label',
  3237. 'Prueba',
  3238. '/label',
  3239. 'This is it!',
  3240. '/div',
  3241. ];
  3242. $this->assertHtml($expected, $result);
  3243. }
  3244. /**
  3245. * testControlDatetimeIdPrefix method
  3246. *
  3247. * Test form->control() with datetime with id prefix.
  3248. *
  3249. * @return void
  3250. */
  3251. public function testControlDatetimeIdPrefix()
  3252. {
  3253. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3254. ->setMethods(['datetime'])
  3255. ->setConstructorArgs([new View()])
  3256. ->getMock();
  3257. $this->Form->create(null, ['idPrefix' => 'prefix']);
  3258. $this->Form->expects($this->once())->method('datetime')
  3259. ->with('prueba', [
  3260. 'type' => 'datetime',
  3261. 'timeFormat' => 24,
  3262. 'minYear' => 2008,
  3263. 'maxYear' => 2011,
  3264. 'interval' => 15,
  3265. 'options' => null,
  3266. 'empty' => false,
  3267. 'id' => 'prefix-prueba',
  3268. 'required' => false,
  3269. 'templateVars' => [],
  3270. ])
  3271. ->will($this->returnValue('This is it!'));
  3272. $result = $this->Form->control('prueba', [
  3273. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3274. 'maxYear' => 2011, 'interval' => 15,
  3275. ]);
  3276. $expected = [
  3277. 'div' => ['class' => 'input datetime'],
  3278. '<label',
  3279. 'Prueba',
  3280. '/label',
  3281. 'This is it!',
  3282. '/div',
  3283. ];
  3284. $this->assertHtml($expected, $result);
  3285. }
  3286. /**
  3287. * testControlCheckboxWithDisabledElements method
  3288. *
  3289. * Test generating checkboxes with disabled elements.
  3290. *
  3291. * @return void
  3292. */
  3293. public function testControlCheckboxWithDisabledElements()
  3294. {
  3295. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3296. $result = $this->Form->control('Contact.multiple', [
  3297. 'multiple' => 'checkbox',
  3298. 'disabled' => 'disabled',
  3299. 'options' => $options,
  3300. ]);
  3301. $expected = [
  3302. ['div' => ['class' => 'input select']],
  3303. ['label' => ['for' => 'contact-multiple']],
  3304. 'Multiple',
  3305. '/label',
  3306. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3307. ['div' => ['class' => 'checkbox']],
  3308. ['label' => ['for' => 'contact-multiple-1']],
  3309. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3310. 'One',
  3311. '/label',
  3312. '/div',
  3313. ['div' => ['class' => 'checkbox']],
  3314. ['label' => ['for' => 'contact-multiple-2']],
  3315. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3316. 'Two',
  3317. '/label',
  3318. '/div',
  3319. ['div' => ['class' => 'checkbox']],
  3320. ['label' => ['for' => 'contact-multiple-3']],
  3321. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3322. 'Three',
  3323. '/label',
  3324. '/div',
  3325. '/div',
  3326. ];
  3327. $this->assertHtml($expected, $result);
  3328. // make sure 50 does only disable 50, and not 50f5c0cf
  3329. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3330. $disabled = [50];
  3331. $expected = [
  3332. ['div' => ['class' => 'input select']],
  3333. ['label' => ['for' => 'contact-multiple']],
  3334. 'Multiple',
  3335. '/label',
  3336. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3337. ['div' => ['class' => 'checkbox']],
  3338. ['label' => ['for' => 'contact-multiple-50']],
  3339. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3340. 'Fifty',
  3341. '/label',
  3342. '/div',
  3343. ['div' => ['class' => 'checkbox']],
  3344. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3345. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3346. 'Stringy',
  3347. '/label',
  3348. '/div',
  3349. '/div',
  3350. ];
  3351. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3352. $this->assertHtml($expected, $result);
  3353. }
  3354. /**
  3355. * testControlWithLeadingInteger method
  3356. *
  3357. * Test input name with leading integer, ensure attributes are generated correctly.
  3358. *
  3359. * @return void
  3360. */
  3361. public function testControlWithLeadingInteger()
  3362. {
  3363. $result = $this->Form->text('0.Node.title');
  3364. $expected = [
  3365. 'input' => ['name' => '0[Node][title]', 'type' => 'text'],
  3366. ];
  3367. $this->assertHtml($expected, $result);
  3368. }
  3369. /**
  3370. * testControlSelectType method
  3371. *
  3372. * Test form->control() with select type inputs.
  3373. *
  3374. * @return void
  3375. */
  3376. public function testControlSelectType()
  3377. {
  3378. $result = $this->Form->control(
  3379. 'email',
  3380. [
  3381. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3382. );
  3383. $expected = [
  3384. 'div' => ['class' => 'input select'],
  3385. 'label' => ['for' => 'email'],
  3386. 'Email',
  3387. '/label',
  3388. ['select' => ['name' => 'email', 'id' => 'email']],
  3389. ['option' => ['value' => '']],
  3390. '/option',
  3391. ['option' => ['value' => 'è']],
  3392. 'Firést',
  3393. '/option',
  3394. ['option' => ['value' => 'é']],
  3395. 'Secoènd',
  3396. '/option',
  3397. '/select',
  3398. '/div',
  3399. ];
  3400. $this->assertHtml($expected, $result);
  3401. $result = $this->Form->control(
  3402. 'email',
  3403. [
  3404. 'options' => ['First', 'Second'], 'empty' => true]
  3405. );
  3406. $expected = [
  3407. 'div' => ['class' => 'input select'],
  3408. 'label' => ['for' => 'email'],
  3409. 'Email',
  3410. '/label',
  3411. ['select' => ['name' => 'email', 'id' => 'email']],
  3412. ['option' => ['value' => '']],
  3413. '/option',
  3414. ['option' => ['value' => '0']],
  3415. 'First',
  3416. '/option',
  3417. ['option' => ['value' => '1']],
  3418. 'Second',
  3419. '/option',
  3420. '/select',
  3421. '/div',
  3422. ];
  3423. $this->assertHtml($expected, $result);
  3424. $result = $this->Form->control('email', [
  3425. 'type' => 'select',
  3426. 'options' => new \ArrayObject(['First', 'Second']),
  3427. 'empty' => true,
  3428. ]);
  3429. $this->assertHtml($expected, $result);
  3430. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3431. $this->View->setRequest(
  3432. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3433. );
  3434. $this->Form->create();
  3435. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3436. $expected = [
  3437. 'div' => ['class' => 'input select'],
  3438. 'label' => ['for' => 'model-user-id'],
  3439. 'User',
  3440. '/label',
  3441. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3442. ['option' => ['value' => '']],
  3443. '/option',
  3444. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3445. 'good',
  3446. '/option',
  3447. ['option' => ['value' => 'other']],
  3448. 'bad',
  3449. '/option',
  3450. '/select',
  3451. '/div',
  3452. ];
  3453. $this->assertHtml($expected, $result);
  3454. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3455. $this->View->setRequest(
  3456. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3457. );
  3458. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3459. $expected = [
  3460. 'div' => ['class' => 'input select'],
  3461. 'label' => ['for' => 'thing-user-id'],
  3462. 'User',
  3463. '/label',
  3464. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3465. ['option' => ['value' => '']],
  3466. 'Some Empty',
  3467. '/option',
  3468. ['option' => ['value' => 'value']],
  3469. 'good',
  3470. '/option',
  3471. ['option' => ['value' => 'other']],
  3472. 'bad',
  3473. '/option',
  3474. '/select',
  3475. '/div',
  3476. ];
  3477. $this->assertHtml($expected, $result);
  3478. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3479. $this->View->setRequest(
  3480. $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
  3481. );
  3482. $this->Form->create();
  3483. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3484. $expected = [
  3485. 'div' => ['class' => 'input select'],
  3486. 'label' => ['for' => 'thing-user-id'],
  3487. 'User',
  3488. '/label',
  3489. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3490. ['option' => ['value' => '']],
  3491. 'Some Empty',
  3492. '/option',
  3493. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3494. 'good',
  3495. '/option',
  3496. ['option' => ['value' => 'other']],
  3497. 'bad',
  3498. '/option',
  3499. '/select',
  3500. '/div',
  3501. ];
  3502. $this->assertHtml($expected, $result);
  3503. $result = $this->Form->control('Publisher.id', [
  3504. 'label' => 'Publisher',
  3505. 'type' => 'select',
  3506. 'multiple' => 'checkbox',
  3507. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2'],
  3508. ]);
  3509. $expected = [
  3510. ['div' => ['class' => 'input select']],
  3511. ['label' => ['for' => 'publisher-id']],
  3512. 'Publisher',
  3513. '/label',
  3514. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3515. ['div' => ['class' => 'checkbox']],
  3516. ['label' => ['for' => 'publisher-id-value-1']],
  3517. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3518. 'Label 1',
  3519. '/label',
  3520. '/div',
  3521. ['div' => ['class' => 'checkbox']],
  3522. ['label' => ['for' => 'publisher-id-value-2']],
  3523. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3524. 'Label 2',
  3525. '/label',
  3526. '/div',
  3527. '/div',
  3528. ];
  3529. $this->assertHtml($expected, $result);
  3530. }
  3531. /**
  3532. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3533. *
  3534. * Test that control() and a non standard primary key makes a hidden input by default.
  3535. *
  3536. * @return void
  3537. */
  3538. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3539. {
  3540. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3541. $this->Form->create($this->article);
  3542. $result = $this->Form->control('title');
  3543. $expected = [
  3544. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3545. ];
  3546. $this->assertHtml($expected, $result);
  3547. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3548. $this->Form->create($this->article);
  3549. $result = $this->Form->control('title');
  3550. $expected = [
  3551. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3552. ];
  3553. $this->assertHtml($expected, $result);
  3554. $result = $this->Form->control('body');
  3555. $expected = [
  3556. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3557. ];
  3558. $this->assertHtml($expected, $result);
  3559. }
  3560. /**
  3561. * testControlOverridingMagicSelectType method
  3562. *
  3563. * Test that overriding the magic select type widget is possible.
  3564. *
  3565. * @return void
  3566. */
  3567. public function testControlOverridingMagicSelectType()
  3568. {
  3569. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3570. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3571. $expected = [
  3572. 'div' => ['class' => 'input text'],
  3573. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3574. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3575. '/div',
  3576. ];
  3577. $this->assertHtml($expected, $result);
  3578. //Check that magic types still work for plural/singular vars
  3579. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3580. $result = $this->Form->control('Model.type');
  3581. $expected = [
  3582. 'div' => ['class' => 'input select'],
  3583. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3584. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3585. ['option' => ['value' => 'value']], 'good', '/option',
  3586. ['option' => ['value' => 'other']], 'bad', '/option',
  3587. '/select',
  3588. '/div',
  3589. ];
  3590. $this->assertHtml($expected, $result);
  3591. }
  3592. /**
  3593. * testControlMagicTypeDoesNotOverride method
  3594. *
  3595. * Test that inferred types do not override developer input.
  3596. *
  3597. * @return void
  3598. */
  3599. public function testControlMagicTypeDoesNotOverride()
  3600. {
  3601. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3602. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3603. $expected = [
  3604. 'div' => ['class' => 'input checkbox'],
  3605. ['input' => [
  3606. 'type' => 'hidden',
  3607. 'name' => 'Model[user]',
  3608. 'value' => 0,
  3609. ]],
  3610. 'label' => ['for' => 'model-user'],
  3611. ['input' => [
  3612. 'name' => 'Model[user]',
  3613. 'type' => 'checkbox',
  3614. 'id' => 'model-user',
  3615. 'value' => 1,
  3616. ]],
  3617. 'User',
  3618. '/label',
  3619. '/div',
  3620. ];
  3621. $this->assertHtml($expected, $result);
  3622. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3623. $options = [
  3624. 1 => 'blue',
  3625. 2 => 'red',
  3626. ];
  3627. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3628. $expected = [
  3629. 'div' => ['class' => 'input select'],
  3630. 'label' => ['for' => 'tags-ids'],
  3631. 'Tags',
  3632. '/label',
  3633. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3634. ['div' => ['class' => 'checkbox']],
  3635. ['label' => ['for' => 'tags-ids-1']],
  3636. ['input' => [
  3637. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3638. 'value' => '1', 'name' => 'tags[_ids][]',
  3639. ]],
  3640. 'blue',
  3641. '/label',
  3642. '/div',
  3643. ['div' => ['class' => 'checkbox']],
  3644. ['label' => ['for' => 'tags-ids-2']],
  3645. ['input' => [
  3646. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3647. 'value' => '2', 'name' => 'tags[_ids][]',
  3648. ]],
  3649. 'red',
  3650. '/label',
  3651. '/div',
  3652. '/div',
  3653. ];
  3654. $this->assertHtml($expected, $result);
  3655. }
  3656. /**
  3657. * testControlMagicSelectForTypeNumber method
  3658. *
  3659. * Test that magic control() selects are created for type=number.
  3660. *
  3661. * @return void
  3662. */
  3663. public function testControlMagicSelectForTypeNumber()
  3664. {
  3665. $this->getTableLocator()->get('ValidateUsers', [
  3666. 'className' => __NAMESPACE__ . '\ValidateUsersTable',
  3667. ]);
  3668. $entity = new Entity(['balance' => 1]);
  3669. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3670. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3671. $result = $this->Form->control('balance');
  3672. $expected = [
  3673. 'div' => ['class' => 'input select'],
  3674. 'label' => ['for' => 'balance'],
  3675. 'Balance',
  3676. '/label',
  3677. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3678. ['option' => ['value' => '0']],
  3679. 'nothing',
  3680. '/option',
  3681. ['option' => ['value' => '1', 'selected' => 'selected']],
  3682. 'some',
  3683. '/option',
  3684. ['option' => ['value' => '100']],
  3685. 'a lot',
  3686. '/option',
  3687. '/select',
  3688. '/div',
  3689. ];
  3690. $this->assertHtml($expected, $result);
  3691. }
  3692. /**
  3693. * testInvalidControlTypeOption method
  3694. *
  3695. * Test invalid 'input' type option to control() function.
  3696. *
  3697. * @return void
  3698. */
  3699. public function testInvalidControlTypeOption()
  3700. {
  3701. $this->expectException(\RuntimeException::class);
  3702. $this->expectExceptionMessage('Invalid type \'input\' used for field \'text\'');
  3703. $this->Form->control('text', ['type' => 'input']);
  3704. }
  3705. /**
  3706. * testControlMagicSelectChangeToRadio method
  3707. *
  3708. * Test that magic control() selects can easily be converted into radio types without error.
  3709. *
  3710. * @return void
  3711. */
  3712. public function testControlMagicSelectChangeToRadio()
  3713. {
  3714. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3715. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3716. $this->assertContains('input type="radio"', $result);
  3717. }
  3718. /**
  3719. * testFormControlSubmit method
  3720. *
  3721. * Test correct results for form::control() and type submit.
  3722. *
  3723. * @return void
  3724. */
  3725. public function testFormControlSubmit()
  3726. {
  3727. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3728. $expected = [
  3729. 'div' => ['class' => 'submit'],
  3730. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3731. '/div',
  3732. ];
  3733. $this->assertHtml($expected, $result);
  3734. }
  3735. /**
  3736. * testFormControls method
  3737. *
  3738. * Test correct results from Form::controls().
  3739. *
  3740. * @return void
  3741. */
  3742. public function testFormControlsLegendFieldset()
  3743. {
  3744. $this->Form->create($this->article);
  3745. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3746. $expected = [
  3747. '<fieldset',
  3748. '<legend',
  3749. 'The Legend',
  3750. '/legend',
  3751. '*/fieldset',
  3752. ];
  3753. $this->assertHtml($expected, $result);
  3754. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3755. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3756. $this->assertContains('<fieldset>', $result);
  3757. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3758. $this->assertNotContains('<legend>', $result);
  3759. $this->assertNotContains('<fieldset>', $result);
  3760. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3761. $this->assertNotContains('<legend>', $result);
  3762. $this->assertNotContains('<fieldset>', $result);
  3763. $this->Form->create($this->article);
  3764. $this->View->setRequest($this->View->getRequest()
  3765. ->withParam('prefix', 'admin')
  3766. ->withParam('action', 'admin_edit')
  3767. ->withParam('controller', 'articles'));
  3768. $result = $this->Form->allControls();
  3769. $expected = [
  3770. '<fieldset',
  3771. '<legend',
  3772. 'New Article',
  3773. '/legend',
  3774. '*/fieldset',
  3775. ];
  3776. $this->assertHtml($expected, $result);
  3777. $this->Form->create($this->article);
  3778. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3779. $expected = [
  3780. '<fieldset',
  3781. '<legend',
  3782. 'The Legend',
  3783. '/legend',
  3784. '*/fieldset',
  3785. ];
  3786. $this->assertHtml($expected, $result);
  3787. $this->Form->create($this->article);
  3788. $result = $this->Form->allControls([], [
  3789. 'fieldset' => [
  3790. 'class' => 'some-class some-other-class',
  3791. 'disabled' => true,
  3792. 'data-param' => 'a-param',
  3793. ],
  3794. 'legend' => 'The Legend',
  3795. ]);
  3796. $expected = [
  3797. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3798. '<legend',
  3799. 'The Legend',
  3800. '/legend',
  3801. '*/fieldset',
  3802. ];
  3803. $this->assertHtml($expected, $result);
  3804. }
  3805. /**
  3806. * testFormControls method
  3807. *
  3808. * Test the controls() method.
  3809. *
  3810. * @return void
  3811. */
  3812. public function testFormControls()
  3813. {
  3814. $this->Form->create($this->article);
  3815. $result = $this->Form->allControls();
  3816. $expected = [
  3817. '<fieldset',
  3818. '<legend', 'New Article', '/legend',
  3819. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3820. ['div' => ['class' => 'input select required']],
  3821. '*/div',
  3822. ['div' => ['class' => 'input text required']],
  3823. '*/div',
  3824. ['div' => ['class' => 'input text']],
  3825. '*/div',
  3826. ['div' => ['class' => 'input text']],
  3827. '*/div',
  3828. '/fieldset',
  3829. ];
  3830. $this->assertHtml($expected, $result);
  3831. $result = $this->Form->allControls([
  3832. 'published' => ['type' => 'boolean'],
  3833. ]);
  3834. $expected = [
  3835. '<fieldset',
  3836. '<legend', 'New Article', '/legend',
  3837. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3838. ['div' => ['class' => 'input select required']],
  3839. '*/div',
  3840. ['div' => ['class' => 'input text required']],
  3841. '*/div',
  3842. ['div' => ['class' => 'input text']],
  3843. '*/div',
  3844. ['div' => ['class' => 'input boolean']],
  3845. '*/div',
  3846. '/fieldset',
  3847. ];
  3848. $this->assertHtml($expected, $result);
  3849. $this->Form->create($this->article);
  3850. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3851. $expected = [
  3852. 'fieldset' => [],
  3853. 'legend' => [],
  3854. 'Hello',
  3855. '/legend',
  3856. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3857. ['div' => ['class' => 'input select required']],
  3858. '*/div',
  3859. ['div' => ['class' => 'input text required']],
  3860. '*/div',
  3861. ['div' => ['class' => 'input text']],
  3862. '*/div',
  3863. ['div' => ['class' => 'input text']],
  3864. '*/div',
  3865. '/fieldset',
  3866. ];
  3867. $this->assertHtml($expected, $result);
  3868. $this->Form->create();
  3869. $expected = [
  3870. 'fieldset' => [],
  3871. ['div' => ['class' => 'input text']],
  3872. 'label' => ['for' => 'foo'],
  3873. 'Foo',
  3874. '/label',
  3875. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3876. '*/div',
  3877. '/fieldset',
  3878. ];
  3879. $result = $this->Form->allControls(
  3880. ['foo' => ['type' => 'text']],
  3881. ['legend' => false]
  3882. );
  3883. $this->assertHtml($expected, $result);
  3884. }
  3885. /**
  3886. * testFormControlsBlacklist method
  3887. *
  3888. * @return void
  3889. */
  3890. public function testFormControlsBlacklist()
  3891. {
  3892. $this->Form->create($this->article);
  3893. $result = $this->Form->allControls([
  3894. 'id' => false,
  3895. ]);
  3896. $expected = [
  3897. '<fieldset',
  3898. '<legend', 'New Article', '/legend',
  3899. ['div' => ['class' => 'input select required']],
  3900. '*/div',
  3901. ['div' => ['class' => 'input text required']],
  3902. '*/div',
  3903. ['div' => ['class' => 'input text']],
  3904. '*/div',
  3905. ['div' => ['class' => 'input text']],
  3906. '*/div',
  3907. '/fieldset',
  3908. ];
  3909. $this->assertHtml($expected, $result);
  3910. $this->Form->create($this->article);
  3911. $result = $this->Form->allControls([
  3912. 'id' => [],
  3913. ]);
  3914. $expected = [
  3915. '<fieldset',
  3916. '<legend', 'New Article', '/legend',
  3917. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3918. ['div' => ['class' => 'input select required']],
  3919. '*/div',
  3920. ['div' => ['class' => 'input text required']],
  3921. '*/div',
  3922. ['div' => ['class' => 'input text']],
  3923. '*/div',
  3924. ['div' => ['class' => 'input text']],
  3925. '*/div',
  3926. '/fieldset',
  3927. ];
  3928. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3929. }
  3930. /**
  3931. * testSelectAsCheckbox method
  3932. *
  3933. * Test multi-select widget with checkbox formatting.
  3934. *
  3935. * @return void
  3936. */
  3937. public function testSelectAsCheckbox()
  3938. {
  3939. $result = $this->Form->select(
  3940. 'Model.multi_field',
  3941. ['first', 'second', 'third'],
  3942. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3943. );
  3944. $expected = [
  3945. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3946. ['div' => ['class' => 'checkbox']],
  3947. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3948. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3949. 'first',
  3950. '/label',
  3951. '/div',
  3952. ['div' => ['class' => 'checkbox']],
  3953. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3954. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3955. 'second',
  3956. '/label',
  3957. '/div',
  3958. ['div' => ['class' => 'checkbox']],
  3959. ['label' => ['for' => 'model-multi-field-2']],
  3960. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3961. 'third',
  3962. '/label',
  3963. '/div',
  3964. ];
  3965. $this->assertHtml($expected, $result);
  3966. $result = $this->Form->select(
  3967. 'Model.multi_field',
  3968. ['1/2' => 'half'],
  3969. ['multiple' => 'checkbox']
  3970. );
  3971. $expected = [
  3972. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3973. ['div' => ['class' => 'checkbox']],
  3974. ['label' => ['for' => 'model-multi-field-1-2']],
  3975. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3976. 'half',
  3977. '/label',
  3978. '/div',
  3979. ];
  3980. $this->assertHtml($expected, $result);
  3981. }
  3982. /**
  3983. * testLabel method
  3984. *
  3985. * Test label generation.
  3986. *
  3987. * @return void
  3988. */
  3989. public function testLabel()
  3990. {
  3991. $result = $this->Form->label('Person.name');
  3992. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3993. $this->assertHtml($expected, $result);
  3994. $result = $this->Form->label('Person.name');
  3995. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3996. $this->assertHtml($expected, $result);
  3997. $result = $this->Form->label('Person.first_name');
  3998. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3999. $this->assertHtml($expected, $result);
  4000. $result = $this->Form->label('Person.first_name', 'Your first name');
  4001. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  4002. $this->assertHtml($expected, $result);
  4003. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  4004. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  4005. $this->assertHtml($expected, $result);
  4006. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  4007. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  4008. $this->assertHtml($expected, $result);
  4009. $result = $this->Form->label('Person.first_name', '');
  4010. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  4011. $this->assertHtml($expected, $result);
  4012. $result = $this->Form->label('Person.2.name', '');
  4013. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  4014. $this->assertHtml($expected, $result);
  4015. }
  4016. /**
  4017. * testLabelContainControl method
  4018. *
  4019. * Test that label() can accept an input with the correct template vars.
  4020. *
  4021. * @return void
  4022. */
  4023. public function testLabelContainControl()
  4024. {
  4025. $this->Form->setTemplates([
  4026. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4027. ]);
  4028. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  4029. 'input' => '<input type="checkbox" name="accept_tos"/>',
  4030. ]);
  4031. $expected = [
  4032. 'label' => ['for' => 'person-accept-terms'],
  4033. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  4034. 'Accept',
  4035. '/label',
  4036. ];
  4037. $this->assertHtml($expected, $result);
  4038. }
  4039. /**
  4040. * testTextbox method
  4041. *
  4042. * Test textbox element generation.
  4043. *
  4044. * @return void
  4045. */
  4046. public function testTextbox()
  4047. {
  4048. $result = $this->Form->text('Model.field');
  4049. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  4050. $this->assertHtml($expected, $result);
  4051. $result = $this->Form->text('Model.field', ['type' => 'password']);
  4052. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  4053. $this->assertHtml($expected, $result);
  4054. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  4055. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  4056. $this->assertHtml($expected, $result);
  4057. }
  4058. /**
  4059. * testTextBoxDataAndError method
  4060. *
  4061. * Test that text() hooks up with request data and error fields.
  4062. *
  4063. * @return void
  4064. */
  4065. public function testTextBoxDataAndError()
  4066. {
  4067. $this->article['errors'] = [
  4068. 'Contact' => ['text' => 'wrong'],
  4069. ];
  4070. $this->View->setRequest($this->View->getRequest()
  4071. ->withData('Model.text', 'test <strong>HTML</strong> values')
  4072. ->withData('Contact.text', 'test'));
  4073. $this->Form->create($this->article);
  4074. $result = $this->Form->text('Model.text');
  4075. $expected = [
  4076. 'input' => [
  4077. 'type' => 'text',
  4078. 'name' => 'Model[text]',
  4079. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  4080. ],
  4081. ];
  4082. $this->assertHtml($expected, $result);
  4083. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  4084. $expected = [
  4085. 'input' => [
  4086. 'type' => 'text',
  4087. 'name' => 'Contact[text]',
  4088. 'value' => 'test',
  4089. 'id' => 'theID',
  4090. 'class' => 'form-error',
  4091. ],
  4092. ];
  4093. $this->assertHtml($expected, $result);
  4094. }
  4095. /**
  4096. * testDefaultValue method
  4097. *
  4098. * Test default value setting.
  4099. *
  4100. * @return void
  4101. */
  4102. public function testTextDefaultValue()
  4103. {
  4104. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  4105. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4106. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  4107. $this->assertHtml($expected, $result);
  4108. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4109. $this->Form->create();
  4110. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4111. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4112. $this->assertHtml($expected, $result);
  4113. $Articles = $this->getTableLocator()->get('Articles');
  4114. $title = $Articles->getSchema()->getColumn('title');
  4115. $Articles->getSchema()->addColumn(
  4116. 'title',
  4117. ['default' => 'default title'] + $title
  4118. );
  4119. $entity = $Articles->newEntity();
  4120. $this->Form->create($entity);
  4121. // Get default value from schema
  4122. $result = $this->Form->text('title');
  4123. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4124. $this->assertHtml($expected, $result);
  4125. // Don't get value from schema
  4126. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4127. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4128. $this->assertHtml($expected, $result);
  4129. // Custom default value overrides default value from schema
  4130. $result = $this->Form->text('title', ['default' => 'override default']);
  4131. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4132. $this->assertHtml($expected, $result);
  4133. // Default value from schema is used only for new entities.
  4134. $entity->setNew(false);
  4135. $result = $this->Form->text('title');
  4136. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4137. $this->assertHtml($expected, $result);
  4138. }
  4139. /**
  4140. * testError method
  4141. *
  4142. * Test field error generation.
  4143. *
  4144. * @return void
  4145. */
  4146. public function testError()
  4147. {
  4148. $this->article['errors'] = [
  4149. 'Article' => ['field' => 'email'],
  4150. ];
  4151. $this->Form->create($this->article);
  4152. $result = $this->Form->error('Article.field');
  4153. $expected = [
  4154. ['div' => ['class' => 'error-message']],
  4155. 'email',
  4156. '/div',
  4157. ];
  4158. $this->assertHtml($expected, $result);
  4159. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4160. $expected = [
  4161. ['div' => ['class' => 'error-message']],
  4162. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4163. '/div',
  4164. ];
  4165. $this->assertHtml($expected, $result);
  4166. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4167. $expected = [
  4168. ['div' => ['class' => 'error-message']],
  4169. '<strong', 'Badness!', '/strong',
  4170. '/div',
  4171. ];
  4172. $this->assertHtml($expected, $result);
  4173. }
  4174. /**
  4175. * testErrorRuleName method
  4176. *
  4177. * Test error translation can use rule names for translating.
  4178. *
  4179. * @return void
  4180. */
  4181. public function testErrorRuleName()
  4182. {
  4183. $this->article['errors'] = [
  4184. 'Article' => [
  4185. 'field' => ['email' => 'Your email was not good'],
  4186. ],
  4187. ];
  4188. $this->Form->create($this->article);
  4189. $result = $this->Form->error('Article.field');
  4190. $expected = [
  4191. ['div' => ['class' => 'error-message']],
  4192. 'Your email was not good',
  4193. '/div',
  4194. ];
  4195. $this->assertHtml($expected, $result);
  4196. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4197. $expected = [
  4198. ['div' => ['class' => 'error-message']],
  4199. 'Email in use',
  4200. '/div',
  4201. ];
  4202. $this->assertHtml($expected, $result);
  4203. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4204. $expected = [
  4205. ['div' => ['class' => 'error-message']],
  4206. 'Email in use',
  4207. '/div',
  4208. ];
  4209. $this->assertHtml($expected, $result);
  4210. $result = $this->Form->error('Article.field', [
  4211. 'email' => 'Key is preferred',
  4212. 'Your email was not good' => 'Email in use',
  4213. ]);
  4214. $expected = [
  4215. ['div' => ['class' => 'error-message']],
  4216. 'Key is preferred',
  4217. '/div',
  4218. ];
  4219. $this->assertHtml($expected, $result);
  4220. }
  4221. /**
  4222. * testErrorMessages method
  4223. *
  4224. * Test error with nested lists.
  4225. *
  4226. * @return void
  4227. */
  4228. public function testErrorMessages()
  4229. {
  4230. $this->article['errors'] = [
  4231. 'Article' => ['field' => 'email'],
  4232. ];
  4233. $this->Form->create($this->article);
  4234. $result = $this->Form->error('Article.field', [
  4235. 'email' => 'No good!',
  4236. ]);
  4237. $expected = [
  4238. 'div' => ['class' => 'error-message'],
  4239. 'No good!',
  4240. '/div',
  4241. ];
  4242. $this->assertHtml($expected, $result);
  4243. }
  4244. /**
  4245. * testErrorMultipleMessages method
  4246. *
  4247. * Test error() with multiple messages.
  4248. *
  4249. * @return void
  4250. */
  4251. public function testErrorMultipleMessages()
  4252. {
  4253. $this->article['errors'] = [
  4254. 'field' => ['notBlank', 'email', 'Something else'],
  4255. ];
  4256. $this->Form->create($this->article);
  4257. $result = $this->Form->error('field', [
  4258. 'notBlank' => 'Cannot be empty',
  4259. 'email' => 'No good!',
  4260. ]);
  4261. $expected = [
  4262. 'div' => ['class' => 'error-message'],
  4263. 'ul' => [],
  4264. '<li', 'Cannot be empty', '/li',
  4265. '<li', 'No good!', '/li',
  4266. '<li', 'Something else', '/li',
  4267. '/ul',
  4268. '/div',
  4269. ];
  4270. $this->assertHtml($expected, $result);
  4271. }
  4272. /**
  4273. * testPassword method
  4274. *
  4275. * Test password element generation.
  4276. *
  4277. * @return void
  4278. */
  4279. public function testPassword()
  4280. {
  4281. $this->article['errors'] = [
  4282. 'Contact' => [
  4283. 'passwd' => 1,
  4284. ],
  4285. ];
  4286. $this->Form->create($this->article);
  4287. $result = $this->Form->password('Contact.field');
  4288. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4289. $this->assertHtml($expected, $result);
  4290. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4291. $this->Form->create($this->article);
  4292. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4293. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4294. $this->assertHtml($expected, $result);
  4295. }
  4296. /**
  4297. * testRadio method
  4298. *
  4299. * Test radio element set generation.
  4300. *
  4301. * @return void
  4302. */
  4303. public function testRadio()
  4304. {
  4305. $result = $this->Form->radio('Model.field', ['option A']);
  4306. $expected = [
  4307. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4308. 'label' => ['for' => 'model-field-0'],
  4309. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4310. 'option A',
  4311. '/label',
  4312. ];
  4313. $this->assertHtml($expected, $result);
  4314. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4315. $this->assertHtml($expected, $result);
  4316. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4317. $expected = [
  4318. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4319. ['label' => ['for' => 'model-field-0']],
  4320. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4321. 'option A',
  4322. '/label',
  4323. ['label' => ['for' => 'model-field-1']],
  4324. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4325. 'option B',
  4326. '/label',
  4327. ];
  4328. $this->assertHtml($expected, $result);
  4329. $result = $this->Form->radio(
  4330. 'Employee.gender',
  4331. ['male' => 'Male', 'female' => 'Female'],
  4332. ['form' => 'my-form']
  4333. );
  4334. $expected = [
  4335. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4336. ['label' => ['for' => 'employee-gender-male']],
  4337. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4338. 'Male',
  4339. '/label',
  4340. ['label' => ['for' => 'employee-gender-female']],
  4341. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4342. 'Female',
  4343. '/label',
  4344. ];
  4345. $this->assertHtml($expected, $result);
  4346. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4347. $expected = [
  4348. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4349. ['label' => ['for' => 'model-custom-0']],
  4350. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4351. 'option A',
  4352. '/label',
  4353. ['label' => ['for' => 'model-custom-1']],
  4354. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4355. 'option B',
  4356. '/label',
  4357. ];
  4358. $this->assertHtml($expected, $result);
  4359. $result = $this->Form->radio(
  4360. 'Employee.gender',
  4361. [
  4362. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4363. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4364. ]
  4365. );
  4366. $expected = [
  4367. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4368. ['label' => ['for' => 'employee-gender-male']],
  4369. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4370. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4371. 'Male',
  4372. '/label',
  4373. ['label' => ['for' => 'employee-gender-female']],
  4374. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4375. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4376. 'Female',
  4377. '/label',
  4378. ];
  4379. $this->assertHtml($expected, $result);
  4380. }
  4381. /**
  4382. * Test radio with complex options and empty disabled data.
  4383. *
  4384. * @return void
  4385. */
  4386. public function testRadioComplexDisabled()
  4387. {
  4388. $options = [
  4389. ['value' => 'r', 'text' => 'red'],
  4390. ['value' => 'b', 'text' => 'blue'],
  4391. ];
  4392. $attrs = ['disabled' => []];
  4393. $result = $this->Form->radio('Model.field', $options, $attrs);
  4394. $expected = [
  4395. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4396. ['label' => ['for' => 'model-field-r']],
  4397. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4398. 'red',
  4399. '/label',
  4400. ['label' => ['for' => 'model-field-b']],
  4401. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4402. 'blue',
  4403. '/label',
  4404. ];
  4405. $this->assertHtml($expected, $result);
  4406. $attrs = ['disabled' => ['r']];
  4407. $result = $this->Form->radio('Model.field', $options, $attrs);
  4408. $this->assertContains('disabled="disabled"', $result);
  4409. }
  4410. /**
  4411. * testRadioDefaultValue method
  4412. *
  4413. * Test default value setting on radio() method.
  4414. *
  4415. * @return void
  4416. */
  4417. public function testRadioDefaultValue()
  4418. {
  4419. $Articles = $this->getTableLocator()->get('Articles');
  4420. $title = $Articles->getSchema()->getColumn('title');
  4421. $Articles->getSchema()->addColumn(
  4422. 'title',
  4423. ['default' => '1'] + $title
  4424. );
  4425. $this->Form->create($Articles->newEntity());
  4426. $result = $this->Form->radio('title', ['option A', 'option B']);
  4427. $expected = [
  4428. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4429. ['label' => ['for' => 'title-0']],
  4430. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4431. 'option A',
  4432. '/label',
  4433. ['label' => ['for' => 'title-1']],
  4434. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4435. 'option B',
  4436. '/label',
  4437. ];
  4438. $this->assertHtml($expected, $result);
  4439. }
  4440. /**
  4441. * Test setting a hiddenField value on radio buttons.
  4442. *
  4443. * @return void
  4444. */
  4445. public function testRadioHiddenFieldValue()
  4446. {
  4447. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4448. $expected = [
  4449. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4450. 'label' => ['for' => 'title-0'],
  4451. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4452. 'option A',
  4453. '/label',
  4454. ];
  4455. $this->assertHtml($expected, $result);
  4456. }
  4457. /**
  4458. * testControlRadio method
  4459. *
  4460. * Test that input works with radio types.
  4461. *
  4462. * @return void
  4463. */
  4464. public function testControlRadio()
  4465. {
  4466. $result = $this->Form->control('test', [
  4467. 'type' => 'radio',
  4468. 'options' => ['A', 'B'],
  4469. ]);
  4470. $expected = [
  4471. ['div' => ['class' => 'input radio']],
  4472. '<label',
  4473. 'Test',
  4474. '/label',
  4475. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4476. ['label' => ['for' => 'test-0']],
  4477. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4478. 'A',
  4479. '/label',
  4480. ['label' => ['for' => 'test-1']],
  4481. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4482. 'B',
  4483. '/label',
  4484. '/div',
  4485. ];
  4486. $this->assertHtml($expected, $result);
  4487. $result = $this->Form->control('test', [
  4488. 'type' => 'radio',
  4489. 'options' => ['A', 'B'],
  4490. 'value' => '0',
  4491. ]);
  4492. $expected = [
  4493. ['div' => ['class' => 'input radio']],
  4494. '<label',
  4495. 'Test',
  4496. '/label',
  4497. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4498. ['label' => ['for' => 'test-0']],
  4499. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4500. 'A',
  4501. '/label',
  4502. ['label' => ['for' => 'test-1']],
  4503. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4504. 'B',
  4505. '/label',
  4506. '/div',
  4507. ];
  4508. $this->assertHtml($expected, $result);
  4509. $result = $this->Form->control('test', [
  4510. 'type' => 'radio',
  4511. 'options' => ['A', 'B'],
  4512. 'label' => false,
  4513. ]);
  4514. $expected = [
  4515. ['div' => ['class' => 'input radio']],
  4516. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4517. ['label' => ['for' => 'test-0']],
  4518. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4519. 'A',
  4520. '/label',
  4521. ['label' => ['for' => 'test-1']],
  4522. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4523. 'B',
  4524. '/label',
  4525. '/div',
  4526. ];
  4527. $this->assertHtml($expected, $result);
  4528. $result = $this->Form->control('accept', [
  4529. 'type' => 'radio',
  4530. 'options' => [
  4531. 1 => 'positive',
  4532. -1 => 'negative',
  4533. ],
  4534. 'label' => false,
  4535. ]);
  4536. $expected = [
  4537. ['div' => ['class' => 'input radio']],
  4538. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => '']],
  4539. ['label' => ['for' => 'accept-1']],
  4540. ['input' => [
  4541. 'type' => 'radio',
  4542. 'name' => 'accept',
  4543. 'value' => '1',
  4544. 'id' => 'accept-1',
  4545. ]],
  4546. 'positive',
  4547. '/label',
  4548. ['label' => ['for' => 'accept--1']],
  4549. ['input' => [
  4550. 'type' => 'radio',
  4551. 'name' => 'accept',
  4552. 'value' => '-1',
  4553. 'id' => 'accept--1',
  4554. ]],
  4555. 'negative',
  4556. '/label',
  4557. '/div',
  4558. ];
  4559. $this->assertHtml($expected, $result);
  4560. }
  4561. /**
  4562. * testRadioNoLabel method
  4563. *
  4564. * Test that radio() works with label = false.
  4565. *
  4566. * @return void
  4567. */
  4568. public function testRadioNoLabel()
  4569. {
  4570. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4571. $expected = [
  4572. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4573. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4574. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4575. ];
  4576. $this->assertHtml($expected, $result);
  4577. }
  4578. /**
  4579. * testRadioControlInsideLabel method
  4580. *
  4581. * Test generating radio input inside label ala twitter bootstrap.
  4582. *
  4583. * @return void
  4584. */
  4585. public function testRadioControlInsideLabel()
  4586. {
  4587. $this->Form->setTemplates([
  4588. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4589. 'radioWrapper' => '{{label}}',
  4590. ]);
  4591. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4592. //@codingStandardsIgnoreStart
  4593. $expected = [
  4594. ['input' => [
  4595. 'type' => 'hidden',
  4596. 'name' => 'Model[field]',
  4597. 'value' => ''
  4598. ]],
  4599. ['label' => ['for' => 'model-field-0']],
  4600. ['input' => [
  4601. 'type' => 'radio',
  4602. 'name' => 'Model[field]',
  4603. 'value' => '0',
  4604. 'id' => 'model-field-0'
  4605. ]],
  4606. 'option A',
  4607. '/label',
  4608. ['label' => ['for' => 'model-field-1']],
  4609. ['input' => [
  4610. 'type' => 'radio',
  4611. 'name' => 'Model[field]',
  4612. 'value' => '1',
  4613. 'id' => 'model-field-1'
  4614. ]],
  4615. 'option B',
  4616. '/label',
  4617. ];
  4618. //@codingStandardsIgnoreEnd
  4619. $this->assertHtml($expected, $result);
  4620. }
  4621. /**
  4622. * testRadioHiddenControlDisabling method
  4623. *
  4624. * Test disabling the hidden input for radio buttons.
  4625. *
  4626. * @return void
  4627. */
  4628. public function testRadioHiddenControlDisabling()
  4629. {
  4630. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4631. $expected = [
  4632. 'label' => ['for' => 'model-1-field-0'],
  4633. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4634. 'option A',
  4635. '/label',
  4636. ];
  4637. $this->assertHtml($expected, $result);
  4638. }
  4639. /**
  4640. * testRadioOutOfRange method
  4641. *
  4642. * Test radio element set generation.
  4643. *
  4644. * @return void
  4645. */
  4646. public function testRadioOutOfRange()
  4647. {
  4648. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4649. $expected = [
  4650. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4651. 'label' => ['for' => 'model-field-v'],
  4652. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4653. 'value',
  4654. '/label',
  4655. ];
  4656. $this->assertHtml($expected, $result);
  4657. }
  4658. /**
  4659. * testSelect method
  4660. *
  4661. * Test select element generation.
  4662. *
  4663. * @return void
  4664. */
  4665. public function testSelect()
  4666. {
  4667. $result = $this->Form->select('Model.field', []);
  4668. $expected = [
  4669. 'select' => ['name' => 'Model[field]'],
  4670. '/select',
  4671. ];
  4672. $this->assertHtml($expected, $result);
  4673. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4674. $this->Form->create();
  4675. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4676. $expected = [
  4677. 'select' => ['name' => 'Model[field]'],
  4678. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4679. 'good',
  4680. '/option',
  4681. ['option' => ['value' => 'other']],
  4682. 'bad',
  4683. '/option',
  4684. '/select',
  4685. ];
  4686. $this->assertHtml($expected, $result);
  4687. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4688. $this->assertHtml($expected, $result);
  4689. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4690. $this->Form->create();
  4691. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4692. $expected = [
  4693. 'select' => ['name' => 'Model[field]'],
  4694. ['option' => ['value' => 'value']],
  4695. 'good',
  4696. '/option',
  4697. ['option' => ['value' => 'other']],
  4698. 'bad',
  4699. '/option',
  4700. '/select',
  4701. ];
  4702. $this->assertHtml($expected, $result);
  4703. $options = [
  4704. ['value' => 'first', 'text' => 'First'],
  4705. ['value' => 'first', 'text' => 'Another First'],
  4706. ];
  4707. $result = $this->Form->select(
  4708. 'Model.field',
  4709. $options,
  4710. ['escape' => false, 'empty' => false]
  4711. );
  4712. $expected = [
  4713. 'select' => ['name' => 'Model[field]'],
  4714. ['option' => ['value' => 'first']],
  4715. 'First',
  4716. '/option',
  4717. ['option' => ['value' => 'first']],
  4718. 'Another First',
  4719. '/option',
  4720. '/select',
  4721. ];
  4722. $this->assertHtml($expected, $result);
  4723. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4724. $this->Form->create();
  4725. $result = $this->Form->select(
  4726. 'Model.contact_id',
  4727. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4728. ['escape' => false, 'empty' => 'pick something']
  4729. );
  4730. $expected = [
  4731. 'select' => ['name' => 'Model[contact_id]'],
  4732. ['option' => ['value' => '']], 'pick something', '/option',
  4733. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4734. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4735. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4736. '/select',
  4737. ];
  4738. $this->assertHtml($expected, $result);
  4739. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4740. $this->Form->create();
  4741. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4742. $expected = [
  4743. 'select' => ['name' => 'Model[field]'],
  4744. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4745. ['option' => ['value' => '1']], 'Yes', '/option',
  4746. '/select',
  4747. ];
  4748. $this->assertHtml($expected, $result);
  4749. }
  4750. /**
  4751. * testSelectEscapeHtml method
  4752. *
  4753. * Test that select() escapes HTML.
  4754. *
  4755. * @return void
  4756. */
  4757. public function testSelectEscapeHtml()
  4758. {
  4759. $result = $this->Form->select(
  4760. 'Model.field',
  4761. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4762. ['empty' => false]
  4763. );
  4764. $expected = [
  4765. 'select' => ['name' => 'Model[field]'],
  4766. ['option' => ['value' => 'first']],
  4767. 'first &quot;html&quot; &lt;chars&gt;',
  4768. '/option',
  4769. ['option' => ['value' => 'second']],
  4770. 'value',
  4771. '/option',
  4772. '/select',
  4773. ];
  4774. $this->assertHtml($expected, $result);
  4775. $result = $this->Form->select(
  4776. 'Model.field',
  4777. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4778. ['escape' => false, 'empty' => false]
  4779. );
  4780. $expected = [
  4781. 'select' => ['name' => 'Model[field]'],
  4782. ['option' => ['value' => 'first']],
  4783. 'first "html" <chars>',
  4784. '/option',
  4785. ['option' => ['value' => 'second']],
  4786. 'value',
  4787. '/option',
  4788. '/select',
  4789. ];
  4790. $this->assertHtml($expected, $result);
  4791. }
  4792. /**
  4793. * testSelectRequired method
  4794. *
  4795. * Test select() with required and disabled attributes.
  4796. *
  4797. * @return void
  4798. */
  4799. public function testSelectRequired()
  4800. {
  4801. $this->article['required'] = [
  4802. 'user_id' => true,
  4803. ];
  4804. $this->Form->create($this->article);
  4805. $result = $this->Form->select('user_id', ['option A']);
  4806. $expected = [
  4807. 'select' => [
  4808. 'name' => 'user_id',
  4809. 'required' => 'required',
  4810. ],
  4811. ['option' => ['value' => '0']], 'option A', '/option',
  4812. '/select',
  4813. ];
  4814. $this->assertHtml($expected, $result);
  4815. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4816. $expected = [
  4817. 'select' => [
  4818. 'name' => 'user_id',
  4819. 'disabled' => 'disabled',
  4820. ],
  4821. ['option' => ['value' => '0']], 'option A', '/option',
  4822. '/select',
  4823. ];
  4824. $this->assertHtml($expected, $result);
  4825. }
  4826. /**
  4827. * testNestedSelect method
  4828. *
  4829. * Test select element generation with optgroups.
  4830. *
  4831. * @return void
  4832. */
  4833. public function testNestedSelect()
  4834. {
  4835. $result = $this->Form->select(
  4836. 'Model.field',
  4837. [1 => 'One', 2 => 'Two', 'Three' => [
  4838. 3 => 'Three', 4 => 'Four', 5 => 'Five',
  4839. ]],
  4840. ['empty' => false]
  4841. );
  4842. $expected = [
  4843. 'select' => ['name' => 'Model[field]'],
  4844. ['option' => ['value' => 1]],
  4845. 'One',
  4846. '/option',
  4847. ['option' => ['value' => 2]],
  4848. 'Two',
  4849. '/option',
  4850. ['optgroup' => ['label' => 'Three']],
  4851. ['option' => ['value' => 3]],
  4852. 'Three',
  4853. '/option',
  4854. ['option' => ['value' => 4]],
  4855. 'Four',
  4856. '/option',
  4857. ['option' => ['value' => 5]],
  4858. 'Five',
  4859. '/option',
  4860. '/optgroup',
  4861. '/select',
  4862. ];
  4863. $this->assertHtml($expected, $result);
  4864. }
  4865. /**
  4866. * testSelectMultiple method
  4867. *
  4868. * Test generation of multiple select elements.
  4869. *
  4870. * @return void
  4871. */
  4872. public function testSelectMultiple()
  4873. {
  4874. $options = ['first', 'second', 'third'];
  4875. $result = $this->Form->select(
  4876. 'Model.multi_field',
  4877. $options,
  4878. ['form' => 'my-form', 'multiple' => true]
  4879. );
  4880. $expected = [
  4881. 'input' => [
  4882. 'type' => 'hidden',
  4883. 'name' => 'Model[multi_field]',
  4884. 'value' => '',
  4885. 'form' => 'my-form',
  4886. ],
  4887. 'select' => [
  4888. 'name' => 'Model[multi_field][]',
  4889. 'multiple' => 'multiple',
  4890. 'form' => 'my-form',
  4891. ],
  4892. ['option' => ['value' => '0']],
  4893. 'first',
  4894. '/option',
  4895. ['option' => ['value' => '1']],
  4896. 'second',
  4897. '/option',
  4898. ['option' => ['value' => '2']],
  4899. 'third',
  4900. '/option',
  4901. '/select',
  4902. ];
  4903. $this->assertHtml($expected, $result);
  4904. $result = $this->Form->select(
  4905. 'Model.multi_field',
  4906. $options,
  4907. ['multiple' => 'multiple', 'form' => 'my-form']
  4908. );
  4909. $this->assertHtml($expected, $result);
  4910. $result = $this->Form->select(
  4911. 'Model.multi_field',
  4912. $options,
  4913. ['form' => 'my-form', 'multiple' => false]
  4914. );
  4915. $this->assertNotContains('multiple', $result);
  4916. }
  4917. /**
  4918. * testCheckboxZeroValue method
  4919. *
  4920. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4921. *
  4922. * @return void
  4923. */
  4924. public function testCheckboxZeroValue()
  4925. {
  4926. $result = $this->Form->control('User.get_spam', [
  4927. 'type' => 'checkbox',
  4928. 'value' => '0',
  4929. 'hiddenField' => '1',
  4930. ]);
  4931. $expected = [
  4932. 'div' => ['class' => 'input checkbox'],
  4933. 'label' => ['for' => 'user-get-spam'],
  4934. ['input' => [
  4935. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4936. 'value' => '1',
  4937. ]],
  4938. ['input' => [
  4939. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4940. 'value' => '0', 'id' => 'user-get-spam',
  4941. ]],
  4942. 'Get Spam',
  4943. '/label',
  4944. '/div',
  4945. ];
  4946. $this->assertHtml($expected, $result);
  4947. }
  4948. /**
  4949. * testHabtmSelectBox method
  4950. *
  4951. * Test generation of habtm select boxes.
  4952. *
  4953. * @return void
  4954. */
  4955. public function testHabtmSelectBox()
  4956. {
  4957. $this->loadFixtures('Articles');
  4958. $options = [
  4959. 1 => 'blue',
  4960. 2 => 'red',
  4961. 3 => 'green',
  4962. ];
  4963. $tags = [
  4964. new Entity(['id' => 1, 'name' => 'blue']),
  4965. new Entity(['id' => 3, 'name' => 'green']),
  4966. ];
  4967. $article = new Article(['tags' => $tags]);
  4968. $this->Form->create($article);
  4969. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4970. $expected = [
  4971. 'div' => ['class' => 'input select'],
  4972. 'label' => ['for' => 'tags-ids'],
  4973. 'Tags',
  4974. '/label',
  4975. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4976. 'select' => [
  4977. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4978. 'multiple' => 'multiple',
  4979. ],
  4980. ['option' => ['value' => '1', 'selected' => 'selected']],
  4981. 'blue',
  4982. '/option',
  4983. ['option' => ['value' => '2']],
  4984. 'red',
  4985. '/option',
  4986. ['option' => ['value' => '3', 'selected' => 'selected']],
  4987. 'green',
  4988. '/option',
  4989. '/select',
  4990. '/div',
  4991. ];
  4992. $this->assertHtml($expected, $result);
  4993. // make sure only 50 is selected, and not 50f5c0cf
  4994. $options = [
  4995. '1' => 'blue',
  4996. '50f5c0cf' => 'red',
  4997. '50' => 'green',
  4998. ];
  4999. $tags = [
  5000. new Entity(['id' => 1, 'name' => 'blue']),
  5001. new Entity(['id' => 50, 'name' => 'green']),
  5002. ];
  5003. $article = new Article(['tags' => $tags]);
  5004. $this->Form->create($article);
  5005. $result = $this->Form->control('tags._ids', ['options' => $options]);
  5006. $expected = [
  5007. 'div' => ['class' => 'input select'],
  5008. 'label' => ['for' => 'tags-ids'],
  5009. 'Tags',
  5010. '/label',
  5011. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  5012. 'select' => [
  5013. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  5014. 'multiple' => 'multiple',
  5015. ],
  5016. ['option' => ['value' => '1', 'selected' => 'selected']],
  5017. 'blue',
  5018. '/option',
  5019. ['option' => ['value' => '50f5c0cf']],
  5020. 'red',
  5021. '/option',
  5022. ['option' => ['value' => '50', 'selected' => 'selected']],
  5023. 'green',
  5024. '/option',
  5025. '/select',
  5026. '/div',
  5027. ];
  5028. $this->assertHtml($expected, $result);
  5029. $spacecraft = [
  5030. 1 => 'Orion',
  5031. 2 => 'Helios',
  5032. ];
  5033. $this->View->viewVars['spacecraft'] = $spacecraft;
  5034. $this->Form->create();
  5035. $result = $this->Form->control('spacecraft._ids');
  5036. $expected = [
  5037. 'div' => ['class' => 'input select'],
  5038. 'label' => ['for' => 'spacecraft-ids'],
  5039. 'Spacecraft',
  5040. '/label',
  5041. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  5042. 'select' => [
  5043. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  5044. 'multiple' => 'multiple',
  5045. ],
  5046. ['option' => ['value' => '1']],
  5047. 'Orion',
  5048. '/option',
  5049. ['option' => ['value' => '2']],
  5050. 'Helios',
  5051. '/option',
  5052. '/select',
  5053. '/div',
  5054. ];
  5055. $this->assertHtml($expected, $result);
  5056. }
  5057. /**
  5058. * testErrorsForBelongsToManySelect method
  5059. *
  5060. * Tests that errors for belongsToMany select fields are being
  5061. * picked up properly.
  5062. *
  5063. * @return void
  5064. */
  5065. public function testErrorsForBelongsToManySelect()
  5066. {
  5067. $spacecraft = [
  5068. 1 => 'Orion',
  5069. 2 => 'Helios',
  5070. ];
  5071. $this->View->viewVars['spacecraft'] = $spacecraft;
  5072. $article = new Article();
  5073. $article->setError('spacecraft', ['Invalid']);
  5074. $this->Form->create($article);
  5075. $result = $this->Form->control('spacecraft._ids');
  5076. $expected = [
  5077. ['div' => ['class' => 'input select error']],
  5078. 'label' => ['for' => 'spacecraft-ids'],
  5079. 'Spacecraft',
  5080. '/label',
  5081. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  5082. 'select' => [
  5083. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  5084. 'multiple' => 'multiple',
  5085. ],
  5086. ['option' => ['value' => '1']],
  5087. 'Orion',
  5088. '/option',
  5089. ['option' => ['value' => '2']],
  5090. 'Helios',
  5091. '/option',
  5092. '/select',
  5093. ['div' => ['class' => 'error-message']],
  5094. 'Invalid',
  5095. '/div',
  5096. '/div',
  5097. ];
  5098. $this->assertHtml($expected, $result);
  5099. }
  5100. /**
  5101. * testSelectMultipleCheckboxes method
  5102. *
  5103. * Test generation of multi select elements in checkbox format.
  5104. *
  5105. * @return void
  5106. */
  5107. public function testSelectMultipleCheckboxes()
  5108. {
  5109. $result = $this->Form->select(
  5110. 'Model.multi_field',
  5111. ['first', 'second', 'third'],
  5112. ['multiple' => 'checkbox']
  5113. );
  5114. $expected = [
  5115. 'input' => [
  5116. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5117. ],
  5118. ['div' => ['class' => 'checkbox']],
  5119. ['label' => ['for' => 'model-multi-field-0']],
  5120. ['input' => [
  5121. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5122. 'value' => '0', 'id' => 'model-multi-field-0',
  5123. ]],
  5124. 'first',
  5125. '/label',
  5126. '/div',
  5127. ['div' => ['class' => 'checkbox']],
  5128. ['label' => ['for' => 'model-multi-field-1']],
  5129. ['input' => [
  5130. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5131. 'value' => '1', 'id' => 'model-multi-field-1',
  5132. ]],
  5133. 'second',
  5134. '/label',
  5135. '/div',
  5136. ['div' => ['class' => 'checkbox']],
  5137. ['label' => ['for' => 'model-multi-field-2']],
  5138. ['input' => [
  5139. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5140. 'value' => '2', 'id' => 'model-multi-field-2',
  5141. ]],
  5142. 'third',
  5143. '/label',
  5144. '/div',
  5145. ];
  5146. $this->assertHtml($expected, $result);
  5147. $result = $this->Form->select(
  5148. 'Model.multi_field',
  5149. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5150. ['multiple' => 'checkbox']
  5151. );
  5152. $expected = [
  5153. 'input' => [
  5154. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5155. ],
  5156. ['div' => ['class' => 'checkbox']],
  5157. ['label' => ['for' => 'model-multi-field-a+']],
  5158. ['input' => [
  5159. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5160. 'value' => 'a+', 'id' => 'model-multi-field-a+',
  5161. ]],
  5162. 'first',
  5163. '/label',
  5164. '/div',
  5165. ['div' => ['class' => 'checkbox']],
  5166. ['label' => ['for' => 'model-multi-field-a++']],
  5167. ['input' => [
  5168. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5169. 'value' => 'a++', 'id' => 'model-multi-field-a++',
  5170. ]],
  5171. 'second',
  5172. '/label',
  5173. '/div',
  5174. ['div' => ['class' => 'checkbox']],
  5175. ['label' => ['for' => 'model-multi-field-a+++']],
  5176. ['input' => [
  5177. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5178. 'value' => 'a+++', 'id' => 'model-multi-field-a+++',
  5179. ]],
  5180. 'third',
  5181. '/label',
  5182. '/div',
  5183. ];
  5184. $this->assertHtml($expected, $result);
  5185. $result = $this->Form->select(
  5186. 'Model.multi_field',
  5187. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5188. ['multiple' => 'checkbox']
  5189. );
  5190. $expected = [
  5191. 'input' => [
  5192. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5193. ],
  5194. ['div' => ['class' => 'checkbox']],
  5195. ['label' => ['for' => 'model-multi-field-a-b']],
  5196. ['input' => [
  5197. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5198. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b',
  5199. ]],
  5200. 'first',
  5201. '/label',
  5202. '/div',
  5203. ['div' => ['class' => 'checkbox']],
  5204. ['label' => ['for' => 'model-multi-field-a-b1']],
  5205. ['input' => [
  5206. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5207. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1',
  5208. ]],
  5209. 'second',
  5210. '/label',
  5211. '/div',
  5212. ['div' => ['class' => 'checkbox']],
  5213. ['label' => ['for' => 'model-multi-field-a-b2']],
  5214. ['input' => [
  5215. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5216. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2',
  5217. ]],
  5218. 'third',
  5219. '/label',
  5220. '/div',
  5221. ];
  5222. $this->assertHtml($expected, $result);
  5223. }
  5224. /**
  5225. * testSelectMultipleCheckboxRequestData method
  5226. *
  5227. * Ensure that multiCheckbox reads from the request data.
  5228. *
  5229. * @return void
  5230. */
  5231. public function testSelectMultipleCheckboxRequestData()
  5232. {
  5233. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  5234. $result = $this->Form->select(
  5235. 'Model.tags',
  5236. ['1' => 'first', 'Array' => 'Array'],
  5237. ['multiple' => 'checkbox']
  5238. );
  5239. $expected = [
  5240. 'input' => [
  5241. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '',
  5242. ],
  5243. ['div' => ['class' => 'checkbox']],
  5244. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5245. ['input' => [
  5246. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5247. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked',
  5248. ]],
  5249. 'first',
  5250. '/label',
  5251. '/div',
  5252. ['div' => ['class' => 'checkbox']],
  5253. ['label' => ['for' => 'model-tags-array']],
  5254. ['input' => [
  5255. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5256. 'value' => 'Array', 'id' => 'model-tags-array',
  5257. ]],
  5258. 'Array',
  5259. '/label',
  5260. '/div',
  5261. ];
  5262. $this->assertHtml($expected, $result);
  5263. }
  5264. /**
  5265. * testSelectMultipleCheckboxSecurity method
  5266. *
  5267. * Checks the security hash array generated for multiple-input checkbox elements.
  5268. *
  5269. * @return void
  5270. */
  5271. public function testSelectMultipleCheckboxSecurity()
  5272. {
  5273. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  5274. $this->assertEquals([], $this->Form->fields);
  5275. $this->Form->select(
  5276. 'Model.multi_field',
  5277. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5278. ['multiple' => 'checkbox']
  5279. );
  5280. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5281. $result = $this->Form->secure($this->Form->fields);
  5282. $key = '8af36fb34e6f2ef8ba0eb473bb4365ec232f3fe5%3A';
  5283. $this->assertContains('"' . $key . '"', $result);
  5284. }
  5285. /**
  5286. * testSelectMultipleSecureWithNoOptions method
  5287. *
  5288. * Multiple select elements should always be secured as they always participate
  5289. * in the POST data.
  5290. *
  5291. * @return void
  5292. */
  5293. public function testSelectMultipleSecureWithNoOptions()
  5294. {
  5295. $this->assertEquals([], $this->Form->fields);
  5296. $this->Form->select(
  5297. 'Model.select',
  5298. [],
  5299. ['multiple' => true]
  5300. );
  5301. $this->assertEquals(['Model.select'], $this->Form->fields);
  5302. }
  5303. /**
  5304. * testSelectNoSecureWithNoOptions method
  5305. *
  5306. * When a select box has no options it should not be added to the fields list
  5307. * as it always fail post validation.
  5308. *
  5309. * @return void
  5310. */
  5311. public function testSelectNoSecureWithNoOptions()
  5312. {
  5313. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testkey'));
  5314. $this->assertEquals([], $this->Form->fields);
  5315. $this->Form->select(
  5316. 'Model.select',
  5317. []
  5318. );
  5319. $this->assertEquals([], $this->Form->fields);
  5320. $this->Form->select(
  5321. 'Model.user_id',
  5322. [],
  5323. ['empty' => true]
  5324. );
  5325. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5326. }
  5327. /**
  5328. * testControlMultipleCheckboxes method
  5329. *
  5330. * Test control() resulting in multi select elements being generated.
  5331. *
  5332. * @return void
  5333. */
  5334. public function testControlMultipleCheckboxes()
  5335. {
  5336. $result = $this->Form->control('Model.multi_field', [
  5337. 'options' => ['first', 'second', 'third'],
  5338. 'multiple' => 'checkbox',
  5339. ]);
  5340. $expected = [
  5341. ['div' => ['class' => 'input select']],
  5342. ['label' => ['for' => 'model-multi-field']],
  5343. 'Multi Field',
  5344. '/label',
  5345. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5346. ['div' => ['class' => 'checkbox']],
  5347. ['label' => ['for' => 'model-multi-field-0']],
  5348. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5349. 'first',
  5350. '/label',
  5351. '/div',
  5352. ['div' => ['class' => 'checkbox']],
  5353. ['label' => ['for' => 'model-multi-field-1']],
  5354. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5355. 'second',
  5356. '/label',
  5357. '/div',
  5358. ['div' => ['class' => 'checkbox']],
  5359. ['label' => ['for' => 'model-multi-field-2']],
  5360. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5361. 'third',
  5362. '/label',
  5363. '/div',
  5364. '/div',
  5365. ];
  5366. $this->assertHtml($expected, $result);
  5367. $result = $this->Form->control('Model.multi_field', [
  5368. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5369. 'multiple' => 'checkbox',
  5370. ]);
  5371. $expected = [
  5372. ['div' => ['class' => 'input select']],
  5373. ['label' => ['for' => 'model-multi-field']],
  5374. 'Multi Field',
  5375. '/label',
  5376. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5377. ['div' => ['class' => 'checkbox']],
  5378. ['label' => ['for' => 'model-multi-field-a']],
  5379. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5380. 'first',
  5381. '/label',
  5382. '/div',
  5383. ['div' => ['class' => 'checkbox']],
  5384. ['label' => ['for' => 'model-multi-field-b']],
  5385. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5386. 'second',
  5387. '/label',
  5388. '/div',
  5389. ['div' => ['class' => 'checkbox']],
  5390. ['label' => ['for' => 'model-multi-field-c']],
  5391. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5392. 'third',
  5393. '/label',
  5394. '/div',
  5395. '/div',
  5396. ];
  5397. $this->assertHtml($expected, $result);
  5398. }
  5399. /**
  5400. * testSelectHiddenFieldOmission method
  5401. *
  5402. * Test that select() with 'hiddenField' => false omits the hidden field.
  5403. *
  5404. * @return void
  5405. */
  5406. public function testSelectHiddenFieldOmission()
  5407. {
  5408. $result = $this->Form->select(
  5409. 'Model.multi_field',
  5410. ['first', 'second'],
  5411. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5412. );
  5413. $this->assertNotContains('type="hidden"', $result);
  5414. }
  5415. /**
  5416. * testSelectCheckboxMultipleOverrideName method
  5417. *
  5418. * Test that select() with multiple = checkbox works with overriding name attribute.
  5419. *
  5420. * @return void
  5421. */
  5422. public function testSelectCheckboxMultipleOverrideName()
  5423. {
  5424. $result = $this->Form->select('category', ['1', '2'], [
  5425. 'multiple' => 'checkbox',
  5426. 'name' => 'fish',
  5427. ]);
  5428. $expected = [
  5429. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5430. ['div' => ['class' => 'checkbox']],
  5431. ['label' => ['for' => 'fish-0']],
  5432. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5433. '1',
  5434. '/label',
  5435. '/div',
  5436. ['div' => ['class' => 'checkbox']],
  5437. ['label' => ['for' => 'fish-1']],
  5438. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5439. '2',
  5440. '/label',
  5441. '/div',
  5442. ];
  5443. $this->assertHtml($expected, $result);
  5444. $result = $this->Form->multiCheckbox(
  5445. 'category',
  5446. new Collection(['1', '2']),
  5447. ['name' => 'fish']
  5448. );
  5449. $this->assertHtml($expected, $result);
  5450. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5451. 'name' => 'fish',
  5452. ]);
  5453. $this->assertHtml($expected, $result);
  5454. }
  5455. /**
  5456. * testControlMultiCheckbox method
  5457. *
  5458. * Test that control() works with multicheckbox.
  5459. *
  5460. * @return void
  5461. */
  5462. public function testControlMultiCheckbox()
  5463. {
  5464. $result = $this->Form->control('category', [
  5465. 'type' => 'multicheckbox',
  5466. 'options' => ['1', '2'],
  5467. ]);
  5468. $expected = [
  5469. ['div' => ['class' => 'input multicheckbox']],
  5470. '<label',
  5471. 'Category',
  5472. '/label',
  5473. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5474. ['div' => ['class' => 'checkbox']],
  5475. ['label' => ['for' => 'category-0']],
  5476. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5477. '1',
  5478. '/label',
  5479. '/div',
  5480. ['div' => ['class' => 'checkbox']],
  5481. ['label' => ['for' => 'category-1']],
  5482. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5483. '2',
  5484. '/label',
  5485. '/div',
  5486. '/div',
  5487. ];
  5488. $this->assertHtml($expected, $result);
  5489. }
  5490. /**
  5491. * testCheckbox method
  5492. *
  5493. * Test generation of checkboxes.
  5494. *
  5495. * @return void
  5496. */
  5497. public function testCheckbox()
  5498. {
  5499. $result = $this->Form->checkbox('Model.field');
  5500. $expected = [
  5501. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5502. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']],
  5503. ];
  5504. $this->assertHtml($expected, $result);
  5505. $result = $this->Form->checkbox('Model.field', [
  5506. 'id' => 'theID',
  5507. 'value' => 'myvalue',
  5508. 'form' => 'my-form',
  5509. ]);
  5510. $expected = [
  5511. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5512. ['input' => [
  5513. 'type' => 'checkbox', 'name' => 'Model[field]',
  5514. 'value' => 'myvalue', 'id' => 'theID',
  5515. 'form' => 'my-form',
  5516. ]],
  5517. ];
  5518. $this->assertHtml($expected, $result);
  5519. }
  5520. /**
  5521. * testCheckboxDefaultValue method
  5522. *
  5523. * Test default value setting on checkbox() method.
  5524. *
  5525. * @return void
  5526. */
  5527. public function testCheckboxDefaultValue()
  5528. {
  5529. $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
  5530. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5531. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5532. $this->assertHtml($expected, $result);
  5533. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5534. $this->Form->create();
  5535. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5536. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5537. $this->assertHtml($expected, $result);
  5538. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5539. $this->Form->create();
  5540. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5541. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5542. $this->assertHtml($expected, $result);
  5543. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5544. $this->Form->create();
  5545. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5546. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5547. $this->assertHtml($expected, $result);
  5548. $Articles = $this->getTableLocator()->get('Articles');
  5549. $Articles->getSchema()->addColumn(
  5550. 'published',
  5551. ['type' => 'boolean', 'null' => false, 'default' => true]
  5552. );
  5553. $this->Form->create($Articles->newEntity());
  5554. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5555. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5556. $this->assertHtml($expected, $result);
  5557. }
  5558. /**
  5559. * testCheckboxCheckedAndError method
  5560. *
  5561. * Test checkbox being checked or having errors.
  5562. *
  5563. * @return void
  5564. */
  5565. public function testCheckboxCheckedAndError()
  5566. {
  5567. $this->article['errors'] = [
  5568. 'published' => true,
  5569. ];
  5570. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5571. $this->Form->create($this->article);
  5572. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5573. $expected = [
  5574. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5575. ['input' => [
  5576. 'type' => 'checkbox',
  5577. 'name' => 'published',
  5578. 'value' => 'myvalue',
  5579. 'id' => 'theID',
  5580. 'checked' => 'checked',
  5581. 'class' => 'form-error',
  5582. ]],
  5583. ];
  5584. $this->assertHtml($expected, $result);
  5585. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5586. $this->Form->create($this->article);
  5587. $result = $this->Form->checkbox('published');
  5588. $expected = [
  5589. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5590. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']],
  5591. ];
  5592. $this->assertHtml($expected, $result);
  5593. }
  5594. /**
  5595. * testCheckboxCustomNameAttribute method
  5596. *
  5597. * Test checkbox() with a custom name attribute.
  5598. *
  5599. * @return void
  5600. */
  5601. public function testCheckboxCustomNameAttribute()
  5602. {
  5603. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5604. $expected = [
  5605. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5606. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']],
  5607. ];
  5608. $this->assertHtml($expected, $result);
  5609. }
  5610. /**
  5611. * testCheckboxHiddenField method
  5612. *
  5613. * Test that the hidden input for checkboxes can be omitted or set to a
  5614. * specific value.
  5615. *
  5616. * @return void
  5617. */
  5618. public function testCheckboxHiddenField()
  5619. {
  5620. $result = $this->Form->checkbox('UserForm.something', [
  5621. 'hiddenField' => false,
  5622. ]);
  5623. $expected = [
  5624. 'input' => [
  5625. 'type' => 'checkbox',
  5626. 'name' => 'UserForm[something]',
  5627. 'value' => '1',
  5628. ],
  5629. ];
  5630. $this->assertHtml($expected, $result);
  5631. $result = $this->Form->checkbox('UserForm.something', [
  5632. 'value' => 'Y',
  5633. 'hiddenField' => 'N',
  5634. ]);
  5635. $expected = [
  5636. ['input' => [
  5637. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5638. 'value' => 'N',
  5639. ]],
  5640. ['input' => [
  5641. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5642. 'value' => 'Y',
  5643. ]],
  5644. ];
  5645. $this->assertHtml($expected, $result);
  5646. }
  5647. /**
  5648. * testTime method
  5649. *
  5650. * Test the time type.
  5651. *
  5652. * @return void
  5653. */
  5654. public function testTime()
  5655. {
  5656. $result = $this->Form->time('start_time', [
  5657. 'timeFormat' => 12,
  5658. 'interval' => 5,
  5659. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm'],
  5660. ]);
  5661. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5662. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5663. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5664. $this->assertNotContains('year', $result);
  5665. $this->assertNotContains('month', $result);
  5666. $this->assertNotContains('day', $result);
  5667. $result = $this->Form->time('start_time', [
  5668. 'timeFormat' => 12,
  5669. 'interval' => 5,
  5670. 'value' => '2014-03-08 16:30:00',
  5671. ]);
  5672. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5673. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5674. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5675. $this->assertNotContains('year', $result);
  5676. $this->assertNotContains('month', $result);
  5677. $this->assertNotContains('day', $result);
  5678. }
  5679. /**
  5680. * testTimeFormat24NoMeridian method
  5681. *
  5682. * Ensure that timeFormat=24 has no merdian.
  5683. *
  5684. * @return void
  5685. */
  5686. public function testTimeFormat24NoMeridian()
  5687. {
  5688. $result = $this->Form->time('start_time', [
  5689. 'timeFormat' => 24,
  5690. 'interval' => 5,
  5691. 'value' => '2014-03-08 16:30:00',
  5692. ]);
  5693. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5694. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5695. $this->assertNotContains('meridian', $result);
  5696. $this->assertNotContains('pm', $result);
  5697. $this->assertNotContains('year', $result);
  5698. $this->assertNotContains('month', $result);
  5699. $this->assertNotContains('day', $result);
  5700. }
  5701. /**
  5702. * testDate method
  5703. *
  5704. * Test the date type.
  5705. *
  5706. * @return void
  5707. */
  5708. public function testDate()
  5709. {
  5710. $result = $this->Form->date('start_day', [
  5711. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08'],
  5712. ]);
  5713. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5714. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5715. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5716. $this->assertNotContains('hour', $result);
  5717. $this->assertNotContains('minute', $result);
  5718. $this->assertNotContains('second', $result);
  5719. $this->assertNotContains('meridian', $result);
  5720. }
  5721. /**
  5722. * testDateTime method
  5723. *
  5724. * Test generation of date/time select elements.
  5725. *
  5726. * @return void
  5727. */
  5728. public function testDateTime()
  5729. {
  5730. extract($this->dateRegex);
  5731. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5732. $now = strtotime('now');
  5733. $expected = [
  5734. ['select' => ['name' => 'Contact[date][year]']],
  5735. ['option' => ['value' => '']],
  5736. '/option',
  5737. $yearsRegex,
  5738. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5739. date('Y', $now),
  5740. '/option',
  5741. '*/select',
  5742. ['select' => ['name' => 'Contact[date][month]']],
  5743. ['option' => ['value' => '']],
  5744. '/option',
  5745. $monthsRegex,
  5746. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5747. date('F', $now),
  5748. '/option',
  5749. '*/select',
  5750. ['select' => ['name' => 'Contact[date][day]']],
  5751. ['option' => ['value' => '']],
  5752. '/option',
  5753. $daysRegex,
  5754. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5755. date('j', $now),
  5756. '/option',
  5757. '*/select',
  5758. ['select' => ['name' => 'Contact[date][hour]']],
  5759. ['option' => ['value' => '']],
  5760. '/option',
  5761. $hoursRegex,
  5762. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5763. date('G', $now),
  5764. '/option',
  5765. '*/select',
  5766. ['select' => ['name' => 'Contact[date][minute]']],
  5767. ['option' => ['value' => '']],
  5768. '/option',
  5769. $minutesRegex,
  5770. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5771. date('i', $now),
  5772. '/option',
  5773. '*/select',
  5774. ];
  5775. $this->assertHtml($expected, $result);
  5776. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5777. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5778. $now = strtotime('now');
  5779. $expected = [
  5780. ['select' => ['name' => 'Contact[date][year]']],
  5781. $yearsRegex,
  5782. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5783. date('Y', $now),
  5784. '/option',
  5785. '*/select',
  5786. ['select' => ['name' => 'Contact[date][month]']],
  5787. $monthsRegex,
  5788. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5789. date('F', $now),
  5790. '/option',
  5791. '*/select',
  5792. ['select' => ['name' => 'Contact[date][day]']],
  5793. $daysRegex,
  5794. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5795. date('j', $now),
  5796. '/option',
  5797. '*/select',
  5798. ['select' => ['name' => 'Contact[date][hour]']],
  5799. $hoursRegex,
  5800. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5801. date('G', $now),
  5802. '/option',
  5803. '*/select',
  5804. ['select' => ['name' => 'Contact[date][minute]']],
  5805. $minutesRegex,
  5806. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5807. date('i', $now),
  5808. '/option',
  5809. '*/select',
  5810. ];
  5811. $this->assertHtml($expected, $result);
  5812. }
  5813. /**
  5814. * testDateTimeSecured method
  5815. *
  5816. * Test that datetime fields are added to protected fields list.
  5817. *
  5818. * @return void
  5819. */
  5820. public function testDateTimeSecured()
  5821. {
  5822. $this->View->setRequest(
  5823. $this->View->getRequest()->withParam('_Token', ['unlockedFields' => []])
  5824. );
  5825. $this->Form->dateTime('Contact.date');
  5826. $expected = [
  5827. 'Contact.date.year',
  5828. 'Contact.date.month',
  5829. 'Contact.date.day',
  5830. 'Contact.date.hour',
  5831. 'Contact.date.minute',
  5832. ];
  5833. $this->assertEquals($expected, $this->Form->fields);
  5834. $this->Form->fields = [];
  5835. $this->Form->date('Contact.published');
  5836. $expected = [
  5837. 'Contact.published.year',
  5838. 'Contact.published.month',
  5839. 'Contact.published.day',
  5840. ];
  5841. $this->assertEquals($expected, $this->Form->fields);
  5842. }
  5843. /**
  5844. * testDateTimeSecuredDisabled method
  5845. *
  5846. * Test that datetime fields are added to protected fields list.
  5847. *
  5848. * @return void
  5849. */
  5850. public function testDateTimeSecuredDisabled()
  5851. {
  5852. $this->View->setRequest(
  5853. $this->View->getRequest()->withParam('_Token', ['unlockedFields' => []])
  5854. );
  5855. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5856. $expected = [];
  5857. $this->assertEquals($expected, $this->Form->fields);
  5858. $this->Form->fields = [];
  5859. $this->Form->date('Contact.published', ['secure' => false]);
  5860. $expected = [];
  5861. $this->assertEquals($expected, $this->Form->fields);
  5862. }
  5863. /**
  5864. * Test empty defaulting to true for datetime.
  5865. *
  5866. * @return void
  5867. */
  5868. public function testDatetimeEmpty()
  5869. {
  5870. extract($this->dateRegex);
  5871. $result = $this->Form->dateTime('Contact.date', [
  5872. 'timeFormat' => 12,
  5873. 'empty' => true,
  5874. 'default' => true,
  5875. ]);
  5876. $expected = [
  5877. ['select' => ['name' => 'Contact[date][year]']],
  5878. $yearsRegex,
  5879. ['option' => ['value' => '']],
  5880. '/option',
  5881. '*/select',
  5882. ['select' => ['name' => 'Contact[date][month]']],
  5883. $monthsRegex,
  5884. ['option' => ['value' => '']],
  5885. '/option',
  5886. '*/select',
  5887. ['select' => ['name' => 'Contact[date][day]']],
  5888. $daysRegex,
  5889. ['option' => ['value' => '']],
  5890. '/option',
  5891. '*/select',
  5892. ['select' => ['name' => 'Contact[date][hour]']],
  5893. $hoursRegex,
  5894. ['option' => ['value' => '']],
  5895. '/option',
  5896. '*/select',
  5897. ['select' => ['name' => 'Contact[date][minute]']],
  5898. $minutesRegex,
  5899. ['option' => ['value' => '']],
  5900. '/option',
  5901. '*/select',
  5902. ['select' => ['name' => 'Contact[date][meridian]']],
  5903. $meridianRegex,
  5904. ['option' => ['value' => '']],
  5905. '/option',
  5906. '*/select',
  5907. ];
  5908. $this->assertHtml($expected, $result);
  5909. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5910. }
  5911. /**
  5912. * Presence check for array form empty options
  5913. *
  5914. * @return void
  5915. */
  5916. public function testDateTimeEmptyAsArrayPresence()
  5917. {
  5918. $result = $this->Form->dateTime('Contact.date', [
  5919. 'empty' => [
  5920. 'day' => 'DAY',
  5921. 'month' => 'MONTH',
  5922. 'year' => 'YEAR',
  5923. 'hour' => 'HOUR',
  5924. 'minute' => 'MINUTE',
  5925. 'meridian' => false,
  5926. ],
  5927. 'default' => true,
  5928. ]);
  5929. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5930. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5931. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5932. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5933. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5934. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5935. }
  5936. /**
  5937. * Test datetime with array empty value, ensuring
  5938. * empty options aren't duplicated.
  5939. *
  5940. * @return void
  5941. */
  5942. public function testDatetimeEmptyArrayForm()
  5943. {
  5944. extract($this->dateRegex);
  5945. $result = $this->Form->dateTime('Contact.date', [
  5946. 'minYear' => '2017',
  5947. 'maxYear' => '2019',
  5948. 'empty' => [
  5949. 'year' => 'pick year',
  5950. 'month' => 'pick month',
  5951. ],
  5952. 'hour' => false,
  5953. 'minute' => false,
  5954. 'second' => false,
  5955. 'meridian' => false,
  5956. ]);
  5957. $expected = [
  5958. ['select' => ['name' => 'Contact[date][year]']],
  5959. ['option' => ['value' => '', 'selected' => 'selected']], 'pick year', '/option',
  5960. '*/select',
  5961. ['select' => ['name' => 'Contact[date][month]']],
  5962. ['option' => ['value' => '', 'selected' => 'selected']], 'pick month', '/option',
  5963. $monthsRegex,
  5964. '*/select',
  5965. ['select' => ['name' => 'Contact[date][day]']],
  5966. $daysRegex,
  5967. '*/select',
  5968. ];
  5969. $this->assertHtml($expected, $result);
  5970. }
  5971. /**
  5972. * Test datetime with array empty value, ensuring
  5973. * empty options aren't duplicated.
  5974. *
  5975. * @return void
  5976. */
  5977. public function testDatetimeEmptyArrayFormKeyValue()
  5978. {
  5979. extract($this->dateRegex);
  5980. $result = $this->Form->dateTime('Contact.date', [
  5981. 'minYear' => '2017',
  5982. 'maxYear' => '2019',
  5983. 'empty' => [
  5984. '' => '-',
  5985. ],
  5986. 'hour' => false,
  5987. 'minute' => false,
  5988. 'second' => false,
  5989. 'meridian' => false,
  5990. ]);
  5991. $expected = [
  5992. ['select' => ['name' => 'Contact[date][year]']],
  5993. ['option' => ['value' => '', 'selected' => 'selected']], '-', '/option',
  5994. '*/select',
  5995. ['select' => ['name' => 'Contact[date][month]']],
  5996. ['option' => ['value' => '', 'selected' => 'selected']], '-', '/option',
  5997. $monthsRegex,
  5998. '*/select',
  5999. ['select' => ['name' => 'Contact[date][day]']],
  6000. ['option' => ['value' => '', 'selected' => 'selected']], '-', '/option',
  6001. $daysRegex,
  6002. '*/select',
  6003. ];
  6004. $this->assertHtml($expected, $result);
  6005. }
  6006. /**
  6007. * testDatetimeMinuteInterval method
  6008. *
  6009. * Test datetime with interval option.
  6010. *
  6011. * @return void
  6012. */
  6013. public function testDatetimeMinuteInterval()
  6014. {
  6015. extract($this->dateRegex);
  6016. $result = $this->Form->dateTime('Contact.date', [
  6017. 'interval' => 5,
  6018. 'value' => '',
  6019. ]);
  6020. $expected = [
  6021. ['select' => ['name' => 'Contact[date][year]']],
  6022. $yearsRegex,
  6023. ['option' => ['selected' => 'selected', 'value' => '']],
  6024. '/option',
  6025. '*/select',
  6026. ['select' => ['name' => 'Contact[date][month]']],
  6027. $monthsRegex,
  6028. ['option' => ['selected' => 'selected', 'value' => '']],
  6029. '/option',
  6030. '*/select',
  6031. ['select' => ['name' => 'Contact[date][day]']],
  6032. $daysRegex,
  6033. ['option' => ['selected' => 'selected', 'value' => '']],
  6034. '/option',
  6035. '*/select',
  6036. ['select' => ['name' => 'Contact[date][hour]']],
  6037. $hoursRegex,
  6038. ['option' => ['selected' => 'selected', 'value' => '']],
  6039. '/option',
  6040. '*/select',
  6041. ['select' => ['name' => 'Contact[date][minute]']],
  6042. $minutesRegex,
  6043. ['option' => ['selected' => 'selected', 'value' => '']],
  6044. '/option',
  6045. ['option' => ['value' => '00']],
  6046. '00',
  6047. '/option',
  6048. ['option' => ['value' => '05']],
  6049. '05',
  6050. '/option',
  6051. ['option' => ['value' => '10']],
  6052. '10',
  6053. '/option',
  6054. '*/select',
  6055. ];
  6056. $this->assertHtml($expected, $result);
  6057. }
  6058. /**
  6059. * testDateTimeRounding method
  6060. *
  6061. * Test dateTime with rounding.
  6062. *
  6063. * @return void
  6064. */
  6065. public function testDateTimeRounding()
  6066. {
  6067. $this->View->setRequest($this->View->getRequest()->withData('Contact', [
  6068. 'date' => [
  6069. 'day' => '13',
  6070. 'month' => '12',
  6071. 'year' => '2010',
  6072. 'hour' => '04',
  6073. 'minute' => '19',
  6074. 'meridian' => 'AM',
  6075. ],
  6076. ]));
  6077. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  6078. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  6079. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  6080. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  6081. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  6082. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  6083. }
  6084. /**
  6085. * testDatetimeWithDefault method
  6086. *
  6087. * Test that datetime() and default values work.
  6088. *
  6089. * @return void
  6090. */
  6091. public function testDatetimeWithDefault()
  6092. {
  6093. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  6094. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  6095. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  6096. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  6097. $result = $this->Form->dateTime('Contact.updated', [
  6098. 'default' => '2009-06-01 11:15:30',
  6099. ]);
  6100. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  6101. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  6102. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  6103. }
  6104. /**
  6105. * testDateTimeAllZeros method
  6106. *
  6107. * Test datetime() with all zeros.
  6108. *
  6109. * @return void
  6110. */
  6111. public function testDateTimeAllZeros()
  6112. {
  6113. $result = $this->Form->dateTime('Contact.date', [
  6114. 'timeFormat' => false,
  6115. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  6116. 'value' => '0000-00-00',
  6117. ]);
  6118. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  6119. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  6120. }
  6121. /**
  6122. * testFormDateTimeMulti method
  6123. *
  6124. * Test multiple datetime element generation.
  6125. *
  6126. * @return void
  6127. */
  6128. public function testFormDateTimeMulti()
  6129. {
  6130. extract($this->dateRegex);
  6131. $result = $this->Form->dateTime('Contact.1.updated');
  6132. $this->assertContains('Contact[1][updated][month]', $result);
  6133. $this->assertContains('Contact[1][updated][day]', $result);
  6134. $this->assertContains('Contact[1][updated][year]', $result);
  6135. $this->assertContains('Contact[1][updated][hour]', $result);
  6136. $this->assertContains('Contact[1][updated][minute]', $result);
  6137. }
  6138. /**
  6139. * testDateTimeLabelIdMatchesFirstControl method
  6140. *
  6141. * When changing the date format, the label should always focus the first select box when
  6142. * clicked.
  6143. *
  6144. * @return void
  6145. */
  6146. public function testDateTimeLabelIdMatchesFirstControl()
  6147. {
  6148. $result = $this->Form->control('Model.date', ['type' => 'date']);
  6149. $this->assertContains('<label>Date</label>', $result);
  6150. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  6151. $this->assertContains('<label>Date</label>', $result);
  6152. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  6153. $this->assertContains('<label>Date</label>', $result);
  6154. }
  6155. /**
  6156. * testDateTimeSecondOptions method
  6157. *
  6158. * Test datetime second=true.
  6159. *
  6160. * @return void
  6161. */
  6162. public function testDateTimeSecondOptions()
  6163. {
  6164. $result = $this->Form->dateTime('updated', ['second' => true]);
  6165. $this->assertContains('updated[second]', $result, 'Should have seconds');
  6166. $result = $this->Form->dateTime('updated', ['second' => []]);
  6167. $this->assertContains('updated[second]', $result, 'Should have seconds');
  6168. $result = $this->Form->dateTime('updated', ['second' => null]);
  6169. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  6170. $result = $this->Form->dateTime('updated', ['second' => false]);
  6171. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  6172. }
  6173. /**
  6174. * testMonth method
  6175. *
  6176. * Test generation of a month input.
  6177. *
  6178. * @return void
  6179. */
  6180. public function testMonth()
  6181. {
  6182. $result = $this->Form->month('Model.field', ['value' => '']);
  6183. $expected = [
  6184. ['select' => ['name' => 'Model[field][month]']],
  6185. ['option' => ['value' => '', 'selected' => 'selected']],
  6186. '/option',
  6187. ['option' => ['value' => '01']],
  6188. date('F', strtotime('2008-01-01 00:00:00')),
  6189. '/option',
  6190. ['option' => ['value' => '02']],
  6191. date('F', strtotime('2008-02-01 00:00:00')),
  6192. '/option',
  6193. '*/select',
  6194. ];
  6195. $this->assertHtml($expected, $result);
  6196. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  6197. $expected = [
  6198. ['select' => ['name' => 'Model[field][month]']],
  6199. ['option' => ['selected' => 'selected', 'value' => '']],
  6200. '/option',
  6201. ['option' => ['value' => '01']],
  6202. date('F', strtotime('2008-01-01 00:00:00')),
  6203. '/option',
  6204. ['option' => ['value' => '02']],
  6205. date('F', strtotime('2008-02-01 00:00:00')),
  6206. '/option',
  6207. '*/select',
  6208. ];
  6209. $this->assertHtml($expected, $result);
  6210. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  6211. $expected = [
  6212. ['select' => ['name' => 'Model[field][month]']],
  6213. ['option' => ['selected' => 'selected', 'value' => '']],
  6214. '/option',
  6215. ['option' => ['value' => '01']],
  6216. '1',
  6217. '/option',
  6218. ['option' => ['value' => '02']],
  6219. '2',
  6220. '/option',
  6221. '*/select',
  6222. ];
  6223. $this->assertHtml($expected, $result);
  6224. $monthNames = [
  6225. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6226. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec',
  6227. ];
  6228. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  6229. $expected = [
  6230. ['select' => ['name' => 'Model[field][month]']],
  6231. ['option' => ['value' => '']],
  6232. '/option',
  6233. ['option' => ['value' => '01', 'selected' => 'selected']],
  6234. 'Jan',
  6235. '/option',
  6236. ['option' => ['value' => '02']],
  6237. 'Feb',
  6238. '/option',
  6239. '*/select',
  6240. ];
  6241. $this->assertHtml($expected, $result);
  6242. $this->View->setRequest(
  6243. $this->View->getRequest()->withData('Project.release', '2050-02-10')
  6244. );
  6245. $this->Form->create();
  6246. $result = $this->Form->month('Project.release');
  6247. $expected = [
  6248. ['select' => ['name' => 'Project[release][month]']],
  6249. ['option' => ['value' => '']],
  6250. '/option',
  6251. ['option' => ['value' => '01']],
  6252. 'January',
  6253. '/option',
  6254. ['option' => ['value' => '02', 'selected' => 'selected']],
  6255. 'February',
  6256. '/option',
  6257. '*/select',
  6258. ];
  6259. $this->assertHtml($expected, $result);
  6260. $result = $this->Form->month('Contact.published', [
  6261. 'empty' => 'Published on',
  6262. ]);
  6263. $this->assertContains('Published on', $result);
  6264. }
  6265. /**
  6266. * testDay method
  6267. *
  6268. * Test generation of a day input.
  6269. *
  6270. * @return void
  6271. */
  6272. public function testDay()
  6273. {
  6274. extract($this->dateRegex);
  6275. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  6276. $expected = [
  6277. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  6278. ['option' => ['selected' => 'selected', 'value' => '']],
  6279. '/option',
  6280. ['option' => ['value' => '01']],
  6281. '1',
  6282. '/option',
  6283. ['option' => ['value' => '02']],
  6284. '2',
  6285. '/option',
  6286. $daysRegex,
  6287. '/select',
  6288. ];
  6289. $this->assertHtml($expected, $result);
  6290. $this->View->setRequest(
  6291. $this->View->getRequest()->withData('Model.field', '2006-10-10 23:12:32')
  6292. );
  6293. $this->Form->create();
  6294. $result = $this->Form->day('Model.field');
  6295. $expected = [
  6296. ['select' => ['name' => 'Model[field][day]']],
  6297. ['option' => ['value' => '']],
  6298. '/option',
  6299. ['option' => ['value' => '01']],
  6300. '1',
  6301. '/option',
  6302. ['option' => ['value' => '02']],
  6303. '2',
  6304. '/option',
  6305. $daysRegex,
  6306. ['option' => ['value' => '10', 'selected' => 'selected']],
  6307. '10',
  6308. '/option',
  6309. $daysRegex,
  6310. '/select',
  6311. ];
  6312. $this->assertHtml($expected, $result);
  6313. $this->View->setRequest($this->View->getRequest()->withData('Model.field', ''));
  6314. $this->Form->create();
  6315. $result = $this->Form->day('Model.field', ['value' => '10']);
  6316. $expected = [
  6317. ['select' => ['name' => 'Model[field][day]']],
  6318. ['option' => ['value' => '']],
  6319. '/option',
  6320. ['option' => ['value' => '01']],
  6321. '1',
  6322. '/option',
  6323. ['option' => ['value' => '02']],
  6324. '2',
  6325. '/option',
  6326. $daysRegex,
  6327. ['option' => ['value' => '10', 'selected' => 'selected']],
  6328. '10',
  6329. '/option',
  6330. $daysRegex,
  6331. '/select',
  6332. ];
  6333. $this->assertHtml($expected, $result);
  6334. $this->View->setRequest(
  6335. $this->View->getRequest()->withData('Project.release', '2050-10-10')
  6336. );
  6337. $this->Form->create();
  6338. $result = $this->Form->day('Project.release');
  6339. $expected = [
  6340. ['select' => ['name' => 'Project[release][day]']],
  6341. ['option' => ['value' => '']],
  6342. '/option',
  6343. ['option' => ['value' => '01']],
  6344. '1',
  6345. '/option',
  6346. ['option' => ['value' => '02']],
  6347. '2',
  6348. '/option',
  6349. $daysRegex,
  6350. ['option' => ['value' => '10', 'selected' => 'selected']],
  6351. '10',
  6352. '/option',
  6353. $daysRegex,
  6354. '/select',
  6355. ];
  6356. $this->assertHtml($expected, $result);
  6357. $result = $this->Form->day('Contact.published', [
  6358. 'empty' => 'Published on',
  6359. ]);
  6360. $this->assertContains('Published on', $result);
  6361. }
  6362. /**
  6363. * testMinute method
  6364. *
  6365. * Test generation of a minute input.
  6366. *
  6367. * @return void
  6368. */
  6369. public function testMinute()
  6370. {
  6371. extract($this->dateRegex);
  6372. $result = $this->Form->minute('Model.field', ['value' => '']);
  6373. $expected = [
  6374. ['select' => ['name' => 'Model[field][minute]']],
  6375. ['option' => ['selected' => 'selected', 'value' => '']],
  6376. '/option',
  6377. ['option' => ['value' => '00']],
  6378. '00',
  6379. '/option',
  6380. ['option' => ['value' => '01']],
  6381. '01',
  6382. '/option',
  6383. ['option' => ['value' => '02']],
  6384. '02',
  6385. '/option',
  6386. $minutesRegex,
  6387. '/select',
  6388. ];
  6389. $this->assertHtml($expected, $result);
  6390. $this->View->setRequest(
  6391. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:12:32')
  6392. );
  6393. $this->Form->create();
  6394. $result = $this->Form->minute('Model.field');
  6395. $expected = [
  6396. ['select' => ['name' => 'Model[field][minute]']],
  6397. ['option' => ['value' => '']],
  6398. '/option',
  6399. ['option' => ['value' => '00']],
  6400. '00',
  6401. '/option',
  6402. ['option' => ['value' => '01']],
  6403. '01',
  6404. '/option',
  6405. ['option' => ['value' => '02']],
  6406. '02',
  6407. '/option',
  6408. $minutesRegex,
  6409. ['option' => ['value' => '12', 'selected' => 'selected']],
  6410. '12',
  6411. '/option',
  6412. $minutesRegex,
  6413. '/select',
  6414. ];
  6415. $this->assertHtml($expected, $result);
  6416. $this->View->setRequest($this->View->getRequest()->withData('Model.field', ''));
  6417. $this->Form->create();
  6418. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6419. $expected = [
  6420. ['select' => ['name' => 'Model[field][minute]']],
  6421. ['option' => ['selected' => 'selected', 'value' => '']],
  6422. '/option',
  6423. ['option' => ['value' => '00']],
  6424. '00',
  6425. '/option',
  6426. ['option' => ['value' => '05']],
  6427. '05',
  6428. '/option',
  6429. ['option' => ['value' => '10']],
  6430. '10',
  6431. '/option',
  6432. $minutesRegex,
  6433. '/select',
  6434. ];
  6435. $this->assertHtml($expected, $result);
  6436. $this->View->setRequest(
  6437. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:10:32')
  6438. );
  6439. $this->Form->create();
  6440. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6441. $expected = [
  6442. ['select' => ['name' => 'Model[field][minute]']],
  6443. ['option' => ['value' => '']],
  6444. '/option',
  6445. ['option' => ['value' => '00']],
  6446. '00',
  6447. '/option',
  6448. ['option' => ['value' => '05']],
  6449. '05',
  6450. '/option',
  6451. ['option' => ['value' => '10', 'selected' => 'selected']],
  6452. '10',
  6453. '/option',
  6454. $minutesRegex,
  6455. '/select',
  6456. ];
  6457. $this->assertHtml($expected, $result);
  6458. }
  6459. /**
  6460. * testMeridian method
  6461. *
  6462. * Test generating an input for the meridian.
  6463. *
  6464. * @return void
  6465. */
  6466. public function testMeridian()
  6467. {
  6468. extract($this->dateRegex);
  6469. $now = new \DateTime();
  6470. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6471. $expected = [
  6472. ['select' => ['name' => 'Model[field][meridian]']],
  6473. ['option' => ['value' => '']],
  6474. '/option',
  6475. $meridianRegex,
  6476. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6477. $now->format('a'),
  6478. '/option',
  6479. '*/select',
  6480. ];
  6481. $this->assertHtml($expected, $result);
  6482. }
  6483. /**
  6484. * testHour method
  6485. *
  6486. * Test generation of an hour input.
  6487. *
  6488. * @return void
  6489. */
  6490. public function testHour()
  6491. {
  6492. extract($this->dateRegex);
  6493. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6494. $expected = [
  6495. ['select' => ['name' => 'Model[field][hour]']],
  6496. ['option' => ['selected' => 'selected', 'value' => '']],
  6497. '/option',
  6498. ['option' => ['value' => '01']],
  6499. '1',
  6500. '/option',
  6501. ['option' => ['value' => '02']],
  6502. '2',
  6503. '/option',
  6504. $hoursRegex,
  6505. '/select',
  6506. ];
  6507. $this->assertHtml($expected, $result);
  6508. $this->View->setRequest(
  6509. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:12:32')
  6510. );
  6511. $this->Form->create();
  6512. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6513. $expected = [
  6514. ['select' => ['name' => 'Model[field][hour]']],
  6515. ['option' => ['value' => '']],
  6516. '/option',
  6517. ['option' => ['value' => '01']],
  6518. '1',
  6519. '/option',
  6520. ['option' => ['value' => '02']],
  6521. '2',
  6522. '/option',
  6523. $hoursRegex,
  6524. ['option' => ['value' => '12', 'selected' => 'selected']],
  6525. '12',
  6526. '/option',
  6527. '/select',
  6528. ];
  6529. $this->assertHtml($expected, $result);
  6530. $this->View->setRequest($this->View->getRequest()->withData('Model.field', ''));
  6531. $this->Form->create();
  6532. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6533. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6534. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6535. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6536. $this->View->setRequest(
  6537. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:12:32')
  6538. );
  6539. $this->Form->create();
  6540. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6541. $expected = [
  6542. ['select' => ['name' => 'Model[field][hour]']],
  6543. ['option' => ['value' => '']],
  6544. '/option',
  6545. ['option' => ['value' => '00', 'selected' => 'selected']],
  6546. '0',
  6547. '/option',
  6548. ['option' => ['value' => '01']],
  6549. '1',
  6550. '/option',
  6551. ['option' => ['value' => '02']],
  6552. '2',
  6553. '/option',
  6554. $hoursRegex,
  6555. '/select',
  6556. ];
  6557. $this->assertHtml($expected, $result);
  6558. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  6559. $this->Form->create();
  6560. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6561. $thisHour = date('H');
  6562. $optValue = date('G');
  6563. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6564. $this->View->setRequest(
  6565. $this->View->getRequest()->withData('Model.field', '2050-10-10 01:12:32')
  6566. );
  6567. $this->Form->create();
  6568. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6569. $expected = [
  6570. ['select' => ['name' => 'Model[field][hour]']],
  6571. ['option' => ['value' => '']],
  6572. '/option',
  6573. ['option' => ['value' => '00']],
  6574. '0',
  6575. '/option',
  6576. ['option' => ['value' => '01', 'selected' => 'selected']],
  6577. '1',
  6578. '/option',
  6579. ['option' => ['value' => '02']],
  6580. '2',
  6581. '/option',
  6582. $hoursRegex,
  6583. '/select',
  6584. ];
  6585. $this->assertHtml($expected, $result);
  6586. }
  6587. /**
  6588. * testYear method
  6589. *
  6590. * Test generation of a year input.
  6591. *
  6592. * @return void
  6593. */
  6594. public function testYear()
  6595. {
  6596. $this->View->setRequest(
  6597. $this->View->getRequest()->withData('Contact.published', '2006-10-10')
  6598. );
  6599. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6600. $expected = [
  6601. ['select' => ['name' => 'Model[field][year]']],
  6602. ['option' => ['selected' => 'selected', 'value' => '']],
  6603. '/option',
  6604. ['option' => ['value' => '2007']],
  6605. '2007',
  6606. '/option',
  6607. ['option' => ['value' => '2006']],
  6608. '2006',
  6609. '/option',
  6610. '/select',
  6611. ];
  6612. $this->assertHtml($expected, $result);
  6613. $result = $this->Form->year('Model.field', [
  6614. 'value' => '',
  6615. 'minYear' => 2006,
  6616. 'maxYear' => 2007,
  6617. 'orderYear' => 'asc',
  6618. ]);
  6619. $expected = [
  6620. ['select' => ['name' => 'Model[field][year]']],
  6621. ['option' => ['selected' => 'selected', 'value' => '']],
  6622. '/option',
  6623. ['option' => ['value' => '2006']],
  6624. '2006',
  6625. '/option',
  6626. ['option' => ['value' => '2007']],
  6627. '2007',
  6628. '/option',
  6629. '/select',
  6630. ];
  6631. $this->assertHtml($expected, $result);
  6632. $result = $this->Form->year('Contact.published', [
  6633. 'empty' => false,
  6634. 'minYear' => 2006,
  6635. 'maxYear' => 2007,
  6636. ]);
  6637. $expected = [
  6638. ['select' => ['name' => 'Contact[published][year]']],
  6639. ['option' => ['value' => '2007']],
  6640. '2007',
  6641. '/option',
  6642. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6643. '2006',
  6644. '/option',
  6645. '/select',
  6646. ];
  6647. $this->assertHtml($expected, $result);
  6648. $result = $this->Form->year('Contact.published', [
  6649. 'empty' => 'Published on',
  6650. ]);
  6651. $this->assertContains('Published on', $result);
  6652. }
  6653. /**
  6654. * testControlDatetimePreEpoch method
  6655. *
  6656. * Test minYear being prior to the unix epoch.
  6657. *
  6658. * @return void
  6659. */
  6660. public function testControlDatetimePreEpoch()
  6661. {
  6662. $start = date('Y') - 80;
  6663. $end = date('Y') - 18;
  6664. $result = $this->Form->control('birth_year', [
  6665. 'type' => 'date',
  6666. 'label' => 'Birth Year',
  6667. 'minYear' => $start,
  6668. 'maxYear' => $end,
  6669. 'month' => false,
  6670. 'day' => false,
  6671. ]);
  6672. $this->assertContains('value="' . $start . '">' . $start, $result);
  6673. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6674. $this->assertNotContains('value="00">00', $result);
  6675. }
  6676. /**
  6677. * test control() datetime & required attributes
  6678. *
  6679. * @return void
  6680. */
  6681. public function testControlDatetimeRequired()
  6682. {
  6683. $result = $this->Form->control('birthday', [
  6684. 'type' => 'date',
  6685. 'required' => true,
  6686. ]);
  6687. $this->assertContains(
  6688. '<select name="birthday[year]" required="required"',
  6689. $result
  6690. );
  6691. }
  6692. /**
  6693. * testYearAutoExpandRange method
  6694. *
  6695. * @return void
  6696. */
  6697. public function testYearAutoExpandRange()
  6698. {
  6699. $this->View->setRequest($this->View->getRequest()->withData('User.birthday', '1930-10-10'));
  6700. $result = $this->Form->year('User.birthday');
  6701. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6702. $result = $matches[1];
  6703. $expected = range(date('Y') + 5, 1930);
  6704. $this->assertEquals($expected, $result);
  6705. $this->View->setRequest($this->View->getRequest()->withData('Project.release', '2050-10-10'));
  6706. $this->Form->create();
  6707. $result = $this->Form->year('Project.release');
  6708. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6709. $result = $matches[1];
  6710. $expected = range(2050, date('Y') - 5);
  6711. $this->assertEquals($expected, $result);
  6712. $this->View->setRequest($this->View->getRequest()->withData('Project.release', '1881-10-10'));
  6713. $this->Form->create();
  6714. $result = $this->Form->year('Project.release', [
  6715. 'minYear' => 1890,
  6716. 'maxYear' => 1900,
  6717. ]);
  6718. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6719. $result = $matches[1];
  6720. $expected = range(1900, 1881);
  6721. $this->assertEquals($expected, $result);
  6722. }
  6723. /**
  6724. * testControlDate method
  6725. *
  6726. * Test that control() accepts the type of date and passes options in.
  6727. *
  6728. * @return void
  6729. */
  6730. public function testControlDate()
  6731. {
  6732. $this->View->setRequest($this->View->getRequest()->withParsedBody([
  6733. 'month_year' => ['month' => date('m')],
  6734. ]));
  6735. $this->Form->create($this->article);
  6736. $result = $this->Form->control('month_year', [
  6737. 'label' => false,
  6738. 'type' => 'date',
  6739. 'minYear' => 2006,
  6740. 'maxYear' => 2008,
  6741. ]);
  6742. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6743. $this->assertNotContains('value="2008" selected="selected"', $result);
  6744. }
  6745. /**
  6746. * testControlLabelFalse method
  6747. *
  6748. * Test the label option being set to false.
  6749. *
  6750. * @return void
  6751. */
  6752. public function testControlLabelFalse()
  6753. {
  6754. $this->Form->create($this->article);
  6755. $result = $this->Form->control('title', ['label' => false]);
  6756. $expected = [
  6757. 'div' => ['class' => 'input text required'],
  6758. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6759. '/div',
  6760. ];
  6761. $this->assertHtml($expected, $result);
  6762. }
  6763. /**
  6764. * testControlDateMaxYear method
  6765. *
  6766. * Let's say we want to only allow users born from 2006 to 2008 to register
  6767. * This being the first signup page, we still don't have any data.
  6768. *
  6769. * @return void
  6770. */
  6771. public function testControlDateMaxYear()
  6772. {
  6773. $this->Form->create($this->article);
  6774. $result = $this->Form->control('birthday', [
  6775. 'label' => false,
  6776. 'type' => 'date',
  6777. 'minYear' => 2006,
  6778. 'maxYear' => 2008,
  6779. 'default' => true,
  6780. ]);
  6781. $this->assertContains('value="2008" selected="selected"', $result);
  6782. $this->assertContains('value="2006"', $result);
  6783. $this->assertNotContains('value="2005"', $result);
  6784. $this->assertNotContains('value="2009"', $result);
  6785. }
  6786. /**
  6787. * testTextArea method
  6788. *
  6789. * Test generation of a textarea input.
  6790. *
  6791. * @return void
  6792. */
  6793. public function testTextArea()
  6794. {
  6795. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  6796. $result = $this->Form->textarea('field');
  6797. $expected = [
  6798. 'textarea' => ['name' => 'field', 'rows' => 5],
  6799. 'some test data',
  6800. '/textarea',
  6801. ];
  6802. $this->assertHtml($expected, $result);
  6803. $result = $this->Form->textarea('user.bio');
  6804. $expected = [
  6805. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6806. '/textarea',
  6807. ];
  6808. $this->assertHtml($expected, $result);
  6809. $this->View->setRequest($this->View->getRequest()
  6810. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6811. $this->Form->create();
  6812. $result = $this->Form->textarea('field');
  6813. $expected = [
  6814. 'textarea' => ['name' => 'field', 'rows' => 5],
  6815. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6816. '/textarea',
  6817. ];
  6818. $this->assertHtml($expected, $result);
  6819. $this->View->setRequest($this->View->getRequest()->withData(
  6820. 'Model.field',
  6821. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  6822. ));
  6823. $this->Form->create();
  6824. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6825. $expected = [
  6826. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6827. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6828. '/textarea',
  6829. ];
  6830. $this->assertHtml($expected, $result);
  6831. $result = $this->Form->textarea('0.OtherModel.field');
  6832. $expected = [
  6833. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6834. '/textarea',
  6835. ];
  6836. $this->assertHtml($expected, $result);
  6837. }
  6838. /**
  6839. * testTextAreaWithStupidCharacters method
  6840. *
  6841. * Test text area with non-ascii characters.
  6842. *
  6843. * @return void
  6844. */
  6845. public function testTextAreaWithStupidCharacters()
  6846. {
  6847. $result = $this->Form->textarea('Post.content', [
  6848. 'value' => 'GREAT®',
  6849. 'rows' => '15',
  6850. 'cols' => '75',
  6851. ]);
  6852. $expected = [
  6853. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6854. 'GREAT®',
  6855. '/textarea',
  6856. ];
  6857. $this->assertHtml($expected, $result);
  6858. }
  6859. /**
  6860. * testTextAreaMaxLength method
  6861. *
  6862. * Test textareas maxlength read from schema.
  6863. *
  6864. * @return void
  6865. */
  6866. public function testTextAreaMaxLength()
  6867. {
  6868. $this->Form->create([
  6869. 'schema' => [
  6870. 'stuff' => ['type' => 'string', 'length' => 10],
  6871. ],
  6872. ]);
  6873. $result = $this->Form->control('other', ['type' => 'textarea']);
  6874. $expected = [
  6875. 'div' => ['class' => 'input textarea'],
  6876. 'label' => ['for' => 'other'],
  6877. 'Other',
  6878. '/label',
  6879. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6880. '/textarea',
  6881. '/div',
  6882. ];
  6883. $this->assertHtml($expected, $result);
  6884. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6885. $expected = [
  6886. 'div' => ['class' => 'input textarea'],
  6887. 'label' => ['for' => 'stuff'],
  6888. 'Stuff',
  6889. '/label',
  6890. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6891. '/textarea',
  6892. '/div',
  6893. ];
  6894. $this->assertHtml($expected, $result);
  6895. }
  6896. /**
  6897. * testHiddenField method
  6898. *
  6899. * Test generation of a hidden input.
  6900. *
  6901. * @return void
  6902. */
  6903. public function testHiddenField()
  6904. {
  6905. $this->article['errors'] = [
  6906. 'field' => true,
  6907. ];
  6908. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  6909. $this->Form->create($this->article);
  6910. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6911. $expected = [
  6912. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6913. $this->assertHtml($expected, $result);
  6914. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6915. $expected = [
  6916. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value'],
  6917. ];
  6918. $this->assertHtml($expected, $result);
  6919. }
  6920. /**
  6921. * testFileUploadField method
  6922. *
  6923. * Test generation of a file upload input.
  6924. *
  6925. * @return void
  6926. */
  6927. public function testFileUploadField()
  6928. {
  6929. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6930. $result = $this->Form->file('Model.upload');
  6931. $this->assertHtml($expected, $result);
  6932. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  6933. 'name' => '', 'type' => '', 'tmp_name' => '',
  6934. 'error' => 4, 'size' => 0,
  6935. ]));
  6936. $result = $this->Form->file('Model.upload');
  6937. $this->assertHtml($expected, $result);
  6938. $this->View->setRequest(
  6939. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  6940. );
  6941. $result = $this->Form->file('Model.upload');
  6942. $this->assertHtml($expected, $result);
  6943. }
  6944. /**
  6945. * testFileUploadOnOtherModel method
  6946. *
  6947. * Test File upload input on a model not used in create().
  6948. *
  6949. * @return void
  6950. */
  6951. public function testFileUploadOnOtherModel()
  6952. {
  6953. $this->Form->create($this->article, ['type' => 'file']);
  6954. $result = $this->Form->file('ValidateProfile.city');
  6955. $expected = [
  6956. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]'],
  6957. ];
  6958. $this->assertHtml($expected, $result);
  6959. }
  6960. /**
  6961. * testButton method
  6962. *
  6963. * Test generation of a form button.
  6964. *
  6965. * @return void
  6966. */
  6967. public function testButton()
  6968. {
  6969. $result = $this->Form->button('Hi');
  6970. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6971. $this->assertHtml($expected, $result);
  6972. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6973. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6974. $this->assertHtml($expected, $result);
  6975. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6976. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6977. $this->assertHtml($expected, $result);
  6978. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6979. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6980. $this->assertHtml($expected, $result);
  6981. $result = $this->Form->button('No type', ['type' => false]);
  6982. $expected = ['button' => [], 'No type', '/button'];
  6983. $this->assertHtml($expected, $result);
  6984. $result = $this->Form->button('Upload Text', [
  6985. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6986. 'escape' => false,
  6987. ]);
  6988. $this->assertNotRegExp('/\&039/', $result);
  6989. }
  6990. /**
  6991. * testButtonUnlockedByDefault method
  6992. *
  6993. * Test that button() makes unlocked fields by default.
  6994. *
  6995. * @return void
  6996. */
  6997. public function testButtonUnlockedByDefault()
  6998. {
  6999. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'secured'));
  7000. $this->Form->button('Save', ['name' => 'save']);
  7001. $this->Form->button('Clear');
  7002. $result = $this->Form->unlockField();
  7003. $this->assertEquals(['save'], $result);
  7004. }
  7005. /**
  7006. * Test generation of a form button with confirm message.
  7007. *
  7008. * @return void
  7009. */
  7010. public function testButtonWithConfirm()
  7011. {
  7012. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  7013. $expected = ['button' => ['type' => 'submit', 'onclick' => 'if (confirm(&quot;Confirm me!&quot;)) { return true; } return false;'], 'Hi', '/button'];
  7014. $this->assertHtml($expected, $result);
  7015. }
  7016. /**
  7017. * testPostButton method
  7018. *
  7019. * @return void
  7020. */
  7021. public function testPostButton()
  7022. {
  7023. $result = $this->Form->postButton('Hi', '/controller/action');
  7024. $expected = [
  7025. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  7026. 'div' => ['style' => 'display:none;'],
  7027. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7028. '/div',
  7029. 'button' => ['type' => 'submit'],
  7030. 'Hi',
  7031. '/button',
  7032. '/form',
  7033. ];
  7034. $this->assertHtml($expected, $result);
  7035. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  7036. $this->assertContains('<input type="hidden" name="extra" value="value"', $result);
  7037. }
  7038. /**
  7039. * testPostButtonMethodType method
  7040. *
  7041. * @return void
  7042. */
  7043. public function testPostButtonMethodType()
  7044. {
  7045. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  7046. $expected = [
  7047. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  7048. 'div' => ['style' => 'display:none;'],
  7049. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  7050. '/div',
  7051. 'button' => ['type' => 'submit'],
  7052. 'Hi',
  7053. '/button',
  7054. '/form',
  7055. ];
  7056. $this->assertHtml($expected, $result);
  7057. }
  7058. /**
  7059. * testPostButtonFormOptions method
  7060. *
  7061. * @return void
  7062. */
  7063. public function testPostButtonFormOptions()
  7064. {
  7065. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  7066. $expected = [
  7067. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  7068. 'div' => ['style' => 'display:none;'],
  7069. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7070. '/div',
  7071. 'button' => ['type' => 'submit'],
  7072. 'Hi',
  7073. '/button',
  7074. '/form',
  7075. ];
  7076. $this->assertHtml($expected, $result);
  7077. }
  7078. /**
  7079. * testPostButtonNestedData method
  7080. *
  7081. * Test using postButton with N dimensional data.
  7082. *
  7083. * @return void
  7084. */
  7085. public function testPostButtonNestedData()
  7086. {
  7087. $data = [
  7088. 'one' => [
  7089. 'two' => [
  7090. 3, 4, 5,
  7091. ],
  7092. ],
  7093. ];
  7094. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  7095. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7096. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7097. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7098. }
  7099. /**
  7100. * testSecurePostButton method
  7101. *
  7102. * Test that postButton adds _Token fields.
  7103. *
  7104. * @return void
  7105. */
  7106. public function testSecurePostButton()
  7107. {
  7108. $this->View->setRequest($this->View->getRequest()
  7109. ->withParam('_csrfToken', 'testkey')
  7110. ->withParam('_Token.unlockedFields', []));
  7111. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  7112. $tokenDebug = urlencode(json_encode([
  7113. '/posts/delete/1',
  7114. [],
  7115. [],
  7116. ]));
  7117. $expected = [
  7118. 'form' => [
  7119. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  7120. ],
  7121. ['div' => ['style' => 'display:none;']],
  7122. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7123. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7124. '/div',
  7125. 'button' => ['type' => 'submit'],
  7126. 'Delete',
  7127. '/button',
  7128. ['div' => ['style' => 'display:none;']],
  7129. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7130. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7131. ['input' => [
  7132. 'type' => 'hidden',
  7133. 'name' => '_Token[debug]',
  7134. 'value' => $tokenDebug,
  7135. 'autocomplete' => 'off',
  7136. ]],
  7137. '/div',
  7138. '/form',
  7139. ];
  7140. $this->assertHtml($expected, $result);
  7141. }
  7142. /**
  7143. * testPostLink method
  7144. *
  7145. * @return void
  7146. */
  7147. public function testPostLink()
  7148. {
  7149. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7150. $expected = [
  7151. 'form' => [
  7152. 'method' => 'post', 'action' => '/posts/delete/1',
  7153. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7154. ],
  7155. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7156. '/form',
  7157. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7158. 'Delete',
  7159. '/a',
  7160. ];
  7161. $this->assertHtml($expected, $result);
  7162. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  7163. $expected = [
  7164. 'form' => [
  7165. 'method' => 'post', 'action' => '/posts/delete/1',
  7166. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7167. ],
  7168. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  7169. '/form',
  7170. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7171. 'Delete',
  7172. '/a',
  7173. ];
  7174. $this->assertHtml($expected, $result);
  7175. $result = $this->Form->postLink(
  7176. 'Delete',
  7177. '/posts/delete/1',
  7178. ['target' => '_blank', 'class' => 'btn btn-danger']
  7179. );
  7180. $expected = [
  7181. 'form' => [
  7182. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  7183. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7184. ],
  7185. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7186. '/form',
  7187. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7188. 'Delete',
  7189. '/a',
  7190. ];
  7191. $this->assertHtml($expected, $result);
  7192. }
  7193. /**
  7194. * testPostLinkWithConfirm method
  7195. *
  7196. * Test the confirm option for postLink().
  7197. *
  7198. * @return void
  7199. */
  7200. public function testPostLinkWithConfirm()
  7201. {
  7202. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  7203. $expected = [
  7204. 'form' => [
  7205. 'method' => 'post', 'action' => '/posts/delete/1',
  7206. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7207. ],
  7208. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7209. '/form',
  7210. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  7211. 'Delete',
  7212. '/a',
  7213. ];
  7214. $this->assertHtml($expected, $result);
  7215. $result = $this->Form->postLink(
  7216. 'Delete',
  7217. '/posts/delete/1',
  7218. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  7219. );
  7220. $expected = [
  7221. 'form' => [
  7222. 'method' => 'post', 'action' => '/posts/delete/1',
  7223. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7224. ],
  7225. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7226. '/form',
  7227. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  7228. 'Delete',
  7229. '/a',
  7230. ];
  7231. $this->assertHtml($expected, $result);
  7232. $this->Form->setTemplates(['confirmJs' => 'if (confirm({{confirmMessage}})) { $(\'form[name="{{formName}}"]\').submit();};']);
  7233. $result = $this->Form->postLink(
  7234. 'Delete',
  7235. '/posts/delete/1',
  7236. ['escape' => false, 'confirm' => 'Confirm this deletion?']
  7237. );
  7238. $expected = [
  7239. 'form' => [
  7240. 'method' => 'post', 'action' => '/posts/delete/1',
  7241. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7242. ],
  7243. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7244. '/form',
  7245. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\("Confirm this deletion\?"\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/'],
  7246. 'Delete',
  7247. '/a',
  7248. ];
  7249. $this->assertHtml($expected, $result);
  7250. }
  7251. /**
  7252. * testPostLinkWithQuery method
  7253. *
  7254. * Test postLink() with query string args.
  7255. *
  7256. * @return void
  7257. */
  7258. public function testPostLinkWithQuery()
  7259. {
  7260. $result = $this->Form->postLink(
  7261. 'Delete',
  7262. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  7263. );
  7264. $expected = [
  7265. 'form' => [
  7266. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  7267. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7268. ],
  7269. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7270. '/form',
  7271. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7272. 'Delete',
  7273. '/a',
  7274. ];
  7275. $this->assertHtml($expected, $result);
  7276. }
  7277. /**
  7278. * testPostLinkWithData method
  7279. *
  7280. * Test postLink with additional data.
  7281. *
  7282. * @return void
  7283. */
  7284. public function testPostLinkWithData()
  7285. {
  7286. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  7287. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  7288. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  7289. $this->Form->create($entity);
  7290. $this->Form->end();
  7291. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  7292. $this->assertContains(
  7293. '<input type="hidden" name="name" value="show"',
  7294. $result,
  7295. 'should not contain entity data.'
  7296. );
  7297. }
  7298. /**
  7299. * testPostLinkSecurityHash method
  7300. *
  7301. * Test that security hashes for postLink include the url.
  7302. *
  7303. * @return void
  7304. */
  7305. public function testPostLinkSecurityHash()
  7306. {
  7307. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7308. $hash .= '%3Aid';
  7309. $this->View->setRequest($this->View->getRequest()->withParam('_Token.key', 'test'));
  7310. $result = $this->Form->postLink(
  7311. 'Delete',
  7312. '/posts/delete/1',
  7313. ['data' => ['id' => 1]]
  7314. );
  7315. $tokenDebug = urlencode(json_encode([
  7316. '/posts/delete/1',
  7317. [
  7318. 'id' => 1,
  7319. ],
  7320. [],
  7321. ]));
  7322. $expected = [
  7323. 'form' => [
  7324. 'method' => 'post', 'action' => '/posts/delete/1',
  7325. 'name', 'style' => 'display:none;',
  7326. ],
  7327. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7328. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7329. 'div' => ['style' => 'display:none;'],
  7330. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7331. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7332. ['input' => [
  7333. 'type' => 'hidden',
  7334. 'name' => '_Token[debug]',
  7335. 'value' => $tokenDebug,
  7336. 'autocomplete' => 'off',
  7337. ]],
  7338. '/div',
  7339. '/form',
  7340. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7341. 'Delete',
  7342. '/a',
  7343. ];
  7344. $this->assertHtml($expected, $result);
  7345. }
  7346. /**
  7347. * testPostLinkSecurityHashBlockMode method
  7348. *
  7349. * Test that postLink doesn't modify the fields in the containing form.
  7350. *
  7351. * postLink() calls inside open forms should not modify the field list
  7352. * for the form.
  7353. *
  7354. * @return void
  7355. */
  7356. public function testPostLinkSecurityHashBlockMode()
  7357. {
  7358. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  7359. $hash .= '%3A';
  7360. $this->View->setRequest($this->View->getRequest()->withParam('_Token.key', 'test'));
  7361. $this->Form->create(null, ['url' => ['action' => 'add']]);
  7362. $this->Form->control('title');
  7363. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7364. $result = $this->View->fetch('postLink');
  7365. $this->assertEquals(['title'], $this->Form->fields);
  7366. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  7367. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  7368. }
  7369. /**
  7370. * testPostLinkSecurityHashNoDebugMode method
  7371. *
  7372. * Test that security does not include debug token if debug is false.
  7373. *
  7374. * @return void
  7375. */
  7376. public function testPostLinkSecurityHashNoDebugMode()
  7377. {
  7378. Configure::write('debug', false);
  7379. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7380. $hash .= '%3Aid';
  7381. $this->View->setRequest($this->View->getRequest()
  7382. ->withParam('_Token.key', 'test'));
  7383. $result = $this->Form->postLink(
  7384. 'Delete',
  7385. '/posts/delete/1',
  7386. ['data' => ['id' => 1]]
  7387. );
  7388. $expected = [
  7389. 'form' => [
  7390. 'method' => 'post', 'action' => '/posts/delete/1',
  7391. 'name', 'style' => 'display:none;',
  7392. ],
  7393. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7394. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7395. 'div' => ['style' => 'display:none;'],
  7396. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7397. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7398. '/div',
  7399. '/form',
  7400. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7401. 'Delete',
  7402. '/a',
  7403. ];
  7404. $this->assertHtml($expected, $result);
  7405. }
  7406. /**
  7407. * testPostLinkNestedData method
  7408. *
  7409. * Test using postLink with N dimensional data.
  7410. *
  7411. * @return void
  7412. */
  7413. public function testPostLinkNestedData()
  7414. {
  7415. $data = [
  7416. 'one' => [
  7417. 'two' => [
  7418. 3, 4, 5,
  7419. ],
  7420. ],
  7421. ];
  7422. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7423. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7424. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7425. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7426. }
  7427. /**
  7428. * testPostLinkAfterGetForm method
  7429. *
  7430. * Test creating postLinks after a GET form.
  7431. *
  7432. * @return void
  7433. */
  7434. public function testPostLinkAfterGetForm()
  7435. {
  7436. $this->View->setRequest($this->View->getRequest()
  7437. ->withParam('_csrfToken', 'testkey')
  7438. ->withParam('_Token', 'val'));
  7439. $this->Form->create($this->article, ['type' => 'get']);
  7440. $this->Form->end();
  7441. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7442. $tokenDebug = urlencode(json_encode([
  7443. '/posts/delete/1',
  7444. [],
  7445. [],
  7446. ]));
  7447. $expected = [
  7448. 'form' => [
  7449. 'method' => 'post', 'action' => '/posts/delete/1',
  7450. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7451. ],
  7452. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7453. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7454. 'div' => ['style' => 'display:none;'],
  7455. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7456. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7457. ['input' => [
  7458. 'type' => 'hidden', 'name' => '_Token[debug]',
  7459. 'value' => $tokenDebug,
  7460. 'autocomplete' => 'off',
  7461. ]],
  7462. '/div',
  7463. '/form',
  7464. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7465. 'Delete',
  7466. '/a',
  7467. ];
  7468. $this->assertHtml($expected, $result);
  7469. }
  7470. /**
  7471. * testPostLinkFormBuffer method
  7472. *
  7473. * Test that postLink adds form tags to view block.
  7474. *
  7475. * @return void
  7476. */
  7477. public function testPostLinkFormBuffer()
  7478. {
  7479. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7480. $expected = [
  7481. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7482. 'Delete',
  7483. '/a',
  7484. ];
  7485. $this->assertHtml($expected, $result);
  7486. $result = $this->View->fetch('postLink');
  7487. $expected = [
  7488. 'form' => [
  7489. 'method' => 'post', 'action' => '/posts/delete/1',
  7490. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7491. ],
  7492. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7493. '/form',
  7494. ];
  7495. $this->assertHtml($expected, $result);
  7496. $result = $this->Form->postLink(
  7497. 'Delete',
  7498. '/posts/delete/2',
  7499. ['block' => true, 'method' => 'DELETE']
  7500. );
  7501. $expected = [
  7502. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7503. 'Delete',
  7504. '/a',
  7505. ];
  7506. $this->assertHtml($expected, $result);
  7507. $result = $this->View->fetch('postLink');
  7508. $expected = [
  7509. 'form' => [
  7510. 'method' => 'post', 'action' => '/posts/delete/1',
  7511. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7512. ],
  7513. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7514. '/form',
  7515. [
  7516. 'form' => [
  7517. 'method' => 'post', 'action' => '/posts/delete/2',
  7518. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7519. ],
  7520. ],
  7521. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7522. '/form',
  7523. ];
  7524. $this->assertHtml($expected, $result);
  7525. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7526. $expected = [
  7527. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7528. 'Delete',
  7529. '/a',
  7530. ];
  7531. $this->assertHtml($expected, $result);
  7532. $result = $this->View->fetch('foobar');
  7533. $expected = [
  7534. 'form' => [
  7535. 'method' => 'post', 'action' => '/posts/delete/1',
  7536. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7537. ],
  7538. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7539. '/form',
  7540. ];
  7541. $this->assertHtml($expected, $result);
  7542. }
  7543. /**
  7544. * testSubmitButton method
  7545. *
  7546. * @return void
  7547. */
  7548. public function testSubmitButton()
  7549. {
  7550. $result = $this->Form->submit('');
  7551. $expected = [
  7552. 'div' => ['class' => 'submit'],
  7553. 'input' => ['type' => 'submit', 'value' => ''],
  7554. '/div',
  7555. ];
  7556. $this->assertHtml($expected, $result);
  7557. $result = $this->Form->submit('Test Submit');
  7558. $expected = [
  7559. 'div' => ['class' => 'submit'],
  7560. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7561. '/div',
  7562. ];
  7563. $this->assertHtml($expected, $result);
  7564. $result = $this->Form->submit('Next >');
  7565. $expected = [
  7566. 'div' => ['class' => 'submit'],
  7567. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7568. '/div',
  7569. ];
  7570. $this->assertHtml($expected, $result);
  7571. $result = $this->Form->submit('Next >', ['escape' => false]);
  7572. $expected = [
  7573. 'div' => ['class' => 'submit'],
  7574. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7575. '/div',
  7576. ];
  7577. $this->assertHtml($expected, $result);
  7578. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7579. $expected = [
  7580. 'div' => ['class' => 'submit'],
  7581. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7582. '/div',
  7583. ];
  7584. $this->assertHtml($expected, $result);
  7585. }
  7586. /**
  7587. * testSubmitImage method
  7588. *
  7589. * Test image submit types.
  7590. *
  7591. * @return void
  7592. */
  7593. public function testSubmitImage()
  7594. {
  7595. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7596. $expected = [
  7597. 'div' => ['class' => 'submit'],
  7598. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7599. '/div',
  7600. ];
  7601. $this->assertHtml($expected, $result);
  7602. $result = $this->Form->submit('/relative/cake.power.gif');
  7603. $expected = [
  7604. 'div' => ['class' => 'submit'],
  7605. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7606. '/div',
  7607. ];
  7608. $this->assertHtml($expected, $result);
  7609. $result = $this->Form->submit('cake.power.gif');
  7610. $expected = [
  7611. 'div' => ['class' => 'submit'],
  7612. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7613. '/div',
  7614. ];
  7615. $this->assertHtml($expected, $result);
  7616. $result = $this->Form->submit('Not.an.image');
  7617. $expected = [
  7618. 'div' => ['class' => 'submit'],
  7619. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7620. '/div',
  7621. ];
  7622. $this->assertHtml($expected, $result);
  7623. }
  7624. /**
  7625. * testSubmitUnlockedByDefault method
  7626. *
  7627. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7628. * be submitted at a time.
  7629. *
  7630. * @return void
  7631. */
  7632. public function testSubmitUnlockedByDefault()
  7633. {
  7634. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'secured'));
  7635. $this->Form->submit('Go go');
  7636. $this->Form->submit('Save', ['name' => 'save']);
  7637. $result = $this->Form->unlockField();
  7638. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7639. }
  7640. /**
  7641. * testSubmitImageTimestamp method
  7642. *
  7643. * Test submit image with timestamps.
  7644. *
  7645. * @return void
  7646. */
  7647. public function testSubmitImageTimestamp()
  7648. {
  7649. Configure::write('Asset.timestamp', 'force');
  7650. $result = $this->Form->submit('cake.power.gif');
  7651. $expected = [
  7652. 'div' => ['class' => 'submit'],
  7653. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7654. '/div',
  7655. ];
  7656. $this->assertHtml($expected, $result);
  7657. }
  7658. /**
  7659. * testDateTimeWithGetForms method
  7660. *
  7661. * Test that datetime() works with GET style forms.
  7662. *
  7663. * @return void
  7664. */
  7665. public function testDateTimeWithGetForms()
  7666. {
  7667. extract($this->dateRegex);
  7668. $this->Form->create($this->article, ['type' => 'get']);
  7669. $result = $this->Form->datetime('created');
  7670. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7671. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7672. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7673. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7674. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7675. }
  7676. /**
  7677. * testForMagicControlNonExistingNorValidated method
  7678. *
  7679. * @return void
  7680. */
  7681. public function testForMagicControlNonExistingNorValidated()
  7682. {
  7683. $this->Form->create($this->article);
  7684. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7685. $result = $this->Form->control('non_existing_nor_validated');
  7686. $expected = [
  7687. 'label' => ['for' => 'non-existing-nor-validated'],
  7688. 'Non Existing Nor Validated',
  7689. '/label',
  7690. 'input' => [
  7691. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7692. 'id' => 'non-existing-nor-validated',
  7693. ],
  7694. ];
  7695. $this->assertHtml($expected, $result);
  7696. $result = $this->Form->control('non_existing_nor_validated', [
  7697. 'val' => 'my value',
  7698. ]);
  7699. $expected = [
  7700. 'label' => ['for' => 'non-existing-nor-validated'],
  7701. 'Non Existing Nor Validated',
  7702. '/label',
  7703. 'input' => [
  7704. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7705. 'value' => 'my value', 'id' => 'non-existing-nor-validated',
  7706. ],
  7707. ];
  7708. $this->assertHtml($expected, $result);
  7709. $this->View->setRequest(
  7710. $this->View->getRequest()->withData('non_existing_nor_validated', 'CakePHP magic')
  7711. );
  7712. $this->Form->create($this->article);
  7713. $result = $this->Form->control('non_existing_nor_validated');
  7714. $expected = [
  7715. 'label' => ['for' => 'non-existing-nor-validated'],
  7716. 'Non Existing Nor Validated',
  7717. '/label',
  7718. 'input' => [
  7719. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7720. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated',
  7721. ],
  7722. ];
  7723. $this->assertHtml($expected, $result);
  7724. }
  7725. /**
  7726. * testFormMagicControlLabel method
  7727. *
  7728. * @return void
  7729. */
  7730. public function testFormMagicControlLabel()
  7731. {
  7732. $this->getTableLocator()->get('Contacts', [
  7733. 'className' => __NAMESPACE__ . '\ContactsTable',
  7734. ]);
  7735. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7736. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7737. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7738. $expected = [
  7739. 'label' => ['for' => 'contacts-name'],
  7740. 'My label',
  7741. '/label',
  7742. 'input' => [
  7743. 'type' => 'text', 'name' => 'Contacts[name]',
  7744. 'id' => 'contacts-name', 'maxlength' => '255',
  7745. ],
  7746. ];
  7747. $this->assertHtml($expected, $result);
  7748. $result = $this->Form->control('name', [
  7749. 'label' => ['class' => 'mandatory'],
  7750. ]);
  7751. $expected = [
  7752. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7753. 'Name',
  7754. '/label',
  7755. 'input' => [
  7756. 'type' => 'text', 'name' => 'name',
  7757. 'id' => 'name', 'maxlength' => '255',
  7758. ],
  7759. ];
  7760. $this->assertHtml($expected, $result);
  7761. $result = $this->Form->control('name', [
  7762. 'div' => false,
  7763. 'label' => ['class' => 'mandatory', 'text' => 'My label'],
  7764. ]);
  7765. $expected = [
  7766. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7767. 'My label',
  7768. '/label',
  7769. 'input' => [
  7770. 'type' => 'text', 'name' => 'name',
  7771. 'id' => 'name', 'maxlength' => '255',
  7772. ],
  7773. ];
  7774. $this->assertHtml($expected, $result);
  7775. $result = $this->Form->control('Contact.name', [
  7776. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id'],
  7777. ]);
  7778. $expected = [
  7779. 'label' => ['for' => 'my_id'],
  7780. 'Name',
  7781. '/label',
  7782. 'input' => [
  7783. 'type' => 'text', 'name' => 'Contact[name]',
  7784. 'id' => 'my_id', 'maxlength' => '255',
  7785. ],
  7786. ];
  7787. $this->assertHtml($expected, $result);
  7788. $result = $this->Form->control('1.id');
  7789. $expected = ['input' => [
  7790. 'type' => 'hidden', 'name' => '1[id]',
  7791. 'id' => '1-id',
  7792. ]];
  7793. $this->assertHtml($expected, $result);
  7794. $result = $this->Form->control('1.name');
  7795. $expected = [
  7796. 'label' => ['for' => '1-name'],
  7797. 'Name',
  7798. '/label',
  7799. 'input' => [
  7800. 'type' => 'text', 'name' => '1[name]',
  7801. 'id' => '1-name', 'maxlength' => '255',
  7802. ],
  7803. ];
  7804. $this->assertHtml($expected, $result);
  7805. }
  7806. /**
  7807. * testFormEnd method
  7808. *
  7809. * @return void
  7810. */
  7811. public function testFormEnd()
  7812. {
  7813. $this->assertEquals('</form>', $this->Form->end());
  7814. }
  7815. /**
  7816. * testMultiRecordForm method
  7817. *
  7818. * Test the generation of fields for a multi record form.
  7819. *
  7820. * @return void
  7821. */
  7822. public function testMultiRecordForm()
  7823. {
  7824. $this->loadFixtures('Articles', 'Comments');
  7825. $articles = $this->getTableLocator()->get('Articles');
  7826. $articles->hasMany('Comments');
  7827. $comment = new Entity(['comment' => 'Value']);
  7828. $article = new Article(['comments' => [$comment]]);
  7829. $this->Form->create([$article]);
  7830. $result = $this->Form->control('0.comments.1.comment');
  7831. //@codingStandardsIgnoreStart
  7832. $expected = [
  7833. 'div' => ['class' => 'input textarea'],
  7834. 'label' => ['for' => '0-comments-1-comment'],
  7835. 'Comment',
  7836. '/label',
  7837. 'textarea' => [
  7838. 'name',
  7839. 'id' => '0-comments-1-comment',
  7840. 'rows' => 5
  7841. ],
  7842. '/textarea',
  7843. '/div'
  7844. ];
  7845. //@codingStandardsIgnoreEnd
  7846. $this->assertHtml($expected, $result);
  7847. $result = $this->Form->control('0.comments.0.comment');
  7848. //@codingStandardsIgnoreStart
  7849. $expected = [
  7850. 'div' => ['class' => 'input textarea'],
  7851. 'label' => ['for' => '0-comments-0-comment'],
  7852. 'Comment',
  7853. '/label',
  7854. 'textarea' => [
  7855. 'name',
  7856. 'id' => '0-comments-0-comment',
  7857. 'rows' => 5
  7858. ],
  7859. 'Value',
  7860. '/textarea',
  7861. '/div'
  7862. ];
  7863. //@codingStandardsIgnoreEnd
  7864. $this->assertHtml($expected, $result);
  7865. $comment->setError('comment', ['Not valid']);
  7866. $result = $this->Form->control('0.comments.0.comment');
  7867. //@codingStandardsIgnoreStart
  7868. $expected = [
  7869. 'div' => ['class' => 'input textarea error'],
  7870. 'label' => ['for' => '0-comments-0-comment'],
  7871. 'Comment',
  7872. '/label',
  7873. 'textarea' => [
  7874. 'name',
  7875. 'class' => 'form-error',
  7876. 'id' => '0-comments-0-comment',
  7877. 'rows' => 5
  7878. ],
  7879. 'Value',
  7880. '/textarea',
  7881. ['div' => ['class' => 'error-message']],
  7882. 'Not valid',
  7883. '/div',
  7884. '/div'
  7885. ];
  7886. //@codingStandardsIgnoreEnd
  7887. $this->assertHtml($expected, $result);
  7888. $this->getTableLocator()->get('Comments')
  7889. ->getValidator('default')
  7890. ->allowEmpty('comment', false);
  7891. $result = $this->Form->control('0.comments.1.comment');
  7892. //@codingStandardsIgnoreStart
  7893. $expected = [
  7894. 'div' => ['class' => 'input textarea required'],
  7895. 'label' => ['for' => '0-comments-1-comment'],
  7896. 'Comment',
  7897. '/label',
  7898. 'textarea' => [
  7899. 'name',
  7900. 'required' => 'required',
  7901. 'id' => '0-comments-1-comment',
  7902. 'rows' => 5,
  7903. ],
  7904. '/textarea',
  7905. '/div'
  7906. ];
  7907. //@codingStandardsIgnoreEnd
  7908. $this->assertHtml($expected, $result);
  7909. }
  7910. /**
  7911. * testHtml5Controls method
  7912. *
  7913. * Test that some html5 inputs + FormHelper::__call() work.
  7914. *
  7915. * @return void
  7916. */
  7917. public function testHtml5Controls()
  7918. {
  7919. $result = $this->Form->email('User.email');
  7920. $expected = [
  7921. 'input' => ['type' => 'email', 'name' => 'User[email]'],
  7922. ];
  7923. $this->assertHtml($expected, $result);
  7924. $result = $this->Form->search('User.query');
  7925. $expected = [
  7926. 'input' => ['type' => 'search', 'name' => 'User[query]'],
  7927. ];
  7928. $this->assertHtml($expected, $result);
  7929. $result = $this->Form->search('User.query', ['value' => 'test']);
  7930. $expected = [
  7931. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test'],
  7932. ];
  7933. $this->assertHtml($expected, $result);
  7934. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7935. $expected = [
  7936. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test'],
  7937. ];
  7938. $this->assertHtml($expected, $result);
  7939. }
  7940. /**
  7941. * testHtml5ControlWithControl method
  7942. *
  7943. * Test accessing html5 inputs through control().
  7944. *
  7945. * @return void
  7946. */
  7947. public function testHtml5ControlWithControl()
  7948. {
  7949. $this->Form->create();
  7950. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7951. $result = $this->Form->control('website', [
  7952. 'type' => 'url',
  7953. 'val' => 'http://domain.tld',
  7954. 'label' => false,
  7955. ]);
  7956. $expected = [
  7957. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld'],
  7958. ];
  7959. $this->assertHtml($expected, $result);
  7960. }
  7961. /**
  7962. * testHtml5ControlException method
  7963. *
  7964. * Test errors when field name is missing.
  7965. *
  7966. * @return void
  7967. */
  7968. public function testHtml5ControlException()
  7969. {
  7970. $this->expectException(\Cake\Core\Exception\Exception::class);
  7971. $this->Form->email();
  7972. }
  7973. /**
  7974. * tests fields that are required use custom validation messages
  7975. *
  7976. * @return void
  7977. */
  7978. public function testHtml5ErrorMessage()
  7979. {
  7980. $this->Form->setConfig('autoSetCustomValidity', true);
  7981. $validator = (new \Cake\Validation\Validator())
  7982. ->requirePresence('email', true, 'Custom error message')
  7983. ->requirePresence('password')
  7984. ->alphaNumeric('password')
  7985. ->notBlank('phone');
  7986. $table = $this->getTableLocator()->get('Contacts', [
  7987. 'className' => __NAMESPACE__ . '\ContactsTable',
  7988. ]);
  7989. $table->setValidator('default', $validator);
  7990. $contact = new Entity();
  7991. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7992. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7993. $result = $this->Form->control('password');
  7994. $expected = [
  7995. 'label' => ['for' => 'password'],
  7996. 'Password',
  7997. '/label',
  7998. 'input' => [
  7999. 'id' => 'password',
  8000. 'name' => 'password',
  8001. 'type' => 'password',
  8002. 'value' => '',
  8003. 'required' => 'required',
  8004. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8005. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.validity.valid) this.setCustomValidity(&#039;This field is required&#039;)',
  8006. ],
  8007. ];
  8008. $this->assertHtml($expected, $result);
  8009. $result = $this->Form->control('phone');
  8010. $expected = [
  8011. 'label' => ['for' => 'phone'],
  8012. 'Phone',
  8013. '/label',
  8014. 'input' => [
  8015. 'id' => 'phone',
  8016. 'name' => 'phone',
  8017. 'type' => 'tel',
  8018. 'value' => '',
  8019. 'maxlength' => 255,
  8020. 'required' => 'required',
  8021. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8022. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.validity.valid) this.setCustomValidity(&#039;This field cannot be left empty&#039;)',
  8023. ],
  8024. ];
  8025. $this->assertHtml($expected, $result);
  8026. $result = $this->Form->control('email');
  8027. $expected = [
  8028. 'label' => ['for' => 'email'],
  8029. 'Email',
  8030. '/label',
  8031. 'input' => [
  8032. 'id' => 'email',
  8033. 'name' => 'email',
  8034. 'type' => 'email',
  8035. 'value' => '',
  8036. 'maxlength' => 255,
  8037. 'required' => 'required',
  8038. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8039. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.validity.valid) this.setCustomValidity(&#039;Custom error message&#039;)',
  8040. ],
  8041. ];
  8042. $this->assertHtml($expected, $result);
  8043. }
  8044. /**
  8045. * tests that custom validation messages are in templateVars
  8046. *
  8047. * @return void
  8048. */
  8049. public function testHtml5ErrorMessageInTemplateVars()
  8050. {
  8051. $validator = (new \Cake\Validation\Validator())
  8052. ->requirePresence('email', true, 'Custom error "message" & entities')
  8053. ->requirePresence('password')
  8054. ->alphaNumeric('password')
  8055. ->notBlank('phone');
  8056. $table = $this->getTableLocator()->get('Contacts', [
  8057. 'className' => __NAMESPACE__ . '\ContactsTable',
  8058. ]);
  8059. $table->setValidator('default', $validator);
  8060. $contact = new Entity();
  8061. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  8062. $this->Form->setTemplates([
  8063. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  8064. 'inputContainer' => '{{content}}',
  8065. ]);
  8066. $result = $this->Form->control('password');
  8067. $expected = [
  8068. 'label' => ['for' => 'password'],
  8069. 'Password',
  8070. '/label',
  8071. 'input' => [
  8072. 'id' => 'password',
  8073. 'name' => 'password',
  8074. 'type' => 'password',
  8075. 'value' => '',
  8076. 'required' => 'required',
  8077. 'data-message' => 'This field is required',
  8078. ],
  8079. ];
  8080. $this->assertHtml($expected, $result);
  8081. $result = $this->Form->control('phone');
  8082. $expected = [
  8083. 'label' => ['for' => 'phone'],
  8084. 'Phone',
  8085. '/label',
  8086. 'input' => [
  8087. 'id' => 'phone',
  8088. 'name' => 'phone',
  8089. 'type' => 'tel',
  8090. 'value' => '',
  8091. 'maxlength' => 255,
  8092. 'required' => 'required',
  8093. 'data-message' => 'This field cannot be left empty',
  8094. ],
  8095. ];
  8096. $this->assertHtml($expected, $result);
  8097. $result = $this->Form->control('email', [
  8098. 'templateVars' => [
  8099. 'custom' => 'data-custom="1"',
  8100. ],
  8101. ]);
  8102. $expected = [
  8103. 'label' => ['for' => 'email'],
  8104. 'Email',
  8105. '/label',
  8106. 'input' => [
  8107. 'id' => 'email',
  8108. 'name' => 'email',
  8109. 'type' => 'email',
  8110. 'value' => '',
  8111. 'maxlength' => 255,
  8112. 'required' => 'required',
  8113. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  8114. 'data-custom' => '1',
  8115. ],
  8116. ];
  8117. $this->assertHtml($expected, $result);
  8118. }
  8119. /**
  8120. * testRequiredAttribute method
  8121. *
  8122. * Tests that formhelper sets required attributes.
  8123. *
  8124. * @return void
  8125. */
  8126. public function testRequiredAttribute()
  8127. {
  8128. $this->article['required'] = [
  8129. 'title' => true,
  8130. 'body' => false,
  8131. ];
  8132. $this->Form->create($this->article);
  8133. $result = $this->Form->control('title');
  8134. $expected = [
  8135. 'div' => ['class' => 'input text required'],
  8136. 'label' => ['for' => 'title'],
  8137. 'Title',
  8138. '/label',
  8139. 'input' => [
  8140. 'type' => 'text',
  8141. 'name' => 'title',
  8142. 'id' => 'title',
  8143. 'required' => 'required',
  8144. ],
  8145. '/div',
  8146. ];
  8147. $this->assertHtml($expected, $result);
  8148. $result = $this->Form->control('title', ['required' => false]);
  8149. $this->assertNotContains('required', $result);
  8150. $result = $this->Form->control('body');
  8151. $expected = [
  8152. 'div' => ['class' => 'input text'],
  8153. 'label' => ['for' => 'body'],
  8154. 'Body',
  8155. '/label',
  8156. 'input' => [
  8157. 'type' => 'text',
  8158. 'name' => 'body',
  8159. 'id' => 'body',
  8160. ],
  8161. '/div',
  8162. ];
  8163. $this->assertHtml($expected, $result);
  8164. $result = $this->Form->control('body', ['required' => true]);
  8165. $this->assertContains('required', $result);
  8166. }
  8167. /**
  8168. * testControlsNotNested method
  8169. *
  8170. * Tests that it is possible to put inputs outside of the label.
  8171. *
  8172. * @return void
  8173. */
  8174. public function testControlsNotNested()
  8175. {
  8176. $this->Form->setTemplates([
  8177. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  8178. 'formGroup' => '{{input}}{{label}}',
  8179. ]);
  8180. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  8181. $expected = [
  8182. 'div' => ['class' => 'input checkbox'],
  8183. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  8184. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  8185. 'label' => ['for' => 'foo'],
  8186. 'Foo',
  8187. '/label',
  8188. '/div',
  8189. ];
  8190. $this->assertHtml($expected, $result);
  8191. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  8192. $expected = [
  8193. 'div' => ['class' => 'input checkbox'],
  8194. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  8195. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  8196. '/div',
  8197. ];
  8198. $this->assertHtml($expected, $result);
  8199. $result = $this->Form->control('confirm', [
  8200. 'type' => 'radio',
  8201. 'options' => ['Y' => 'Yes', 'N' => 'No'],
  8202. ]);
  8203. $expected = [
  8204. 'div' => ['class' => 'input radio'],
  8205. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  8206. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  8207. ['label' => ['for' => 'confirm-y']],
  8208. 'Yes',
  8209. '/label',
  8210. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  8211. ['label' => ['for' => 'confirm-n']],
  8212. 'No',
  8213. '/label',
  8214. '<label',
  8215. 'Confirm',
  8216. '/label',
  8217. '/div',
  8218. ];
  8219. $this->assertHtml($expected, $result);
  8220. $result = $this->Form->select('category', ['1', '2'], [
  8221. 'multiple' => 'checkbox',
  8222. 'name' => 'fish',
  8223. ]);
  8224. $expected = [
  8225. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  8226. ['div' => ['class' => 'checkbox']],
  8227. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  8228. ['label' => ['for' => 'fish-0']],
  8229. '1',
  8230. '/label',
  8231. '/div',
  8232. ['div' => ['class' => 'checkbox']],
  8233. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  8234. ['label' => ['for' => 'fish-1']],
  8235. '2',
  8236. '/label',
  8237. '/div',
  8238. ];
  8239. $this->assertHtml($expected, $result);
  8240. }
  8241. /**
  8242. * testControlContainerTemplates method
  8243. *
  8244. * Test that *Container templates are used by input.
  8245. *
  8246. * @return void
  8247. */
  8248. public function testControlContainerTemplates()
  8249. {
  8250. $this->Form->setTemplates([
  8251. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  8252. 'radioContainer' => '<div class="rad">{{content}}</div>',
  8253. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  8254. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  8255. ]);
  8256. $this->article['errors'] = [
  8257. 'Article' => ['published' => 'error message'],
  8258. ];
  8259. $this->Form->create($this->article);
  8260. $result = $this->Form->control('accept', [
  8261. 'type' => 'checkbox',
  8262. ]);
  8263. $expected = [
  8264. 'div' => ['class' => 'check'],
  8265. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  8266. 'label' => ['for' => 'accept'],
  8267. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  8268. 'Accept',
  8269. '/label',
  8270. '/div',
  8271. ];
  8272. $this->assertHtml($expected, $result);
  8273. $result = $this->Form->control('accept', [
  8274. 'type' => 'radio',
  8275. 'options' => ['Y', 'N'],
  8276. ]);
  8277. $this->assertContains('<div class="rad">', $result);
  8278. $result = $this->Form->control('Article.published', [
  8279. 'type' => 'radio',
  8280. 'options' => ['Y', 'N'],
  8281. ]);
  8282. $this->assertContains('<div class="rad err">', $result);
  8283. $result = $this->Form->control('Article.created', [
  8284. 'type' => 'datetime',
  8285. ]);
  8286. $this->assertContains('<div class="dt">', $result);
  8287. }
  8288. /**
  8289. * testFormGroupTemplates method
  8290. *
  8291. * Test that *Container templates are used by input.
  8292. *
  8293. * @return void
  8294. */
  8295. public function testFormGroupTemplates()
  8296. {
  8297. $this->Form->setTemplates([
  8298. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  8299. ]);
  8300. $this->Form->create($this->article);
  8301. $result = $this->Form->control('accept', [
  8302. 'type' => 'radio',
  8303. 'options' => ['Y', 'N'],
  8304. ]);
  8305. $this->assertContains('<div class="radio">', $result);
  8306. }
  8307. /**
  8308. * testResetTemplates method
  8309. *
  8310. * Test resetting templates.
  8311. *
  8312. * @return void
  8313. */
  8314. public function testResetTemplates()
  8315. {
  8316. $this->Form->setTemplates(['input' => '<input/>']);
  8317. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  8318. $this->Form->resetTemplates();
  8319. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  8320. }
  8321. /**
  8322. * testContext method
  8323. *
  8324. * Test the context method.
  8325. *
  8326. * @return void
  8327. */
  8328. public function testContext()
  8329. {
  8330. $result = $this->Form->context();
  8331. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  8332. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  8333. $this->assertSame($mock, $this->Form->context($mock));
  8334. $this->assertSame($mock, $this->Form->context());
  8335. }
  8336. /**
  8337. * testAutoDomId method
  8338. *
  8339. * @return void
  8340. */
  8341. public function testAutoDomId()
  8342. {
  8343. $result = $this->Form->text('field', ['id' => true]);
  8344. $expected = [
  8345. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  8346. ];
  8347. $this->assertHtml($expected, $result);
  8348. // Ensure id => doesn't cause problem when multiple inputs are generated.
  8349. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  8350. $expected = [
  8351. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  8352. ['label' => ['for' => 'field-0']],
  8353. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  8354. 'option A',
  8355. '/label',
  8356. ['label' => ['for' => 'field-1']],
  8357. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  8358. 'option B',
  8359. '/label',
  8360. ];
  8361. $this->assertHtml($expected, $result);
  8362. $result = $this->Form->select(
  8363. 'multi_field',
  8364. ['first', 'second'],
  8365. ['multiple' => 'checkbox', 'id' => true]
  8366. );
  8367. $expected = [
  8368. 'input' => [
  8369. 'type' => 'hidden', 'name' => 'multi_field', 'value' => '',
  8370. ],
  8371. ['div' => ['class' => 'checkbox']],
  8372. ['label' => ['for' => 'multi-field-0']],
  8373. ['input' => [
  8374. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8375. 'value' => '0', 'id' => 'multi-field-0',
  8376. ]],
  8377. 'first',
  8378. '/label',
  8379. '/div',
  8380. ['div' => ['class' => 'checkbox']],
  8381. ['label' => ['for' => 'multi-field-1']],
  8382. ['input' => [
  8383. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8384. 'value' => '1', 'id' => 'multi-field-1',
  8385. ]],
  8386. 'second',
  8387. '/label',
  8388. '/div',
  8389. ];
  8390. $this->assertHtml($expected, $result);
  8391. }
  8392. /**
  8393. * Test the basic setters and getters for value sources
  8394. *
  8395. * @return void
  8396. */
  8397. public function testFormValueSourcesSettersGetters()
  8398. {
  8399. $this->View->setRequest($this->View->getRequest()
  8400. ->withData('id', '1')
  8401. ->withQueryParams(['id' => '2']));
  8402. $expected = ['context'];
  8403. $result = $this->Form->getValueSources();
  8404. $this->assertEquals($expected, $result);
  8405. $expected = ['query', 'data', 'context'];
  8406. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  8407. $result = $this->Form->getValueSources();
  8408. $this->assertEquals($expected, $result);
  8409. $this->Form->setValueSources(['context']);
  8410. $expected = '1';
  8411. $result = $this->Form->getSourceValue('id');
  8412. $this->assertEquals($expected, $result);
  8413. $this->Form->setValueSources('query');
  8414. $expected = ['query'];
  8415. $result = $this->Form->getValueSources();
  8416. $this->assertEquals($expected, $result);
  8417. $expected = '2';
  8418. $result = $this->Form->getSourceValue('id');
  8419. $this->assertEquals($expected, $result);
  8420. $this->Form->setValueSources(['data']);
  8421. $expected = '1';
  8422. $result = $this->Form->getSourceValue('id');
  8423. $this->assertEquals($expected, $result);
  8424. $this->Form->setValueSources(['query', 'data']);
  8425. $expected = '2';
  8426. $result = $this->Form->getSourceValue('id');
  8427. $this->assertEquals($expected, $result);
  8428. }
  8429. /**
  8430. * Tests the different input rendering values based on sources values switching
  8431. *
  8432. * @return void
  8433. */
  8434. public function testFormValueSourcesSingleSwitchRendering()
  8435. {
  8436. $this->loadFixtures('Articles');
  8437. $articles = $this->getTableLocator()->get('Articles');
  8438. $article = new Article();
  8439. $articles->patchEntity($article, ['id' => '3']);
  8440. $this->Form->create($article);
  8441. $this->Form->setValueSources(['context']);
  8442. $result = $this->Form->control('id');
  8443. $expected = [
  8444. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8445. ];
  8446. $this->assertHtml($expected, $result);
  8447. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  8448. $this->Form->setValueSources(['query']);
  8449. $result = $this->Form->control('id');
  8450. $expected = [
  8451. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8452. ];
  8453. $this->assertHtml($expected, $result);
  8454. $this->View->setRequest($this->View->getRequest()
  8455. ->withQueryParams(['id' => '5a'])
  8456. ->withData('id', '5b'));
  8457. $this->Form->setValueSources(['context']);
  8458. $this->Form->create($article);
  8459. $result = $this->Form->control('id');
  8460. $expected = [
  8461. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8462. ];
  8463. $this->assertHtml($expected, $result);
  8464. $this->Form->setValueSources(['data']);
  8465. $this->Form->create($article);
  8466. $result = $this->Form->control('id');
  8467. $expected = [
  8468. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8469. ];
  8470. $this->assertHtml($expected, $result);
  8471. $this->Form->setValueSources(['query']);
  8472. $result = $this->Form->control('id');
  8473. $expected = [
  8474. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  8475. ];
  8476. $this->assertHtml($expected, $result);
  8477. }
  8478. /**
  8479. * Tests the different input rendering values based on sources values switching while supplying
  8480. * an entity (base context) and multiple sources (such as data, query)
  8481. *
  8482. * @return void
  8483. */
  8484. public function testFormValueSourcesListSwitchRendering()
  8485. {
  8486. $this->loadFixtures('Articles');
  8487. $articles = $this->getTableLocator()->get('Articles');
  8488. $article = new Article();
  8489. $articles->patchEntity($article, ['id' => '3']);
  8490. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  8491. $this->Form->create($article);
  8492. $this->Form->setValueSources(['context', 'query']);
  8493. $result = $this->Form->control('id');
  8494. $expected = [
  8495. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8496. ];
  8497. $this->assertHtml($expected, $result);
  8498. $this->Form->setValueSources(['query', 'context']);
  8499. $result = $this->Form->control('id');
  8500. $expected = [
  8501. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8502. ];
  8503. $this->assertHtml($expected, $result);
  8504. $this->Form->setValueSources(['data', 'query', 'context']);
  8505. $result = $this->Form->control('id');
  8506. $expected = [
  8507. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8508. ];
  8509. $this->assertHtml($expected, $result);
  8510. $this->View->setRequest($this->View->getRequest()
  8511. ->withData('id', '8')
  8512. ->withQueryParams(['id' => '9']));
  8513. $this->Form->setValueSources(['data', 'query', 'context']);
  8514. $result = $this->Form->control('id');
  8515. $expected = [
  8516. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  8517. ];
  8518. $this->assertHtml($expected, $result);
  8519. }
  8520. /**
  8521. * Test the different form input renderings based on values sources switchings through form options
  8522. *
  8523. * @return void
  8524. */
  8525. public function testFormValueSourcesSwitchViaOptionsRendering()
  8526. {
  8527. $this->loadFixtures('Articles');
  8528. $articles = $this->getTableLocator()->get('Articles');
  8529. $article = new Article();
  8530. $articles->patchEntity($article, ['id' => '3']);
  8531. $this->View->setRequest(
  8532. $this->View->getRequest()->withData('id', 4)->withQueryParams(['id' => 5])
  8533. );
  8534. $this->Form->create($article, ['valueSources' => 'query']);
  8535. $result = $this->Form->control('id');
  8536. $expected = [
  8537. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8538. ];
  8539. $this->assertHtml($expected, $result);
  8540. $result = $this->Form->getSourceValue('id');
  8541. $this->assertEquals('5', $result);
  8542. $this->Form->setValueSources(['context']);
  8543. $this->Form->create($article, ['valueSources' => 'query']);
  8544. $result = $this->Form->control('id');
  8545. $expected = [
  8546. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8547. ];
  8548. $this->assertHtml($expected, $result);
  8549. $result = $this->Form->getSourceValue('id');
  8550. $this->assertEquals('5', $result);
  8551. $this->Form->setValueSources(['query']);
  8552. $this->Form->create($article, ['valueSources' => 'data']);
  8553. $result = $this->Form->control('id');
  8554. $expected = [
  8555. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8556. ];
  8557. $this->assertHtml($expected, $result);
  8558. $result = $this->Form->getSourceValue('id');
  8559. $this->assertEquals('4', $result);
  8560. $this->Form->setValueSources(['query']);
  8561. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8562. $result = $this->Form->control('id');
  8563. $expected = [
  8564. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8565. ];
  8566. $this->assertHtml($expected, $result);
  8567. $result = $this->Form->getSourceValue('id');
  8568. $this->assertEquals('4', $result);
  8569. }
  8570. /**
  8571. * Test the different form input renderings based on values sources switchings through form options
  8572. *
  8573. * @return void
  8574. */
  8575. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8576. {
  8577. $this->loadFixtures('Articles');
  8578. $articles = $this->getTableLocator()->get('Articles');
  8579. $article = new Article();
  8580. $articles->patchEntity($article, ['id' => '3']);
  8581. $this->View->setRequest(
  8582. $this->View->getRequest()->withData('id', 10)->withQueryParams(['id' => 11])
  8583. );
  8584. $this->Form->setValueSources(['context'])
  8585. ->create($article, ['valueSources' => ['query', 'data']]);
  8586. $result = $this->Form->control('id');
  8587. $expected = [
  8588. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8589. ];
  8590. $this->assertHtml($expected, $result);
  8591. $result = $this->Form->getSourceValue('id');
  8592. $this->assertEquals('11', $result);
  8593. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  8594. $this->Form->setValueSources(['context'])
  8595. ->create($article, ['valueSources' => ['query', 'data']]);
  8596. $result = $this->Form->control('id');
  8597. $expected = [
  8598. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8599. ];
  8600. $this->assertHtml($expected, $result);
  8601. $result = $this->Form->getSourceValue('id');
  8602. $this->assertEquals('10', $result);
  8603. }
  8604. /**
  8605. * Test the different form values sources resetting through From::end();
  8606. *
  8607. * @return void
  8608. */
  8609. public function testFormValueSourcesResetViaEnd()
  8610. {
  8611. $expected = ['context'];
  8612. $result = $this->Form->getValueSources();
  8613. $this->assertEquals($expected, $result);
  8614. $expected = ['query', 'context', 'data'];
  8615. $this->Form->setValueSources(['query', 'context', 'data']);
  8616. $result = $this->Form->getValueSources();
  8617. $this->assertEquals($expected, $result);
  8618. $this->Form->create();
  8619. $result = $this->Form->getValueSources();
  8620. $this->assertEquals($expected, $result);
  8621. $this->Form->end();
  8622. $result = $this->Form->getValueSources();
  8623. $this->assertEquals(['context'], $result);
  8624. }
  8625. /**
  8626. * Test sources values defaults handling
  8627. *
  8628. * @return void
  8629. */
  8630. public function testFormValueSourcesDefaults()
  8631. {
  8632. $this->View->setRequest(
  8633. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  8634. );
  8635. $this->Form->create();
  8636. $result = $this->Form->password('password');
  8637. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8638. $this->assertHtml($expected, $result);
  8639. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8640. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8641. $this->assertHtml($expected, $result);
  8642. $this->Form->setValueSources('query');
  8643. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8644. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8645. $this->assertHtml($expected, $result);
  8646. $this->Form->setValueSources('data');
  8647. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8648. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8649. $this->assertHtml($expected, $result);
  8650. }
  8651. /**
  8652. * Test sources values schema defaults handling
  8653. *
  8654. * @return void
  8655. */
  8656. public function testSourcesValueDoesntExistPassThrough()
  8657. {
  8658. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  8659. $articles = $this->getTableLocator()->get('Articles');
  8660. $entity = $articles->newEntity();
  8661. $this->Form->create($entity);
  8662. $this->Form->setValueSources(['query', 'context']);
  8663. $result = $this->Form->getSourceValue('category');
  8664. $this->assertEquals('sesame-cookies', $result);
  8665. $this->Form->setValueSources(['context', 'query']);
  8666. $result = $this->Form->getSourceValue('category');
  8667. $this->assertEquals('sesame-cookies', $result);
  8668. }
  8669. /**
  8670. * testNestedLabelInput method
  8671. *
  8672. * Test the `nestedInput` parameter
  8673. *
  8674. * @group deprecated
  8675. * @return void
  8676. */
  8677. public function testNestedLabelInput()
  8678. {
  8679. $this->deprecated(function () {
  8680. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8681. $expected = [
  8682. 'div' => ['class' => 'input text'],
  8683. 'label' => ['for' => 'foo'],
  8684. ['input' => [
  8685. 'type' => 'text',
  8686. 'name' => 'foo',
  8687. 'id' => 'foo',
  8688. ]],
  8689. 'Foo',
  8690. '/label',
  8691. '/div',
  8692. ];
  8693. $this->assertHtml($expected, $result);
  8694. });
  8695. }
  8696. /**
  8697. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8698. *
  8699. * This test makes sure `false` excludes the label from the render
  8700. *
  8701. * @return void
  8702. */
  8703. public function testControlLabelManipulationDisableLabels()
  8704. {
  8705. $result = $this->Form->control('test', [
  8706. 'type' => 'radio',
  8707. 'options' => ['A', 'B'],
  8708. 'labelOptions' => false,
  8709. ]);
  8710. $expected = [
  8711. ['div' => ['class' => 'input radio']],
  8712. '<label',
  8713. 'Test',
  8714. '/label',
  8715. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8716. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8717. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8718. '/div',
  8719. ];
  8720. $this->assertHtml($expected, $result);
  8721. $result = $this->Form->control('checkbox1', [
  8722. 'label' => 'My checkboxes',
  8723. 'multiple' => 'checkbox',
  8724. 'type' => 'select',
  8725. 'options' => [
  8726. ['text' => 'First Checkbox', 'value' => 1],
  8727. ['text' => 'Second Checkbox', 'value' => 2],
  8728. ],
  8729. 'labelOptions' => false,
  8730. ]);
  8731. $expected = [
  8732. ['div' => ['class' => 'input select']],
  8733. ['label' => ['for' => 'checkbox1']],
  8734. 'My checkboxes',
  8735. '/label',
  8736. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8737. ['div' => ['class' => 'checkbox']],
  8738. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8739. '/div',
  8740. ['div' => ['class' => 'checkbox']],
  8741. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8742. '/div',
  8743. '/div',
  8744. ];
  8745. $this->assertHtml($expected, $result);
  8746. }
  8747. /**
  8748. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8749. *
  8750. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8751. * added to the class if checked.
  8752. *
  8753. * Also checks to make sure any custom attributes are rendered correctly
  8754. *
  8755. * @return void
  8756. */
  8757. public function testControlLabelManipulationRadios()
  8758. {
  8759. $result = $this->Form->control('test', [
  8760. 'type' => 'radio',
  8761. 'options' => ['A', 'B'],
  8762. 'labelOptions' => ['class' => 'custom-class'],
  8763. ]);
  8764. $expected = [
  8765. ['div' => ['class' => 'input radio']],
  8766. '<label',
  8767. 'Test',
  8768. '/label',
  8769. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8770. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8771. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8772. 'A',
  8773. '/label',
  8774. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8775. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8776. 'B',
  8777. '/label',
  8778. '/div',
  8779. ];
  8780. $this->assertHtml($expected, $result);
  8781. $result = $this->Form->control('test', [
  8782. 'type' => 'radio',
  8783. 'options' => ['A', 'B'],
  8784. 'value' => 1,
  8785. 'labelOptions' => ['class' => 'custom-class'],
  8786. ]);
  8787. $expected = [
  8788. ['div' => ['class' => 'input radio']],
  8789. '<label',
  8790. 'Test',
  8791. '/label',
  8792. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8793. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8794. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8795. 'A',
  8796. '/label',
  8797. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8798. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8799. 'B',
  8800. '/label',
  8801. '/div',
  8802. ];
  8803. $this->assertHtml($expected, $result);
  8804. $result = $this->Form->control('test', [
  8805. 'type' => 'radio',
  8806. 'options' => ['A', 'B'],
  8807. 'value' => 1,
  8808. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']],
  8809. ]);
  8810. $expected = [
  8811. ['div' => ['class' => 'input radio']],
  8812. '<label',
  8813. 'Test',
  8814. '/label',
  8815. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8816. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8817. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8818. 'A',
  8819. '/label',
  8820. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8821. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8822. 'B',
  8823. '/label',
  8824. '/div',
  8825. ];
  8826. $this->assertHtml($expected, $result);
  8827. $result = $this->Form->radio('test', ['A', 'B'], [
  8828. 'label' => [
  8829. 'class' => ['custom-class', 'another-class'],
  8830. 'data-name' => 'bob',
  8831. ],
  8832. 'value' => 1,
  8833. ]);
  8834. $expected = [
  8835. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8836. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8837. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8838. 'A',
  8839. '/label',
  8840. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8841. ['input' => [
  8842. 'type' => 'radio',
  8843. 'name' => 'test',
  8844. 'value' => '1',
  8845. 'id' => 'test-1',
  8846. 'checked' => 'checked',
  8847. ]],
  8848. 'B',
  8849. '/label',
  8850. ];
  8851. $this->assertHtml($expected, $result);
  8852. }
  8853. /**
  8854. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8855. *
  8856. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8857. * added to the class if checked.
  8858. *
  8859. * Also checks to make sure any custom attributes are rendered correctly
  8860. *
  8861. * @return void
  8862. */
  8863. public function testControlLabelManipulationCheckboxes()
  8864. {
  8865. $result = $this->Form->control('checkbox1', [
  8866. 'label' => 'My checkboxes',
  8867. 'multiple' => 'checkbox',
  8868. 'type' => 'select',
  8869. 'options' => [
  8870. ['text' => 'First Checkbox', 'value' => 1],
  8871. ['text' => 'Second Checkbox', 'value' => 2],
  8872. ],
  8873. 'labelOptions' => ['class' => 'custom-class'],
  8874. 'value' => ['1'],
  8875. ]);
  8876. $expected = [
  8877. ['div' => ['class' => 'input select']],
  8878. ['label' => ['for' => 'checkbox1']],
  8879. 'My checkboxes',
  8880. '/label',
  8881. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8882. ['div' => ['class' => 'checkbox']],
  8883. ['label' => [
  8884. 'class' => 'custom-class selected',
  8885. 'for' => 'checkbox1-1',
  8886. ]],
  8887. ['input' => [
  8888. 'type' => 'checkbox',
  8889. 'name' => 'checkbox1[]',
  8890. 'value' => '1',
  8891. 'id' => 'checkbox1-1',
  8892. 'checked' => 'checked',
  8893. ]],
  8894. 'First Checkbox',
  8895. '/label',
  8896. '/div',
  8897. ['div' => ['class' => 'checkbox']],
  8898. ['label' => [
  8899. 'class' => 'custom-class',
  8900. 'for' => 'checkbox1-2',
  8901. ]],
  8902. ['input' => [
  8903. 'type' => 'checkbox',
  8904. 'name' => 'checkbox1[]',
  8905. 'value' => '2',
  8906. 'id' => 'checkbox1-2',
  8907. ]],
  8908. 'Second Checkbox',
  8909. '/label',
  8910. '/div',
  8911. '/div',
  8912. ];
  8913. $this->assertHtml($expected, $result);
  8914. $result = $this->Form->control('checkbox1', [
  8915. 'label' => 'My checkboxes',
  8916. 'multiple' => 'checkbox',
  8917. 'type' => 'select',
  8918. 'options' => [
  8919. ['text' => 'First Checkbox', 'value' => 1],
  8920. ['text' => 'Second Checkbox', 'value' => 2],
  8921. ],
  8922. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8923. 'value' => ['1'],
  8924. ]);
  8925. $expected = [
  8926. ['div' => ['class' => 'input select']],
  8927. ['label' => ['for' => 'checkbox1']],
  8928. 'My checkboxes',
  8929. '/label',
  8930. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8931. ['div' => ['class' => 'checkbox']],
  8932. ['label' => [
  8933. 'class' => 'custom-class another-class selected',
  8934. 'data-name' => 'bob',
  8935. 'for' => 'checkbox1-1',
  8936. ]],
  8937. ['input' => [
  8938. 'type' => 'checkbox',
  8939. 'name' => 'checkbox1[]',
  8940. 'value' => '1',
  8941. 'id' => 'checkbox1-1',
  8942. 'checked' => 'checked',
  8943. ]],
  8944. 'First Checkbox',
  8945. '/label',
  8946. '/div',
  8947. ['div' => ['class' => 'checkbox']],
  8948. ['label' => [
  8949. 'class' => 'custom-class another-class',
  8950. 'data-name' => 'bob',
  8951. 'for' => 'checkbox1-2',
  8952. ]],
  8953. ['input' => [
  8954. 'type' => 'checkbox',
  8955. 'name' => 'checkbox1[]',
  8956. 'value' => '2',
  8957. 'id' => 'checkbox1-2',
  8958. ]],
  8959. 'Second Checkbox',
  8960. '/label',
  8961. '/div',
  8962. '/div',
  8963. ];
  8964. $this->assertHtml($expected, $result);
  8965. }
  8966. /**
  8967. * testControlMaxLengthArrayContext method
  8968. *
  8969. * Test control() with maxlength attribute in Array Context.
  8970. *
  8971. * @return void
  8972. */
  8973. public function testControlMaxLengthArrayContext()
  8974. {
  8975. $this->article['schema'] = [
  8976. 'title' => ['length' => 10],
  8977. ];
  8978. $this->Form->create($this->article);
  8979. $result = $this->Form->control('title');
  8980. $expected = [
  8981. 'div' => ['class'],
  8982. 'label' => ['for'],
  8983. 'Title',
  8984. '/label',
  8985. 'input' => [
  8986. 'id',
  8987. 'name' => 'title',
  8988. 'type' => 'text',
  8989. 'required' => 'required',
  8990. 'maxlength' => 10,
  8991. ],
  8992. '/div',
  8993. ];
  8994. $this->assertHtml($expected, $result);
  8995. }
  8996. /**
  8997. * testControlMaxLengthEntityContext method
  8998. *
  8999. * Test control() with maxlength attribute in Entity Context.
  9000. *
  9001. * @return void
  9002. */
  9003. public function testControlMaxLengthEntityContext()
  9004. {
  9005. $this->article['schema']['title']['length'] = 45;
  9006. $validator = new Validator();
  9007. $validator->maxLength('title', 10);
  9008. $article = new EntityContext(
  9009. new ServerRequest(),
  9010. [
  9011. 'entity' => new Entity($this->article),
  9012. 'table' => new Table([
  9013. 'schema' => $this->article['schema'],
  9014. 'validator' => $validator,
  9015. ]),
  9016. ]
  9017. );
  9018. $this->Form->create($article);
  9019. $result = $this->Form->control('title');
  9020. $expected = [
  9021. 'div' => ['class'],
  9022. 'label' => ['for'],
  9023. 'Title',
  9024. '/label',
  9025. 'input' => [
  9026. 'id',
  9027. 'name' => 'title',
  9028. 'type' => 'text',
  9029. 'required' => 'required',
  9030. 'maxlength' => 10,
  9031. ],
  9032. '/div',
  9033. ];
  9034. $this->assertHtml($expected, $result);
  9035. $this->article['schema']['title']['length'] = 45;
  9036. $validator = new Validator();
  9037. $validator->maxLength('title', 55);
  9038. $article = new EntityContext(
  9039. new ServerRequest(),
  9040. [
  9041. 'entity' => new Entity($this->article),
  9042. 'table' => new Table([
  9043. 'schema' => $this->article['schema'],
  9044. 'validator' => $validator,
  9045. ]),
  9046. ]
  9047. );
  9048. $this->Form->create($article);
  9049. $result = $this->Form->control('title');
  9050. $expected = [
  9051. 'div' => ['class'],
  9052. 'label' => ['for'],
  9053. 'Title',
  9054. '/label',
  9055. 'input' => [
  9056. 'id',
  9057. 'name' => 'title',
  9058. 'type' => 'text',
  9059. 'required' => 'required',
  9060. 'maxlength' => 55, // Length set in validator should take precedence over schema.
  9061. ],
  9062. '/div',
  9063. ];
  9064. $this->assertHtml($expected, $result);
  9065. $this->article['schema']['title']['length'] = 45;
  9066. $validator = new Validator();
  9067. $validator->maxLength('title', 55);
  9068. $article = new EntityContext(
  9069. new ServerRequest(),
  9070. [
  9071. 'entity' => new Entity($this->article),
  9072. 'table' => new Table([
  9073. 'schema' => $this->article['schema'],
  9074. 'validator' => $validator,
  9075. ]),
  9076. ]
  9077. );
  9078. $this->Form->create($article);
  9079. $result = $this->Form->control('title', ['maxlength' => 10]);
  9080. $expected = [
  9081. 'div' => ['class'],
  9082. 'label' => ['for'],
  9083. 'Title',
  9084. '/label',
  9085. 'input' => [
  9086. 'id',
  9087. 'name' => 'title',
  9088. 'type' => 'text',
  9089. 'required' => 'required',
  9090. 'maxlength' => 10, // Length set in options should take highest precedence.
  9091. ],
  9092. '/div',
  9093. ];
  9094. $this->assertHtml($expected, $result);
  9095. }
  9096. /**
  9097. * testControlMinMaxLengthEntityContext method
  9098. *
  9099. * Test control() with maxlength attribute in Entity Context sets the minimum val.
  9100. *
  9101. * @return void
  9102. */
  9103. public function testControlMinMaxLengthEntityContext()
  9104. {
  9105. $validator = new Validator();
  9106. $validator->maxLength('title', 10);
  9107. $article = new EntityContext(
  9108. new ServerRequest(),
  9109. [
  9110. 'entity' => new Entity($this->article),
  9111. 'table' => new Table([
  9112. 'schema' => $this->article['schema'],
  9113. 'validator' => $validator,
  9114. ]),
  9115. ]
  9116. );
  9117. $this->Form->create($article);
  9118. $result = $this->Form->control('title');
  9119. $expected = [
  9120. 'div' => ['class'],
  9121. 'label' => ['for'],
  9122. 'Title',
  9123. '/label',
  9124. 'input' => [
  9125. 'id',
  9126. 'name' => 'title',
  9127. 'type' => 'text',
  9128. 'required' => 'required',
  9129. 'maxlength' => 10,
  9130. ],
  9131. '/div',
  9132. ];
  9133. $this->assertHtml($expected, $result);
  9134. }
  9135. /**
  9136. * testControlMaxLengthFormContext method
  9137. *
  9138. * Test control() with maxlength attribute in Form Context.
  9139. *
  9140. * @return void
  9141. */
  9142. public function testControlMaxLengthFormContext()
  9143. {
  9144. $validator = new Validator();
  9145. $validator->maxLength('title', 10);
  9146. $form = new Form();
  9147. $form->setValidator('default', $validator);
  9148. $this->Form->create($form);
  9149. $result = $this->Form->control('title');
  9150. $expected = [
  9151. 'div' => ['class'],
  9152. 'label' => ['for'],
  9153. 'Title',
  9154. '/label',
  9155. 'input' => [
  9156. 'id',
  9157. 'name' => 'title',
  9158. 'type' => 'text',
  9159. 'required' => 'required',
  9160. 'maxlength' => 10,
  9161. ],
  9162. '/div',
  9163. ];
  9164. $this->assertHtml($expected, $result);
  9165. }
  9166. }