FormHelperTest.php 317 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 1.2.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\View\Helper;
  17. use Cake\Collection\Collection;
  18. use Cake\Core\Configure;
  19. use Cake\Form\Form;
  20. use Cake\Http\ServerRequest;
  21. use Cake\ORM\Entity;
  22. use Cake\ORM\Table;
  23. use Cake\Routing\Router;
  24. use Cake\TestSuite\TestCase;
  25. use Cake\Utility\Security;
  26. use Cake\View\Helper\FormHelper;
  27. use Cake\View\View;
  28. use Cake\View\Widget\WidgetLocator;
  29. /**
  30. * Test stub.
  31. */
  32. class Article extends Entity
  33. {
  34. }
  35. /**
  36. * Contact class
  37. */
  38. class ContactsTable extends Table
  39. {
  40. /**
  41. * Default schema
  42. *
  43. * @var array
  44. */
  45. protected $_schema = [
  46. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  47. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  48. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  49. 'phone' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  50. 'password' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  51. 'published' => ['type' => 'date', 'null' => true, 'default' => null, 'length' => null],
  52. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  53. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  54. 'age' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => null],
  55. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  56. ];
  57. /**
  58. * Initializes the schema
  59. *
  60. * @return void
  61. */
  62. public function initialize(array $config): void
  63. {
  64. $this->setSchema($this->_schema);
  65. }
  66. }
  67. /**
  68. * ValidateUser class
  69. */
  70. class ValidateUsersTable extends Table
  71. {
  72. /**
  73. * schema method
  74. *
  75. * @var array
  76. */
  77. protected $_schema = [
  78. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  79. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  80. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  81. 'balance' => ['type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2],
  82. 'cost_decimal' => ['type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3],
  83. 'null_decimal' => ['type' => 'decimal', 'null' => false, 'length' => null, 'precision' => null],
  84. 'ratio' => ['type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6],
  85. 'population' => ['type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0],
  86. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  87. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  88. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  89. ];
  90. /**
  91. * Initializes the schema
  92. *
  93. * @return void
  94. */
  95. public function initialize(array $config): void
  96. {
  97. $this->setSchema($this->_schema);
  98. }
  99. }
  100. /**
  101. * FormHelperTest class
  102. *
  103. * @property \Cake\View\Helper\FormHelper $Form
  104. * @property \Cake\View\View $View
  105. */
  106. class FormHelperTest extends TestCase
  107. {
  108. /**
  109. * Fixtures to be used
  110. *
  111. * @var array
  112. */
  113. public $fixtures = ['core.articles', 'core.comments'];
  114. /**
  115. * Do not load the fixtures by default
  116. *
  117. * @var bool
  118. */
  119. public $autoFixtures = false;
  120. /**
  121. * @var array
  122. */
  123. protected $article = [];
  124. /**
  125. * setUp method
  126. *
  127. * @return void
  128. */
  129. public function setUp()
  130. {
  131. parent::setUp();
  132. Configure::write('Config.language', 'eng');
  133. Configure::write('App.base', '');
  134. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  135. $request = new ServerRequest([
  136. 'webroot' => '',
  137. 'base' => '',
  138. 'url' => '/articles/add',
  139. 'params' => [
  140. 'controller' => 'articles',
  141. 'action' => 'add',
  142. ],
  143. ]);
  144. $this->View = new View($request);
  145. $this->Form = new FormHelper($this->View);
  146. $this->dateRegex = [
  147. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  148. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  149. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  150. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  151. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  152. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  153. ];
  154. $this->article = [
  155. 'schema' => [
  156. 'id' => ['type' => 'integer'],
  157. 'author_id' => ['type' => 'integer', 'null' => true],
  158. 'title' => ['type' => 'string', 'null' => true],
  159. 'body' => 'text',
  160. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  161. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  162. ],
  163. 'required' => [
  164. 'author_id' => true,
  165. 'title' => true,
  166. ],
  167. ];
  168. Security::setSalt('foo!');
  169. Router::connect('/:controller', ['action' => 'index']);
  170. Router::connect('/:controller/:action/*');
  171. }
  172. /**
  173. * tearDown method
  174. *
  175. * @return void
  176. */
  177. public function tearDown()
  178. {
  179. parent::tearDown();
  180. unset($this->Form, $this->Controller, $this->View);
  181. $this->getTableLocator()->clear();
  182. }
  183. /**
  184. * Test construct() with the templates option.
  185. *
  186. * @return void
  187. */
  188. public function testConstructTemplatesFile()
  189. {
  190. $helper = new FormHelper($this->View, [
  191. 'templates' => 'htmlhelper_tags',
  192. ]);
  193. $result = $helper->control('name');
  194. $this->assertContains('<input', $result);
  195. }
  196. /**
  197. * Test that when specifying custom widgets the config array for that widget
  198. * is overwritten instead of merged.
  199. *
  200. * @return void
  201. */
  202. public function testConstructWithWidgets()
  203. {
  204. $config = [
  205. 'widgets' => [
  206. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select'],
  207. ],
  208. ];
  209. $helper = new FormHelper($this->View, $config);
  210. $locator = $helper->getWidgetLocator();
  211. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  212. }
  213. /**
  214. * Test that when specifying custom widgets config file and it should be
  215. * added to widgets array. WidgetLocator will load widgets in constructor.
  216. *
  217. * @return void
  218. */
  219. public function testConstructWithWidgetsConfig()
  220. {
  221. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  222. $locator = $helper->getWidgetLocator();
  223. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  224. }
  225. /**
  226. * Test setting the widget locator
  227. *
  228. * @return void
  229. */
  230. public function testSetAndGetWidgetLocator()
  231. {
  232. $helper = new FormHelper($this->View);
  233. $locator = new WidgetLocator($helper->templater(), $this->View);
  234. $helper->setWidgetLocator($locator);
  235. $this->assertSame($locator, $helper->getWidgetLocator());
  236. }
  237. /**
  238. * Test overridding grouped input types which controls generation of "for"
  239. * attribute of labels.
  240. *
  241. * @return void
  242. */
  243. public function testConstructWithGroupedInputTypes()
  244. {
  245. $helper = new FormHelper($this->View, [
  246. 'groupedInputTypes' => ['radio'],
  247. ]);
  248. $result = $helper->control('when', ['type' => 'datetime']);
  249. $this->assertContains('<label for="when">When</label>', $result);
  250. }
  251. /**
  252. * Test registering a new widget class and rendering it.
  253. *
  254. * @return void
  255. */
  256. public function testAddWidgetAndRenderWidget()
  257. {
  258. $data = [
  259. 'val' => 1,
  260. ];
  261. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  262. $this->Form->addWidget('test', $mock);
  263. $mock->expects($this->once())
  264. ->method('render')
  265. ->with($data)
  266. ->will($this->returnValue('HTML'));
  267. $result = $this->Form->widget('test', $data);
  268. $this->assertEquals('HTML', $result);
  269. }
  270. /**
  271. * Test that secureFields() of widget is called after calling render(),
  272. * not before.
  273. *
  274. * @return void
  275. */
  276. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  277. {
  278. $data = [
  279. 'val' => 1,
  280. 'name' => 'test',
  281. ];
  282. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  283. $this->Form->addWidget('test', $mock);
  284. $mock->expects($this->at(0))
  285. ->method('render')
  286. ->with($data)
  287. ->will($this->returnValue('HTML'));
  288. $mock->expects($this->at(1))
  289. ->method('secureFields')
  290. ->with($data)
  291. ->will($this->returnValue(['test']));
  292. $result = $this->Form->widget('test', $data + ['secure' => true]);
  293. $this->assertEquals('HTML', $result);
  294. }
  295. /**
  296. * Test that empty string is not added to secure fields list when
  297. * rendering input widget without name.
  298. *
  299. * @return void
  300. */
  301. public function testRenderingWidgetWithEmptyName()
  302. {
  303. $this->assertEquals([], $this->Form->fields);
  304. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  305. $this->assertEquals('<select name=""></select>', $result);
  306. $this->assertEquals([], $this->Form->fields);
  307. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  308. $this->assertEquals('<select name="0"></select>', $result);
  309. $this->assertEquals(['0'], $this->Form->fields);
  310. }
  311. /**
  312. * Test registering an invalid widget class.
  313. *
  314. * @return void
  315. */
  316. public function testAddWidgetInvalid()
  317. {
  318. $this->expectException(\RuntimeException::class);
  319. $mock = new \StdClass();
  320. $this->Form->addWidget('test', $mock);
  321. $this->Form->widget('test');
  322. }
  323. /**
  324. * Test adding a new context class.
  325. *
  326. * @return void
  327. */
  328. public function testAddContextProvider()
  329. {
  330. $context = 'My data';
  331. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  332. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  333. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  334. $this->assertEquals($context, $data['entity']);
  335. return $stub;
  336. });
  337. $this->Form->create($context);
  338. $result = $this->Form->context();
  339. $this->assertSame($stub, $result);
  340. }
  341. /**
  342. * Test replacing a context class.
  343. *
  344. * @return void
  345. */
  346. public function testAddContextProviderReplace()
  347. {
  348. $entity = new Article();
  349. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  350. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  351. return $stub;
  352. });
  353. $this->Form->create($entity);
  354. $result = $this->Form->context();
  355. $this->assertSame($stub, $result);
  356. }
  357. /**
  358. * Test overriding a context class.
  359. *
  360. * @return void
  361. */
  362. public function testAddContextProviderAdd()
  363. {
  364. $entity = new Article();
  365. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  366. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  367. if ($data['entity'] instanceof Entity) {
  368. return $stub;
  369. }
  370. });
  371. $this->Form->create($entity);
  372. $result = $this->Form->context();
  373. $this->assertSame($stub, $result);
  374. }
  375. /**
  376. * Test adding an invalid context class.
  377. *
  378. * @return void
  379. */
  380. public function testAddContextProviderInvalid()
  381. {
  382. $this->expectException(\RuntimeException::class);
  383. $this->expectExceptionMessage('Context providers must return object implementing Cake\View\Form\ContextInterface. Got "stdClass" instead.');
  384. $context = 'My data';
  385. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  386. return new \StdClass();
  387. });
  388. $this->Form->create($context);
  389. }
  390. /**
  391. * Provides context options for create().
  392. *
  393. * @return array
  394. */
  395. public function contextSelectionProvider()
  396. {
  397. $entity = new Article();
  398. $collection = new Collection([$entity]);
  399. $emptyCollection = new Collection([]);
  400. $arrayObject = new \ArrayObject([]);
  401. $data = [
  402. 'schema' => [
  403. 'title' => ['type' => 'string'],
  404. ],
  405. ];
  406. $form = new Form();
  407. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  408. return [
  409. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  410. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  411. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  412. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  413. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  414. 'form' => [$form, 'Cake\View\Form\FormContext'],
  415. 'none' => [null, 'Cake\View\Form\NullContext'],
  416. 'false' => [false, 'Cake\View\Form\NullContext'],
  417. 'custom' => [$custom, get_class($custom)],
  418. ];
  419. }
  420. /**
  421. * Test default context selection in create()
  422. *
  423. * @dataProvider contextSelectionProvider
  424. * @return void
  425. */
  426. public function testCreateContextSelectionBuiltIn($data, $class)
  427. {
  428. $this->loadFixtures('Articles');
  429. $this->Form->create($data);
  430. $this->assertInstanceOf($class, $this->Form->context());
  431. }
  432. /**
  433. * Data provider for type option.
  434. *
  435. * @return array
  436. */
  437. public static function requestTypeProvider()
  438. {
  439. return [
  440. // type, method, override
  441. ['post', 'post', 'POST'],
  442. ['put', 'post', 'PUT'],
  443. ['patch', 'post', 'PATCH'],
  444. ['delete', 'post', 'DELETE'],
  445. ];
  446. }
  447. /**
  448. * Test creating file forms.
  449. *
  450. * @return void
  451. */
  452. public function testCreateFile()
  453. {
  454. $encoding = strtolower(Configure::read('App.encoding'));
  455. $result = $this->Form->create(false, ['type' => 'file']);
  456. $expected = [
  457. 'form' => [
  458. 'method' => 'post', 'action' => '/articles/add',
  459. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data',
  460. ],
  461. 'div' => ['style' => 'display:none;'],
  462. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  463. '/div',
  464. ];
  465. $this->assertHtml($expected, $result);
  466. }
  467. /**
  468. * Test creating GET forms.
  469. *
  470. * @return void
  471. */
  472. public function testCreateGet()
  473. {
  474. $encoding = strtolower(Configure::read('App.encoding'));
  475. $result = $this->Form->create(false, ['type' => 'get']);
  476. $expected = ['form' => [
  477. 'method' => 'get', 'action' => '/articles/add',
  478. 'accept-charset' => $encoding,
  479. ]];
  480. $this->assertHtml($expected, $result);
  481. }
  482. /**
  483. * Test explicit method/enctype options.
  484. *
  485. * Explicit method overwrites inferred method from 'type'
  486. *
  487. * @return void
  488. */
  489. public function testCreateExplicitMethodEnctype()
  490. {
  491. $encoding = strtolower(Configure::read('App.encoding'));
  492. $result = $this->Form->create(false, [
  493. 'type' => 'get',
  494. 'method' => 'put',
  495. 'enctype' => 'multipart/form-data',
  496. ]);
  497. $expected = ['form' => [
  498. 'method' => 'put',
  499. 'action' => '/articles/add',
  500. 'enctype' => 'multipart/form-data',
  501. 'accept-charset' => $encoding,
  502. ]];
  503. $this->assertHtml($expected, $result);
  504. }
  505. /**
  506. * Test create() with the templates option.
  507. *
  508. * @return void
  509. */
  510. public function testCreateTemplatesArray()
  511. {
  512. $result = $this->Form->create($this->article, [
  513. 'templates' => [
  514. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  515. ],
  516. ]);
  517. $expected = [
  518. 'form' => [
  519. 'class' => 'form-horizontal',
  520. 'method' => 'post',
  521. 'action' => '/articles/add',
  522. 'accept-charset' => 'utf-8',
  523. ],
  524. ];
  525. $this->assertHtml($expected, $result);
  526. }
  527. /**
  528. * Test create() with the templates option.
  529. *
  530. * @return void
  531. */
  532. public function testCreateTemplatesFile()
  533. {
  534. $result = $this->Form->create($this->article, [
  535. 'templates' => 'htmlhelper_tags',
  536. ]);
  537. $expected = [
  538. 'start form',
  539. 'div' => ['class' => 'hidden'],
  540. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  541. '/div',
  542. ];
  543. $this->assertHtml($expected, $result);
  544. }
  545. /**
  546. * Test that create() and end() restore templates.
  547. *
  548. * @return void
  549. */
  550. public function testCreateEndRestoreTemplates()
  551. {
  552. $this->Form->create($this->article, [
  553. 'templates' => ['input' => 'custom input element'],
  554. ]);
  555. $this->Form->end();
  556. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  557. }
  558. /**
  559. * Test using template vars in various templates used by control() method.
  560. *
  561. * @return void
  562. */
  563. public function testControlTemplateVars()
  564. {
  565. $result = $this->Form->control('text', [
  566. 'templates' => [
  567. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  568. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  569. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  570. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  571. ],
  572. 'templateVars' => [
  573. 'forinput' => 'in-input',
  574. 'forlabel' => 'in-label',
  575. 'forgroup' => 'in-group',
  576. 'forcontainer' => 'in-container',
  577. ],
  578. ]);
  579. $expected = [
  580. 'div' => ['class'],
  581. 'label' => ['for'],
  582. 'Text in-label',
  583. '/label',
  584. 'in-group',
  585. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  586. 'in-container',
  587. '/div',
  588. ];
  589. $this->assertHtml($expected, $result);
  590. }
  591. /**
  592. * Test ensuring template variables work in template files loaded
  593. * during control().
  594. *
  595. * @return void
  596. */
  597. public function testControlTemplatesFromFile()
  598. {
  599. $result = $this->Form->control('title', [
  600. 'templates' => 'test_templates',
  601. 'templateVars' => [
  602. 'forcontainer' => 'container-data',
  603. ],
  604. ]);
  605. $expected = [
  606. 'div' => ['class'],
  607. 'label' => ['for'],
  608. 'Title',
  609. '/label',
  610. 'input' => ['name', 'type' => 'text', 'id'],
  611. 'container-data',
  612. '/div',
  613. ];
  614. $this->assertHtml($expected, $result);
  615. }
  616. /**
  617. * Test using template vars in inputSubmit and submitContainer template.
  618. *
  619. * @return void
  620. */
  621. public function testSubmitTemplateVars()
  622. {
  623. $this->Form->setTemplates([
  624. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  625. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>',
  626. ]);
  627. $result = $this->Form->submit('Submit', [
  628. 'templateVars' => [
  629. 'forinput' => 'in-input',
  630. 'forcontainer' => 'in-container',
  631. ],
  632. ]);
  633. $expected = [
  634. 'div' => ['class'],
  635. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  636. 'in-container',
  637. '/div',
  638. ];
  639. $this->assertHtml($expected, $result);
  640. }
  641. /**
  642. * test the create() method
  643. *
  644. * @dataProvider requestTypeProvider
  645. * @return void
  646. */
  647. public function testCreateTypeOptions($type, $method, $override)
  648. {
  649. $encoding = strtolower(Configure::read('App.encoding'));
  650. $result = $this->Form->create(false, ['type' => $type]);
  651. $expected = [
  652. 'form' => [
  653. 'method' => $method, 'action' => '/articles/add',
  654. 'accept-charset' => $encoding,
  655. ],
  656. 'div' => ['style' => 'display:none;'],
  657. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  658. '/div',
  659. ];
  660. $this->assertHtml($expected, $result);
  661. }
  662. /**
  663. * Test using template vars in Create (formStart template)
  664. *
  665. * @return void
  666. */
  667. public function testCreateTemplateVars()
  668. {
  669. $result = $this->Form->create($this->article, [
  670. 'templates' => [
  671. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  672. ],
  673. 'templateVars' => ['header' => 'headertext'],
  674. ]);
  675. $expected = [
  676. 'h4' => ['class'],
  677. 'headertext',
  678. '/h4',
  679. 'form' => [
  680. 'method' => 'post',
  681. 'action' => '/articles/add',
  682. 'accept-charset' => 'utf-8',
  683. ],
  684. ];
  685. $this->assertHtml($expected, $result);
  686. }
  687. /**
  688. * Test opening a form for an update operation.
  689. *
  690. * @return void
  691. */
  692. public function testCreateUpdateForm()
  693. {
  694. $encoding = strtolower(Configure::read('App.encoding'));
  695. $this->View->setRequest($this->View->getRequest()
  696. ->withRequestTarget('/articles/edit/1')
  697. ->withParam('action', 'edit'));
  698. $this->article['defaults']['id'] = 1;
  699. $result = $this->Form->create($this->article);
  700. $expected = [
  701. 'form' => [
  702. 'method' => 'post',
  703. 'action' => '/articles/edit/1',
  704. 'accept-charset' => $encoding,
  705. ],
  706. 'div' => ['style' => 'display:none;'],
  707. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  708. '/div',
  709. ];
  710. $this->assertHtml($expected, $result);
  711. }
  712. /**
  713. * test create() with automatic url generation
  714. *
  715. * @return void
  716. */
  717. public function testCreateAutoUrl()
  718. {
  719. $encoding = strtolower(Configure::read('App.encoding'));
  720. $this->View->setRequest($this->View->getRequest()
  721. ->withRequestTarget('/articles/delete/10')
  722. ->withParam('action', 'delete'));
  723. $result = $this->Form->create($this->article);
  724. $expected = [
  725. 'form' => [
  726. 'method' => 'post', 'action' => '/articles/delete/10',
  727. 'accept-charset' => $encoding,
  728. ],
  729. 'div' => ['style' => 'display:none;'],
  730. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  731. '/div',
  732. ];
  733. $this->assertHtml($expected, $result);
  734. $this->article['defaults'] = ['id' => 1];
  735. $this->View->setRequest($this->View->getRequest()
  736. ->withRequestTarget('/articles/edit/1')
  737. ->withParam('action', 'delete'));
  738. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit', 1]]);
  739. $expected = [
  740. 'form' => [
  741. 'method' => 'post',
  742. 'action' => '/articles/edit/1',
  743. 'accept-charset' => $encoding,
  744. ],
  745. 'div' => ['style' => 'display:none;'],
  746. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  747. '/div',
  748. ];
  749. $this->assertHtml($expected, $result);
  750. $this->View->setRequest($this->View->getRequest()
  751. ->withParam('action', 'add'));
  752. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish', 1]]);
  753. $expected = [
  754. 'form' => [
  755. 'method' => 'post',
  756. 'action' => '/articles/publish/1',
  757. 'accept-charset' => $encoding,
  758. ],
  759. 'div' => ['style' => 'display:none;'],
  760. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  761. '/div',
  762. ];
  763. $this->assertHtml($expected, $result);
  764. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  765. $expected = [
  766. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  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('controller', 'Pages'));
  774. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup', 1]]);
  775. $expected = [
  776. 'form' => [
  777. 'method' => 'post', 'action' => '/Pages/signup/1',
  778. 'accept-charset' => $encoding,
  779. ],
  780. 'div' => ['style' => 'display:none;'],
  781. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  782. '/div',
  783. ];
  784. $this->assertHtml($expected, $result);
  785. }
  786. /**
  787. * Test create() with no URL (no "action" attribute for <form> tag)
  788. *
  789. * @return void
  790. */
  791. public function testCreateNoUrl()
  792. {
  793. $result = $this->Form->create(false, ['url' => false]);
  794. $expected = [
  795. 'form' => [
  796. 'method' => 'post',
  797. 'accept-charset' => strtolower(Configure::read('App.encoding')),
  798. ],
  799. 'div' => ['style' => 'display:none;'],
  800. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  801. '/div',
  802. ];
  803. $this->assertHtml($expected, $result);
  804. }
  805. /**
  806. * test create() with a custom route
  807. *
  808. * @return void
  809. */
  810. public function testCreateCustomRoute()
  811. {
  812. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  813. $encoding = strtolower(Configure::read('App.encoding'));
  814. $this->View->setRequest($this->View->getRequest()
  815. ->withParam('controller', 'users'));
  816. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  817. $expected = [
  818. 'form' => [
  819. 'method' => 'post', 'action' => '/login',
  820. 'accept-charset' => $encoding,
  821. ],
  822. 'div' => ['style' => 'display:none;'],
  823. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  824. '/div',
  825. ];
  826. $this->assertHtml($expected, $result);
  827. Router::connect(
  828. '/new-article',
  829. ['controller' => 'articles', 'action' => 'myaction'],
  830. ['_name' => 'my-route']
  831. );
  832. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  833. $expected = [
  834. 'form' => [
  835. 'method' => 'post', 'action' => '/new-article',
  836. 'accept-charset' => $encoding,
  837. ],
  838. 'div' => ['style' => 'display:none;'],
  839. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  840. '/div',
  841. ];
  842. $this->assertHtml($expected, $result);
  843. }
  844. /**
  845. * test automatic accept-charset overriding
  846. *
  847. * @return void
  848. */
  849. public function testCreateWithAcceptCharset()
  850. {
  851. $result = $this->Form->create(
  852. $this->article,
  853. [
  854. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1',
  855. ]
  856. );
  857. $expected = [
  858. 'form' => [
  859. 'method' => 'post', 'action' => '/articles',
  860. 'accept-charset' => 'iso-8859-1',
  861. ],
  862. 'div' => ['style' => 'display:none;'],
  863. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  864. '/div',
  865. ];
  866. $this->assertHtml($expected, $result);
  867. }
  868. /**
  869. * Test base form URL when url param is passed with multiple parameters (&)
  870. *
  871. */
  872. public function testCreateQueryStringRequest()
  873. {
  874. $encoding = strtolower(Configure::read('App.encoding'));
  875. $result = $this->Form->create($this->article, [
  876. 'type' => 'post',
  877. 'escape' => false,
  878. 'url' => [
  879. 'controller' => 'controller',
  880. 'action' => 'action',
  881. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  882. ],
  883. ]);
  884. $expected = [
  885. 'form' => [
  886. 'method' => 'post',
  887. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  888. 'accept-charset' => $encoding,
  889. ],
  890. 'div' => ['style' => 'display:none;'],
  891. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  892. '/div',
  893. ];
  894. $this->assertHtml($expected, $result);
  895. $result = $this->Form->create($this->article, [
  896. 'type' => 'post',
  897. 'url' => [
  898. 'controller' => 'controller',
  899. 'action' => 'action',
  900. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  901. ],
  902. ]);
  903. $this->assertHtml($expected, $result);
  904. }
  905. /**
  906. * test that create() doesn't cause errors by multiple id's being in the primary key
  907. * as could happen with multiple select or checkboxes.
  908. *
  909. * @return void
  910. */
  911. public function testCreateWithMultipleIdInData()
  912. {
  913. $encoding = strtolower(Configure::read('App.encoding'));
  914. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  915. $result = $this->Form->create($this->article);
  916. $expected = [
  917. 'form' => [
  918. 'method' => 'post',
  919. 'action' => '/articles/add',
  920. 'accept-charset' => $encoding,
  921. ],
  922. 'div' => ['style' => 'display:none;'],
  923. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  924. '/div',
  925. ];
  926. $this->assertHtml($expected, $result);
  927. }
  928. /**
  929. * test that create() doesn't add in extra passed params.
  930. *
  931. * @return void
  932. */
  933. public function testCreatePassedArgs()
  934. {
  935. $encoding = strtolower(Configure::read('App.encoding'));
  936. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  937. $result = $this->Form->create($this->article, [
  938. 'type' => 'post',
  939. 'escape' => false,
  940. 'url' => [
  941. 'action' => 'edit',
  942. 'myparam',
  943. ],
  944. ]);
  945. $expected = [
  946. 'form' => [
  947. 'method' => 'post',
  948. 'action' => '/articles/edit/myparam',
  949. 'accept-charset' => $encoding,
  950. ],
  951. 'div' => ['style' => 'display:none;'],
  952. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  953. '/div',
  954. ];
  955. $this->assertHtml($expected, $result);
  956. }
  957. /**
  958. * test creating a get form, and get form inputs.
  959. *
  960. * @return void
  961. */
  962. public function testGetFormCreate()
  963. {
  964. $encoding = strtolower(Configure::read('App.encoding'));
  965. $result = $this->Form->create($this->article, ['type' => 'get']);
  966. $expected = ['form' => [
  967. 'method' => 'get', 'action' => '/articles/add',
  968. 'accept-charset' => $encoding,
  969. ]];
  970. $this->assertHtml($expected, $result);
  971. $result = $this->Form->text('title');
  972. $expected = ['input' => [
  973. 'name' => 'title', 'type' => 'text', 'required' => 'required',
  974. ]];
  975. $this->assertHtml($expected, $result);
  976. $result = $this->Form->password('password');
  977. $expected = ['input' => [
  978. 'name' => 'password', 'type' => 'password',
  979. ]];
  980. $this->assertHtml($expected, $result);
  981. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  982. $result = $this->Form->text('user_form');
  983. $expected = ['input' => [
  984. 'name' => 'user_form', 'type' => 'text',
  985. ]];
  986. $this->assertHtml($expected, $result);
  987. }
  988. /**
  989. * test get form, and inputs when the model param is false
  990. *
  991. * @return void
  992. */
  993. public function testGetFormWithFalseModel()
  994. {
  995. $encoding = strtolower(Configure::read('App.encoding'));
  996. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'contact_test'));
  997. $result = $this->Form->create(false, [
  998. 'type' => 'get', 'url' => ['controller' => 'contact_test'],
  999. ]);
  1000. $expected = ['form' => [
  1001. 'method' => 'get', 'action' => '/contact_test/add',
  1002. 'accept-charset' => $encoding,
  1003. ]];
  1004. $this->assertHtml($expected, $result);
  1005. $result = $this->Form->text('reason');
  1006. $expected = [
  1007. 'input' => ['type' => 'text', 'name' => 'reason'],
  1008. ];
  1009. $this->assertHtml($expected, $result);
  1010. }
  1011. /**
  1012. * testFormCreateWithSecurity method
  1013. *
  1014. * Test form->create() with security key.
  1015. *
  1016. * @return void
  1017. */
  1018. public function testCreateWithSecurity()
  1019. {
  1020. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  1021. $encoding = strtolower(Configure::read('App.encoding'));
  1022. $result = $this->Form->create($this->article, [
  1023. 'url' => '/articles/publish',
  1024. ]);
  1025. $expected = [
  1026. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  1027. 'div' => ['style' => 'display:none;'],
  1028. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1029. ['input' => [
  1030. 'type' => 'hidden',
  1031. 'name' => '_csrfToken',
  1032. 'value' => 'testKey',
  1033. 'autocomplete' => 'off',
  1034. ]],
  1035. '/div',
  1036. ];
  1037. $this->assertHtml($expected, $result);
  1038. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1039. $expected['form']['id'] = 'MyForm';
  1040. $this->assertHtml($expected, $result);
  1041. }
  1042. /**
  1043. * testFormCreateGetNoSecurity method
  1044. *
  1045. * Test form->create() with no security key as its a get form
  1046. *
  1047. * @return void
  1048. */
  1049. public function testCreateEndGetNoSecurity()
  1050. {
  1051. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  1052. $article = new Article();
  1053. $result = $this->Form->create($article, [
  1054. 'type' => 'get',
  1055. 'url' => '/contacts/add',
  1056. ]);
  1057. $this->assertNotContains('testKey', $result);
  1058. $result = $this->Form->end();
  1059. $this->assertNotContains('testKey', $result);
  1060. }
  1061. /**
  1062. * test that create() clears the fields property so it starts fresh
  1063. *
  1064. * @return void
  1065. */
  1066. public function testCreateClearingFields()
  1067. {
  1068. $this->Form->fields = ['model_id'];
  1069. $this->Form->create($this->article);
  1070. $this->assertEquals([], $this->Form->fields);
  1071. }
  1072. /**
  1073. * Tests form hash generation with model-less data
  1074. *
  1075. * @return void
  1076. */
  1077. public function testValidateHashNoModel()
  1078. {
  1079. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1080. $fields = ['anything'];
  1081. $result = $this->Form->secure($fields);
  1082. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1083. $this->assertContains($hash, $result);
  1084. }
  1085. /**
  1086. * Tests that hidden fields generated for checkboxes don't get locked
  1087. *
  1088. * @return void
  1089. */
  1090. public function testNoCheckboxLocking()
  1091. {
  1092. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1093. $this->assertSame([], $this->Form->fields);
  1094. $this->Form->checkbox('check', ['value' => '1']);
  1095. $this->assertSame($this->Form->fields, ['check']);
  1096. }
  1097. /**
  1098. * testFormSecurityFields method
  1099. *
  1100. * Test generation of secure form hash generation.
  1101. *
  1102. * @return void
  1103. */
  1104. public function testFormSecurityFields()
  1105. {
  1106. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1107. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1108. $result = $this->Form->secure($fields);
  1109. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1110. $hash .= ':' . 'Model.valid';
  1111. $hash = urlencode($hash);
  1112. $tokenDebug = urlencode(json_encode([
  1113. '',
  1114. $fields,
  1115. [],
  1116. ]));
  1117. $expected = [
  1118. 'div' => ['style' => 'display:none;'],
  1119. ['input' => [
  1120. 'type' => 'hidden',
  1121. 'name' => '_Token[fields]',
  1122. 'value' => $hash,
  1123. 'autocomplete' => 'off',
  1124. ]],
  1125. ['input' => [
  1126. 'type' => 'hidden',
  1127. 'name' => '_Token[unlocked]',
  1128. 'value' => '',
  1129. 'autocomplete' => 'off',
  1130. ]],
  1131. ['input' => [
  1132. 'type' => 'hidden',
  1133. 'name' => '_Token[debug]',
  1134. 'value' => $tokenDebug,
  1135. 'autocomplete' => 'off',
  1136. ]],
  1137. '/div',
  1138. ];
  1139. $this->assertHtml($expected, $result);
  1140. }
  1141. /**
  1142. * testFormSecurityFields method
  1143. *
  1144. * Test debug token is not generated if debug is false
  1145. *
  1146. * @return void
  1147. */
  1148. public function testFormSecurityFieldsNoDebugMode()
  1149. {
  1150. Configure::write('debug', false);
  1151. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1152. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1153. $result = $this->Form->secure($fields);
  1154. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1155. $hash .= ':' . 'Model.valid';
  1156. $hash = urlencode($hash);
  1157. $expected = [
  1158. 'div' => ['style' => 'display:none;'],
  1159. ['input' => [
  1160. 'type' => 'hidden',
  1161. 'name' => '_Token[fields]',
  1162. 'autocomplete' => 'off',
  1163. 'value' => $hash,
  1164. ]],
  1165. ['input' => [
  1166. 'type' => 'hidden',
  1167. 'name' => '_Token[unlocked]',
  1168. 'autocomplete' => 'off',
  1169. 'value' => '',
  1170. ]],
  1171. '/div',
  1172. ];
  1173. $this->assertHtml($expected, $result);
  1174. }
  1175. /**
  1176. * Tests correct generation of number fields for smallint
  1177. *
  1178. * @return void
  1179. */
  1180. public function testTextFieldGenerationForSmallint()
  1181. {
  1182. $this->article['schema'] = [
  1183. 'foo' => [
  1184. 'type' => 'smallinteger',
  1185. 'null' => false,
  1186. 'default' => null,
  1187. 'length' => 10,
  1188. ],
  1189. ];
  1190. $this->Form->create($this->article);
  1191. $result = $this->Form->control('foo');
  1192. $this->assertContains('class="input number"', $result);
  1193. $this->assertContains('type="number"', $result);
  1194. }
  1195. /**
  1196. * Tests correct generation of number fields for tinyint
  1197. *
  1198. * @return void
  1199. */
  1200. public function testTextFieldGenerationForTinyint()
  1201. {
  1202. $this->article['schema'] = [
  1203. 'foo' => [
  1204. 'type' => 'tinyinteger',
  1205. 'null' => false,
  1206. 'default' => null,
  1207. 'length' => 10,
  1208. ],
  1209. ];
  1210. $this->Form->create($this->article);
  1211. $result = $this->Form->control('foo');
  1212. $this->assertContains('class="input number"', $result);
  1213. $this->assertContains('type="number"', $result);
  1214. }
  1215. /**
  1216. * Tests correct generation of number fields for double and float fields
  1217. *
  1218. * @return void
  1219. */
  1220. public function testTextFieldGenerationForFloats()
  1221. {
  1222. $this->article['schema'] = [
  1223. 'foo' => [
  1224. 'type' => 'float',
  1225. 'null' => false,
  1226. 'default' => null,
  1227. 'length' => 10,
  1228. ],
  1229. ];
  1230. $this->Form->create($this->article);
  1231. $result = $this->Form->control('foo');
  1232. $expected = [
  1233. 'div' => ['class' => 'input number'],
  1234. 'label' => ['for' => 'foo'],
  1235. 'Foo',
  1236. '/label',
  1237. ['input' => [
  1238. 'type' => 'number',
  1239. 'name' => 'foo',
  1240. 'id' => 'foo',
  1241. 'step' => 'any',
  1242. ]],
  1243. '/div',
  1244. ];
  1245. $this->assertHtml($expected, $result);
  1246. $result = $this->Form->control('foo', ['step' => 0.5]);
  1247. $expected = [
  1248. 'div' => ['class' => 'input number'],
  1249. 'label' => ['for' => 'foo'],
  1250. 'Foo',
  1251. '/label',
  1252. ['input' => [
  1253. 'type' => 'number',
  1254. 'name' => 'foo',
  1255. 'id' => 'foo',
  1256. 'step' => '0.5',
  1257. ]],
  1258. '/div',
  1259. ];
  1260. $this->assertHtml($expected, $result);
  1261. }
  1262. /**
  1263. * Tests correct generation of number fields for integer fields
  1264. *
  1265. * @return void
  1266. */
  1267. public function testTextFieldTypeNumberGenerationForIntegers()
  1268. {
  1269. $this->getTableLocator()->get('Contacts', [
  1270. 'className' => __NAMESPACE__ . '\ContactsTable',
  1271. ]);
  1272. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1273. $result = $this->Form->control('age');
  1274. $expected = [
  1275. 'div' => ['class' => 'input number'],
  1276. 'label' => ['for' => 'age'],
  1277. 'Age',
  1278. '/label',
  1279. ['input' => [
  1280. 'type' => 'number', 'name' => 'age',
  1281. 'id' => 'age',
  1282. ]],
  1283. '/div',
  1284. ];
  1285. $this->assertHtml($expected, $result);
  1286. }
  1287. /**
  1288. * Tests correct generation of file upload fields for binary fields
  1289. *
  1290. * @return void
  1291. */
  1292. public function testFileUploadFieldTypeGenerationForBinaries()
  1293. {
  1294. $table = $this->getTableLocator()->get('Contacts', [
  1295. 'className' => __NAMESPACE__ . '\ContactsTable',
  1296. ]);
  1297. $table->setSchema(['foo' => [
  1298. 'type' => 'binary',
  1299. 'null' => false,
  1300. 'default' => null,
  1301. 'length' => 1024,
  1302. ]]);
  1303. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1304. $result = $this->Form->control('foo');
  1305. $expected = [
  1306. 'div' => ['class' => 'input file'],
  1307. 'label' => ['for' => 'foo'],
  1308. 'Foo',
  1309. '/label',
  1310. ['input' => [
  1311. 'type' => 'file', 'name' => 'foo',
  1312. 'id' => 'foo',
  1313. ]],
  1314. '/div',
  1315. ];
  1316. $this->assertHtml($expected, $result);
  1317. }
  1318. /**
  1319. * testFormSecurityMultipleFields method
  1320. *
  1321. * Test secure() with multiple row form. Ensure hash is correct.
  1322. *
  1323. * @return void
  1324. */
  1325. public function testFormSecurityMultipleFields()
  1326. {
  1327. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1328. $fields = [
  1329. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1330. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1331. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0',
  1332. ];
  1333. $result = $this->Form->secure($fields);
  1334. $hash = '8670192c3f040bf58680479060b4755b7a5c3596' .
  1335. '%3AModel.0.hidden%7CModel.0.valid%7CModel.1.hidden%7CModel.1.valid';
  1336. $tokenDebug = urlencode(json_encode([
  1337. '',
  1338. $fields,
  1339. [],
  1340. ]));
  1341. $expected = [
  1342. 'div' => ['style' => 'display:none;'],
  1343. ['input' => [
  1344. 'type' => 'hidden',
  1345. 'name' => '_Token[fields]',
  1346. 'value' => $hash,
  1347. 'autocomplete' => 'off',
  1348. ]],
  1349. ['input' => [
  1350. 'type' => 'hidden',
  1351. 'name' => '_Token[unlocked]',
  1352. 'autocomplete' => 'off',
  1353. 'value' => '',
  1354. ]],
  1355. ['input' => [
  1356. 'type' => 'hidden',
  1357. 'name' => '_Token[debug]',
  1358. 'value' => $tokenDebug,
  1359. 'autocomplete' => 'off',
  1360. ]],
  1361. '/div',
  1362. ];
  1363. $this->assertHtml($expected, $result);
  1364. }
  1365. /**
  1366. * testFormSecurityMultipleSubmitButtons
  1367. *
  1368. * test form submit generation and ensure that _Token is only created on end()
  1369. *
  1370. * @return void
  1371. */
  1372. public function testFormSecurityMultipleSubmitButtons()
  1373. {
  1374. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1375. $this->Form->create($this->article);
  1376. $this->Form->text('Address.title');
  1377. $this->Form->text('Address.first_name');
  1378. $result = $this->Form->submit('Save', ['name' => 'save']);
  1379. $expected = [
  1380. 'div' => ['class' => 'submit'],
  1381. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1382. '/div',
  1383. ];
  1384. $this->assertHtml($expected, $result);
  1385. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1386. $expected = [
  1387. 'div' => ['class' => 'submit'],
  1388. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1389. '/div',
  1390. ];
  1391. $this->assertHtml($expected, $result);
  1392. $result = $this->Form->end();
  1393. $tokenDebug = urlencode(json_encode([
  1394. '/articles/add',
  1395. [
  1396. 'Address.title',
  1397. 'Address.first_name',
  1398. ],
  1399. ['save', 'cancel'],
  1400. ]));
  1401. $expected = [
  1402. 'div' => ['style' => 'display:none;'],
  1403. ['input' => [
  1404. 'type' => 'hidden',
  1405. 'name' => '_Token[fields]',
  1406. 'autocomplete',
  1407. 'value',
  1408. ]],
  1409. ['input' => [
  1410. 'type' => 'hidden',
  1411. 'name' => '_Token[unlocked]',
  1412. 'value' => 'cancel%7Csave',
  1413. 'autocomplete' => 'off',
  1414. ]],
  1415. ['input' => [
  1416. 'type' => 'hidden',
  1417. 'name' => '_Token[debug]',
  1418. 'value' => $tokenDebug,
  1419. 'autocomplete' => 'off',
  1420. ]],
  1421. '/div',
  1422. ];
  1423. $this->assertHtml($expected, $result);
  1424. }
  1425. /**
  1426. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1427. *
  1428. * @return void
  1429. */
  1430. public function testSecurityButtonNestedNamed()
  1431. {
  1432. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  1433. $this->Form->create('Addresses');
  1434. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1435. $result = $this->Form->unlockField();
  1436. $this->assertEquals(['Address.button'], $result);
  1437. }
  1438. /**
  1439. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1440. *
  1441. * @return void
  1442. */
  1443. public function testSecuritySubmitNestedNamed()
  1444. {
  1445. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1446. $this->Form->create($this->article);
  1447. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1448. $result = $this->Form->unlockField();
  1449. $this->assertEquals(['Address.button'], $result);
  1450. }
  1451. /**
  1452. * Test that the correct fields are unlocked for image submits with no names.
  1453. *
  1454. * @return void
  1455. */
  1456. public function testSecuritySubmitImageNoName()
  1457. {
  1458. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1459. $this->Form->create(false);
  1460. $result = $this->Form->submit('save.png');
  1461. $expected = [
  1462. 'div' => ['class' => 'submit'],
  1463. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1464. '/div',
  1465. ];
  1466. $this->assertHtml($expected, $result);
  1467. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1468. }
  1469. /**
  1470. * Test that the correct fields are unlocked for image submits with names.
  1471. *
  1472. * @return void
  1473. */
  1474. public function testSecuritySubmitImageName()
  1475. {
  1476. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1477. $this->Form->create(null);
  1478. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1479. $expected = [
  1480. 'div' => ['class' => 'submit'],
  1481. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1482. '/div',
  1483. ];
  1484. $this->assertHtml($expected, $result);
  1485. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1486. }
  1487. /**
  1488. * testFormSecurityMultipleControlFields method
  1489. *
  1490. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1491. *
  1492. * @return void
  1493. */
  1494. public function testFormSecurityMultipleControlFields()
  1495. {
  1496. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1497. $this->Form->create();
  1498. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1499. $this->Form->control('Addresses.0.title');
  1500. $this->Form->control('Addresses.0.first_name');
  1501. $this->Form->control('Addresses.0.last_name');
  1502. $this->Form->control('Addresses.0.address');
  1503. $this->Form->control('Addresses.0.city');
  1504. $this->Form->control('Addresses.0.phone');
  1505. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1506. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1507. $this->Form->control('Addresses.1.title');
  1508. $this->Form->control('Addresses.1.first_name');
  1509. $this->Form->control('Addresses.1.last_name');
  1510. $this->Form->control('Addresses.1.address');
  1511. $this->Form->control('Addresses.1.city');
  1512. $this->Form->control('Addresses.1.phone');
  1513. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1514. $result = $this->Form->secure($this->Form->fields);
  1515. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1516. $tokenDebug = urlencode(json_encode([
  1517. '/articles/add',
  1518. [
  1519. 'Addresses.0.id' => '123456',
  1520. 'Addresses.0.title',
  1521. 'Addresses.0.first_name',
  1522. 'Addresses.0.last_name',
  1523. 'Addresses.0.address',
  1524. 'Addresses.0.city',
  1525. 'Addresses.0.phone',
  1526. 'Addresses.0.primary',
  1527. 'Addresses.1.id' => '654321',
  1528. 'Addresses.1.title',
  1529. 'Addresses.1.first_name',
  1530. 'Addresses.1.last_name',
  1531. 'Addresses.1.address',
  1532. 'Addresses.1.city',
  1533. 'Addresses.1.phone',
  1534. 'Addresses.1.primary',
  1535. ],
  1536. [],
  1537. ]));
  1538. $expected = [
  1539. 'div' => ['style' => 'display:none;'],
  1540. ['input' => [
  1541. 'type' => 'hidden',
  1542. 'name' => '_Token[fields]',
  1543. 'value' => $hash,
  1544. 'autocomplete' => 'off',
  1545. ]],
  1546. ['input' => [
  1547. 'type' => 'hidden',
  1548. 'name' => '_Token[unlocked]',
  1549. 'autocomplete' => 'off',
  1550. 'value' => '',
  1551. ]],
  1552. ['input' => [
  1553. 'type' => 'hidden',
  1554. 'name' => '_Token[debug]',
  1555. 'value' => $tokenDebug,
  1556. 'autocomplete' => 'off',
  1557. ]],
  1558. '/div',
  1559. ];
  1560. $this->assertHtml($expected, $result);
  1561. }
  1562. /**
  1563. * testFormSecurityArrayFields method
  1564. *
  1565. * Test form security with Model.field.0 style inputs.
  1566. *
  1567. * @return void
  1568. */
  1569. public function testFormSecurityArrayFields()
  1570. {
  1571. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1572. $this->Form->create();
  1573. $this->Form->text('Address.primary.1');
  1574. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1575. $this->Form->text('Address.secondary.1.0');
  1576. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1577. }
  1578. /**
  1579. * testFormSecurityMultipleControlDisabledFields method
  1580. *
  1581. * Test secure form generation with multiple records and disabled fields.
  1582. *
  1583. * @return void
  1584. */
  1585. public function testFormSecurityMultipleControlDisabledFields()
  1586. {
  1587. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1588. 'unlockedFields' => ['first_name', 'address'],
  1589. ]));
  1590. $this->Form->create();
  1591. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1592. $this->Form->text('Addresses.0.title');
  1593. $this->Form->text('Addresses.0.first_name');
  1594. $this->Form->text('Addresses.0.last_name');
  1595. $this->Form->text('Addresses.0.address');
  1596. $this->Form->text('Addresses.0.city');
  1597. $this->Form->text('Addresses.0.phone');
  1598. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1599. $this->Form->text('Addresses.1.title');
  1600. $this->Form->text('Addresses.1.first_name');
  1601. $this->Form->text('Addresses.1.last_name');
  1602. $this->Form->text('Addresses.1.address');
  1603. $this->Form->text('Addresses.1.city');
  1604. $this->Form->text('Addresses.1.phone');
  1605. $result = $this->Form->secure($this->Form->fields);
  1606. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1607. $tokenDebug = urlencode(json_encode([
  1608. '/articles/add',
  1609. [
  1610. 'Addresses.0.id' => '123456',
  1611. 'Addresses.0.title',
  1612. 'Addresses.0.last_name',
  1613. 'Addresses.0.city',
  1614. 'Addresses.0.phone',
  1615. 'Addresses.1.id' => '654321',
  1616. 'Addresses.1.title',
  1617. 'Addresses.1.last_name',
  1618. 'Addresses.1.city',
  1619. 'Addresses.1.phone',
  1620. ],
  1621. [
  1622. 'first_name',
  1623. 'address',
  1624. ],
  1625. ]));
  1626. $expected = [
  1627. 'div' => ['style' => 'display:none;'],
  1628. ['input' => [
  1629. 'type' => 'hidden',
  1630. 'name' => '_Token[fields]',
  1631. 'autocomplete' => 'off',
  1632. 'value' => $hash,
  1633. ]],
  1634. ['input' => [
  1635. 'type' => 'hidden',
  1636. 'name' => '_Token[unlocked]',
  1637. 'autocomplete' => 'off',
  1638. 'value' => 'address%7Cfirst_name',
  1639. ]],
  1640. ['input' => [
  1641. 'type' => 'hidden',
  1642. 'name' => '_Token[debug]',
  1643. 'autocomplete' => 'off',
  1644. 'value' => $tokenDebug,
  1645. ]],
  1646. '/div',
  1647. ];
  1648. $this->assertHtml($expected, $result);
  1649. }
  1650. /**
  1651. * testFormSecurityControlDisabledFields method
  1652. *
  1653. * Test single record form with disabled fields.
  1654. *
  1655. * @return void
  1656. */
  1657. public function testFormSecurityControlUnlockedFields()
  1658. {
  1659. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1660. 'unlockedFields' => ['first_name', 'address'],
  1661. ]));
  1662. $this->Form->create();
  1663. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1664. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1665. $this->Form->text('Addresses.title');
  1666. $this->Form->text('Addresses.first_name');
  1667. $this->Form->text('Addresses.last_name');
  1668. $this->Form->text('Addresses.address');
  1669. $this->Form->text('Addresses.city');
  1670. $this->Form->text('Addresses.phone');
  1671. $result = $this->Form->fields;
  1672. $expected = [
  1673. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1674. 'Addresses.city', 'Addresses.phone',
  1675. ];
  1676. $this->assertEquals($expected, $result);
  1677. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1678. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1679. $tokenDebug = urlencode(json_encode([
  1680. '/articles/add',
  1681. [
  1682. 'Addresses.id' => '123456',
  1683. 'Addresses.title',
  1684. 'Addresses.last_name',
  1685. 'Addresses.city',
  1686. 'Addresses.phone',
  1687. ],
  1688. [
  1689. 'first_name',
  1690. 'address',
  1691. ],
  1692. ]));
  1693. $expected = [
  1694. 'div' => ['style' => 'display:none;'],
  1695. ['input' => [
  1696. 'type' => 'hidden',
  1697. 'name' => '_Token[fields]',
  1698. 'value' => $hash,
  1699. 'autocomplete' => 'off',
  1700. 'data-foo' => 'bar',
  1701. ]],
  1702. ['input' => [
  1703. 'type' => 'hidden',
  1704. 'name' => '_Token[unlocked]',
  1705. 'value' => 'address%7Cfirst_name',
  1706. 'autocomplete' => 'off',
  1707. 'data-foo' => 'bar',
  1708. ]],
  1709. ['input' => [
  1710. 'type' => 'hidden', 'name' => '_Token[debug]',
  1711. 'value' => $tokenDebug,
  1712. 'autocomplete' => 'off',
  1713. 'data-foo' => 'bar',
  1714. ]],
  1715. '/div',
  1716. ];
  1717. $this->assertHtml($expected, $result);
  1718. }
  1719. /**
  1720. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1721. *
  1722. * Test single record form with debugSecurity param.
  1723. *
  1724. * @return void
  1725. */
  1726. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1727. {
  1728. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1729. 'unlockedFields' => ['first_name', 'address'],
  1730. ]));
  1731. $this->Form->create();
  1732. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1733. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1734. $this->Form->text('Addresses.title');
  1735. $this->Form->text('Addresses.first_name');
  1736. $this->Form->text('Addresses.last_name');
  1737. $this->Form->text('Addresses.address');
  1738. $this->Form->text('Addresses.city');
  1739. $this->Form->text('Addresses.phone');
  1740. $result = $this->Form->fields;
  1741. $expected = [
  1742. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1743. 'Addresses.city', 'Addresses.phone',
  1744. ];
  1745. $this->assertEquals($expected, $result);
  1746. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1747. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1748. $tokenDebug = urlencode(json_encode([
  1749. '/articles/add',
  1750. [
  1751. 'Addresses.id' => '123456',
  1752. 'Addresses.title',
  1753. 'Addresses.last_name',
  1754. 'Addresses.city',
  1755. 'Addresses.phone',
  1756. ],
  1757. [
  1758. 'first_name',
  1759. 'address',
  1760. ],
  1761. ]));
  1762. $expected = [
  1763. 'div' => ['style' => 'display:none;'],
  1764. ['input' => [
  1765. 'type' => 'hidden',
  1766. 'name' => '_Token[fields]',
  1767. 'value' => $hash,
  1768. 'autocomplete' => 'off',
  1769. 'data-foo' => 'bar',
  1770. ]],
  1771. ['input' => [
  1772. 'type' => 'hidden',
  1773. 'name' => '_Token[unlocked]',
  1774. 'value' => 'address%7Cfirst_name',
  1775. 'autocomplete' => 'off',
  1776. 'data-foo' => 'bar',
  1777. ]],
  1778. ['input' => [
  1779. 'type' => 'hidden', 'name' => '_Token[debug]',
  1780. 'value' => $tokenDebug,
  1781. 'autocomplete' => 'off',
  1782. 'data-foo' => 'bar',
  1783. ]],
  1784. '/div',
  1785. ];
  1786. $this->assertHtml($expected, $result);
  1787. }
  1788. /**
  1789. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1790. *
  1791. * Debug is false, debugSecurity is true -> no debug
  1792. *
  1793. * @return void
  1794. */
  1795. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1796. {
  1797. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1798. 'unlockedFields' => ['first_name', 'address'],
  1799. ]));
  1800. $this->Form->create();
  1801. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1802. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1803. $this->Form->text('Addresses.title');
  1804. $this->Form->text('Addresses.first_name');
  1805. $this->Form->text('Addresses.last_name');
  1806. $this->Form->text('Addresses.address');
  1807. $this->Form->text('Addresses.city');
  1808. $this->Form->text('Addresses.phone');
  1809. $result = $this->Form->fields;
  1810. $expected = [
  1811. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1812. 'Addresses.city', 'Addresses.phone',
  1813. ];
  1814. $this->assertEquals($expected, $result);
  1815. Configure::write('debug', false);
  1816. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1817. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1818. $expected = [
  1819. 'div' => ['style' => 'display:none;'],
  1820. ['input' => [
  1821. 'type' => 'hidden',
  1822. 'name' => '_Token[fields]',
  1823. 'value' => $hash,
  1824. 'autocomplete' => 'off',
  1825. 'data-foo' => 'bar',
  1826. ]],
  1827. ['input' => [
  1828. 'type' => 'hidden',
  1829. 'name' => '_Token[unlocked]',
  1830. 'value' => 'address%7Cfirst_name',
  1831. 'autocomplete' => 'off',
  1832. 'data-foo' => 'bar',
  1833. ]],
  1834. '/div',
  1835. ];
  1836. $this->assertHtml($expected, $result);
  1837. }
  1838. /**
  1839. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1840. *
  1841. * Test single record form with debugSecurity param.
  1842. *
  1843. * @return void
  1844. */
  1845. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1846. {
  1847. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1848. 'unlockedFields' => ['first_name', 'address'],
  1849. ]));
  1850. $this->Form->create();
  1851. $this->assertEquals(
  1852. $this->View->getRequest()->getParam('_Token.unlockedFields'),
  1853. $this->Form->unlockField()
  1854. );
  1855. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1856. $this->Form->text('Addresses.title');
  1857. $this->Form->text('Addresses.first_name');
  1858. $this->Form->text('Addresses.last_name');
  1859. $this->Form->text('Addresses.address');
  1860. $this->Form->text('Addresses.city');
  1861. $this->Form->text('Addresses.phone');
  1862. $result = $this->Form->fields;
  1863. $expected = [
  1864. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1865. 'Addresses.city', 'Addresses.phone',
  1866. ];
  1867. $this->assertEquals($expected, $result);
  1868. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1869. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1870. $expected = [
  1871. 'div' => ['style' => 'display:none;'],
  1872. ['input' => [
  1873. 'type' => 'hidden',
  1874. 'name' => '_Token[fields]',
  1875. 'value' => $hash,
  1876. 'autocomplete' => 'off',
  1877. 'data-foo' => 'bar',
  1878. ]],
  1879. ['input' => [
  1880. 'type' => 'hidden',
  1881. 'name' => '_Token[unlocked]',
  1882. 'value' => 'address%7Cfirst_name',
  1883. 'autocomplete' => 'off',
  1884. 'data-foo' => 'bar',
  1885. ]],
  1886. '/div',
  1887. ];
  1888. $this->assertHtml($expected, $result);
  1889. }
  1890. /**
  1891. * testFormSecureWithCustomNameAttribute method
  1892. *
  1893. * Test securing inputs with custom name attributes.
  1894. *
  1895. * @return void
  1896. */
  1897. public function testFormSecureWithCustomNameAttribute()
  1898. {
  1899. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1900. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1901. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1902. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1903. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1904. }
  1905. /**
  1906. * testFormSecuredControl method
  1907. *
  1908. * Test generation of entire secure form, assertions made on control() output.
  1909. *
  1910. * @return void
  1911. */
  1912. public function testFormSecuredControl()
  1913. {
  1914. $this->View->setRequest($this->View->getRequest()
  1915. ->withParam('_Token', 'stuff')
  1916. ->withParam('_csrfToken', 'testKey'));
  1917. $this->article['schema'] = [
  1918. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1919. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1920. ];
  1921. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1922. $encoding = strtolower(Configure::read('App.encoding'));
  1923. $expected = [
  1924. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1925. 'div' => ['style' => 'display:none;'],
  1926. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1927. ['input' => [
  1928. 'type' => 'hidden',
  1929. 'name' => '_csrfToken',
  1930. 'value' => 'testKey',
  1931. 'autocomplete' => 'off',
  1932. ]],
  1933. '/div',
  1934. ];
  1935. $this->assertHtml($expected, $result);
  1936. $result = $this->Form->control('ratio');
  1937. $expected = [
  1938. 'div' => ['class'],
  1939. 'label' => ['for'],
  1940. 'Ratio',
  1941. '/label',
  1942. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1943. '/div',
  1944. ];
  1945. $this->assertHtml($expected, $result);
  1946. $result = $this->Form->control('population');
  1947. $expected = [
  1948. 'div' => ['class'],
  1949. 'label' => ['for'],
  1950. 'Population',
  1951. '/label',
  1952. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1953. '/div',
  1954. ];
  1955. $this->assertHtml($expected, $result);
  1956. $result = $this->Form->control('published', ['type' => 'text']);
  1957. $expected = [
  1958. 'div' => ['class' => 'input text'],
  1959. 'label' => ['for' => 'published'],
  1960. 'Published',
  1961. '/label',
  1962. ['input' => [
  1963. 'type' => 'text',
  1964. 'name' => 'published',
  1965. 'id' => 'published',
  1966. ]],
  1967. '/div',
  1968. ];
  1969. $this->assertHtml($expected, $result);
  1970. $result = $this->Form->control('other', ['type' => 'text']);
  1971. $expected = [
  1972. 'div' => ['class' => 'input text'],
  1973. 'label' => ['for' => 'other'],
  1974. 'Other',
  1975. '/label',
  1976. ['input' => [
  1977. 'type' => 'text',
  1978. 'name' => 'other',
  1979. 'id',
  1980. ]],
  1981. '/div',
  1982. ];
  1983. $this->assertHtml($expected, $result);
  1984. $result = $this->Form->hidden('stuff');
  1985. $expected = [
  1986. 'input' => [
  1987. 'type' => 'hidden',
  1988. 'name' => 'stuff',
  1989. ],
  1990. ];
  1991. $this->assertHtml($expected, $result);
  1992. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1993. $expected = ['input' => [
  1994. 'type' => 'hidden',
  1995. 'name' => 'hidden',
  1996. 'value' => '0',
  1997. ]];
  1998. $this->assertHtml($expected, $result);
  1999. $result = $this->Form->control('something', ['type' => 'checkbox']);
  2000. $expected = [
  2001. 'div' => ['class' => 'input checkbox'],
  2002. ['input' => [
  2003. 'type' => 'hidden',
  2004. 'name' => 'something',
  2005. 'value' => '0',
  2006. ]],
  2007. 'label' => ['for' => 'something'],
  2008. ['input' => [
  2009. 'type' => 'checkbox',
  2010. 'name' => 'something',
  2011. 'value' => '1',
  2012. 'id' => 'something',
  2013. ]],
  2014. 'Something',
  2015. '/label',
  2016. '/div',
  2017. ];
  2018. $this->assertHtml($expected, $result);
  2019. $result = $this->Form->fields;
  2020. $expectedFields = [
  2021. 'ratio',
  2022. 'population',
  2023. 'published',
  2024. 'other',
  2025. 'stuff' => '',
  2026. 'hidden' => '0',
  2027. 'something',
  2028. ];
  2029. $this->assertEquals($expectedFields, $result);
  2030. $result = $this->Form->secure($this->Form->fields);
  2031. $tokenDebug = urlencode(json_encode([
  2032. '/articles/add',
  2033. $expectedFields,
  2034. [],
  2035. ]));
  2036. $expected = [
  2037. 'div' => ['style' => 'display:none;'],
  2038. ['input' => [
  2039. 'type' => 'hidden',
  2040. 'name' => '_Token[fields]',
  2041. 'value',
  2042. 'autocomplete',
  2043. ]],
  2044. ['input' => [
  2045. 'type' => 'hidden',
  2046. 'name' => '_Token[unlocked]',
  2047. 'value' => '',
  2048. 'autocomplete' => 'off',
  2049. ]],
  2050. ['input' => [
  2051. 'type' => 'hidden', 'name' => '_Token[debug]',
  2052. 'value' => $tokenDebug,
  2053. 'autocomplete' => 'off',
  2054. ]],
  2055. '/div',
  2056. ];
  2057. $this->assertHtml($expected, $result);
  2058. }
  2059. /**
  2060. * testSecuredControlCustomName method
  2061. *
  2062. * Test secured inputs with custom names.
  2063. *
  2064. * @return void
  2065. */
  2066. public function testSecuredControlCustomName()
  2067. {
  2068. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2069. $this->assertEquals([], $this->Form->fields);
  2070. $this->Form->text('text_input', [
  2071. 'name' => 'Option[General.default_role]',
  2072. ]);
  2073. $expected = ['Option.General.default_role'];
  2074. $this->assertEquals($expected, $this->Form->fields);
  2075. $this->Form->select('select_box', [1, 2], [
  2076. 'name' => 'Option[General.select_role]',
  2077. ]);
  2078. $expected[] = 'Option.General.select_role';
  2079. $this->assertEquals($expected, $this->Form->fields);
  2080. $this->Form->text('other.things[]');
  2081. $expected[] = 'other.things';
  2082. $this->assertEquals($expected, $this->Form->fields);
  2083. }
  2084. /**
  2085. * testSecuredControlDuplicate method
  2086. *
  2087. * Test that a hidden field followed by a visible field
  2088. * undoes the hidden field locking.
  2089. *
  2090. * @return void
  2091. */
  2092. public function testSecuredControlDuplicate()
  2093. {
  2094. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2095. $this->assertEquals([], $this->Form->fields);
  2096. $this->Form->control('text_val', [
  2097. 'type' => 'hidden',
  2098. 'value' => 'some text',
  2099. ]);
  2100. $expected = ['text_val' => 'some text'];
  2101. $this->assertEquals($expected, $this->Form->fields);
  2102. $this->Form->control('text_val', [
  2103. 'type' => 'text',
  2104. ]);
  2105. $expected = ['text_val'];
  2106. $this->assertEquals($expected, $this->Form->fields);
  2107. }
  2108. /**
  2109. * testFormSecuredFileControl method
  2110. *
  2111. * Tests that the correct keys are added to the field hash index.
  2112. *
  2113. * @return void
  2114. */
  2115. public function testFormSecuredFileControl()
  2116. {
  2117. $this->assertEquals([], $this->Form->fields);
  2118. $this->Form->file('Attachment.file');
  2119. $expected = [
  2120. 'Attachment.file.name', 'Attachment.file.type',
  2121. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2122. 'Attachment.file.size',
  2123. ];
  2124. $this->assertEquals($expected, $this->Form->fields);
  2125. }
  2126. /**
  2127. * testFormSecuredMultipleSelect method
  2128. *
  2129. * Test that multiple selects keys are added to field hash.
  2130. *
  2131. * @return void
  2132. */
  2133. public function testFormSecuredMultipleSelect()
  2134. {
  2135. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  2136. $this->assertEquals([], $this->Form->fields);
  2137. $options = ['1' => 'one', '2' => 'two'];
  2138. $this->Form->select('Model.select', $options);
  2139. $expected = ['Model.select'];
  2140. $this->assertEquals($expected, $this->Form->fields);
  2141. $this->Form->fields = [];
  2142. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2143. $this->assertEquals($expected, $this->Form->fields);
  2144. }
  2145. /**
  2146. * testFormSecuredRadio method
  2147. *
  2148. * @return void
  2149. */
  2150. public function testFormSecuredRadio()
  2151. {
  2152. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2153. $this->assertEquals([], $this->Form->fields);
  2154. $options = ['1' => 'option1', '2' => 'option2'];
  2155. $this->Form->radio('Test.test', $options);
  2156. $expected = ['Test.test'];
  2157. $this->assertEquals($expected, $this->Form->fields);
  2158. $this->Form->radio('Test.all', $options, [
  2159. 'disabled' => ['option1', 'option2'],
  2160. ]);
  2161. $expected = ['Test.test', 'Test.all' => ''];
  2162. $this->assertEquals($expected, $this->Form->fields);
  2163. $this->Form->radio('Test.some', $options, [
  2164. 'disabled' => ['option1'],
  2165. ]);
  2166. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2167. $this->assertEquals($expected, $this->Form->fields);
  2168. }
  2169. /**
  2170. * testFormSecuredAndDisabledNotAssoc method
  2171. *
  2172. * Test that when disabled is in a list based attribute array it works.
  2173. *
  2174. * @return void
  2175. */
  2176. public function testFormSecuredAndDisabledNotAssoc()
  2177. {
  2178. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2179. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2180. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2181. $this->Form->text('Model.text', ['disabled']);
  2182. $this->Form->textarea('Model.textarea', ['disabled']);
  2183. $this->Form->password('Model.password', ['disabled']);
  2184. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2185. $expected = [
  2186. 'Model.radio' => '',
  2187. ];
  2188. $this->assertEquals($expected, $this->Form->fields);
  2189. }
  2190. /**
  2191. * testFormSecuredAndDisabled method
  2192. *
  2193. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2194. * hashing.
  2195. *
  2196. * @return void
  2197. */
  2198. public function testFormSecuredAndDisabled()
  2199. {
  2200. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2201. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2202. $this->Form->text('Model.text', ['disabled' => true]);
  2203. $this->Form->password('Model.text', ['disabled' => true]);
  2204. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2205. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2206. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2207. $this->Form->year('Model.year', ['disabled' => true]);
  2208. $this->Form->month('Model.month', ['disabled' => true]);
  2209. $this->Form->day('Model.day', ['disabled' => true]);
  2210. $this->Form->hour('Model.hour', ['disabled' => true]);
  2211. $this->Form->minute('Model.minute', ['disabled' => true]);
  2212. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2213. $expected = [
  2214. 'Model.radio' => '',
  2215. ];
  2216. $this->assertEquals($expected, $this->Form->fields);
  2217. }
  2218. /**
  2219. * testDisableSecurityUsingForm method
  2220. *
  2221. * @return void
  2222. */
  2223. public function testDisableSecurityUsingForm()
  2224. {
  2225. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2226. 'disabledFields' => [],
  2227. ]));
  2228. $this->Form->create();
  2229. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2230. $this->Form->text('Addresses.title');
  2231. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2232. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2233. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2234. $result = $this->Form->fields;
  2235. $expected = [
  2236. 'Addresses.id' => '123456', 'Addresses.title',
  2237. ];
  2238. $this->assertEquals($expected, $result);
  2239. }
  2240. /**
  2241. * testUnlockFieldAddsToList method
  2242. *
  2243. * Test disableField.
  2244. *
  2245. * @return void
  2246. */
  2247. public function testUnlockFieldAddsToList()
  2248. {
  2249. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2250. 'unlockedFields' => [],
  2251. ]));
  2252. $this->Form->unlockField('Contact.name');
  2253. $this->Form->text('Contact.name');
  2254. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2255. $this->assertEquals([], $this->Form->fields);
  2256. }
  2257. /**
  2258. * testUnlockFieldRemovingFromFields method
  2259. *
  2260. * Test unlockField removing from fields array.
  2261. *
  2262. * @return void
  2263. */
  2264. public function testUnlockFieldRemovingFromFields()
  2265. {
  2266. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2267. 'unlockedFields' => [],
  2268. ]));
  2269. $this->Form->create($this->article);
  2270. $this->Form->hidden('Article.id', ['value' => 1]);
  2271. $this->Form->text('Article.title');
  2272. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2273. $this->assertContains('Article.title', $this->Form->fields, 'Field should be secured.');
  2274. $this->Form->unlockField('Article.title');
  2275. $this->Form->unlockField('Article.id');
  2276. $this->assertEquals([], $this->Form->fields);
  2277. }
  2278. /**
  2279. * testResetUnlockFields method
  2280. *
  2281. * Test reset unlockFields, when create new form.
  2282. *
  2283. * @return void
  2284. */
  2285. public function testResetUnlockFields()
  2286. {
  2287. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2288. 'key' => 'testKey',
  2289. 'unlockedFields' => [],
  2290. ]));
  2291. $this->Form->unlockField('Contact.id');
  2292. $this->Form->create('Contact');
  2293. $this->Form->hidden('Contact.id', ['value' => 1]);
  2294. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2295. $this->Form->end();
  2296. $this->Form->create('Contact');
  2297. $this->Form->hidden('Contact.id', ['value' => 1]);
  2298. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2299. }
  2300. /**
  2301. * testSecuredFormUrlIgnoresHost method
  2302. *
  2303. * Test that only the path + query elements of a form's URL show up in their hash.
  2304. *
  2305. * @return void
  2306. */
  2307. public function testSecuredFormUrlIgnoresHost()
  2308. {
  2309. $this->View->setRequest($this->View->getRequest()->withParam('_Token', ['key' => 'testKey']));
  2310. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2311. $this->Form->create($this->article, [
  2312. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]],
  2313. ]);
  2314. $result = $this->Form->secure();
  2315. $this->assertContains($expected, $result);
  2316. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2317. $result = $this->Form->secure();
  2318. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2319. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2320. $result = $this->Form->secure();
  2321. $this->assertContains($expected, $result, 'URL path + query should work.');
  2322. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2323. $result = $this->Form->secure();
  2324. $this->assertNotContains($expected, $result, 'URL is different');
  2325. }
  2326. /**
  2327. * testSecuredFormUrlHasHtmlAndIdentifier method
  2328. *
  2329. * Test that URL, HTML and identifier show up in their hashes.
  2330. *
  2331. * @return void
  2332. */
  2333. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2334. {
  2335. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2336. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2337. $this->Form->create($this->article, [
  2338. 'url' => [
  2339. 'controller' => 'articles',
  2340. 'action' => 'view',
  2341. '?' => [
  2342. 'page' => 1,
  2343. 'limit' => 10,
  2344. 'html' => '<>"',
  2345. ],
  2346. '#' => 'result',
  2347. ],
  2348. ]);
  2349. $result = $this->Form->secure();
  2350. $this->assertContains($expected, $result);
  2351. $this->Form->create($this->article, [
  2352. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2353. ]);
  2354. $result = $this->Form->secure();
  2355. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2356. $this->Form->create($this->article, [
  2357. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2358. ]);
  2359. $result = $this->Form->secure();
  2360. $this->assertContains($expected, $result, 'URL path + query should work.');
  2361. }
  2362. /**
  2363. * testErrorMessageDisplay method
  2364. *
  2365. * Test error message display.
  2366. *
  2367. * @return void
  2368. */
  2369. public function testErrorMessageDisplay()
  2370. {
  2371. $this->article['errors'] = [
  2372. 'Article' => [
  2373. 'title' => 'error message',
  2374. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2375. ],
  2376. ];
  2377. $this->Form->create($this->article);
  2378. $result = $this->Form->control('Article.title');
  2379. $expected = [
  2380. 'div' => ['class' => 'input text error'],
  2381. 'label' => ['for' => 'article-title'],
  2382. 'Title',
  2383. '/label',
  2384. 'input' => [
  2385. 'type' => 'text', 'name' => 'Article[title]',
  2386. 'id' => 'article-title', 'class' => 'form-error',
  2387. ],
  2388. ['div' => ['class' => 'error-message']],
  2389. 'error message',
  2390. '/div',
  2391. '/div',
  2392. ];
  2393. $this->assertHtml($expected, $result);
  2394. $result = $this->Form->control('Article.title', [
  2395. 'templates' => [
  2396. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>',
  2397. ],
  2398. ]);
  2399. $expected = [
  2400. 'div' => ['class' => 'input text error'],
  2401. 'label' => ['for' => 'article-title'],
  2402. 'Title',
  2403. '/label',
  2404. 'input' => [
  2405. 'type' => 'text', 'name' => 'Article[title]',
  2406. 'id' => 'article-title', 'class' => 'form-error',
  2407. ],
  2408. '/div',
  2409. ];
  2410. $this->assertHtml($expected, $result);
  2411. $result = $this->Form->control('Article.content');
  2412. $expected = [
  2413. 'div' => ['class' => 'input text error'],
  2414. 'label' => ['for' => 'article-content'],
  2415. 'Content',
  2416. '/label',
  2417. 'input' => [
  2418. 'type' => 'text', 'name' => 'Article[content]',
  2419. 'id' => 'article-content', 'class' => 'form-error',
  2420. ],
  2421. ['div' => ['class' => 'error-message']],
  2422. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2423. '/div',
  2424. '/div',
  2425. ];
  2426. $this->assertHtml($expected, $result);
  2427. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2428. $expected = [
  2429. 'div' => ['class' => 'input text error'],
  2430. 'label' => ['for' => 'article-content'],
  2431. 'Content',
  2432. '/label',
  2433. 'input' => [
  2434. 'type' => 'text', 'name' => 'Article[content]',
  2435. 'id' => 'article-content', 'class' => 'form-error',
  2436. ],
  2437. ['div' => ['class' => 'error-message']],
  2438. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2439. '/div',
  2440. '/div',
  2441. ];
  2442. $this->assertHtml($expected, $result);
  2443. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2444. $expected = [
  2445. 'div' => ['class' => 'input text error'],
  2446. 'label' => ['for' => 'article-content'],
  2447. 'Content',
  2448. '/label',
  2449. 'input' => [
  2450. 'type' => 'text', 'name' => 'Article[content]',
  2451. 'id' => 'article-content', 'class' => 'form-error',
  2452. ],
  2453. ['div' => ['class' => 'error-message']],
  2454. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2455. '/div',
  2456. '/div',
  2457. ];
  2458. $this->assertHtml($expected, $result);
  2459. }
  2460. /**
  2461. * testEmptyErrorValidation method
  2462. *
  2463. * Test validation errors, when validation message is an empty string.
  2464. *
  2465. * @return void
  2466. */
  2467. public function testEmptyErrorValidation()
  2468. {
  2469. $this->article['errors'] = [
  2470. 'Article' => ['title' => ''],
  2471. ];
  2472. $this->Form->create($this->article);
  2473. $result = $this->Form->control('Article.title');
  2474. $expected = [
  2475. 'div' => ['class' => 'input text error'],
  2476. 'label' => ['for' => 'article-title'],
  2477. 'Title',
  2478. '/label',
  2479. 'input' => [
  2480. 'type' => 'text', 'name' => 'Article[title]',
  2481. 'id' => 'article-title', 'class' => 'form-error',
  2482. ],
  2483. ['div' => ['class' => 'error-message']],
  2484. [],
  2485. '/div',
  2486. '/div',
  2487. ];
  2488. $this->assertHtml($expected, $result);
  2489. }
  2490. /**
  2491. * testEmptyControlErrorValidation method
  2492. *
  2493. * Test validation errors, when calling control() overriding validation message by an empty string.
  2494. *
  2495. * @return void
  2496. */
  2497. public function testEmptyControlErrorValidation()
  2498. {
  2499. $this->article['errors'] = [
  2500. 'Article' => ['title' => 'error message'],
  2501. ];
  2502. $this->Form->create($this->article);
  2503. $result = $this->Form->control('Article.title', ['error' => '']);
  2504. $expected = [
  2505. 'div' => ['class' => 'input text error'],
  2506. 'label' => ['for' => 'article-title'],
  2507. 'Title',
  2508. '/label',
  2509. 'input' => [
  2510. 'type' => 'text', 'name' => 'Article[title]',
  2511. 'id' => 'article-title', 'class' => 'form-error',
  2512. ],
  2513. ['div' => ['class' => 'error-message']],
  2514. [],
  2515. '/div',
  2516. '/div',
  2517. ];
  2518. $this->assertHtml($expected, $result);
  2519. }
  2520. /**
  2521. * testControlErrorMessage method
  2522. *
  2523. * Test validation errors, when calling control() overriding validation messages.
  2524. *
  2525. * @return void
  2526. */
  2527. public function testControlErrorMessage()
  2528. {
  2529. $this->article['errors'] = [
  2530. 'title' => ['error message'],
  2531. ];
  2532. $this->Form->create($this->article);
  2533. $result = $this->Form->control('title', [
  2534. 'error' => 'Custom error!',
  2535. ]);
  2536. $expected = [
  2537. 'div' => ['class' => 'input text required error'],
  2538. 'label' => ['for' => 'title'],
  2539. 'Title',
  2540. '/label',
  2541. 'input' => [
  2542. 'type' => 'text', 'name' => 'title',
  2543. 'id' => 'title', 'class' => 'form-error',
  2544. 'required' => 'required',
  2545. ],
  2546. ['div' => ['class' => 'error-message']],
  2547. 'Custom error!',
  2548. '/div',
  2549. '/div',
  2550. ];
  2551. $this->assertHtml($expected, $result);
  2552. $result = $this->Form->control('title', [
  2553. 'error' => ['error message' => 'Custom error!'],
  2554. ]);
  2555. $expected = [
  2556. 'div' => ['class' => 'input text required error'],
  2557. 'label' => ['for' => 'title'],
  2558. 'Title',
  2559. '/label',
  2560. 'input' => [
  2561. 'type' => 'text',
  2562. 'name' => 'title',
  2563. 'id' => 'title',
  2564. '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. }
  2574. /**
  2575. * testFormValidationAssociated method
  2576. *
  2577. * Tests displaying errors for nested entities.
  2578. *
  2579. * @return void
  2580. */
  2581. public function testFormValidationAssociated()
  2582. {
  2583. $nested = new Entity(['foo' => 'bar']);
  2584. $nested->setError('foo', ['not a valid bar']);
  2585. $entity = new Entity(['nested' => $nested]);
  2586. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2587. $result = $this->Form->error('nested.foo');
  2588. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2589. }
  2590. /**
  2591. * testFormValidationAssociatedSecondLevel method
  2592. *
  2593. * Test form error display with associated model.
  2594. *
  2595. * @return void
  2596. */
  2597. public function testFormValidationAssociatedSecondLevel()
  2598. {
  2599. $inner = new Entity(['bar' => 'baz']);
  2600. $nested = new Entity(['foo' => $inner]);
  2601. $entity = new Entity(['nested' => $nested]);
  2602. $inner->setError('bar', ['not a valid one']);
  2603. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2604. $result = $this->Form->error('nested.foo.bar');
  2605. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2606. }
  2607. /**
  2608. * testFormValidationMultiRecord method
  2609. *
  2610. * Test form error display with multiple records.
  2611. *
  2612. * @return void
  2613. */
  2614. public function testFormValidationMultiRecord()
  2615. {
  2616. $one = new Entity();
  2617. $two = new Entity();
  2618. $this->getTableLocator()->get('Contacts', [
  2619. 'className' => __NAMESPACE__ . '\ContactsTable',
  2620. ]);
  2621. $one->set('email', '');
  2622. $one->setError('email', ['invalid email']);
  2623. $two->set('name', '');
  2624. $two->setError('name', ['This is wrong']);
  2625. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2626. $result = $this->Form->control('0.email');
  2627. $expected = [
  2628. 'div' => ['class' => 'input email error'],
  2629. 'label' => ['for' => '0-email'],
  2630. 'Email',
  2631. '/label',
  2632. 'input' => [
  2633. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2634. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2635. ],
  2636. ['div' => ['class' => 'error-message']],
  2637. 'invalid email',
  2638. '/div',
  2639. '/div',
  2640. ];
  2641. $this->assertHtml($expected, $result);
  2642. $result = $this->Form->control('1.name');
  2643. $expected = [
  2644. 'div' => ['class' => 'input text error'],
  2645. 'label' => ['for' => '1-name'],
  2646. 'Name',
  2647. '/label',
  2648. 'input' => [
  2649. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2650. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2651. ],
  2652. ['div' => ['class' => 'error-message']],
  2653. 'This is wrong',
  2654. '/div',
  2655. '/div',
  2656. ];
  2657. $this->assertHtml($expected, $result);
  2658. }
  2659. /**
  2660. * testControl method
  2661. *
  2662. * Test various incarnations of control().
  2663. *
  2664. * @return void
  2665. */
  2666. public function testControl()
  2667. {
  2668. $this->getTableLocator()->get('ValidateUsers', [
  2669. 'className' => __NAMESPACE__ . '\ValidateUsersTable',
  2670. ]);
  2671. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2672. $result = $this->Form->control('ValidateUsers.balance');
  2673. $expected = [
  2674. 'div' => ['class'],
  2675. 'label' => ['for'],
  2676. 'Balance',
  2677. '/label',
  2678. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2679. '/div',
  2680. ];
  2681. $this->assertHtml($expected, $result);
  2682. $result = $this->Form->control('ValidateUser.cost_decimal');
  2683. $expected = [
  2684. 'div' => ['class'],
  2685. 'label' => ['for'],
  2686. 'Cost Decimal',
  2687. '/label',
  2688. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2689. '/div',
  2690. ];
  2691. $this->assertHtml($expected, $result);
  2692. $result = $this->Form->control('ValidateUser.null_decimal');
  2693. $expected = [
  2694. 'div' => ['class'],
  2695. 'label' => ['for'],
  2696. 'Null Decimal',
  2697. '/label',
  2698. 'input' => ['name', 'type' => 'number', 'id'],
  2699. '/div',
  2700. ];
  2701. $this->assertHtml($expected, $result);
  2702. }
  2703. /**
  2704. * testControlCustomization method
  2705. *
  2706. * Tests the input method and passing custom options.
  2707. *
  2708. * @return void
  2709. */
  2710. public function testControlCustomization()
  2711. {
  2712. $this->getTableLocator()->get('Contacts', [
  2713. 'className' => __NAMESPACE__ . '\ContactsTable',
  2714. ]);
  2715. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2716. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2717. $expected = [
  2718. 'div' => ['class' => 'input email'],
  2719. 'label' => ['for' => 'custom'],
  2720. 'Email',
  2721. '/label',
  2722. ['input' => [
  2723. 'type' => 'email', 'name' => 'Contact[email]',
  2724. 'id' => 'custom', 'maxlength' => 255,
  2725. ]],
  2726. '/div',
  2727. ];
  2728. $this->assertHtml($expected, $result);
  2729. $result = $this->Form->control('Contact.email', [
  2730. 'templates' => ['inputContainer' => '<div>{{content}}</div>'],
  2731. ]);
  2732. $expected = [
  2733. '<div',
  2734. 'label' => ['for' => 'contact-email'],
  2735. 'Email',
  2736. '/label',
  2737. ['input' => [
  2738. 'type' => 'email', 'name' => 'Contact[email]',
  2739. 'id' => 'contact-email', 'maxlength' => 255,
  2740. ]],
  2741. '/div',
  2742. ];
  2743. $this->assertHtml($expected, $result);
  2744. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2745. $expected = [
  2746. 'div' => ['class' => 'input text'],
  2747. 'label' => ['for' => 'contact-email'],
  2748. 'Email',
  2749. '/label',
  2750. ['input' => [
  2751. 'type' => 'text', 'name' => 'Contact[email]',
  2752. 'id' => 'contact-email', 'maxlength' => '255',
  2753. ]],
  2754. '/div',
  2755. ];
  2756. $this->assertHtml($expected, $result);
  2757. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2758. $expected = [
  2759. 'div' => ['class' => 'input text'],
  2760. 'label' => ['for' => 'contact-5-email'],
  2761. 'Email',
  2762. '/label',
  2763. ['input' => [
  2764. 'type' => 'text', 'name' => 'Contact[5][email]',
  2765. 'id' => 'contact-5-email', 'maxlength' => '255',
  2766. ]],
  2767. '/div',
  2768. ];
  2769. $this->assertHtml($expected, $result);
  2770. $result = $this->Form->control('Contact.password');
  2771. $expected = [
  2772. 'div' => ['class' => 'input password'],
  2773. 'label' => ['for' => 'contact-password'],
  2774. 'Password',
  2775. '/label',
  2776. ['input' => [
  2777. 'type' => 'password', 'name' => 'Contact[password]',
  2778. 'id' => 'contact-password',
  2779. ]],
  2780. '/div',
  2781. ];
  2782. $this->assertHtml($expected, $result);
  2783. $result = $this->Form->control('Contact.email', [
  2784. 'type' => 'file', 'class' => 'textbox',
  2785. ]);
  2786. $expected = [
  2787. 'div' => ['class' => 'input file'],
  2788. 'label' => ['for' => 'contact-email'],
  2789. 'Email',
  2790. '/label',
  2791. ['input' => [
  2792. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2793. 'id' => 'contact-email',
  2794. ]],
  2795. '/div',
  2796. ];
  2797. $this->assertHtml($expected, $result);
  2798. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2799. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2800. $result = $this->Form->control('phone');
  2801. $expected = [
  2802. 'div' => ['class' => 'input tel'],
  2803. 'label' => ['for' => 'phone'],
  2804. 'Phone',
  2805. '/label',
  2806. ['input' => [
  2807. 'type' => 'tel', 'name' => 'phone',
  2808. 'value' => 'Hello &amp; World &gt; weird chars',
  2809. 'id' => 'phone', 'maxlength' => 255,
  2810. ]],
  2811. '/div',
  2812. ];
  2813. $this->assertHtml($expected, $result);
  2814. $this->View->setRequest(
  2815. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2816. );
  2817. $this->Form->create();
  2818. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2819. $expected = [
  2820. 'div' => ['class' => 'input text'],
  2821. 'label' => ['for' => 'myId'],
  2822. 'Field',
  2823. '/label',
  2824. 'input' => [
  2825. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2826. 'value' => 'My value', 'id' => 'myId',
  2827. ],
  2828. '/div',
  2829. ];
  2830. $this->assertHtml($expected, $result);
  2831. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2832. $this->Form->create();
  2833. $entity->setError('field', 'Badness!');
  2834. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2835. $result = $this->Form->control('field');
  2836. $expected = [
  2837. 'div' => ['class' => 'input text error'],
  2838. 'label' => ['for' => 'field'],
  2839. 'Field',
  2840. '/label',
  2841. 'input' => [
  2842. 'type' => 'text', 'name' => 'field',
  2843. 'id' => 'field', 'class' => 'form-error',
  2844. ],
  2845. ['div' => ['class' => 'error-message']],
  2846. 'Badness!',
  2847. '/div',
  2848. '/div',
  2849. ];
  2850. $this->assertHtml($expected, $result);
  2851. $result = $this->Form->control('field', [
  2852. 'templates' => [
  2853. 'inputContainerError' => '{{content}}{{error}}',
  2854. 'error' => '<span class="error-message">{{content}}</span>',
  2855. ],
  2856. ]);
  2857. $expected = [
  2858. 'label' => ['for' => 'field'],
  2859. 'Field',
  2860. '/label',
  2861. 'input' => [
  2862. 'type' => 'text', 'name' => 'field',
  2863. 'id' => 'field', 'class' => 'form-error',
  2864. ],
  2865. ['span' => ['class' => 'error-message']],
  2866. 'Badness!',
  2867. '/span',
  2868. ];
  2869. $this->assertHtml($expected, $result);
  2870. $entity->setError('field', ['minLength'], true);
  2871. $result = $this->Form->control('field', [
  2872. 'error' => [
  2873. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2874. 'maxLength' => 'login too large',
  2875. ],
  2876. ]);
  2877. $expected = [
  2878. 'div' => ['class' => 'input text error'],
  2879. 'label' => ['for' => 'field'],
  2880. 'Field',
  2881. '/label',
  2882. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2883. ['div' => ['class' => 'error-message']],
  2884. 'Le login doit contenir au moins 2 caractères',
  2885. '/div',
  2886. '/div',
  2887. ];
  2888. $this->assertHtml($expected, $result);
  2889. $entity->setError('field', ['maxLength'], true);
  2890. $result = $this->Form->control('field', [
  2891. 'error' => [
  2892. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2893. 'maxLength' => 'login too large',
  2894. ],
  2895. ]);
  2896. $expected = [
  2897. 'div' => ['class' => 'input text error'],
  2898. 'label' => ['for' => 'field'],
  2899. 'Field',
  2900. '/label',
  2901. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2902. ['div' => ['class' => 'error-message']],
  2903. 'login too large',
  2904. '/div',
  2905. '/div',
  2906. ];
  2907. $this->assertHtml($expected, $result);
  2908. }
  2909. /**
  2910. * testControlWithTemplateFile method
  2911. *
  2912. * Test that control() accepts a template file.
  2913. *
  2914. * @return void
  2915. */
  2916. public function testControlWithTemplateFile()
  2917. {
  2918. $result = $this->Form->control('field', [
  2919. 'templates' => 'htmlhelper_tags',
  2920. ]);
  2921. $expected = [
  2922. 'label' => ['for' => 'field'],
  2923. 'Field',
  2924. '/label',
  2925. 'input' => [
  2926. 'type' => 'text', 'name' => 'field',
  2927. 'id' => 'field',
  2928. ],
  2929. ];
  2930. $this->assertHtml($expected, $result);
  2931. }
  2932. /**
  2933. * testNestedControlsEndWithBrackets method
  2934. *
  2935. * Test that nested inputs end with brackets.
  2936. *
  2937. * @return void
  2938. */
  2939. public function testNestedControlsEndWithBrackets()
  2940. {
  2941. $result = $this->Form->text('nested.text[]');
  2942. $expected = [
  2943. 'input' => [
  2944. 'type' => 'text', 'name' => 'nested[text][]',
  2945. ],
  2946. ];
  2947. $this->assertHtml($expected, $result);
  2948. $result = $this->Form->file('nested.file[]');
  2949. $expected = [
  2950. 'input' => [
  2951. 'type' => 'file', 'name' => 'nested[file][]',
  2952. ],
  2953. ];
  2954. $this->assertHtml($expected, $result);
  2955. }
  2956. /**
  2957. * testCreateIdPrefix method
  2958. *
  2959. * Test id prefix.
  2960. *
  2961. * @return void
  2962. */
  2963. public function testCreateIdPrefix()
  2964. {
  2965. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2966. $result = $this->Form->control('field');
  2967. $expected = [
  2968. 'div' => ['class' => 'input text'],
  2969. 'label' => ['for' => 'prefix-field'],
  2970. 'Field',
  2971. '/label',
  2972. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2973. '/div',
  2974. ];
  2975. $this->assertHtml($expected, $result);
  2976. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2977. $expected = [
  2978. 'div' => ['class' => 'input text'],
  2979. 'label' => ['for' => 'custom-id'],
  2980. 'Field',
  2981. '/label',
  2982. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2983. '/div',
  2984. ];
  2985. $this->assertHtml($expected, $result);
  2986. $result = $this->Form->radio('Model.field', ['option A']);
  2987. $expected = [
  2988. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2989. 'label' => ['for' => 'prefix-model-field-0'],
  2990. ['input' => [
  2991. 'type' => 'radio',
  2992. 'name' => 'Model[field]',
  2993. 'value' => '0',
  2994. 'id' => 'prefix-model-field-0',
  2995. ]],
  2996. 'option A',
  2997. '/label',
  2998. ];
  2999. $this->assertHtml($expected, $result);
  3000. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  3001. $expected = [
  3002. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  3003. 'label' => ['for' => 'prefix-model-field-0'],
  3004. ['input' => [
  3005. 'type' => 'radio',
  3006. 'name' => 'Model[field]',
  3007. 'value' => '0',
  3008. 'id' => 'prefix-model-field-0',
  3009. ]],
  3010. 'option A',
  3011. '/label',
  3012. ];
  3013. $this->assertHtml($expected, $result);
  3014. $result = $this->Form->select(
  3015. 'Model.multi_field',
  3016. ['first'],
  3017. ['multiple' => 'checkbox']
  3018. );
  3019. $expected = [
  3020. 'input' => [
  3021. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  3022. ],
  3023. ['div' => ['class' => 'checkbox']],
  3024. ['label' => ['for' => 'prefix-model-multi-field-0']],
  3025. ['input' => [
  3026. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3027. 'value' => '0', 'id' => 'prefix-model-multi-field-0',
  3028. ]],
  3029. 'first',
  3030. '/label',
  3031. '/div',
  3032. ];
  3033. $this->assertHtml($expected, $result);
  3034. $this->Form->end();
  3035. $result = $this->Form->control('field');
  3036. $expected = [
  3037. 'div' => ['class' => 'input text'],
  3038. 'label' => ['for' => 'field'],
  3039. 'Field',
  3040. '/label',
  3041. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  3042. '/div',
  3043. ];
  3044. $this->assertHtml($expected, $result);
  3045. }
  3046. /**
  3047. * testControlZero method
  3048. *
  3049. * Test that inputs with 0 can be created.
  3050. *
  3051. * @return void
  3052. */
  3053. public function testControlZero()
  3054. {
  3055. $this->getTableLocator()->get('Contacts', [
  3056. 'className' => __NAMESPACE__ . '\ContactsTable',
  3057. ]);
  3058. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  3059. $result = $this->Form->control('0');
  3060. $expected = [
  3061. 'div' => ['class' => 'input text'],
  3062. 'label' => ['for' => '0'], '/label',
  3063. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  3064. '/div',
  3065. ];
  3066. $this->assertHtml($expected, $result);
  3067. }
  3068. /**
  3069. * testControlCheckbox method
  3070. *
  3071. * Test control() with checkbox creation.
  3072. *
  3073. * @return void
  3074. */
  3075. public function testControlCheckbox()
  3076. {
  3077. $articles = $this->getTableLocator()->get('Articles');
  3078. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3079. $article = $articles->newEntity();
  3080. $this->Form->create($article);
  3081. $result = $this->Form->control('Articles.active');
  3082. $expected = [
  3083. 'div' => ['class' => 'input checkbox'],
  3084. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3085. 'label' => ['for' => 'articles-active'],
  3086. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3087. 'Active',
  3088. '/label',
  3089. '/div',
  3090. ];
  3091. $this->assertHtml($expected, $result);
  3092. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3093. $expected = [
  3094. 'div' => ['class' => 'input checkbox'],
  3095. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3096. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3097. '/div',
  3098. ];
  3099. $this->assertHtml($expected, $result);
  3100. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3101. $expected = [
  3102. 'div' => ['class' => 'input checkbox'],
  3103. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3104. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3105. '/div',
  3106. ];
  3107. $this->assertHtml($expected, $result);
  3108. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3109. $expected = [
  3110. 'div' => ['class' => 'input checkbox'],
  3111. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3112. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3113. '/div',
  3114. ];
  3115. $this->assertHtml($expected, $result);
  3116. $result = $this->Form->control('Articles.disabled', [
  3117. 'label' => 'Disabled',
  3118. 'type' => 'checkbox',
  3119. 'data-foo' => 'disabled',
  3120. ]);
  3121. $expected = [
  3122. 'div' => ['class' => 'input checkbox'],
  3123. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3124. 'label' => ['for' => 'articles-disabled'],
  3125. ['input' => [
  3126. 'type' => 'checkbox',
  3127. 'name' => 'Articles[disabled]',
  3128. 'value' => '1',
  3129. 'id' => 'articles-disabled',
  3130. 'data-foo' => 'disabled',
  3131. ]],
  3132. 'Disabled',
  3133. '/label',
  3134. '/div',
  3135. ];
  3136. $this->assertHtml($expected, $result);
  3137. $result = $this->Form->control('Articles.confirm', [
  3138. 'label' => 'Confirm <b>me</b>!',
  3139. 'type' => 'checkbox',
  3140. 'escape' => false,
  3141. ]);
  3142. $expected = [
  3143. 'div' => ['class' => 'input checkbox'],
  3144. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3145. 'label' => ['for' => 'articles-confirm'],
  3146. ['input' => [
  3147. 'type' => 'checkbox',
  3148. 'name' => 'Articles[confirm]',
  3149. 'value' => '1',
  3150. 'id' => 'articles-confirm',
  3151. ]],
  3152. 'Confirm <b>me</b>!',
  3153. '/label',
  3154. '/div',
  3155. ];
  3156. $this->assertHtml($expected, $result);
  3157. }
  3158. /**
  3159. * testControlHidden method
  3160. *
  3161. * Test that control() does not create wrapping div and label tag for hidden fields.
  3162. *
  3163. * @return void
  3164. */
  3165. public function testControlHidden()
  3166. {
  3167. $this->getTableLocator()->get('ValidateUsers', [
  3168. 'className' => __NAMESPACE__ . '\ValidateUsersTable',
  3169. ]);
  3170. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3171. $result = $this->Form->control('ValidateUser.id');
  3172. $expected = [
  3173. 'input' => ['name', 'type' => 'hidden', 'id'],
  3174. ];
  3175. $this->assertHtml($expected, $result);
  3176. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3177. $expected = [
  3178. 'input' => ['name', 'type' => 'hidden', 'id'],
  3179. ];
  3180. $this->assertHtml($expected, $result);
  3181. }
  3182. /**
  3183. * testControlDatetime method
  3184. *
  3185. * Test form->control() with datetime.
  3186. *
  3187. * @return void
  3188. */
  3189. public function testControlDatetime()
  3190. {
  3191. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3192. ->setMethods(['datetime'])
  3193. ->setConstructorArgs([new View()])
  3194. ->getMock();
  3195. $this->Form->expects($this->once())->method('datetime')
  3196. ->with('prueba', [
  3197. 'type' => 'datetime',
  3198. 'timeFormat' => 24,
  3199. 'minYear' => 2008,
  3200. 'maxYear' => 2011,
  3201. 'interval' => 15,
  3202. 'options' => null,
  3203. 'empty' => false,
  3204. 'id' => 'prueba',
  3205. 'required' => false,
  3206. 'templateVars' => [],
  3207. ])
  3208. ->will($this->returnValue('This is it!'));
  3209. $result = $this->Form->control('prueba', [
  3210. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3211. 'maxYear' => 2011, 'interval' => 15,
  3212. ]);
  3213. $expected = [
  3214. 'div' => ['class' => 'input datetime'],
  3215. '<label',
  3216. 'Prueba',
  3217. '/label',
  3218. 'This is it!',
  3219. '/div',
  3220. ];
  3221. $this->assertHtml($expected, $result);
  3222. }
  3223. /**
  3224. * testControlDatetimeIdPrefix method
  3225. *
  3226. * Test form->control() with datetime with id prefix.
  3227. *
  3228. * @return void
  3229. */
  3230. public function testControlDatetimeIdPrefix()
  3231. {
  3232. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3233. ->setMethods(['datetime'])
  3234. ->setConstructorArgs([new View()])
  3235. ->getMock();
  3236. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3237. $this->Form->expects($this->once())->method('datetime')
  3238. ->with('prueba', [
  3239. 'type' => 'datetime',
  3240. 'timeFormat' => 24,
  3241. 'minYear' => 2008,
  3242. 'maxYear' => 2011,
  3243. 'interval' => 15,
  3244. 'options' => null,
  3245. 'empty' => false,
  3246. 'id' => 'prefix-prueba',
  3247. 'required' => false,
  3248. 'templateVars' => [],
  3249. ])
  3250. ->will($this->returnValue('This is it!'));
  3251. $result = $this->Form->control('prueba', [
  3252. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3253. 'maxYear' => 2011, 'interval' => 15,
  3254. ]);
  3255. $expected = [
  3256. 'div' => ['class' => 'input datetime'],
  3257. '<label',
  3258. 'Prueba',
  3259. '/label',
  3260. 'This is it!',
  3261. '/div',
  3262. ];
  3263. $this->assertHtml($expected, $result);
  3264. }
  3265. /**
  3266. * testControlCheckboxWithDisabledElements method
  3267. *
  3268. * Test generating checkboxes with disabled elements.
  3269. *
  3270. * @return void
  3271. */
  3272. public function testControlCheckboxWithDisabledElements()
  3273. {
  3274. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3275. $result = $this->Form->control('Contact.multiple', [
  3276. 'multiple' => 'checkbox',
  3277. 'disabled' => 'disabled',
  3278. 'options' => $options,
  3279. ]);
  3280. $expected = [
  3281. ['div' => ['class' => 'input select']],
  3282. ['label' => ['for' => 'contact-multiple']],
  3283. 'Multiple',
  3284. '/label',
  3285. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3286. ['div' => ['class' => 'checkbox']],
  3287. ['label' => ['for' => 'contact-multiple-1']],
  3288. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3289. 'One',
  3290. '/label',
  3291. '/div',
  3292. ['div' => ['class' => 'checkbox']],
  3293. ['label' => ['for' => 'contact-multiple-2']],
  3294. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3295. 'Two',
  3296. '/label',
  3297. '/div',
  3298. ['div' => ['class' => 'checkbox']],
  3299. ['label' => ['for' => 'contact-multiple-3']],
  3300. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3301. 'Three',
  3302. '/label',
  3303. '/div',
  3304. '/div',
  3305. ];
  3306. $this->assertHtml($expected, $result);
  3307. // make sure 50 does only disable 50, and not 50f5c0cf
  3308. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3309. $disabled = [50];
  3310. $expected = [
  3311. ['div' => ['class' => 'input select']],
  3312. ['label' => ['for' => 'contact-multiple']],
  3313. 'Multiple',
  3314. '/label',
  3315. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3316. ['div' => ['class' => 'checkbox']],
  3317. ['label' => ['for' => 'contact-multiple-50']],
  3318. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3319. 'Fifty',
  3320. '/label',
  3321. '/div',
  3322. ['div' => ['class' => 'checkbox']],
  3323. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3324. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3325. 'Stringy',
  3326. '/label',
  3327. '/div',
  3328. '/div',
  3329. ];
  3330. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3331. $this->assertHtml($expected, $result);
  3332. }
  3333. /**
  3334. * testControlWithLeadingInteger method
  3335. *
  3336. * Test input name with leading integer, ensure attributes are generated correctly.
  3337. *
  3338. * @return void
  3339. */
  3340. public function testControlWithLeadingInteger()
  3341. {
  3342. $result = $this->Form->text('0.Node.title');
  3343. $expected = [
  3344. 'input' => ['name' => '0[Node][title]', 'type' => 'text'],
  3345. ];
  3346. $this->assertHtml($expected, $result);
  3347. }
  3348. /**
  3349. * testControlSelectType method
  3350. *
  3351. * Test form->control() with select type inputs.
  3352. *
  3353. * @return void
  3354. */
  3355. public function testControlSelectType()
  3356. {
  3357. $result = $this->Form->control(
  3358. 'email',
  3359. [
  3360. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3361. );
  3362. $expected = [
  3363. 'div' => ['class' => 'input select'],
  3364. 'label' => ['for' => 'email'],
  3365. 'Email',
  3366. '/label',
  3367. ['select' => ['name' => 'email', 'id' => 'email']],
  3368. ['option' => ['value' => '']],
  3369. '/option',
  3370. ['option' => ['value' => 'è']],
  3371. 'Firést',
  3372. '/option',
  3373. ['option' => ['value' => 'é']],
  3374. 'Secoènd',
  3375. '/option',
  3376. '/select',
  3377. '/div',
  3378. ];
  3379. $this->assertHtml($expected, $result);
  3380. $result = $this->Form->control(
  3381. 'email',
  3382. [
  3383. 'options' => ['First', 'Second'], 'empty' => true]
  3384. );
  3385. $expected = [
  3386. 'div' => ['class' => 'input select'],
  3387. 'label' => ['for' => 'email'],
  3388. 'Email',
  3389. '/label',
  3390. ['select' => ['name' => 'email', 'id' => 'email']],
  3391. ['option' => ['value' => '']],
  3392. '/option',
  3393. ['option' => ['value' => '0']],
  3394. 'First',
  3395. '/option',
  3396. ['option' => ['value' => '1']],
  3397. 'Second',
  3398. '/option',
  3399. '/select',
  3400. '/div',
  3401. ];
  3402. $this->assertHtml($expected, $result);
  3403. $result = $this->Form->control('email', [
  3404. 'type' => 'select',
  3405. 'options' => new \ArrayObject(['First', 'Second']),
  3406. 'empty' => true,
  3407. ]);
  3408. $this->assertHtml($expected, $result);
  3409. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3410. $this->View->setRequest(
  3411. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3412. );
  3413. $this->Form->create();
  3414. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3415. $expected = [
  3416. 'div' => ['class' => 'input select'],
  3417. 'label' => ['for' => 'model-user-id'],
  3418. 'User',
  3419. '/label',
  3420. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3421. ['option' => ['value' => '']],
  3422. '/option',
  3423. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3424. 'good',
  3425. '/option',
  3426. ['option' => ['value' => 'other']],
  3427. 'bad',
  3428. '/option',
  3429. '/select',
  3430. '/div',
  3431. ];
  3432. $this->assertHtml($expected, $result);
  3433. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3434. $this->View->setRequest(
  3435. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3436. );
  3437. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3438. $expected = [
  3439. 'div' => ['class' => 'input select'],
  3440. 'label' => ['for' => 'thing-user-id'],
  3441. 'User',
  3442. '/label',
  3443. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3444. ['option' => ['value' => '']],
  3445. 'Some Empty',
  3446. '/option',
  3447. ['option' => ['value' => 'value']],
  3448. 'good',
  3449. '/option',
  3450. ['option' => ['value' => 'other']],
  3451. 'bad',
  3452. '/option',
  3453. '/select',
  3454. '/div',
  3455. ];
  3456. $this->assertHtml($expected, $result);
  3457. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3458. $this->View->setRequest(
  3459. $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
  3460. );
  3461. $this->Form->create();
  3462. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3463. $expected = [
  3464. 'div' => ['class' => 'input select'],
  3465. 'label' => ['for' => 'thing-user-id'],
  3466. 'User',
  3467. '/label',
  3468. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3469. ['option' => ['value' => '']],
  3470. 'Some Empty',
  3471. '/option',
  3472. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3473. 'good',
  3474. '/option',
  3475. ['option' => ['value' => 'other']],
  3476. 'bad',
  3477. '/option',
  3478. '/select',
  3479. '/div',
  3480. ];
  3481. $this->assertHtml($expected, $result);
  3482. $result = $this->Form->control('Publisher.id', [
  3483. 'label' => 'Publisher',
  3484. 'type' => 'select',
  3485. 'multiple' => 'checkbox',
  3486. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2'],
  3487. ]);
  3488. $expected = [
  3489. ['div' => ['class' => 'input select']],
  3490. ['label' => ['for' => 'publisher-id']],
  3491. 'Publisher',
  3492. '/label',
  3493. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3494. ['div' => ['class' => 'checkbox']],
  3495. ['label' => ['for' => 'publisher-id-value-1']],
  3496. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3497. 'Label 1',
  3498. '/label',
  3499. '/div',
  3500. ['div' => ['class' => 'checkbox']],
  3501. ['label' => ['for' => 'publisher-id-value-2']],
  3502. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3503. 'Label 2',
  3504. '/label',
  3505. '/div',
  3506. '/div',
  3507. ];
  3508. $this->assertHtml($expected, $result);
  3509. }
  3510. /**
  3511. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3512. *
  3513. * Test that control() and a non standard primary key makes a hidden input by default.
  3514. *
  3515. * @return void
  3516. */
  3517. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3518. {
  3519. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3520. $this->Form->create($this->article);
  3521. $result = $this->Form->control('title');
  3522. $expected = [
  3523. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3524. ];
  3525. $this->assertHtml($expected, $result);
  3526. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3527. $this->Form->create($this->article);
  3528. $result = $this->Form->control('title');
  3529. $expected = [
  3530. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3531. ];
  3532. $this->assertHtml($expected, $result);
  3533. $result = $this->Form->control('body');
  3534. $expected = [
  3535. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3536. ];
  3537. $this->assertHtml($expected, $result);
  3538. }
  3539. /**
  3540. * testControlOverridingMagicSelectType method
  3541. *
  3542. * Test that overriding the magic select type widget is possible.
  3543. *
  3544. * @return void
  3545. */
  3546. public function testControlOverridingMagicSelectType()
  3547. {
  3548. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3549. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3550. $expected = [
  3551. 'div' => ['class' => 'input text'],
  3552. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3553. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3554. '/div',
  3555. ];
  3556. $this->assertHtml($expected, $result);
  3557. //Check that magic types still work for plural/singular vars
  3558. $this->View->set('types', ['value' => 'good', 'other' => 'bad']);
  3559. $result = $this->Form->control('Model.type');
  3560. $expected = [
  3561. 'div' => ['class' => 'input select'],
  3562. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3563. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3564. ['option' => ['value' => 'value']], 'good', '/option',
  3565. ['option' => ['value' => 'other']], 'bad', '/option',
  3566. '/select',
  3567. '/div',
  3568. ];
  3569. $this->assertHtml($expected, $result);
  3570. }
  3571. /**
  3572. * testControlMagicTypeDoesNotOverride method
  3573. *
  3574. * Test that inferred types do not override developer input.
  3575. *
  3576. * @return void
  3577. */
  3578. public function testControlMagicTypeDoesNotOverride()
  3579. {
  3580. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3581. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3582. $expected = [
  3583. 'div' => ['class' => 'input checkbox'],
  3584. ['input' => [
  3585. 'type' => 'hidden',
  3586. 'name' => 'Model[user]',
  3587. 'value' => 0,
  3588. ]],
  3589. 'label' => ['for' => 'model-user'],
  3590. ['input' => [
  3591. 'name' => 'Model[user]',
  3592. 'type' => 'checkbox',
  3593. 'id' => 'model-user',
  3594. 'value' => 1,
  3595. ]],
  3596. 'User',
  3597. '/label',
  3598. '/div',
  3599. ];
  3600. $this->assertHtml($expected, $result);
  3601. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3602. $options = [
  3603. 1 => 'blue',
  3604. 2 => 'red',
  3605. ];
  3606. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3607. $expected = [
  3608. 'div' => ['class' => 'input select'],
  3609. 'label' => ['for' => 'tags-ids'],
  3610. 'Tags',
  3611. '/label',
  3612. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3613. ['div' => ['class' => 'checkbox']],
  3614. ['label' => ['for' => 'tags-ids-1']],
  3615. ['input' => [
  3616. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3617. 'value' => '1', 'name' => 'tags[_ids][]',
  3618. ]],
  3619. 'blue',
  3620. '/label',
  3621. '/div',
  3622. ['div' => ['class' => 'checkbox']],
  3623. ['label' => ['for' => 'tags-ids-2']],
  3624. ['input' => [
  3625. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3626. 'value' => '2', 'name' => 'tags[_ids][]',
  3627. ]],
  3628. 'red',
  3629. '/label',
  3630. '/div',
  3631. '/div',
  3632. ];
  3633. $this->assertHtml($expected, $result);
  3634. }
  3635. /**
  3636. * testControlMagicSelectForTypeNumber method
  3637. *
  3638. * Test that magic control() selects are created for type=number.
  3639. *
  3640. * @return void
  3641. */
  3642. public function testControlMagicSelectForTypeNumber()
  3643. {
  3644. $this->getTableLocator()->get('ValidateUsers', [
  3645. 'className' => __NAMESPACE__ . '\ValidateUsersTable',
  3646. ]);
  3647. $entity = new Entity(['balance' => 1]);
  3648. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3649. $this->View->set('balances', [0 => 'nothing', 1 => 'some', 100 => 'a lot']);
  3650. $result = $this->Form->control('balance');
  3651. $expected = [
  3652. 'div' => ['class' => 'input select'],
  3653. 'label' => ['for' => 'balance'],
  3654. 'Balance',
  3655. '/label',
  3656. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3657. ['option' => ['value' => '0']],
  3658. 'nothing',
  3659. '/option',
  3660. ['option' => ['value' => '1', 'selected' => 'selected']],
  3661. 'some',
  3662. '/option',
  3663. ['option' => ['value' => '100']],
  3664. 'a lot',
  3665. '/option',
  3666. '/select',
  3667. '/div',
  3668. ];
  3669. $this->assertHtml($expected, $result);
  3670. }
  3671. /**
  3672. * testInvalidControlTypeOption method
  3673. *
  3674. * Test invalid 'input' type option to control() function.
  3675. *
  3676. * @return void
  3677. */
  3678. public function testInvalidControlTypeOption()
  3679. {
  3680. $this->expectException(\RuntimeException::class);
  3681. $this->expectExceptionMessage('Invalid type \'input\' used for field \'text\'');
  3682. $this->Form->control('text', ['type' => 'input']);
  3683. }
  3684. /**
  3685. * testControlMagicSelectChangeToRadio method
  3686. *
  3687. * Test that magic control() selects can easily be converted into radio types without error.
  3688. *
  3689. * @return void
  3690. */
  3691. public function testControlMagicSelectChangeToRadio()
  3692. {
  3693. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3694. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3695. $this->assertContains('input type="radio"', $result);
  3696. }
  3697. /**
  3698. * testFormControlSubmit method
  3699. *
  3700. * Test correct results for form::control() and type submit.
  3701. *
  3702. * @return void
  3703. */
  3704. public function testFormControlSubmit()
  3705. {
  3706. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3707. $expected = [
  3708. 'div' => ['class' => 'submit'],
  3709. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3710. '/div',
  3711. ];
  3712. $this->assertHtml($expected, $result);
  3713. }
  3714. /**
  3715. * testFormControls method
  3716. *
  3717. * Test correct results from Form::controls().
  3718. *
  3719. * @return void
  3720. */
  3721. public function testFormControlsLegendFieldset()
  3722. {
  3723. $this->Form->create($this->article);
  3724. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3725. $expected = [
  3726. '<fieldset',
  3727. '<legend',
  3728. 'The Legend',
  3729. '/legend',
  3730. '*/fieldset',
  3731. ];
  3732. $this->assertHtml($expected, $result);
  3733. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3734. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3735. $this->assertContains('<fieldset>', $result);
  3736. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3737. $this->assertNotContains('<legend>', $result);
  3738. $this->assertNotContains('<fieldset>', $result);
  3739. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3740. $this->assertNotContains('<legend>', $result);
  3741. $this->assertNotContains('<fieldset>', $result);
  3742. $this->Form->create($this->article);
  3743. $this->View->setRequest($this->View->getRequest()
  3744. ->withParam('prefix', 'admin')
  3745. ->withParam('action', 'admin_edit')
  3746. ->withParam('controller', 'articles'));
  3747. $result = $this->Form->allControls();
  3748. $expected = [
  3749. '<fieldset',
  3750. '<legend',
  3751. 'New Article',
  3752. '/legend',
  3753. '*/fieldset',
  3754. ];
  3755. $this->assertHtml($expected, $result);
  3756. $this->Form->create($this->article);
  3757. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3758. $expected = [
  3759. '<fieldset',
  3760. '<legend',
  3761. 'The Legend',
  3762. '/legend',
  3763. '*/fieldset',
  3764. ];
  3765. $this->assertHtml($expected, $result);
  3766. $this->Form->create($this->article);
  3767. $result = $this->Form->allControls([], [
  3768. 'fieldset' => [
  3769. 'class' => 'some-class some-other-class',
  3770. 'disabled' => true,
  3771. 'data-param' => 'a-param',
  3772. ],
  3773. 'legend' => 'The Legend',
  3774. ]);
  3775. $expected = [
  3776. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3777. '<legend',
  3778. 'The Legend',
  3779. '/legend',
  3780. '*/fieldset',
  3781. ];
  3782. $this->assertHtml($expected, $result);
  3783. }
  3784. /**
  3785. * testFormControls method
  3786. *
  3787. * Test the controls() method.
  3788. *
  3789. * @return void
  3790. */
  3791. public function testFormControls()
  3792. {
  3793. $this->Form->create($this->article);
  3794. $result = $this->Form->allControls();
  3795. $expected = [
  3796. '<fieldset',
  3797. '<legend', 'New Article', '/legend',
  3798. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3799. ['div' => ['class' => 'input select required']],
  3800. '*/div',
  3801. ['div' => ['class' => 'input text required']],
  3802. '*/div',
  3803. ['div' => ['class' => 'input text']],
  3804. '*/div',
  3805. ['div' => ['class' => 'input text']],
  3806. '*/div',
  3807. '/fieldset',
  3808. ];
  3809. $this->assertHtml($expected, $result);
  3810. $result = $this->Form->allControls([
  3811. 'published' => ['type' => 'boolean'],
  3812. ]);
  3813. $expected = [
  3814. '<fieldset',
  3815. '<legend', 'New Article', '/legend',
  3816. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3817. ['div' => ['class' => 'input select required']],
  3818. '*/div',
  3819. ['div' => ['class' => 'input text required']],
  3820. '*/div',
  3821. ['div' => ['class' => 'input text']],
  3822. '*/div',
  3823. ['div' => ['class' => 'input boolean']],
  3824. '*/div',
  3825. '/fieldset',
  3826. ];
  3827. $this->assertHtml($expected, $result);
  3828. $this->Form->create($this->article);
  3829. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3830. $expected = [
  3831. 'fieldset' => [],
  3832. 'legend' => [],
  3833. 'Hello',
  3834. '/legend',
  3835. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3836. ['div' => ['class' => 'input select required']],
  3837. '*/div',
  3838. ['div' => ['class' => 'input text required']],
  3839. '*/div',
  3840. ['div' => ['class' => 'input text']],
  3841. '*/div',
  3842. ['div' => ['class' => 'input text']],
  3843. '*/div',
  3844. '/fieldset',
  3845. ];
  3846. $this->assertHtml($expected, $result);
  3847. $this->Form->create(false);
  3848. $expected = [
  3849. 'fieldset' => [],
  3850. ['div' => ['class' => 'input text']],
  3851. 'label' => ['for' => 'foo'],
  3852. 'Foo',
  3853. '/label',
  3854. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3855. '*/div',
  3856. '/fieldset',
  3857. ];
  3858. $result = $this->Form->allControls(
  3859. ['foo' => ['type' => 'text']],
  3860. ['legend' => false]
  3861. );
  3862. $this->assertHtml($expected, $result);
  3863. }
  3864. /**
  3865. * testFormControlsBlacklist method
  3866. *
  3867. * @return void
  3868. */
  3869. public function testFormControlsBlacklist()
  3870. {
  3871. $this->Form->create($this->article);
  3872. $result = $this->Form->allControls([
  3873. 'id' => false,
  3874. ]);
  3875. $expected = [
  3876. '<fieldset',
  3877. '<legend', 'New Article', '/legend',
  3878. ['div' => ['class' => 'input select required']],
  3879. '*/div',
  3880. ['div' => ['class' => 'input text required']],
  3881. '*/div',
  3882. ['div' => ['class' => 'input text']],
  3883. '*/div',
  3884. ['div' => ['class' => 'input text']],
  3885. '*/div',
  3886. '/fieldset',
  3887. ];
  3888. $this->assertHtml($expected, $result);
  3889. $this->Form->create($this->article);
  3890. $result = $this->Form->allControls([
  3891. 'id' => [],
  3892. ]);
  3893. $expected = [
  3894. '<fieldset',
  3895. '<legend', 'New Article', '/legend',
  3896. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3897. ['div' => ['class' => 'input select required']],
  3898. '*/div',
  3899. ['div' => ['class' => 'input text required']],
  3900. '*/div',
  3901. ['div' => ['class' => 'input text']],
  3902. '*/div',
  3903. ['div' => ['class' => 'input text']],
  3904. '*/div',
  3905. '/fieldset',
  3906. ];
  3907. $this->assertHtml($expected, $result, true);
  3908. }
  3909. /**
  3910. * testSelectAsCheckbox method
  3911. *
  3912. * Test multi-select widget with checkbox formatting.
  3913. *
  3914. * @return void
  3915. */
  3916. public function testSelectAsCheckbox()
  3917. {
  3918. $result = $this->Form->select(
  3919. 'Model.multi_field',
  3920. ['first', 'second', 'third'],
  3921. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3922. );
  3923. $expected = [
  3924. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3925. ['div' => ['class' => 'checkbox']],
  3926. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3927. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3928. 'first',
  3929. '/label',
  3930. '/div',
  3931. ['div' => ['class' => 'checkbox']],
  3932. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3933. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3934. 'second',
  3935. '/label',
  3936. '/div',
  3937. ['div' => ['class' => 'checkbox']],
  3938. ['label' => ['for' => 'model-multi-field-2']],
  3939. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3940. 'third',
  3941. '/label',
  3942. '/div',
  3943. ];
  3944. $this->assertHtml($expected, $result);
  3945. $result = $this->Form->select(
  3946. 'Model.multi_field',
  3947. ['1/2' => 'half'],
  3948. ['multiple' => 'checkbox']
  3949. );
  3950. $expected = [
  3951. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3952. ['div' => ['class' => 'checkbox']],
  3953. ['label' => ['for' => 'model-multi-field-1-2']],
  3954. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3955. 'half',
  3956. '/label',
  3957. '/div',
  3958. ];
  3959. $this->assertHtml($expected, $result);
  3960. }
  3961. /**
  3962. * testLabel method
  3963. *
  3964. * Test label generation.
  3965. *
  3966. * @return void
  3967. */
  3968. public function testLabel()
  3969. {
  3970. $result = $this->Form->label('Person.name');
  3971. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3972. $this->assertHtml($expected, $result);
  3973. $result = $this->Form->label('Person.name');
  3974. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3975. $this->assertHtml($expected, $result);
  3976. $result = $this->Form->label('Person.first_name');
  3977. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3978. $this->assertHtml($expected, $result);
  3979. $result = $this->Form->label('Person.first_name', 'Your first name');
  3980. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3981. $this->assertHtml($expected, $result);
  3982. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3983. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3984. $this->assertHtml($expected, $result);
  3985. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3986. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3987. $this->assertHtml($expected, $result);
  3988. $result = $this->Form->label('Person.first_name', '');
  3989. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3990. $this->assertHtml($expected, $result);
  3991. $result = $this->Form->label('Person.2.name', '');
  3992. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3993. $this->assertHtml($expected, $result);
  3994. }
  3995. /**
  3996. * testLabelContainControl method
  3997. *
  3998. * Test that label() can accept an input with the correct template vars.
  3999. *
  4000. * @return void
  4001. */
  4002. public function testLabelContainControl()
  4003. {
  4004. $this->Form->setTemplates([
  4005. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4006. ]);
  4007. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  4008. 'input' => '<input type="checkbox" name="accept_tos"/>',
  4009. ]);
  4010. $expected = [
  4011. 'label' => ['for' => 'person-accept-terms'],
  4012. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  4013. 'Accept',
  4014. '/label',
  4015. ];
  4016. $this->assertHtml($expected, $result);
  4017. }
  4018. /**
  4019. * testTextbox method
  4020. *
  4021. * Test textbox element generation.
  4022. *
  4023. * @return void
  4024. */
  4025. public function testTextbox()
  4026. {
  4027. $result = $this->Form->text('Model.field');
  4028. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  4029. $this->assertHtml($expected, $result);
  4030. $result = $this->Form->text('Model.field', ['type' => 'password']);
  4031. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  4032. $this->assertHtml($expected, $result);
  4033. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  4034. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  4035. $this->assertHtml($expected, $result);
  4036. }
  4037. /**
  4038. * testTextBoxDataAndError method
  4039. *
  4040. * Test that text() hooks up with request data and error fields.
  4041. *
  4042. * @return void
  4043. */
  4044. public function testTextBoxDataAndError()
  4045. {
  4046. $this->article['errors'] = [
  4047. 'Contact' => ['text' => 'wrong'],
  4048. ];
  4049. $this->View->setRequest($this->View->getRequest()
  4050. ->withData('Model.text', 'test <strong>HTML</strong> values')
  4051. ->withData('Contact.text', 'test'));
  4052. $this->Form->create($this->article);
  4053. $result = $this->Form->text('Model.text');
  4054. $expected = [
  4055. 'input' => [
  4056. 'type' => 'text',
  4057. 'name' => 'Model[text]',
  4058. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  4059. ],
  4060. ];
  4061. $this->assertHtml($expected, $result);
  4062. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  4063. $expected = [
  4064. 'input' => [
  4065. 'type' => 'text',
  4066. 'name' => 'Contact[text]',
  4067. 'value' => 'test',
  4068. 'id' => 'theID',
  4069. 'class' => 'form-error',
  4070. ],
  4071. ];
  4072. $this->assertHtml($expected, $result);
  4073. }
  4074. /**
  4075. * testDefaultValue method
  4076. *
  4077. * Test default value setting.
  4078. *
  4079. * @return void
  4080. */
  4081. public function testTextDefaultValue()
  4082. {
  4083. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  4084. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4085. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  4086. $this->assertHtml($expected, $result);
  4087. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4088. $this->Form->create();
  4089. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4090. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4091. $this->assertHtml($expected, $result);
  4092. $Articles = $this->getTableLocator()->get('Articles');
  4093. $title = $Articles->getSchema()->getColumn('title');
  4094. $Articles->getSchema()->addColumn(
  4095. 'title',
  4096. ['default' => 'default title'] + $title
  4097. );
  4098. $entity = $Articles->newEntity();
  4099. $this->Form->create($entity);
  4100. // Get default value from schema
  4101. $result = $this->Form->text('title');
  4102. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4103. $this->assertHtml($expected, $result);
  4104. // Don't get value from schema
  4105. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4106. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4107. $this->assertHtml($expected, $result);
  4108. // Custom default value overrides default value from schema
  4109. $result = $this->Form->text('title', ['default' => 'override default']);
  4110. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4111. $this->assertHtml($expected, $result);
  4112. // Default value from schema is used only for new entities.
  4113. $entity->isNew(false);
  4114. $result = $this->Form->text('title');
  4115. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4116. $this->assertHtml($expected, $result);
  4117. }
  4118. /**
  4119. * testError method
  4120. *
  4121. * Test field error generation.
  4122. *
  4123. * @return void
  4124. */
  4125. public function testError()
  4126. {
  4127. $this->article['errors'] = [
  4128. 'Article' => ['field' => 'email'],
  4129. ];
  4130. $this->Form->create($this->article);
  4131. $result = $this->Form->error('Article.field');
  4132. $expected = [
  4133. ['div' => ['class' => 'error-message']],
  4134. 'email',
  4135. '/div',
  4136. ];
  4137. $this->assertHtml($expected, $result);
  4138. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4139. $expected = [
  4140. ['div' => ['class' => 'error-message']],
  4141. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4142. '/div',
  4143. ];
  4144. $this->assertHtml($expected, $result);
  4145. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4146. $expected = [
  4147. ['div' => ['class' => 'error-message']],
  4148. '<strong', 'Badness!', '/strong',
  4149. '/div',
  4150. ];
  4151. $this->assertHtml($expected, $result);
  4152. }
  4153. /**
  4154. * testErrorRuleName method
  4155. *
  4156. * Test error translation can use rule names for translating.
  4157. *
  4158. * @return void
  4159. */
  4160. public function testErrorRuleName()
  4161. {
  4162. $this->article['errors'] = [
  4163. 'Article' => [
  4164. 'field' => ['email' => 'Your email was not good'],
  4165. ],
  4166. ];
  4167. $this->Form->create($this->article);
  4168. $result = $this->Form->error('Article.field');
  4169. $expected = [
  4170. ['div' => ['class' => 'error-message']],
  4171. 'Your email was not good',
  4172. '/div',
  4173. ];
  4174. $this->assertHtml($expected, $result);
  4175. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4176. $expected = [
  4177. ['div' => ['class' => 'error-message']],
  4178. 'Email in use',
  4179. '/div',
  4180. ];
  4181. $this->assertHtml($expected, $result);
  4182. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4183. $expected = [
  4184. ['div' => ['class' => 'error-message']],
  4185. 'Email in use',
  4186. '/div',
  4187. ];
  4188. $this->assertHtml($expected, $result);
  4189. $result = $this->Form->error('Article.field', [
  4190. 'email' => 'Key is preferred',
  4191. 'Your email was not good' => 'Email in use',
  4192. ]);
  4193. $expected = [
  4194. ['div' => ['class' => 'error-message']],
  4195. 'Key is preferred',
  4196. '/div',
  4197. ];
  4198. $this->assertHtml($expected, $result);
  4199. }
  4200. /**
  4201. * testErrorMessages method
  4202. *
  4203. * Test error with nested lists.
  4204. *
  4205. * @return void
  4206. */
  4207. public function testErrorMessages()
  4208. {
  4209. $this->article['errors'] = [
  4210. 'Article' => ['field' => 'email'],
  4211. ];
  4212. $this->Form->create($this->article);
  4213. $result = $this->Form->error('Article.field', [
  4214. 'email' => 'No good!',
  4215. ]);
  4216. $expected = [
  4217. 'div' => ['class' => 'error-message'],
  4218. 'No good!',
  4219. '/div',
  4220. ];
  4221. $this->assertHtml($expected, $result);
  4222. }
  4223. /**
  4224. * testErrorMultipleMessages method
  4225. *
  4226. * Test error() with multiple messages.
  4227. *
  4228. * @return void
  4229. */
  4230. public function testErrorMultipleMessages()
  4231. {
  4232. $this->article['errors'] = [
  4233. 'field' => ['notBlank', 'email', 'Something else'],
  4234. ];
  4235. $this->Form->create($this->article);
  4236. $result = $this->Form->error('field', [
  4237. 'notBlank' => 'Cannot be empty',
  4238. 'email' => 'No good!',
  4239. ]);
  4240. $expected = [
  4241. 'div' => ['class' => 'error-message'],
  4242. 'ul' => [],
  4243. '<li', 'Cannot be empty', '/li',
  4244. '<li', 'No good!', '/li',
  4245. '<li', 'Something else', '/li',
  4246. '/ul',
  4247. '/div',
  4248. ];
  4249. $this->assertHtml($expected, $result);
  4250. }
  4251. /**
  4252. * testPassword method
  4253. *
  4254. * Test password element generation.
  4255. *
  4256. * @return void
  4257. */
  4258. public function testPassword()
  4259. {
  4260. $this->article['errors'] = [
  4261. 'Contact' => [
  4262. 'passwd' => 1,
  4263. ],
  4264. ];
  4265. $this->Form->create($this->article);
  4266. $result = $this->Form->password('Contact.field');
  4267. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4268. $this->assertHtml($expected, $result);
  4269. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4270. $this->Form->create($this->article);
  4271. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4272. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4273. $this->assertHtml($expected, $result);
  4274. }
  4275. /**
  4276. * testRadio method
  4277. *
  4278. * Test radio element set generation.
  4279. *
  4280. * @return void
  4281. */
  4282. public function testRadio()
  4283. {
  4284. $result = $this->Form->radio('Model.field', ['option A']);
  4285. $expected = [
  4286. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4287. 'label' => ['for' => 'model-field-0'],
  4288. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4289. 'option A',
  4290. '/label',
  4291. ];
  4292. $this->assertHtml($expected, $result);
  4293. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4294. $this->assertHtml($expected, $result);
  4295. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4296. $expected = [
  4297. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4298. ['label' => ['for' => 'model-field-0']],
  4299. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4300. 'option A',
  4301. '/label',
  4302. ['label' => ['for' => 'model-field-1']],
  4303. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4304. 'option B',
  4305. '/label',
  4306. ];
  4307. $this->assertHtml($expected, $result);
  4308. $result = $this->Form->radio(
  4309. 'Employee.gender',
  4310. ['male' => 'Male', 'female' => 'Female'],
  4311. ['form' => 'my-form']
  4312. );
  4313. $expected = [
  4314. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4315. ['label' => ['for' => 'employee-gender-male']],
  4316. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4317. 'Male',
  4318. '/label',
  4319. ['label' => ['for' => 'employee-gender-female']],
  4320. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4321. 'Female',
  4322. '/label',
  4323. ];
  4324. $this->assertHtml($expected, $result);
  4325. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4326. $expected = [
  4327. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4328. ['label' => ['for' => 'model-custom-0']],
  4329. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4330. 'option A',
  4331. '/label',
  4332. ['label' => ['for' => 'model-custom-1']],
  4333. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4334. 'option B',
  4335. '/label',
  4336. ];
  4337. $this->assertHtml($expected, $result);
  4338. $result = $this->Form->radio(
  4339. 'Employee.gender',
  4340. [
  4341. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4342. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4343. ]
  4344. );
  4345. $expected = [
  4346. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4347. ['label' => ['for' => 'employee-gender-male']],
  4348. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4349. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4350. 'Male',
  4351. '/label',
  4352. ['label' => ['for' => 'employee-gender-female']],
  4353. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4354. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4355. 'Female',
  4356. '/label',
  4357. ];
  4358. $this->assertHtml($expected, $result);
  4359. }
  4360. /**
  4361. * Test radio with complex options and empty disabled data.
  4362. *
  4363. * @return void
  4364. */
  4365. public function testRadioComplexDisabled()
  4366. {
  4367. $options = [
  4368. ['value' => 'r', 'text' => 'red'],
  4369. ['value' => 'b', 'text' => 'blue'],
  4370. ];
  4371. $attrs = ['disabled' => []];
  4372. $result = $this->Form->radio('Model.field', $options, $attrs);
  4373. $expected = [
  4374. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4375. ['label' => ['for' => 'model-field-r']],
  4376. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4377. 'red',
  4378. '/label',
  4379. ['label' => ['for' => 'model-field-b']],
  4380. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4381. 'blue',
  4382. '/label',
  4383. ];
  4384. $this->assertHtml($expected, $result);
  4385. $attrs = ['disabled' => ['r']];
  4386. $result = $this->Form->radio('Model.field', $options, $attrs);
  4387. $this->assertContains('disabled="disabled"', $result);
  4388. }
  4389. /**
  4390. * testRadioDefaultValue method
  4391. *
  4392. * Test default value setting on radio() method.
  4393. *
  4394. * @return void
  4395. */
  4396. public function testRadioDefaultValue()
  4397. {
  4398. $Articles = $this->getTableLocator()->get('Articles');
  4399. $title = $Articles->getSchema()->getColumn('title');
  4400. $Articles->getSchema()->addColumn(
  4401. 'title',
  4402. ['default' => '1'] + $title
  4403. );
  4404. $this->Form->create($Articles->newEntity());
  4405. $result = $this->Form->radio('title', ['option A', 'option B']);
  4406. $expected = [
  4407. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4408. ['label' => ['for' => 'title-0']],
  4409. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4410. 'option A',
  4411. '/label',
  4412. ['label' => ['for' => 'title-1']],
  4413. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4414. 'option B',
  4415. '/label',
  4416. ];
  4417. $this->assertHtml($expected, $result);
  4418. }
  4419. /**
  4420. * Test setting a hiddenField value on radio buttons.
  4421. *
  4422. * @return void
  4423. */
  4424. public function testRadioHiddenFieldValue()
  4425. {
  4426. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4427. $expected = [
  4428. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4429. 'label' => ['for' => 'title-0'],
  4430. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4431. 'option A',
  4432. '/label',
  4433. ];
  4434. $this->assertHtml($expected, $result);
  4435. }
  4436. /**
  4437. * testControlRadio method
  4438. *
  4439. * Test that input works with radio types.
  4440. *
  4441. * @return void
  4442. */
  4443. public function testControlRadio()
  4444. {
  4445. $result = $this->Form->control('test', [
  4446. 'type' => 'radio',
  4447. 'options' => ['A', 'B'],
  4448. ]);
  4449. $expected = [
  4450. ['div' => ['class' => 'input radio']],
  4451. '<label',
  4452. 'Test',
  4453. '/label',
  4454. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4455. ['label' => ['for' => 'test-0']],
  4456. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4457. 'A',
  4458. '/label',
  4459. ['label' => ['for' => 'test-1']],
  4460. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4461. 'B',
  4462. '/label',
  4463. '/div',
  4464. ];
  4465. $this->assertHtml($expected, $result);
  4466. $result = $this->Form->control('test', [
  4467. 'type' => 'radio',
  4468. 'options' => ['A', 'B'],
  4469. 'value' => '0',
  4470. ]);
  4471. $expected = [
  4472. ['div' => ['class' => 'input radio']],
  4473. '<label',
  4474. 'Test',
  4475. '/label',
  4476. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4477. ['label' => ['for' => 'test-0']],
  4478. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4479. 'A',
  4480. '/label',
  4481. ['label' => ['for' => 'test-1']],
  4482. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4483. 'B',
  4484. '/label',
  4485. '/div',
  4486. ];
  4487. $this->assertHtml($expected, $result);
  4488. $result = $this->Form->control('test', [
  4489. 'type' => 'radio',
  4490. 'options' => ['A', 'B'],
  4491. 'label' => false,
  4492. ]);
  4493. $expected = [
  4494. ['div' => ['class' => 'input radio']],
  4495. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4496. ['label' => ['for' => 'test-0']],
  4497. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4498. 'A',
  4499. '/label',
  4500. ['label' => ['for' => 'test-1']],
  4501. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4502. 'B',
  4503. '/label',
  4504. '/div',
  4505. ];
  4506. $this->assertHtml($expected, $result);
  4507. }
  4508. /**
  4509. * testRadioNoLabel method
  4510. *
  4511. * Test that radio() works with label = false.
  4512. *
  4513. * @return void
  4514. */
  4515. public function testRadioNoLabel()
  4516. {
  4517. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4518. $expected = [
  4519. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4520. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4521. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4522. ];
  4523. $this->assertHtml($expected, $result);
  4524. }
  4525. /**
  4526. * testRadioControlInsideLabel method
  4527. *
  4528. * Test generating radio input inside label ala twitter bootstrap.
  4529. *
  4530. * @return void
  4531. */
  4532. public function testRadioControlInsideLabel()
  4533. {
  4534. $this->Form->setTemplates([
  4535. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4536. 'radioWrapper' => '{{label}}',
  4537. ]);
  4538. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4539. //@codingStandardsIgnoreStart
  4540. $expected = [
  4541. ['input' => [
  4542. 'type' => 'hidden',
  4543. 'name' => 'Model[field]',
  4544. 'value' => ''
  4545. ]],
  4546. ['label' => ['for' => 'model-field-0']],
  4547. ['input' => [
  4548. 'type' => 'radio',
  4549. 'name' => 'Model[field]',
  4550. 'value' => '0',
  4551. 'id' => 'model-field-0'
  4552. ]],
  4553. 'option A',
  4554. '/label',
  4555. ['label' => ['for' => 'model-field-1']],
  4556. ['input' => [
  4557. 'type' => 'radio',
  4558. 'name' => 'Model[field]',
  4559. 'value' => '1',
  4560. 'id' => 'model-field-1'
  4561. ]],
  4562. 'option B',
  4563. '/label',
  4564. ];
  4565. //@codingStandardsIgnoreEnd
  4566. $this->assertHtml($expected, $result);
  4567. }
  4568. /**
  4569. * testRadioHiddenControlDisabling method
  4570. *
  4571. * Test disabling the hidden input for radio buttons.
  4572. *
  4573. * @return void
  4574. */
  4575. public function testRadioHiddenControlDisabling()
  4576. {
  4577. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4578. $expected = [
  4579. 'label' => ['for' => 'model-1-field-0'],
  4580. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4581. 'option A',
  4582. '/label',
  4583. ];
  4584. $this->assertHtml($expected, $result);
  4585. }
  4586. /**
  4587. * testRadioOutOfRange method
  4588. *
  4589. * Test radio element set generation.
  4590. *
  4591. * @return void
  4592. */
  4593. public function testRadioOutOfRange()
  4594. {
  4595. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4596. $expected = [
  4597. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4598. 'label' => ['for' => 'model-field-v'],
  4599. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4600. 'value',
  4601. '/label',
  4602. ];
  4603. $this->assertHtml($expected, $result);
  4604. }
  4605. /**
  4606. * testSelect method
  4607. *
  4608. * Test select element generation.
  4609. *
  4610. * @return void
  4611. */
  4612. public function testSelect()
  4613. {
  4614. $result = $this->Form->select('Model.field', []);
  4615. $expected = [
  4616. 'select' => ['name' => 'Model[field]'],
  4617. '/select',
  4618. ];
  4619. $this->assertHtml($expected, $result);
  4620. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4621. $this->Form->create();
  4622. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4623. $expected = [
  4624. 'select' => ['name' => 'Model[field]'],
  4625. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4626. 'good',
  4627. '/option',
  4628. ['option' => ['value' => 'other']],
  4629. 'bad',
  4630. '/option',
  4631. '/select',
  4632. ];
  4633. $this->assertHtml($expected, $result);
  4634. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4635. $this->assertHtml($expected, $result);
  4636. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4637. $this->Form->create();
  4638. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4639. $expected = [
  4640. 'select' => ['name' => 'Model[field]'],
  4641. ['option' => ['value' => 'value']],
  4642. 'good',
  4643. '/option',
  4644. ['option' => ['value' => 'other']],
  4645. 'bad',
  4646. '/option',
  4647. '/select',
  4648. ];
  4649. $this->assertHtml($expected, $result);
  4650. $options = [
  4651. ['value' => 'first', 'text' => 'First'],
  4652. ['value' => 'first', 'text' => 'Another First'],
  4653. ];
  4654. $result = $this->Form->select(
  4655. 'Model.field',
  4656. $options,
  4657. ['escape' => false, 'empty' => false]
  4658. );
  4659. $expected = [
  4660. 'select' => ['name' => 'Model[field]'],
  4661. ['option' => ['value' => 'first']],
  4662. 'First',
  4663. '/option',
  4664. ['option' => ['value' => 'first']],
  4665. 'Another First',
  4666. '/option',
  4667. '/select',
  4668. ];
  4669. $this->assertHtml($expected, $result);
  4670. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4671. $this->Form->create();
  4672. $result = $this->Form->select(
  4673. 'Model.contact_id',
  4674. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4675. ['escape' => false, 'empty' => 'pick something']
  4676. );
  4677. $expected = [
  4678. 'select' => ['name' => 'Model[contact_id]'],
  4679. ['option' => ['value' => '']], 'pick something', '/option',
  4680. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4681. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4682. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4683. '/select',
  4684. ];
  4685. $this->assertHtml($expected, $result);
  4686. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4687. $this->Form->create();
  4688. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4689. $expected = [
  4690. 'select' => ['name' => 'Model[field]'],
  4691. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4692. ['option' => ['value' => '1']], 'Yes', '/option',
  4693. '/select',
  4694. ];
  4695. $this->assertHtml($expected, $result);
  4696. }
  4697. /**
  4698. * testSelectEscapeHtml method
  4699. *
  4700. * Test that select() escapes HTML.
  4701. *
  4702. * @return void
  4703. */
  4704. public function testSelectEscapeHtml()
  4705. {
  4706. $result = $this->Form->select(
  4707. 'Model.field',
  4708. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4709. ['empty' => false]
  4710. );
  4711. $expected = [
  4712. 'select' => ['name' => 'Model[field]'],
  4713. ['option' => ['value' => 'first']],
  4714. 'first &quot;html&quot; &lt;chars&gt;',
  4715. '/option',
  4716. ['option' => ['value' => 'second']],
  4717. 'value',
  4718. '/option',
  4719. '/select',
  4720. ];
  4721. $this->assertHtml($expected, $result);
  4722. $result = $this->Form->select(
  4723. 'Model.field',
  4724. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4725. ['escape' => false, 'empty' => false]
  4726. );
  4727. $expected = [
  4728. 'select' => ['name' => 'Model[field]'],
  4729. ['option' => ['value' => 'first']],
  4730. 'first "html" <chars>',
  4731. '/option',
  4732. ['option' => ['value' => 'second']],
  4733. 'value',
  4734. '/option',
  4735. '/select',
  4736. ];
  4737. $this->assertHtml($expected, $result);
  4738. }
  4739. /**
  4740. * testSelectRequired method
  4741. *
  4742. * Test select() with required and disabled attributes.
  4743. *
  4744. * @return void
  4745. */
  4746. public function testSelectRequired()
  4747. {
  4748. $this->article['required'] = [
  4749. 'user_id' => true,
  4750. ];
  4751. $this->Form->create($this->article);
  4752. $result = $this->Form->select('user_id', ['option A']);
  4753. $expected = [
  4754. 'select' => [
  4755. 'name' => 'user_id',
  4756. 'required' => 'required',
  4757. ],
  4758. ['option' => ['value' => '0']], 'option A', '/option',
  4759. '/select',
  4760. ];
  4761. $this->assertHtml($expected, $result);
  4762. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4763. $expected = [
  4764. 'select' => [
  4765. 'name' => 'user_id',
  4766. 'disabled' => 'disabled',
  4767. ],
  4768. ['option' => ['value' => '0']], 'option A', '/option',
  4769. '/select',
  4770. ];
  4771. $this->assertHtml($expected, $result);
  4772. }
  4773. /**
  4774. * testNestedSelect method
  4775. *
  4776. * Test select element generation with optgroups.
  4777. *
  4778. * @return void
  4779. */
  4780. public function testNestedSelect()
  4781. {
  4782. $result = $this->Form->select(
  4783. 'Model.field',
  4784. [1 => 'One', 2 => 'Two', 'Three' => [
  4785. 3 => 'Three', 4 => 'Four', 5 => 'Five',
  4786. ]],
  4787. ['empty' => false]
  4788. );
  4789. $expected = [
  4790. 'select' => ['name' => 'Model[field]'],
  4791. ['option' => ['value' => 1]],
  4792. 'One',
  4793. '/option',
  4794. ['option' => ['value' => 2]],
  4795. 'Two',
  4796. '/option',
  4797. ['optgroup' => ['label' => 'Three']],
  4798. ['option' => ['value' => 3]],
  4799. 'Three',
  4800. '/option',
  4801. ['option' => ['value' => 4]],
  4802. 'Four',
  4803. '/option',
  4804. ['option' => ['value' => 5]],
  4805. 'Five',
  4806. '/option',
  4807. '/optgroup',
  4808. '/select',
  4809. ];
  4810. $this->assertHtml($expected, $result);
  4811. }
  4812. /**
  4813. * testSelectMultiple method
  4814. *
  4815. * Test generation of multiple select elements.
  4816. *
  4817. * @return void
  4818. */
  4819. public function testSelectMultiple()
  4820. {
  4821. $options = ['first', 'second', 'third'];
  4822. $result = $this->Form->select(
  4823. 'Model.multi_field',
  4824. $options,
  4825. ['form' => 'my-form', 'multiple' => true]
  4826. );
  4827. $expected = [
  4828. 'input' => [
  4829. 'type' => 'hidden',
  4830. 'name' => 'Model[multi_field]',
  4831. 'value' => '',
  4832. 'form' => 'my-form',
  4833. ],
  4834. 'select' => [
  4835. 'name' => 'Model[multi_field][]',
  4836. 'multiple' => 'multiple',
  4837. 'form' => 'my-form',
  4838. ],
  4839. ['option' => ['value' => '0']],
  4840. 'first',
  4841. '/option',
  4842. ['option' => ['value' => '1']],
  4843. 'second',
  4844. '/option',
  4845. ['option' => ['value' => '2']],
  4846. 'third',
  4847. '/option',
  4848. '/select',
  4849. ];
  4850. $this->assertHtml($expected, $result);
  4851. $result = $this->Form->select(
  4852. 'Model.multi_field',
  4853. $options,
  4854. ['multiple' => 'multiple', 'form' => 'my-form']
  4855. );
  4856. $this->assertHtml($expected, $result);
  4857. $result = $this->Form->select(
  4858. 'Model.multi_field',
  4859. $options,
  4860. ['form' => 'my-form', 'multiple' => false]
  4861. );
  4862. $this->assertNotContains('multiple', $result);
  4863. }
  4864. /**
  4865. * testCheckboxZeroValue method
  4866. *
  4867. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4868. *
  4869. * @return void
  4870. */
  4871. public function testCheckboxZeroValue()
  4872. {
  4873. $result = $this->Form->control('User.get_spam', [
  4874. 'type' => 'checkbox',
  4875. 'value' => '0',
  4876. 'hiddenField' => '1',
  4877. ]);
  4878. $expected = [
  4879. 'div' => ['class' => 'input checkbox'],
  4880. 'label' => ['for' => 'user-get-spam'],
  4881. ['input' => [
  4882. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4883. 'value' => '1',
  4884. ]],
  4885. ['input' => [
  4886. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4887. 'value' => '0', 'id' => 'user-get-spam',
  4888. ]],
  4889. 'Get Spam',
  4890. '/label',
  4891. '/div',
  4892. ];
  4893. $this->assertHtml($expected, $result);
  4894. }
  4895. /**
  4896. * testHabtmSelectBox method
  4897. *
  4898. * Test generation of habtm select boxes.
  4899. *
  4900. * @return void
  4901. */
  4902. public function testHabtmSelectBox()
  4903. {
  4904. $this->loadFixtures('Articles');
  4905. $options = [
  4906. 1 => 'blue',
  4907. 2 => 'red',
  4908. 3 => 'green',
  4909. ];
  4910. $tags = [
  4911. new Entity(['id' => 1, 'name' => 'blue']),
  4912. new Entity(['id' => 3, 'name' => 'green']),
  4913. ];
  4914. $article = new Article(['tags' => $tags]);
  4915. $this->Form->create($article);
  4916. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4917. $expected = [
  4918. 'div' => ['class' => 'input select'],
  4919. 'label' => ['for' => 'tags-ids'],
  4920. 'Tags',
  4921. '/label',
  4922. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4923. 'select' => [
  4924. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4925. 'multiple' => 'multiple',
  4926. ],
  4927. ['option' => ['value' => '1', 'selected' => 'selected']],
  4928. 'blue',
  4929. '/option',
  4930. ['option' => ['value' => '2']],
  4931. 'red',
  4932. '/option',
  4933. ['option' => ['value' => '3', 'selected' => 'selected']],
  4934. 'green',
  4935. '/option',
  4936. '/select',
  4937. '/div',
  4938. ];
  4939. $this->assertHtml($expected, $result);
  4940. // make sure only 50 is selected, and not 50f5c0cf
  4941. $options = [
  4942. '1' => 'blue',
  4943. '50f5c0cf' => 'red',
  4944. '50' => 'green',
  4945. ];
  4946. $tags = [
  4947. new Entity(['id' => 1, 'name' => 'blue']),
  4948. new Entity(['id' => 50, 'name' => 'green']),
  4949. ];
  4950. $article = new Article(['tags' => $tags]);
  4951. $this->Form->create($article);
  4952. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4953. $expected = [
  4954. 'div' => ['class' => 'input select'],
  4955. 'label' => ['for' => 'tags-ids'],
  4956. 'Tags',
  4957. '/label',
  4958. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4959. 'select' => [
  4960. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4961. 'multiple' => 'multiple',
  4962. ],
  4963. ['option' => ['value' => '1', 'selected' => 'selected']],
  4964. 'blue',
  4965. '/option',
  4966. ['option' => ['value' => '50f5c0cf']],
  4967. 'red',
  4968. '/option',
  4969. ['option' => ['value' => '50', 'selected' => 'selected']],
  4970. 'green',
  4971. '/option',
  4972. '/select',
  4973. '/div',
  4974. ];
  4975. $this->assertHtml($expected, $result);
  4976. $spacecraft = [
  4977. 1 => 'Orion',
  4978. 2 => 'Helios',
  4979. ];
  4980. $this->View->set('spacecraft', $spacecraft);
  4981. $this->Form->create();
  4982. $result = $this->Form->control('spacecraft._ids');
  4983. $expected = [
  4984. 'div' => ['class' => 'input select'],
  4985. 'label' => ['for' => 'spacecraft-ids'],
  4986. 'Spacecraft',
  4987. '/label',
  4988. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4989. 'select' => [
  4990. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4991. 'multiple' => 'multiple',
  4992. ],
  4993. ['option' => ['value' => '1']],
  4994. 'Orion',
  4995. '/option',
  4996. ['option' => ['value' => '2']],
  4997. 'Helios',
  4998. '/option',
  4999. '/select',
  5000. '/div',
  5001. ];
  5002. $this->assertHtml($expected, $result);
  5003. }
  5004. /**
  5005. * testErrorsForBelongsToManySelect method
  5006. *
  5007. * Tests that errors for belongsToMany select fields are being
  5008. * picked up properly.
  5009. *
  5010. * @return void
  5011. */
  5012. public function testErrorsForBelongsToManySelect()
  5013. {
  5014. $spacecraft = [
  5015. 1 => 'Orion',
  5016. 2 => 'Helios',
  5017. ];
  5018. $this->View->set('spacecraft', $spacecraft);
  5019. $article = new Article();
  5020. $article->setError('spacecraft', ['Invalid']);
  5021. $this->Form->create($article);
  5022. $result = $this->Form->control('spacecraft._ids');
  5023. $expected = [
  5024. ['div' => ['class' => 'input select error']],
  5025. 'label' => ['for' => 'spacecraft-ids'],
  5026. 'Spacecraft',
  5027. '/label',
  5028. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  5029. 'select' => [
  5030. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  5031. 'multiple' => 'multiple',
  5032. ],
  5033. ['option' => ['value' => '1']],
  5034. 'Orion',
  5035. '/option',
  5036. ['option' => ['value' => '2']],
  5037. 'Helios',
  5038. '/option',
  5039. '/select',
  5040. ['div' => ['class' => 'error-message']],
  5041. 'Invalid',
  5042. '/div',
  5043. '/div',
  5044. ];
  5045. $this->assertHtml($expected, $result);
  5046. }
  5047. /**
  5048. * testSelectMultipleCheckboxes method
  5049. *
  5050. * Test generation of multi select elements in checkbox format.
  5051. *
  5052. * @return void
  5053. */
  5054. public function testSelectMultipleCheckboxes()
  5055. {
  5056. $result = $this->Form->select(
  5057. 'Model.multi_field',
  5058. ['first', 'second', 'third'],
  5059. ['multiple' => 'checkbox']
  5060. );
  5061. $expected = [
  5062. 'input' => [
  5063. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5064. ],
  5065. ['div' => ['class' => 'checkbox']],
  5066. ['label' => ['for' => 'model-multi-field-0']],
  5067. ['input' => [
  5068. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5069. 'value' => '0', 'id' => 'model-multi-field-0',
  5070. ]],
  5071. 'first',
  5072. '/label',
  5073. '/div',
  5074. ['div' => ['class' => 'checkbox']],
  5075. ['label' => ['for' => 'model-multi-field-1']],
  5076. ['input' => [
  5077. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5078. 'value' => '1', 'id' => 'model-multi-field-1',
  5079. ]],
  5080. 'second',
  5081. '/label',
  5082. '/div',
  5083. ['div' => ['class' => 'checkbox']],
  5084. ['label' => ['for' => 'model-multi-field-2']],
  5085. ['input' => [
  5086. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5087. 'value' => '2', 'id' => 'model-multi-field-2',
  5088. ]],
  5089. 'third',
  5090. '/label',
  5091. '/div',
  5092. ];
  5093. $this->assertHtml($expected, $result);
  5094. $result = $this->Form->select(
  5095. 'Model.multi_field',
  5096. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5097. ['multiple' => 'checkbox']
  5098. );
  5099. $expected = [
  5100. 'input' => [
  5101. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5102. ],
  5103. ['div' => ['class' => 'checkbox']],
  5104. ['label' => ['for' => 'model-multi-field-a+']],
  5105. ['input' => [
  5106. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5107. 'value' => 'a+', 'id' => 'model-multi-field-a+',
  5108. ]],
  5109. 'first',
  5110. '/label',
  5111. '/div',
  5112. ['div' => ['class' => 'checkbox']],
  5113. ['label' => ['for' => 'model-multi-field-a++']],
  5114. ['input' => [
  5115. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5116. 'value' => 'a++', 'id' => 'model-multi-field-a++',
  5117. ]],
  5118. 'second',
  5119. '/label',
  5120. '/div',
  5121. ['div' => ['class' => 'checkbox']],
  5122. ['label' => ['for' => 'model-multi-field-a+++']],
  5123. ['input' => [
  5124. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5125. 'value' => 'a+++', 'id' => 'model-multi-field-a+++',
  5126. ]],
  5127. 'third',
  5128. '/label',
  5129. '/div',
  5130. ];
  5131. $this->assertHtml($expected, $result);
  5132. $result = $this->Form->select(
  5133. 'Model.multi_field',
  5134. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5135. ['multiple' => 'checkbox']
  5136. );
  5137. $expected = [
  5138. 'input' => [
  5139. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5140. ],
  5141. ['div' => ['class' => 'checkbox']],
  5142. ['label' => ['for' => 'model-multi-field-a-b']],
  5143. ['input' => [
  5144. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5145. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b',
  5146. ]],
  5147. 'first',
  5148. '/label',
  5149. '/div',
  5150. ['div' => ['class' => 'checkbox']],
  5151. ['label' => ['for' => 'model-multi-field-a-b1']],
  5152. ['input' => [
  5153. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5154. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1',
  5155. ]],
  5156. 'second',
  5157. '/label',
  5158. '/div',
  5159. ['div' => ['class' => 'checkbox']],
  5160. ['label' => ['for' => 'model-multi-field-a-b2']],
  5161. ['input' => [
  5162. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5163. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2',
  5164. ]],
  5165. 'third',
  5166. '/label',
  5167. '/div',
  5168. ];
  5169. $this->assertHtml($expected, $result);
  5170. }
  5171. /**
  5172. * testSelectMultipleCheckboxRequestData method
  5173. *
  5174. * Ensure that multiCheckbox reads from the request data.
  5175. *
  5176. * @return void
  5177. */
  5178. public function testSelectMultipleCheckboxRequestData()
  5179. {
  5180. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  5181. $result = $this->Form->select(
  5182. 'Model.tags',
  5183. ['1' => 'first', 'Array' => 'Array'],
  5184. ['multiple' => 'checkbox']
  5185. );
  5186. $expected = [
  5187. 'input' => [
  5188. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '',
  5189. ],
  5190. ['div' => ['class' => 'checkbox']],
  5191. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5192. ['input' => [
  5193. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5194. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked',
  5195. ]],
  5196. 'first',
  5197. '/label',
  5198. '/div',
  5199. ['div' => ['class' => 'checkbox']],
  5200. ['label' => ['for' => 'model-tags-array']],
  5201. ['input' => [
  5202. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5203. 'value' => 'Array', 'id' => 'model-tags-array',
  5204. ]],
  5205. 'Array',
  5206. '/label',
  5207. '/div',
  5208. ];
  5209. $this->assertHtml($expected, $result);
  5210. }
  5211. /**
  5212. * testSelectMultipleCheckboxSecurity method
  5213. *
  5214. * Checks the security hash array generated for multiple-input checkbox elements.
  5215. *
  5216. * @return void
  5217. */
  5218. public function testSelectMultipleCheckboxSecurity()
  5219. {
  5220. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  5221. $this->assertEquals([], $this->Form->fields);
  5222. $this->Form->select(
  5223. 'Model.multi_field',
  5224. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5225. ['multiple' => 'checkbox']
  5226. );
  5227. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5228. $result = $this->Form->secure($this->Form->fields);
  5229. $key = '8af36fb34e6f2ef8ba0eb473bb4365ec232f3fe5%3A';
  5230. $this->assertContains('"' . $key . '"', $result);
  5231. }
  5232. /**
  5233. * testSelectMultipleSecureWithNoOptions method
  5234. *
  5235. * Multiple select elements should always be secured as they always participate
  5236. * in the POST data.
  5237. *
  5238. * @return void
  5239. */
  5240. public function testSelectMultipleSecureWithNoOptions()
  5241. {
  5242. $this->assertEquals([], $this->Form->fields);
  5243. $this->Form->select(
  5244. 'Model.select',
  5245. [],
  5246. ['multiple' => true]
  5247. );
  5248. $this->assertEquals(['Model.select'], $this->Form->fields);
  5249. }
  5250. /**
  5251. * testSelectNoSecureWithNoOptions method
  5252. *
  5253. * When a select box has no options it should not be added to the fields list
  5254. * as it always fail post validation.
  5255. *
  5256. * @return void
  5257. */
  5258. public function testSelectNoSecureWithNoOptions()
  5259. {
  5260. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testkey'));
  5261. $this->assertEquals([], $this->Form->fields);
  5262. $this->Form->select(
  5263. 'Model.select',
  5264. []
  5265. );
  5266. $this->assertEquals([], $this->Form->fields);
  5267. $this->Form->select(
  5268. 'Model.user_id',
  5269. [],
  5270. ['empty' => true]
  5271. );
  5272. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5273. }
  5274. /**
  5275. * testControlMultipleCheckboxes method
  5276. *
  5277. * Test control() resulting in multi select elements being generated.
  5278. *
  5279. * @return void
  5280. */
  5281. public function testControlMultipleCheckboxes()
  5282. {
  5283. $result = $this->Form->control('Model.multi_field', [
  5284. 'options' => ['first', 'second', 'third'],
  5285. 'multiple' => 'checkbox',
  5286. ]);
  5287. $expected = [
  5288. ['div' => ['class' => 'input select']],
  5289. ['label' => ['for' => 'model-multi-field']],
  5290. 'Multi Field',
  5291. '/label',
  5292. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5293. ['div' => ['class' => 'checkbox']],
  5294. ['label' => ['for' => 'model-multi-field-0']],
  5295. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5296. 'first',
  5297. '/label',
  5298. '/div',
  5299. ['div' => ['class' => 'checkbox']],
  5300. ['label' => ['for' => 'model-multi-field-1']],
  5301. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5302. 'second',
  5303. '/label',
  5304. '/div',
  5305. ['div' => ['class' => 'checkbox']],
  5306. ['label' => ['for' => 'model-multi-field-2']],
  5307. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5308. 'third',
  5309. '/label',
  5310. '/div',
  5311. '/div',
  5312. ];
  5313. $this->assertHtml($expected, $result);
  5314. $result = $this->Form->control('Model.multi_field', [
  5315. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5316. 'multiple' => 'checkbox',
  5317. ]);
  5318. $expected = [
  5319. ['div' => ['class' => 'input select']],
  5320. ['label' => ['for' => 'model-multi-field']],
  5321. 'Multi Field',
  5322. '/label',
  5323. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5324. ['div' => ['class' => 'checkbox']],
  5325. ['label' => ['for' => 'model-multi-field-a']],
  5326. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5327. 'first',
  5328. '/label',
  5329. '/div',
  5330. ['div' => ['class' => 'checkbox']],
  5331. ['label' => ['for' => 'model-multi-field-b']],
  5332. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5333. 'second',
  5334. '/label',
  5335. '/div',
  5336. ['div' => ['class' => 'checkbox']],
  5337. ['label' => ['for' => 'model-multi-field-c']],
  5338. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5339. 'third',
  5340. '/label',
  5341. '/div',
  5342. '/div',
  5343. ];
  5344. $this->assertHtml($expected, $result);
  5345. }
  5346. /**
  5347. * testSelectHiddenFieldOmission method
  5348. *
  5349. * Test that select() with 'hiddenField' => false omits the hidden field.
  5350. *
  5351. * @return void
  5352. */
  5353. public function testSelectHiddenFieldOmission()
  5354. {
  5355. $result = $this->Form->select(
  5356. 'Model.multi_field',
  5357. ['first', 'second'],
  5358. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5359. );
  5360. $this->assertNotContains('type="hidden"', $result);
  5361. }
  5362. /**
  5363. * testSelectCheckboxMultipleOverrideName method
  5364. *
  5365. * Test that select() with multiple = checkbox works with overriding name attribute.
  5366. *
  5367. * @return void
  5368. */
  5369. public function testSelectCheckboxMultipleOverrideName()
  5370. {
  5371. $result = $this->Form->select('category', ['1', '2'], [
  5372. 'multiple' => 'checkbox',
  5373. 'name' => 'fish',
  5374. ]);
  5375. $expected = [
  5376. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5377. ['div' => ['class' => 'checkbox']],
  5378. ['label' => ['for' => 'fish-0']],
  5379. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5380. '1',
  5381. '/label',
  5382. '/div',
  5383. ['div' => ['class' => 'checkbox']],
  5384. ['label' => ['for' => 'fish-1']],
  5385. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5386. '2',
  5387. '/label',
  5388. '/div',
  5389. ];
  5390. $this->assertHtml($expected, $result);
  5391. $result = $this->Form->multiCheckbox(
  5392. 'category',
  5393. new Collection(['1', '2']),
  5394. ['name' => 'fish']
  5395. );
  5396. $this->assertHtml($expected, $result);
  5397. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5398. 'name' => 'fish',
  5399. ]);
  5400. $this->assertHtml($expected, $result);
  5401. }
  5402. /**
  5403. * testControlMultiCheckbox method
  5404. *
  5405. * Test that control() works with multicheckbox.
  5406. *
  5407. * @return void
  5408. */
  5409. public function testControlMultiCheckbox()
  5410. {
  5411. $result = $this->Form->control('category', [
  5412. 'type' => 'multicheckbox',
  5413. 'options' => ['1', '2'],
  5414. ]);
  5415. $expected = [
  5416. ['div' => ['class' => 'input multicheckbox']],
  5417. '<label',
  5418. 'Category',
  5419. '/label',
  5420. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5421. ['div' => ['class' => 'checkbox']],
  5422. ['label' => ['for' => 'category-0']],
  5423. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5424. '1',
  5425. '/label',
  5426. '/div',
  5427. ['div' => ['class' => 'checkbox']],
  5428. ['label' => ['for' => 'category-1']],
  5429. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5430. '2',
  5431. '/label',
  5432. '/div',
  5433. '/div',
  5434. ];
  5435. $this->assertHtml($expected, $result);
  5436. }
  5437. /**
  5438. * testCheckbox method
  5439. *
  5440. * Test generation of checkboxes.
  5441. *
  5442. * @return void
  5443. */
  5444. public function testCheckbox()
  5445. {
  5446. $result = $this->Form->checkbox('Model.field');
  5447. $expected = [
  5448. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5449. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']],
  5450. ];
  5451. $this->assertHtml($expected, $result);
  5452. $result = $this->Form->checkbox('Model.field', [
  5453. 'id' => 'theID',
  5454. 'value' => 'myvalue',
  5455. 'form' => 'my-form',
  5456. ]);
  5457. $expected = [
  5458. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5459. ['input' => [
  5460. 'type' => 'checkbox', 'name' => 'Model[field]',
  5461. 'value' => 'myvalue', 'id' => 'theID',
  5462. 'form' => 'my-form',
  5463. ]],
  5464. ];
  5465. $this->assertHtml($expected, $result);
  5466. }
  5467. /**
  5468. * testCheckboxDefaultValue method
  5469. *
  5470. * Test default value setting on checkbox() method.
  5471. *
  5472. * @return void
  5473. */
  5474. public function testCheckboxDefaultValue()
  5475. {
  5476. $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
  5477. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5478. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5479. $this->assertHtml($expected, $result);
  5480. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5481. $this->Form->create();
  5482. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5483. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5484. $this->assertHtml($expected, $result);
  5485. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5486. $this->Form->create();
  5487. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5488. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5489. $this->assertHtml($expected, $result);
  5490. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5491. $this->Form->create();
  5492. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5493. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5494. $this->assertHtml($expected, $result);
  5495. $Articles = $this->getTableLocator()->get('Articles');
  5496. $Articles->getSchema()->addColumn(
  5497. 'published',
  5498. ['type' => 'boolean', 'null' => false, 'default' => true]
  5499. );
  5500. $this->Form->create($Articles->newEntity());
  5501. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5502. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5503. $this->assertHtml($expected, $result);
  5504. }
  5505. /**
  5506. * testCheckboxCheckedAndError method
  5507. *
  5508. * Test checkbox being checked or having errors.
  5509. *
  5510. * @return void
  5511. */
  5512. public function testCheckboxCheckedAndError()
  5513. {
  5514. $this->article['errors'] = [
  5515. 'published' => true,
  5516. ];
  5517. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5518. $this->Form->create($this->article);
  5519. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5520. $expected = [
  5521. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5522. ['input' => [
  5523. 'type' => 'checkbox',
  5524. 'name' => 'published',
  5525. 'value' => 'myvalue',
  5526. 'id' => 'theID',
  5527. 'checked' => 'checked',
  5528. 'class' => 'form-error',
  5529. ]],
  5530. ];
  5531. $this->assertHtml($expected, $result);
  5532. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5533. $this->Form->create($this->article);
  5534. $result = $this->Form->checkbox('published');
  5535. $expected = [
  5536. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5537. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']],
  5538. ];
  5539. $this->assertHtml($expected, $result);
  5540. }
  5541. /**
  5542. * testCheckboxCustomNameAttribute method
  5543. *
  5544. * Test checkbox() with a custom name attribute.
  5545. *
  5546. * @return void
  5547. */
  5548. public function testCheckboxCustomNameAttribute()
  5549. {
  5550. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5551. $expected = [
  5552. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5553. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']],
  5554. ];
  5555. $this->assertHtml($expected, $result);
  5556. }
  5557. /**
  5558. * testCheckboxHiddenField method
  5559. *
  5560. * Test that the hidden input for checkboxes can be omitted or set to a
  5561. * specific value.
  5562. *
  5563. * @return void
  5564. */
  5565. public function testCheckboxHiddenField()
  5566. {
  5567. $result = $this->Form->checkbox('UserForm.something', [
  5568. 'hiddenField' => false,
  5569. ]);
  5570. $expected = [
  5571. 'input' => [
  5572. 'type' => 'checkbox',
  5573. 'name' => 'UserForm[something]',
  5574. 'value' => '1',
  5575. ],
  5576. ];
  5577. $this->assertHtml($expected, $result);
  5578. $result = $this->Form->checkbox('UserForm.something', [
  5579. 'value' => 'Y',
  5580. 'hiddenField' => 'N',
  5581. ]);
  5582. $expected = [
  5583. ['input' => [
  5584. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5585. 'value' => 'N',
  5586. ]],
  5587. ['input' => [
  5588. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5589. 'value' => 'Y',
  5590. ]],
  5591. ];
  5592. $this->assertHtml($expected, $result);
  5593. }
  5594. /**
  5595. * testTime method
  5596. *
  5597. * Test the time type.
  5598. *
  5599. * @return void
  5600. */
  5601. public function testTime()
  5602. {
  5603. $result = $this->Form->time('start_time', [
  5604. 'timeFormat' => 12,
  5605. 'interval' => 5,
  5606. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm'],
  5607. ]);
  5608. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5609. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5610. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5611. $this->assertNotContains('year', $result);
  5612. $this->assertNotContains('month', $result);
  5613. $this->assertNotContains('day', $result);
  5614. $result = $this->Form->time('start_time', [
  5615. 'timeFormat' => 12,
  5616. 'interval' => 5,
  5617. 'value' => '2014-03-08 16:30:00',
  5618. ]);
  5619. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5620. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5621. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5622. $this->assertNotContains('year', $result);
  5623. $this->assertNotContains('month', $result);
  5624. $this->assertNotContains('day', $result);
  5625. }
  5626. /**
  5627. * testTimeFormat24NoMeridian method
  5628. *
  5629. * Ensure that timeFormat=24 has no merdian.
  5630. *
  5631. * @return void.
  5632. */
  5633. public function testTimeFormat24NoMeridian()
  5634. {
  5635. $result = $this->Form->time('start_time', [
  5636. 'timeFormat' => 24,
  5637. 'interval' => 5,
  5638. 'value' => '2014-03-08 16:30:00',
  5639. ]);
  5640. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5641. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5642. $this->assertNotContains('meridian', $result);
  5643. $this->assertNotContains('pm', $result);
  5644. $this->assertNotContains('year', $result);
  5645. $this->assertNotContains('month', $result);
  5646. $this->assertNotContains('day', $result);
  5647. }
  5648. /**
  5649. * testDate method
  5650. *
  5651. * Test the date type.
  5652. *
  5653. * @return void
  5654. */
  5655. public function testDate()
  5656. {
  5657. $result = $this->Form->date('start_day', [
  5658. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08'],
  5659. ]);
  5660. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5661. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5662. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5663. $this->assertNotContains('hour', $result);
  5664. $this->assertNotContains('minute', $result);
  5665. $this->assertNotContains('second', $result);
  5666. $this->assertNotContains('meridian', $result);
  5667. }
  5668. /**
  5669. * testDateTime method
  5670. *
  5671. * Test generation of date/time select elements.
  5672. *
  5673. * @return void
  5674. */
  5675. public function testDateTime()
  5676. {
  5677. extract($this->dateRegex);
  5678. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5679. $now = strtotime('now');
  5680. $expected = [
  5681. ['select' => ['name' => 'Contact[date][year]']],
  5682. ['option' => ['value' => '']],
  5683. '/option',
  5684. $yearsRegex,
  5685. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5686. date('Y', $now),
  5687. '/option',
  5688. '*/select',
  5689. ['select' => ['name' => 'Contact[date][month]']],
  5690. ['option' => ['value' => '']],
  5691. '/option',
  5692. $monthsRegex,
  5693. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5694. date('F', $now),
  5695. '/option',
  5696. '*/select',
  5697. ['select' => ['name' => 'Contact[date][day]']],
  5698. ['option' => ['value' => '']],
  5699. '/option',
  5700. $daysRegex,
  5701. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5702. date('j', $now),
  5703. '/option',
  5704. '*/select',
  5705. ['select' => ['name' => 'Contact[date][hour]']],
  5706. ['option' => ['value' => '']],
  5707. '/option',
  5708. $hoursRegex,
  5709. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5710. date('G', $now),
  5711. '/option',
  5712. '*/select',
  5713. ['select' => ['name' => 'Contact[date][minute]']],
  5714. ['option' => ['value' => '']],
  5715. '/option',
  5716. $minutesRegex,
  5717. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5718. date('i', $now),
  5719. '/option',
  5720. '*/select',
  5721. ];
  5722. $this->assertHtml($expected, $result);
  5723. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5724. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5725. $now = strtotime('now');
  5726. $expected = [
  5727. ['select' => ['name' => 'Contact[date][year]']],
  5728. $yearsRegex,
  5729. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5730. date('Y', $now),
  5731. '/option',
  5732. '*/select',
  5733. ['select' => ['name' => 'Contact[date][month]']],
  5734. $monthsRegex,
  5735. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5736. date('F', $now),
  5737. '/option',
  5738. '*/select',
  5739. ['select' => ['name' => 'Contact[date][day]']],
  5740. $daysRegex,
  5741. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5742. date('j', $now),
  5743. '/option',
  5744. '*/select',
  5745. ['select' => ['name' => 'Contact[date][hour]']],
  5746. $hoursRegex,
  5747. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5748. date('G', $now),
  5749. '/option',
  5750. '*/select',
  5751. ['select' => ['name' => 'Contact[date][minute]']],
  5752. $minutesRegex,
  5753. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5754. date('i', $now),
  5755. '/option',
  5756. '*/select',
  5757. ];
  5758. $this->assertHtml($expected, $result);
  5759. }
  5760. /**
  5761. * testDateTimeSecured method
  5762. *
  5763. * Test that datetime fields are added to protected fields list.
  5764. *
  5765. * @return void
  5766. */
  5767. public function testDateTimeSecured()
  5768. {
  5769. $this->View->setRequest(
  5770. $this->View->getRequest()->withParam('_Token', ['unlockedFields' => []])
  5771. );
  5772. $this->Form->dateTime('Contact.date');
  5773. $expected = [
  5774. 'Contact.date.year',
  5775. 'Contact.date.month',
  5776. 'Contact.date.day',
  5777. 'Contact.date.hour',
  5778. 'Contact.date.minute',
  5779. ];
  5780. $this->assertEquals($expected, $this->Form->fields);
  5781. $this->Form->fields = [];
  5782. $this->Form->date('Contact.published');
  5783. $expected = [
  5784. 'Contact.published.year',
  5785. 'Contact.published.month',
  5786. 'Contact.published.day',
  5787. ];
  5788. $this->assertEquals($expected, $this->Form->fields);
  5789. }
  5790. /**
  5791. * testDateTimeSecuredDisabled method
  5792. *
  5793. * Test that datetime fields are added to protected fields list.
  5794. *
  5795. * @return void
  5796. */
  5797. public function testDateTimeSecuredDisabled()
  5798. {
  5799. $this->View->setRequest(
  5800. $this->View->getRequest()->withParam('_Token', ['unlockedFields' => []])
  5801. );
  5802. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5803. $expected = [];
  5804. $this->assertEquals($expected, $this->Form->fields);
  5805. $this->Form->fields = [];
  5806. $this->Form->date('Contact.published', ['secure' => false]);
  5807. $expected = [];
  5808. $this->assertEquals($expected, $this->Form->fields);
  5809. }
  5810. /**
  5811. * testDatetimeEmpty method
  5812. *
  5813. * Test empty defaulting to true for datetime.
  5814. *
  5815. * @return void
  5816. */
  5817. public function testDatetimeEmpty()
  5818. {
  5819. extract($this->dateRegex);
  5820. $result = $this->Form->dateTime('Contact.date', [
  5821. 'timeFormat' => 12,
  5822. 'empty' => true,
  5823. 'default' => true,
  5824. ]);
  5825. $expected = [
  5826. ['select' => ['name' => 'Contact[date][year]']],
  5827. $yearsRegex,
  5828. ['option' => ['value' => '']],
  5829. '/option',
  5830. '*/select',
  5831. ['select' => ['name' => 'Contact[date][month]']],
  5832. $monthsRegex,
  5833. ['option' => ['value' => '']],
  5834. '/option',
  5835. '*/select',
  5836. ['select' => ['name' => 'Contact[date][day]']],
  5837. $daysRegex,
  5838. ['option' => ['value' => '']],
  5839. '/option',
  5840. '*/select',
  5841. ['select' => ['name' => 'Contact[date][hour]']],
  5842. $hoursRegex,
  5843. ['option' => ['value' => '']],
  5844. '/option',
  5845. '*/select',
  5846. ['select' => ['name' => 'Contact[date][minute]']],
  5847. $minutesRegex,
  5848. ['option' => ['value' => '']],
  5849. '/option',
  5850. '*/select',
  5851. ['select' => ['name' => 'Contact[date][meridian]']],
  5852. $meridianRegex,
  5853. ['option' => ['value' => '']],
  5854. '/option',
  5855. '*/select',
  5856. ];
  5857. $this->assertHtml($expected, $result);
  5858. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5859. }
  5860. /**
  5861. * testDatetimeMinuteInterval method
  5862. *
  5863. * Test datetime with interval option.
  5864. *
  5865. * @return void
  5866. */
  5867. public function testDatetimeMinuteInterval()
  5868. {
  5869. extract($this->dateRegex);
  5870. $result = $this->Form->dateTime('Contact.date', [
  5871. 'interval' => 5,
  5872. 'value' => '',
  5873. ]);
  5874. $expected = [
  5875. ['select' => ['name' => 'Contact[date][year]']],
  5876. $yearsRegex,
  5877. ['option' => ['selected' => 'selected', 'value' => '']],
  5878. '/option',
  5879. '*/select',
  5880. ['select' => ['name' => 'Contact[date][month]']],
  5881. $monthsRegex,
  5882. ['option' => ['selected' => 'selected', 'value' => '']],
  5883. '/option',
  5884. '*/select',
  5885. ['select' => ['name' => 'Contact[date][day]']],
  5886. $daysRegex,
  5887. ['option' => ['selected' => 'selected', 'value' => '']],
  5888. '/option',
  5889. '*/select',
  5890. ['select' => ['name' => 'Contact[date][hour]']],
  5891. $hoursRegex,
  5892. ['option' => ['selected' => 'selected', 'value' => '']],
  5893. '/option',
  5894. '*/select',
  5895. ['select' => ['name' => 'Contact[date][minute]']],
  5896. $minutesRegex,
  5897. ['option' => ['selected' => 'selected', 'value' => '']],
  5898. '/option',
  5899. ['option' => ['value' => '00']],
  5900. '00',
  5901. '/option',
  5902. ['option' => ['value' => '05']],
  5903. '05',
  5904. '/option',
  5905. ['option' => ['value' => '10']],
  5906. '10',
  5907. '/option',
  5908. '*/select',
  5909. ];
  5910. $this->assertHtml($expected, $result);
  5911. }
  5912. /**
  5913. * testDateTimeRounding method
  5914. *
  5915. * Test dateTime with rounding.
  5916. *
  5917. * @return void
  5918. */
  5919. public function testDateTimeRounding()
  5920. {
  5921. $this->View->setRequest($this->View->getRequest()->withData('Contact', [
  5922. 'date' => [
  5923. 'day' => '13',
  5924. 'month' => '12',
  5925. 'year' => '2010',
  5926. 'hour' => '04',
  5927. 'minute' => '19',
  5928. 'meridian' => 'AM',
  5929. ],
  5930. ]));
  5931. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5932. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5933. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5934. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5935. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5936. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5937. }
  5938. /**
  5939. * testDatetimeWithDefault method
  5940. *
  5941. * Test that datetime() and default values work.
  5942. *
  5943. * @return void
  5944. */
  5945. public function testDatetimeWithDefault()
  5946. {
  5947. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5948. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5949. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5950. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5951. $result = $this->Form->dateTime('Contact.updated', [
  5952. 'default' => '2009-06-01 11:15:30',
  5953. ]);
  5954. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5955. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5956. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5957. }
  5958. /**
  5959. * testDateTimeAllZeros method
  5960. *
  5961. * Test datetime() with all zeros.
  5962. *
  5963. * @return void
  5964. */
  5965. public function testDateTimeAllZeros()
  5966. {
  5967. $result = $this->Form->dateTime('Contact.date', [
  5968. 'timeFormat' => false,
  5969. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5970. 'value' => '0000-00-00',
  5971. ]);
  5972. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5973. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5974. }
  5975. /**
  5976. * testDateTimeEmptyAsArray method
  5977. *
  5978. * @return void
  5979. */
  5980. public function testDateTimeEmptyAsArray()
  5981. {
  5982. $result = $this->Form->dateTime('Contact.date', [
  5983. 'empty' => [
  5984. 'day' => 'DAY',
  5985. 'month' => 'MONTH',
  5986. 'year' => 'YEAR',
  5987. 'hour' => 'HOUR',
  5988. 'minute' => 'MINUTE',
  5989. 'meridian' => false,
  5990. ],
  5991. 'default' => true,
  5992. ]);
  5993. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5994. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5995. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5996. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5997. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5998. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5999. $result = $this->Form->dateTime('Contact.date', [
  6000. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  6001. 'default' => true,
  6002. ]);
  6003. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  6004. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  6005. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  6006. }
  6007. /**
  6008. * testFormDateTimeMulti method
  6009. *
  6010. * Test multiple datetime element generation.
  6011. *
  6012. * @return void
  6013. */
  6014. public function testFormDateTimeMulti()
  6015. {
  6016. extract($this->dateRegex);
  6017. $result = $this->Form->dateTime('Contact.1.updated');
  6018. $this->assertContains('Contact[1][updated][month]', $result);
  6019. $this->assertContains('Contact[1][updated][day]', $result);
  6020. $this->assertContains('Contact[1][updated][year]', $result);
  6021. $this->assertContains('Contact[1][updated][hour]', $result);
  6022. $this->assertContains('Contact[1][updated][minute]', $result);
  6023. }
  6024. /**
  6025. * testDateTimeLabelIdMatchesFirstControl method
  6026. *
  6027. * When changing the date format, the label should always focus the first select box when
  6028. * clicked.
  6029. *
  6030. * @return void
  6031. */
  6032. public function testDateTimeLabelIdMatchesFirstControl()
  6033. {
  6034. $result = $this->Form->control('Model.date', ['type' => 'date']);
  6035. $this->assertContains('<label>Date</label>', $result);
  6036. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  6037. $this->assertContains('<label>Date</label>', $result);
  6038. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  6039. $this->assertContains('<label>Date</label>', $result);
  6040. }
  6041. /**
  6042. * testDateTimeSecondOptions method
  6043. *
  6044. * Test datetime second=true.
  6045. *
  6046. * @return void
  6047. */
  6048. public function testDateTimeSecondOptions()
  6049. {
  6050. $result = $this->Form->dateTime('updated', ['second' => true]);
  6051. $this->assertContains('updated[second]', $result, 'Should have seconds');
  6052. $result = $this->Form->dateTime('updated', ['second' => []]);
  6053. $this->assertContains('updated[second]', $result, 'Should have seconds');
  6054. $result = $this->Form->dateTime('updated', ['second' => null]);
  6055. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  6056. $result = $this->Form->dateTime('updated', ['second' => false]);
  6057. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  6058. }
  6059. /**
  6060. * testMonth method
  6061. *
  6062. * Test generation of a month input.
  6063. *
  6064. * @return void
  6065. */
  6066. public function testMonth()
  6067. {
  6068. $result = $this->Form->month('Model.field', ['value' => '']);
  6069. $expected = [
  6070. ['select' => ['name' => 'Model[field][month]']],
  6071. ['option' => ['value' => '', 'selected' => 'selected']],
  6072. '/option',
  6073. ['option' => ['value' => '01']],
  6074. date('F', strtotime('2008-01-01 00:00:00')),
  6075. '/option',
  6076. ['option' => ['value' => '02']],
  6077. date('F', strtotime('2008-02-01 00:00:00')),
  6078. '/option',
  6079. '*/select',
  6080. ];
  6081. $this->assertHtml($expected, $result);
  6082. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  6083. $expected = [
  6084. ['select' => ['name' => 'Model[field][month]']],
  6085. ['option' => ['selected' => 'selected', 'value' => '']],
  6086. '/option',
  6087. ['option' => ['value' => '01']],
  6088. date('F', strtotime('2008-01-01 00:00:00')),
  6089. '/option',
  6090. ['option' => ['value' => '02']],
  6091. date('F', strtotime('2008-02-01 00:00:00')),
  6092. '/option',
  6093. '*/select',
  6094. ];
  6095. $this->assertHtml($expected, $result);
  6096. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  6097. $expected = [
  6098. ['select' => ['name' => 'Model[field][month]']],
  6099. ['option' => ['selected' => 'selected', 'value' => '']],
  6100. '/option',
  6101. ['option' => ['value' => '01']],
  6102. '1',
  6103. '/option',
  6104. ['option' => ['value' => '02']],
  6105. '2',
  6106. '/option',
  6107. '*/select',
  6108. ];
  6109. $this->assertHtml($expected, $result);
  6110. $monthNames = [
  6111. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6112. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec',
  6113. ];
  6114. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  6115. $expected = [
  6116. ['select' => ['name' => 'Model[field][month]']],
  6117. ['option' => ['value' => '']],
  6118. '/option',
  6119. ['option' => ['value' => '01', 'selected' => 'selected']],
  6120. 'Jan',
  6121. '/option',
  6122. ['option' => ['value' => '02']],
  6123. 'Feb',
  6124. '/option',
  6125. '*/select',
  6126. ];
  6127. $this->assertHtml($expected, $result);
  6128. $this->View->setRequest(
  6129. $this->View->getRequest()->withData('Project.release', '2050-02-10')
  6130. );
  6131. $this->Form->create();
  6132. $result = $this->Form->month('Project.release');
  6133. $expected = [
  6134. ['select' => ['name' => 'Project[release][month]']],
  6135. ['option' => ['value' => '']],
  6136. '/option',
  6137. ['option' => ['value' => '01']],
  6138. 'January',
  6139. '/option',
  6140. ['option' => ['value' => '02', 'selected' => 'selected']],
  6141. 'February',
  6142. '/option',
  6143. '*/select',
  6144. ];
  6145. $this->assertHtml($expected, $result);
  6146. $result = $this->Form->month('Contact.published', [
  6147. 'empty' => 'Published on',
  6148. ]);
  6149. $this->assertContains('Published on', $result);
  6150. }
  6151. /**
  6152. * testDay method
  6153. *
  6154. * Test generation of a day input.
  6155. *
  6156. * @return void
  6157. */
  6158. public function testDay()
  6159. {
  6160. extract($this->dateRegex);
  6161. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  6162. $expected = [
  6163. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  6164. ['option' => ['selected' => 'selected', 'value' => '']],
  6165. '/option',
  6166. ['option' => ['value' => '01']],
  6167. '1',
  6168. '/option',
  6169. ['option' => ['value' => '02']],
  6170. '2',
  6171. '/option',
  6172. $daysRegex,
  6173. '/select',
  6174. ];
  6175. $this->assertHtml($expected, $result);
  6176. $this->View->setRequest(
  6177. $this->View->getRequest()->withData('Model.field', '2006-10-10 23:12:32')
  6178. );
  6179. $this->Form->create();
  6180. $result = $this->Form->day('Model.field');
  6181. $expected = [
  6182. ['select' => ['name' => 'Model[field][day]']],
  6183. ['option' => ['value' => '']],
  6184. '/option',
  6185. ['option' => ['value' => '01']],
  6186. '1',
  6187. '/option',
  6188. ['option' => ['value' => '02']],
  6189. '2',
  6190. '/option',
  6191. $daysRegex,
  6192. ['option' => ['value' => '10', 'selected' => 'selected']],
  6193. '10',
  6194. '/option',
  6195. $daysRegex,
  6196. '/select',
  6197. ];
  6198. $this->assertHtml($expected, $result);
  6199. $this->View->setRequest($this->View->getRequest()->withData('Model.field', ''));
  6200. $this->Form->create();
  6201. $result = $this->Form->day('Model.field', ['value' => '10']);
  6202. $expected = [
  6203. ['select' => ['name' => 'Model[field][day]']],
  6204. ['option' => ['value' => '']],
  6205. '/option',
  6206. ['option' => ['value' => '01']],
  6207. '1',
  6208. '/option',
  6209. ['option' => ['value' => '02']],
  6210. '2',
  6211. '/option',
  6212. $daysRegex,
  6213. ['option' => ['value' => '10', 'selected' => 'selected']],
  6214. '10',
  6215. '/option',
  6216. $daysRegex,
  6217. '/select',
  6218. ];
  6219. $this->assertHtml($expected, $result);
  6220. $this->View->setRequest(
  6221. $this->View->getRequest()->withData('Project.release', '2050-10-10')
  6222. );
  6223. $this->Form->create();
  6224. $result = $this->Form->day('Project.release');
  6225. $expected = [
  6226. ['select' => ['name' => 'Project[release][day]']],
  6227. ['option' => ['value' => '']],
  6228. '/option',
  6229. ['option' => ['value' => '01']],
  6230. '1',
  6231. '/option',
  6232. ['option' => ['value' => '02']],
  6233. '2',
  6234. '/option',
  6235. $daysRegex,
  6236. ['option' => ['value' => '10', 'selected' => 'selected']],
  6237. '10',
  6238. '/option',
  6239. $daysRegex,
  6240. '/select',
  6241. ];
  6242. $this->assertHtml($expected, $result);
  6243. $result = $this->Form->day('Contact.published', [
  6244. 'empty' => 'Published on',
  6245. ]);
  6246. $this->assertContains('Published on', $result);
  6247. }
  6248. /**
  6249. * testMinute method
  6250. *
  6251. * Test generation of a minute input.
  6252. *
  6253. * @return void
  6254. */
  6255. public function testMinute()
  6256. {
  6257. extract($this->dateRegex);
  6258. $result = $this->Form->minute('Model.field', ['value' => '']);
  6259. $expected = [
  6260. ['select' => ['name' => 'Model[field][minute]']],
  6261. ['option' => ['selected' => 'selected', 'value' => '']],
  6262. '/option',
  6263. ['option' => ['value' => '00']],
  6264. '00',
  6265. '/option',
  6266. ['option' => ['value' => '01']],
  6267. '01',
  6268. '/option',
  6269. ['option' => ['value' => '02']],
  6270. '02',
  6271. '/option',
  6272. $minutesRegex,
  6273. '/select',
  6274. ];
  6275. $this->assertHtml($expected, $result);
  6276. $this->View->setRequest(
  6277. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:12:32')
  6278. );
  6279. $this->Form->create();
  6280. $result = $this->Form->minute('Model.field');
  6281. $expected = [
  6282. ['select' => ['name' => 'Model[field][minute]']],
  6283. ['option' => ['value' => '']],
  6284. '/option',
  6285. ['option' => ['value' => '00']],
  6286. '00',
  6287. '/option',
  6288. ['option' => ['value' => '01']],
  6289. '01',
  6290. '/option',
  6291. ['option' => ['value' => '02']],
  6292. '02',
  6293. '/option',
  6294. $minutesRegex,
  6295. ['option' => ['value' => '12', 'selected' => 'selected']],
  6296. '12',
  6297. '/option',
  6298. $minutesRegex,
  6299. '/select',
  6300. ];
  6301. $this->assertHtml($expected, $result);
  6302. $this->View->setRequest($this->View->getRequest()->withData('Model.field', ''));
  6303. $this->Form->create();
  6304. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6305. $expected = [
  6306. ['select' => ['name' => 'Model[field][minute]']],
  6307. ['option' => ['selected' => 'selected', 'value' => '']],
  6308. '/option',
  6309. ['option' => ['value' => '00']],
  6310. '00',
  6311. '/option',
  6312. ['option' => ['value' => '05']],
  6313. '05',
  6314. '/option',
  6315. ['option' => ['value' => '10']],
  6316. '10',
  6317. '/option',
  6318. $minutesRegex,
  6319. '/select',
  6320. ];
  6321. $this->assertHtml($expected, $result);
  6322. $this->View->setRequest(
  6323. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:10:32')
  6324. );
  6325. $this->Form->create();
  6326. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6327. $expected = [
  6328. ['select' => ['name' => 'Model[field][minute]']],
  6329. ['option' => ['value' => '']],
  6330. '/option',
  6331. ['option' => ['value' => '00']],
  6332. '00',
  6333. '/option',
  6334. ['option' => ['value' => '05']],
  6335. '05',
  6336. '/option',
  6337. ['option' => ['value' => '10', 'selected' => 'selected']],
  6338. '10',
  6339. '/option',
  6340. $minutesRegex,
  6341. '/select',
  6342. ];
  6343. $this->assertHtml($expected, $result);
  6344. }
  6345. /**
  6346. * testMeridian method
  6347. *
  6348. * Test generating an input for the meridian.
  6349. *
  6350. * @return void
  6351. */
  6352. public function testMeridian()
  6353. {
  6354. extract($this->dateRegex);
  6355. $now = new \DateTime();
  6356. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6357. $expected = [
  6358. ['select' => ['name' => 'Model[field][meridian]']],
  6359. ['option' => ['value' => '']],
  6360. '/option',
  6361. $meridianRegex,
  6362. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6363. $now->format('a'),
  6364. '/option',
  6365. '*/select',
  6366. ];
  6367. $this->assertHtml($expected, $result);
  6368. }
  6369. /**
  6370. * testHour method
  6371. *
  6372. * Test generation of an hour input.
  6373. *
  6374. * @return void
  6375. */
  6376. public function testHour()
  6377. {
  6378. extract($this->dateRegex);
  6379. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6380. $expected = [
  6381. ['select' => ['name' => 'Model[field][hour]']],
  6382. ['option' => ['selected' => 'selected', 'value' => '']],
  6383. '/option',
  6384. ['option' => ['value' => '01']],
  6385. '1',
  6386. '/option',
  6387. ['option' => ['value' => '02']],
  6388. '2',
  6389. '/option',
  6390. $hoursRegex,
  6391. '/select',
  6392. ];
  6393. $this->assertHtml($expected, $result);
  6394. $this->View->setRequest(
  6395. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:12:32')
  6396. );
  6397. $this->Form->create();
  6398. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6399. $expected = [
  6400. ['select' => ['name' => 'Model[field][hour]']],
  6401. ['option' => ['value' => '']],
  6402. '/option',
  6403. ['option' => ['value' => '01']],
  6404. '1',
  6405. '/option',
  6406. ['option' => ['value' => '02']],
  6407. '2',
  6408. '/option',
  6409. $hoursRegex,
  6410. ['option' => ['value' => '12', 'selected' => 'selected']],
  6411. '12',
  6412. '/option',
  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->hour('Model.field', ['format' => 24, 'value' => '23']);
  6419. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6420. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6421. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6422. $this->View->setRequest(
  6423. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:12:32')
  6424. );
  6425. $this->Form->create();
  6426. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6427. $expected = [
  6428. ['select' => ['name' => 'Model[field][hour]']],
  6429. ['option' => ['value' => '']],
  6430. '/option',
  6431. ['option' => ['value' => '00', 'selected' => 'selected']],
  6432. '0',
  6433. '/option',
  6434. ['option' => ['value' => '01']],
  6435. '1',
  6436. '/option',
  6437. ['option' => ['value' => '02']],
  6438. '2',
  6439. '/option',
  6440. $hoursRegex,
  6441. '/select',
  6442. ];
  6443. $this->assertHtml($expected, $result);
  6444. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  6445. $this->Form->create();
  6446. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6447. $thisHour = date('H');
  6448. $optValue = date('G');
  6449. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6450. $this->View->setRequest(
  6451. $this->View->getRequest()->withData('Model.field', '2050-10-10 01:12:32')
  6452. );
  6453. $this->Form->create();
  6454. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6455. $expected = [
  6456. ['select' => ['name' => 'Model[field][hour]']],
  6457. ['option' => ['value' => '']],
  6458. '/option',
  6459. ['option' => ['value' => '00']],
  6460. '0',
  6461. '/option',
  6462. ['option' => ['value' => '01', 'selected' => 'selected']],
  6463. '1',
  6464. '/option',
  6465. ['option' => ['value' => '02']],
  6466. '2',
  6467. '/option',
  6468. $hoursRegex,
  6469. '/select',
  6470. ];
  6471. $this->assertHtml($expected, $result);
  6472. }
  6473. /**
  6474. * testYear method
  6475. *
  6476. * Test generation of a year input.
  6477. *
  6478. * @return void
  6479. */
  6480. public function testYear()
  6481. {
  6482. $this->View->setRequest(
  6483. $this->View->getRequest()->withData('Contact.published', '2006-10-10')
  6484. );
  6485. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6486. $expected = [
  6487. ['select' => ['name' => 'Model[field][year]']],
  6488. ['option' => ['selected' => 'selected', 'value' => '']],
  6489. '/option',
  6490. ['option' => ['value' => '2007']],
  6491. '2007',
  6492. '/option',
  6493. ['option' => ['value' => '2006']],
  6494. '2006',
  6495. '/option',
  6496. '/select',
  6497. ];
  6498. $this->assertHtml($expected, $result);
  6499. $result = $this->Form->year('Model.field', [
  6500. 'value' => '',
  6501. 'minYear' => 2006,
  6502. 'maxYear' => 2007,
  6503. 'orderYear' => 'asc',
  6504. ]);
  6505. $expected = [
  6506. ['select' => ['name' => 'Model[field][year]']],
  6507. ['option' => ['selected' => 'selected', 'value' => '']],
  6508. '/option',
  6509. ['option' => ['value' => '2006']],
  6510. '2006',
  6511. '/option',
  6512. ['option' => ['value' => '2007']],
  6513. '2007',
  6514. '/option',
  6515. '/select',
  6516. ];
  6517. $this->assertHtml($expected, $result);
  6518. $result = $this->Form->year('Contact.published', [
  6519. 'empty' => false,
  6520. 'minYear' => 2006,
  6521. 'maxYear' => 2007,
  6522. ]);
  6523. $expected = [
  6524. ['select' => ['name' => 'Contact[published][year]']],
  6525. ['option' => ['value' => '2007']],
  6526. '2007',
  6527. '/option',
  6528. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6529. '2006',
  6530. '/option',
  6531. '/select',
  6532. ];
  6533. $this->assertHtml($expected, $result);
  6534. $result = $this->Form->year('Contact.published', [
  6535. 'empty' => 'Published on',
  6536. ]);
  6537. $this->assertContains('Published on', $result);
  6538. }
  6539. /**
  6540. * testControlDatetimePreEpoch method
  6541. *
  6542. * Test minYear being prior to the unix epoch.
  6543. *
  6544. * @return void
  6545. */
  6546. public function testControlDatetimePreEpoch()
  6547. {
  6548. $start = date('Y') - 80;
  6549. $end = date('Y') - 18;
  6550. $result = $this->Form->control('birth_year', [
  6551. 'type' => 'date',
  6552. 'label' => 'Birth Year',
  6553. 'minYear' => $start,
  6554. 'maxYear' => $end,
  6555. 'month' => false,
  6556. 'day' => false,
  6557. ]);
  6558. $this->assertContains('value="' . $start . '">' . $start, $result);
  6559. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6560. $this->assertNotContains('value="00">00', $result);
  6561. }
  6562. /**
  6563. * test control() datetime & required attributes
  6564. *
  6565. * @return void
  6566. */
  6567. public function testControlDatetimeRequired()
  6568. {
  6569. $result = $this->Form->control('birthday', [
  6570. 'type' => 'date',
  6571. 'required' => true,
  6572. ]);
  6573. $this->assertContains(
  6574. '<select name="birthday[year]" required="required"',
  6575. $result
  6576. );
  6577. }
  6578. /**
  6579. * testYearAutoExpandRange method
  6580. *
  6581. * @return void
  6582. */
  6583. public function testYearAutoExpandRange()
  6584. {
  6585. $this->View->setRequest($this->View->getRequest()->withData('User.birthday', '1930-10-10'));
  6586. $result = $this->Form->year('User.birthday');
  6587. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6588. $result = $matches[1];
  6589. $expected = range(date('Y') + 5, 1930);
  6590. $this->assertEquals($expected, $result);
  6591. $this->View->setRequest($this->View->getRequest()->withData('Project.release', '2050-10-10'));
  6592. $this->Form->create();
  6593. $result = $this->Form->year('Project.release');
  6594. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6595. $result = $matches[1];
  6596. $expected = range(2050, date('Y') - 5);
  6597. $this->assertEquals($expected, $result);
  6598. $this->View->setRequest($this->View->getRequest()->withData('Project.release', '1881-10-10'));
  6599. $this->Form->create();
  6600. $result = $this->Form->year('Project.release', [
  6601. 'minYear' => 1890,
  6602. 'maxYear' => 1900,
  6603. ]);
  6604. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6605. $result = $matches[1];
  6606. $expected = range(1900, 1881);
  6607. $this->assertEquals($expected, $result);
  6608. }
  6609. /**
  6610. * testControlDate method
  6611. *
  6612. * Test that control() accepts the type of date and passes options in.
  6613. *
  6614. * @return void
  6615. */
  6616. public function testControlDate()
  6617. {
  6618. $this->View->setRequest($this->View->getRequest()->withParsedBody([
  6619. 'month_year' => ['month' => date('m')],
  6620. ]));
  6621. $this->Form->create($this->article);
  6622. $result = $this->Form->control('month_year', [
  6623. 'label' => false,
  6624. 'type' => 'date',
  6625. 'minYear' => 2006,
  6626. 'maxYear' => 2008,
  6627. ]);
  6628. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6629. $this->assertNotContains('value="2008" selected="selected"', $result);
  6630. }
  6631. /**
  6632. * testControlLabelFalse method
  6633. *
  6634. * Test the label option being set to false.
  6635. *
  6636. * @return void
  6637. */
  6638. public function testControlLabelFalse()
  6639. {
  6640. $this->Form->create($this->article);
  6641. $result = $this->Form->control('title', ['label' => false]);
  6642. $expected = [
  6643. 'div' => ['class' => 'input text required'],
  6644. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6645. '/div',
  6646. ];
  6647. $this->assertHtml($expected, $result);
  6648. }
  6649. /**
  6650. * testControlDateMaxYear method
  6651. *
  6652. * Let's say we want to only allow users born from 2006 to 2008 to register
  6653. * This being the first signup page, we still don't have any data.
  6654. *
  6655. * @return void
  6656. */
  6657. public function testControlDateMaxYear()
  6658. {
  6659. $this->Form->create($this->article);
  6660. $result = $this->Form->control('birthday', [
  6661. 'label' => false,
  6662. 'type' => 'date',
  6663. 'minYear' => 2006,
  6664. 'maxYear' => 2008,
  6665. 'default' => true,
  6666. ]);
  6667. $this->assertContains('value="2008" selected="selected"', $result);
  6668. $this->assertContains('value="2006"', $result);
  6669. $this->assertNotContains('value="2005"', $result);
  6670. $this->assertNotContains('value="2009"', $result);
  6671. }
  6672. /**
  6673. * testTextArea method
  6674. *
  6675. * Test generation of a textarea input.
  6676. *
  6677. * @return void
  6678. */
  6679. public function testTextArea()
  6680. {
  6681. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  6682. $result = $this->Form->textarea('field');
  6683. $expected = [
  6684. 'textarea' => ['name' => 'field', 'rows' => 5],
  6685. 'some test data',
  6686. '/textarea',
  6687. ];
  6688. $this->assertHtml($expected, $result);
  6689. $result = $this->Form->textarea('user.bio');
  6690. $expected = [
  6691. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6692. '/textarea',
  6693. ];
  6694. $this->assertHtml($expected, $result);
  6695. $this->View->setRequest($this->View->getRequest()
  6696. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6697. $this->Form->create();
  6698. $result = $this->Form->textarea('field');
  6699. $expected = [
  6700. 'textarea' => ['name' => 'field', 'rows' => 5],
  6701. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6702. '/textarea',
  6703. ];
  6704. $this->assertHtml($expected, $result);
  6705. $this->View->setRequest($this->View->getRequest()->withData(
  6706. 'Model.field',
  6707. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  6708. ));
  6709. $this->Form->create();
  6710. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6711. $expected = [
  6712. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6713. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6714. '/textarea',
  6715. ];
  6716. $this->assertHtml($expected, $result);
  6717. $result = $this->Form->textarea('0.OtherModel.field');
  6718. $expected = [
  6719. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6720. '/textarea',
  6721. ];
  6722. $this->assertHtml($expected, $result);
  6723. }
  6724. /**
  6725. * testTextAreaWithStupidCharacters method
  6726. *
  6727. * Test text area with non-ascii characters.
  6728. *
  6729. * @return void
  6730. */
  6731. public function testTextAreaWithStupidCharacters()
  6732. {
  6733. $result = $this->Form->textarea('Post.content', [
  6734. 'value' => 'GREAT®',
  6735. 'rows' => '15',
  6736. 'cols' => '75',
  6737. ]);
  6738. $expected = [
  6739. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6740. 'GREAT®',
  6741. '/textarea',
  6742. ];
  6743. $this->assertHtml($expected, $result);
  6744. }
  6745. /**
  6746. * testTextAreaMaxLength method
  6747. *
  6748. * Test textareas maxlength read from schema.
  6749. *
  6750. * @return void
  6751. */
  6752. public function testTextAreaMaxLength()
  6753. {
  6754. $this->Form->create([
  6755. 'schema' => [
  6756. 'stuff' => ['type' => 'string', 'length' => 10],
  6757. ],
  6758. ]);
  6759. $result = $this->Form->control('other', ['type' => 'textarea']);
  6760. $expected = [
  6761. 'div' => ['class' => 'input textarea'],
  6762. 'label' => ['for' => 'other'],
  6763. 'Other',
  6764. '/label',
  6765. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6766. '/textarea',
  6767. '/div',
  6768. ];
  6769. $this->assertHtml($expected, $result);
  6770. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6771. $expected = [
  6772. 'div' => ['class' => 'input textarea'],
  6773. 'label' => ['for' => 'stuff'],
  6774. 'Stuff',
  6775. '/label',
  6776. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6777. '/textarea',
  6778. '/div',
  6779. ];
  6780. $this->assertHtml($expected, $result);
  6781. }
  6782. /**
  6783. * testHiddenField method
  6784. *
  6785. * Test generation of a hidden input.
  6786. *
  6787. * @return void
  6788. */
  6789. public function testHiddenField()
  6790. {
  6791. $this->article['errors'] = [
  6792. 'field' => true,
  6793. ];
  6794. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  6795. $this->Form->create($this->article);
  6796. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6797. $expected = [
  6798. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6799. $this->assertHtml($expected, $result);
  6800. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6801. $expected = [
  6802. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value'],
  6803. ];
  6804. $this->assertHtml($expected, $result);
  6805. }
  6806. /**
  6807. * testFileUploadField method
  6808. *
  6809. * Test generation of a file upload input.
  6810. *
  6811. * @return void
  6812. */
  6813. public function testFileUploadField()
  6814. {
  6815. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6816. $result = $this->Form->file('Model.upload');
  6817. $this->assertHtml($expected, $result);
  6818. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  6819. 'name' => '', 'type' => '', 'tmp_name' => '',
  6820. 'error' => 4, 'size' => 0,
  6821. ]));
  6822. $result = $this->Form->file('Model.upload');
  6823. $this->assertHtml($expected, $result);
  6824. $this->View->setRequest(
  6825. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  6826. );
  6827. $result = $this->Form->file('Model.upload');
  6828. $this->assertHtml($expected, $result);
  6829. }
  6830. /**
  6831. * testFileUploadOnOtherModel method
  6832. *
  6833. * Test File upload input on a model not used in create().
  6834. *
  6835. * @return void
  6836. */
  6837. public function testFileUploadOnOtherModel()
  6838. {
  6839. $this->Form->create($this->article, ['type' => 'file']);
  6840. $result = $this->Form->file('ValidateProfile.city');
  6841. $expected = [
  6842. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]'],
  6843. ];
  6844. $this->assertHtml($expected, $result);
  6845. }
  6846. /**
  6847. * testButton method
  6848. *
  6849. * Test generation of a form button.
  6850. *
  6851. * @return void
  6852. */
  6853. public function testButton()
  6854. {
  6855. $result = $this->Form->button('Hi');
  6856. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6857. $this->assertHtml($expected, $result);
  6858. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6859. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6860. $this->assertHtml($expected, $result);
  6861. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6862. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6863. $this->assertHtml($expected, $result);
  6864. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6865. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6866. $this->assertHtml($expected, $result);
  6867. $result = $this->Form->button('No type', ['type' => false]);
  6868. $expected = ['button' => [], 'No type', '/button'];
  6869. $this->assertHtml($expected, $result);
  6870. $result = $this->Form->button('Upload Text', [
  6871. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6872. 'escape' => false,
  6873. ]);
  6874. $this->assertNotRegExp('/\&039/', $result);
  6875. }
  6876. /**
  6877. * testButtonUnlockedByDefault method
  6878. *
  6879. * Test that button() makes unlocked fields by default.
  6880. *
  6881. * @return void
  6882. */
  6883. public function testButtonUnlockedByDefault()
  6884. {
  6885. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'secured'));
  6886. $this->Form->button('Save', ['name' => 'save']);
  6887. $this->Form->button('Clear');
  6888. $result = $this->Form->unlockField();
  6889. $this->assertEquals(['save'], $result);
  6890. }
  6891. /**
  6892. * Test generation of a form button with confirm message.
  6893. *
  6894. * @return void
  6895. */
  6896. public function testButtonWithConfirm()
  6897. {
  6898. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6899. $expected = ['button' => ['type' => 'submit', 'onclick' => 'if (confirm(&quot;Confirm me!&quot;)) { return true; } return false;'], 'Hi', '/button'];
  6900. $this->assertHtml($expected, $result);
  6901. }
  6902. /**
  6903. * testPostButton method
  6904. *
  6905. * @return void
  6906. */
  6907. public function testPostButton()
  6908. {
  6909. $result = $this->Form->postButton('Hi', '/controller/action');
  6910. $expected = [
  6911. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6912. 'div' => ['style' => 'display:none;'],
  6913. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6914. '/div',
  6915. 'button' => ['type' => 'submit'],
  6916. 'Hi',
  6917. '/button',
  6918. '/form',
  6919. ];
  6920. $this->assertHtml($expected, $result);
  6921. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6922. $this->assertContains('<input type="hidden" name="extra" value="value"', $result);
  6923. }
  6924. /**
  6925. * testPostButtonMethodType method
  6926. *
  6927. * @return void
  6928. */
  6929. public function testPostButtonMethodType()
  6930. {
  6931. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6932. $expected = [
  6933. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6934. 'div' => ['style' => 'display:none;'],
  6935. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6936. '/div',
  6937. 'button' => ['type' => 'submit'],
  6938. 'Hi',
  6939. '/button',
  6940. '/form',
  6941. ];
  6942. $this->assertHtml($expected, $result);
  6943. }
  6944. /**
  6945. * testPostButtonFormOptions method
  6946. *
  6947. * @return void
  6948. */
  6949. public function testPostButtonFormOptions()
  6950. {
  6951. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6952. $expected = [
  6953. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6954. 'div' => ['style' => 'display:none;'],
  6955. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6956. '/div',
  6957. 'button' => ['type' => 'submit'],
  6958. 'Hi',
  6959. '/button',
  6960. '/form',
  6961. ];
  6962. $this->assertHtml($expected, $result);
  6963. }
  6964. /**
  6965. * testPostButtonNestedData method
  6966. *
  6967. * Test using postButton with N dimensional data.
  6968. *
  6969. * @return void
  6970. */
  6971. public function testPostButtonNestedData()
  6972. {
  6973. $data = [
  6974. 'one' => [
  6975. 'two' => [
  6976. 3, 4, 5,
  6977. ],
  6978. ],
  6979. ];
  6980. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6981. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6982. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6983. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6984. }
  6985. /**
  6986. * testSecurePostButton method
  6987. *
  6988. * Test that postButton adds _Token fields.
  6989. *
  6990. * @return void
  6991. */
  6992. public function testSecurePostButton()
  6993. {
  6994. $this->View->setRequest($this->View->getRequest()
  6995. ->withParam('_csrfToken', 'testkey')
  6996. ->withParam('_Token.unlockedFields', []));
  6997. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6998. $tokenDebug = urlencode(json_encode([
  6999. '/posts/delete/1',
  7000. [],
  7001. [],
  7002. ]));
  7003. $expected = [
  7004. 'form' => [
  7005. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  7006. ],
  7007. ['div' => ['style' => 'display:none;']],
  7008. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7009. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7010. '/div',
  7011. 'button' => ['type' => 'submit'],
  7012. 'Delete',
  7013. '/button',
  7014. ['div' => ['style' => 'display:none;']],
  7015. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7016. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7017. ['input' => [
  7018. 'type' => 'hidden',
  7019. 'name' => '_Token[debug]',
  7020. 'value' => $tokenDebug,
  7021. 'autocomplete' => 'off',
  7022. ]],
  7023. '/div',
  7024. '/form',
  7025. ];
  7026. $this->assertHtml($expected, $result);
  7027. }
  7028. /**
  7029. * testPostLink method
  7030. *
  7031. * @return void
  7032. */
  7033. public function testPostLink()
  7034. {
  7035. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7036. $expected = [
  7037. 'form' => [
  7038. 'method' => 'post', 'action' => '/posts/delete/1',
  7039. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7040. ],
  7041. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7042. '/form',
  7043. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7044. 'Delete',
  7045. '/a',
  7046. ];
  7047. $this->assertHtml($expected, $result);
  7048. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  7049. $expected = [
  7050. 'form' => [
  7051. 'method' => 'post', 'action' => '/posts/delete/1',
  7052. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7053. ],
  7054. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  7055. '/form',
  7056. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7057. 'Delete',
  7058. '/a',
  7059. ];
  7060. $this->assertHtml($expected, $result);
  7061. $result = $this->Form->postLink(
  7062. 'Delete',
  7063. '/posts/delete/1',
  7064. ['target' => '_blank', 'class' => 'btn btn-danger']
  7065. );
  7066. $expected = [
  7067. 'form' => [
  7068. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  7069. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7070. ],
  7071. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7072. '/form',
  7073. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7074. 'Delete',
  7075. '/a',
  7076. ];
  7077. $this->assertHtml($expected, $result);
  7078. }
  7079. /**
  7080. * testPostLinkWithConfirm method
  7081. *
  7082. * Test the confirm option for postLink().
  7083. *
  7084. * @return void
  7085. */
  7086. public function testPostLinkWithConfirm()
  7087. {
  7088. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  7089. $expected = [
  7090. 'form' => [
  7091. 'method' => 'post', 'action' => '/posts/delete/1',
  7092. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7093. ],
  7094. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7095. '/form',
  7096. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  7097. 'Delete',
  7098. '/a',
  7099. ];
  7100. $this->assertHtml($expected, $result);
  7101. $result = $this->Form->postLink(
  7102. 'Delete',
  7103. '/posts/delete/1',
  7104. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  7105. );
  7106. $expected = [
  7107. 'form' => [
  7108. 'method' => 'post', 'action' => '/posts/delete/1',
  7109. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7110. ],
  7111. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7112. '/form',
  7113. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  7114. 'Delete',
  7115. '/a',
  7116. ];
  7117. $this->assertHtml($expected, $result);
  7118. $this->Form->setTemplates(['confirmJs' => 'if (confirm({{confirmMessage}})) { $(\'form[name="{{formName}}"]\').submit();};']);
  7119. $result = $this->Form->postLink(
  7120. 'Delete',
  7121. '/posts/delete/1',
  7122. ['escape' => false, 'confirm' => 'Confirm this deletion?']
  7123. );
  7124. $expected = [
  7125. 'form' => [
  7126. 'method' => 'post', 'action' => '/posts/delete/1',
  7127. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7128. ],
  7129. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7130. '/form',
  7131. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\("Confirm this deletion\?"\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/'],
  7132. 'Delete',
  7133. '/a',
  7134. ];
  7135. $this->assertHtml($expected, $result);
  7136. }
  7137. /**
  7138. * testPostLinkWithQuery method
  7139. *
  7140. * Test postLink() with query string args.
  7141. *
  7142. * @return void
  7143. */
  7144. public function testPostLinkWithQuery()
  7145. {
  7146. $result = $this->Form->postLink(
  7147. 'Delete',
  7148. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  7149. );
  7150. $expected = [
  7151. 'form' => [
  7152. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  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. }
  7163. /**
  7164. * testPostLinkWithData method
  7165. *
  7166. * Test postLink with additional data.
  7167. *
  7168. * @return void
  7169. */
  7170. public function testPostLinkWithData()
  7171. {
  7172. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  7173. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  7174. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  7175. $this->Form->create($entity);
  7176. $this->Form->end();
  7177. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  7178. $this->assertContains(
  7179. '<input type="hidden" name="name" value="show"',
  7180. $result,
  7181. 'should not contain entity data.'
  7182. );
  7183. }
  7184. /**
  7185. * testPostLinkSecurityHash method
  7186. *
  7187. * Test that security hashes for postLink include the url.
  7188. *
  7189. * @return void
  7190. */
  7191. public function testPostLinkSecurityHash()
  7192. {
  7193. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7194. $hash .= '%3Aid';
  7195. $this->View->setRequest($this->View->getRequest()->withParam('_Token.key', 'test'));
  7196. $result = $this->Form->postLink(
  7197. 'Delete',
  7198. '/posts/delete/1',
  7199. ['data' => ['id' => 1]]
  7200. );
  7201. $tokenDebug = urlencode(json_encode([
  7202. '/posts/delete/1',
  7203. [
  7204. 'id' => 1,
  7205. ],
  7206. [],
  7207. ]));
  7208. $expected = [
  7209. 'form' => [
  7210. 'method' => 'post', 'action' => '/posts/delete/1',
  7211. 'name', 'style' => 'display:none;',
  7212. ],
  7213. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7214. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7215. 'div' => ['style' => 'display:none;'],
  7216. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7217. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7218. ['input' => [
  7219. 'type' => 'hidden',
  7220. 'name' => '_Token[debug]',
  7221. 'value' => $tokenDebug,
  7222. 'autocomplete' => 'off',
  7223. ]],
  7224. '/div',
  7225. '/form',
  7226. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7227. 'Delete',
  7228. '/a',
  7229. ];
  7230. $this->assertHtml($expected, $result);
  7231. }
  7232. /**
  7233. * testPostLinkSecurityHashBlockMode method
  7234. *
  7235. * Test that postLink doesn't modify the fields in the containing form.
  7236. *
  7237. * postLink() calls inside open forms should not modify the field list
  7238. * for the form.
  7239. *
  7240. * @return void
  7241. */
  7242. public function testPostLinkSecurityHashBlockMode()
  7243. {
  7244. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  7245. $hash .= '%3A';
  7246. $this->View->setRequest($this->View->getRequest()->withParam('_Token.key', 'test'));
  7247. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  7248. $this->Form->control('title');
  7249. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7250. $result = $this->View->fetch('postLink');
  7251. $this->assertEquals(['title'], $this->Form->fields);
  7252. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  7253. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  7254. }
  7255. /**
  7256. * testPostLinkSecurityHashNoDebugMode method
  7257. *
  7258. * Test that security does not include debug token if debug is false.
  7259. *
  7260. * @return void
  7261. */
  7262. public function testPostLinkSecurityHashNoDebugMode()
  7263. {
  7264. Configure::write('debug', false);
  7265. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7266. $hash .= '%3Aid';
  7267. $this->View->setRequest($this->View->getRequest()
  7268. ->withParam('_Token.key', 'test'));
  7269. $result = $this->Form->postLink(
  7270. 'Delete',
  7271. '/posts/delete/1',
  7272. ['data' => ['id' => 1]]
  7273. );
  7274. $expected = [
  7275. 'form' => [
  7276. 'method' => 'post', 'action' => '/posts/delete/1',
  7277. 'name', 'style' => 'display:none;',
  7278. ],
  7279. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7280. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7281. 'div' => ['style' => 'display:none;'],
  7282. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7283. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7284. '/div',
  7285. '/form',
  7286. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7287. 'Delete',
  7288. '/a',
  7289. ];
  7290. $this->assertHtml($expected, $result);
  7291. }
  7292. /**
  7293. * testPostLinkNestedData method
  7294. *
  7295. * Test using postLink with N dimensional data.
  7296. *
  7297. * @return void
  7298. */
  7299. public function testPostLinkNestedData()
  7300. {
  7301. $data = [
  7302. 'one' => [
  7303. 'two' => [
  7304. 3, 4, 5,
  7305. ],
  7306. ],
  7307. ];
  7308. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7309. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7310. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7311. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7312. }
  7313. /**
  7314. * testPostLinkAfterGetForm method
  7315. *
  7316. * Test creating postLinks after a GET form.
  7317. *
  7318. * @return void
  7319. */
  7320. public function testPostLinkAfterGetForm()
  7321. {
  7322. $this->View->setRequest($this->View->getRequest()
  7323. ->withParam('_csrfToken', 'testkey')
  7324. ->withParam('_Token', 'val'));
  7325. $this->Form->create($this->article, ['type' => 'get']);
  7326. $this->Form->end();
  7327. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7328. $tokenDebug = urlencode(json_encode([
  7329. '/posts/delete/1',
  7330. [],
  7331. [],
  7332. ]));
  7333. $expected = [
  7334. 'form' => [
  7335. 'method' => 'post', 'action' => '/posts/delete/1',
  7336. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7337. ],
  7338. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7339. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7340. 'div' => ['style' => 'display:none;'],
  7341. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7342. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7343. ['input' => [
  7344. 'type' => 'hidden', 'name' => '_Token[debug]',
  7345. 'value' => $tokenDebug,
  7346. 'autocomplete' => 'off',
  7347. ]],
  7348. '/div',
  7349. '/form',
  7350. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7351. 'Delete',
  7352. '/a',
  7353. ];
  7354. $this->assertHtml($expected, $result);
  7355. }
  7356. /**
  7357. * testPostLinkFormBuffer method
  7358. *
  7359. * Test that postLink adds form tags to view block.
  7360. *
  7361. * @return void
  7362. */
  7363. public function testPostLinkFormBuffer()
  7364. {
  7365. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7366. $expected = [
  7367. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7368. 'Delete',
  7369. '/a',
  7370. ];
  7371. $this->assertHtml($expected, $result);
  7372. $result = $this->View->fetch('postLink');
  7373. $expected = [
  7374. 'form' => [
  7375. 'method' => 'post', 'action' => '/posts/delete/1',
  7376. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7377. ],
  7378. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7379. '/form',
  7380. ];
  7381. $this->assertHtml($expected, $result);
  7382. $result = $this->Form->postLink(
  7383. 'Delete',
  7384. '/posts/delete/2',
  7385. ['block' => true, 'method' => 'DELETE']
  7386. );
  7387. $expected = [
  7388. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7389. 'Delete',
  7390. '/a',
  7391. ];
  7392. $this->assertHtml($expected, $result);
  7393. $result = $this->View->fetch('postLink');
  7394. $expected = [
  7395. 'form' => [
  7396. 'method' => 'post', 'action' => '/posts/delete/1',
  7397. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7398. ],
  7399. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7400. '/form',
  7401. [
  7402. 'form' => [
  7403. 'method' => 'post', 'action' => '/posts/delete/2',
  7404. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7405. ],
  7406. ],
  7407. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7408. '/form',
  7409. ];
  7410. $this->assertHtml($expected, $result);
  7411. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7412. $expected = [
  7413. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7414. 'Delete',
  7415. '/a',
  7416. ];
  7417. $this->assertHtml($expected, $result);
  7418. $result = $this->View->fetch('foobar');
  7419. $expected = [
  7420. 'form' => [
  7421. 'method' => 'post', 'action' => '/posts/delete/1',
  7422. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7423. ],
  7424. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7425. '/form',
  7426. ];
  7427. $this->assertHtml($expected, $result);
  7428. }
  7429. /**
  7430. * testSubmitButton method
  7431. *
  7432. * @return void
  7433. */
  7434. public function testSubmitButton()
  7435. {
  7436. $result = $this->Form->submit('');
  7437. $expected = [
  7438. 'div' => ['class' => 'submit'],
  7439. 'input' => ['type' => 'submit', 'value' => ''],
  7440. '/div',
  7441. ];
  7442. $this->assertHtml($expected, $result);
  7443. $result = $this->Form->submit('Test Submit');
  7444. $expected = [
  7445. 'div' => ['class' => 'submit'],
  7446. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7447. '/div',
  7448. ];
  7449. $this->assertHtml($expected, $result);
  7450. $result = $this->Form->submit('Next >');
  7451. $expected = [
  7452. 'div' => ['class' => 'submit'],
  7453. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7454. '/div',
  7455. ];
  7456. $this->assertHtml($expected, $result);
  7457. $result = $this->Form->submit('Next >', ['escape' => false]);
  7458. $expected = [
  7459. 'div' => ['class' => 'submit'],
  7460. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7461. '/div',
  7462. ];
  7463. $this->assertHtml($expected, $result);
  7464. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7465. $expected = [
  7466. 'div' => ['class' => 'submit'],
  7467. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7468. '/div',
  7469. ];
  7470. $this->assertHtml($expected, $result);
  7471. }
  7472. /**
  7473. * testSubmitImage method
  7474. *
  7475. * Test image submit types.
  7476. *
  7477. * @return void
  7478. */
  7479. public function testSubmitImage()
  7480. {
  7481. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7482. $expected = [
  7483. 'div' => ['class' => 'submit'],
  7484. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7485. '/div',
  7486. ];
  7487. $this->assertHtml($expected, $result);
  7488. $result = $this->Form->submit('/relative/cake.power.gif');
  7489. $expected = [
  7490. 'div' => ['class' => 'submit'],
  7491. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7492. '/div',
  7493. ];
  7494. $this->assertHtml($expected, $result);
  7495. $result = $this->Form->submit('cake.power.gif');
  7496. $expected = [
  7497. 'div' => ['class' => 'submit'],
  7498. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7499. '/div',
  7500. ];
  7501. $this->assertHtml($expected, $result);
  7502. $result = $this->Form->submit('Not.an.image');
  7503. $expected = [
  7504. 'div' => ['class' => 'submit'],
  7505. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7506. '/div',
  7507. ];
  7508. $this->assertHtml($expected, $result);
  7509. }
  7510. /**
  7511. * testSubmitUnlockedByDefault method
  7512. *
  7513. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7514. * be submitted at a time.
  7515. *
  7516. * @return void
  7517. */
  7518. public function testSubmitUnlockedByDefault()
  7519. {
  7520. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'secured'));
  7521. $this->Form->submit('Go go');
  7522. $this->Form->submit('Save', ['name' => 'save']);
  7523. $result = $this->Form->unlockField();
  7524. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7525. }
  7526. /**
  7527. * testSubmitImageTimestamp method
  7528. *
  7529. * Test submit image with timestamps.
  7530. *
  7531. * @return void
  7532. */
  7533. public function testSubmitImageTimestamp()
  7534. {
  7535. Configure::write('Asset.timestamp', 'force');
  7536. $result = $this->Form->submit('cake.power.gif');
  7537. $expected = [
  7538. 'div' => ['class' => 'submit'],
  7539. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7540. '/div',
  7541. ];
  7542. $this->assertHtml($expected, $result);
  7543. }
  7544. /**
  7545. * testDateTimeWithGetForms method
  7546. *
  7547. * Test that datetime() works with GET style forms.
  7548. *
  7549. * @return void
  7550. */
  7551. public function testDateTimeWithGetForms()
  7552. {
  7553. extract($this->dateRegex);
  7554. $this->Form->create($this->article, ['type' => 'get']);
  7555. $result = $this->Form->datetime('created');
  7556. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7557. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7558. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7559. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7560. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7561. }
  7562. /**
  7563. * testForMagicControlNonExistingNorValidated method
  7564. *
  7565. * @return void
  7566. */
  7567. public function testForMagicControlNonExistingNorValidated()
  7568. {
  7569. $this->Form->create($this->article);
  7570. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7571. $result = $this->Form->control('non_existing_nor_validated');
  7572. $expected = [
  7573. 'label' => ['for' => 'non-existing-nor-validated'],
  7574. 'Non Existing Nor Validated',
  7575. '/label',
  7576. 'input' => [
  7577. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7578. 'id' => 'non-existing-nor-validated',
  7579. ],
  7580. ];
  7581. $this->assertHtml($expected, $result);
  7582. $result = $this->Form->control('non_existing_nor_validated', [
  7583. 'val' => 'my value',
  7584. ]);
  7585. $expected = [
  7586. 'label' => ['for' => 'non-existing-nor-validated'],
  7587. 'Non Existing Nor Validated',
  7588. '/label',
  7589. 'input' => [
  7590. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7591. 'value' => 'my value', 'id' => 'non-existing-nor-validated',
  7592. ],
  7593. ];
  7594. $this->assertHtml($expected, $result);
  7595. $this->View->setRequest(
  7596. $this->View->getRequest()->withData('non_existing_nor_validated', 'CakePHP magic')
  7597. );
  7598. $this->Form->create($this->article);
  7599. $result = $this->Form->control('non_existing_nor_validated');
  7600. $expected = [
  7601. 'label' => ['for' => 'non-existing-nor-validated'],
  7602. 'Non Existing Nor Validated',
  7603. '/label',
  7604. 'input' => [
  7605. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7606. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated',
  7607. ],
  7608. ];
  7609. $this->assertHtml($expected, $result);
  7610. }
  7611. /**
  7612. * testFormMagicControlLabel method
  7613. *
  7614. * @return void
  7615. */
  7616. public function testFormMagicControlLabel()
  7617. {
  7618. $this->getTableLocator()->get('Contacts', [
  7619. 'className' => __NAMESPACE__ . '\ContactsTable',
  7620. ]);
  7621. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7622. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7623. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7624. $expected = [
  7625. 'label' => ['for' => 'contacts-name'],
  7626. 'My label',
  7627. '/label',
  7628. 'input' => [
  7629. 'type' => 'text', 'name' => 'Contacts[name]',
  7630. 'id' => 'contacts-name', 'maxlength' => '255',
  7631. ],
  7632. ];
  7633. $this->assertHtml($expected, $result);
  7634. $result = $this->Form->control('name', [
  7635. 'label' => ['class' => 'mandatory'],
  7636. ]);
  7637. $expected = [
  7638. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7639. 'Name',
  7640. '/label',
  7641. 'input' => [
  7642. 'type' => 'text', 'name' => 'name',
  7643. 'id' => 'name', 'maxlength' => '255',
  7644. ],
  7645. ];
  7646. $this->assertHtml($expected, $result);
  7647. $result = $this->Form->control('name', [
  7648. 'div' => false,
  7649. 'label' => ['class' => 'mandatory', 'text' => 'My label'],
  7650. ]);
  7651. $expected = [
  7652. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7653. 'My label',
  7654. '/label',
  7655. 'input' => [
  7656. 'type' => 'text', 'name' => 'name',
  7657. 'id' => 'name', 'maxlength' => '255',
  7658. ],
  7659. ];
  7660. $this->assertHtml($expected, $result);
  7661. $result = $this->Form->control('Contact.name', [
  7662. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id'],
  7663. ]);
  7664. $expected = [
  7665. 'label' => ['for' => 'my_id'],
  7666. 'Name',
  7667. '/label',
  7668. 'input' => [
  7669. 'type' => 'text', 'name' => 'Contact[name]',
  7670. 'id' => 'my_id', 'maxlength' => '255',
  7671. ],
  7672. ];
  7673. $this->assertHtml($expected, $result);
  7674. $result = $this->Form->control('1.id');
  7675. $expected = ['input' => [
  7676. 'type' => 'hidden', 'name' => '1[id]',
  7677. 'id' => '1-id',
  7678. ]];
  7679. $this->assertHtml($expected, $result);
  7680. $result = $this->Form->control('1.name');
  7681. $expected = [
  7682. 'label' => ['for' => '1-name'],
  7683. 'Name',
  7684. '/label',
  7685. 'input' => [
  7686. 'type' => 'text', 'name' => '1[name]',
  7687. 'id' => '1-name', 'maxlength' => '255',
  7688. ],
  7689. ];
  7690. $this->assertHtml($expected, $result);
  7691. }
  7692. /**
  7693. * testFormEnd method
  7694. *
  7695. * @return void
  7696. */
  7697. public function testFormEnd()
  7698. {
  7699. $this->assertEquals('</form>', $this->Form->end());
  7700. }
  7701. /**
  7702. * testMultiRecordForm method
  7703. *
  7704. * Test the generation of fields for a multi record form.
  7705. *
  7706. * @return void
  7707. */
  7708. public function testMultiRecordForm()
  7709. {
  7710. $this->loadFixtures('Articles', 'Comments');
  7711. $articles = $this->getTableLocator()->get('Articles');
  7712. $articles->hasMany('Comments');
  7713. $comment = new Entity(['comment' => 'Value']);
  7714. $article = new Article(['comments' => [$comment]]);
  7715. $this->Form->create([$article]);
  7716. $result = $this->Form->control('0.comments.1.comment');
  7717. //@codingStandardsIgnoreStart
  7718. $expected = [
  7719. 'div' => ['class' => 'input textarea'],
  7720. 'label' => ['for' => '0-comments-1-comment'],
  7721. 'Comment',
  7722. '/label',
  7723. 'textarea' => [
  7724. 'name',
  7725. 'id' => '0-comments-1-comment',
  7726. 'rows' => 5
  7727. ],
  7728. '/textarea',
  7729. '/div'
  7730. ];
  7731. //@codingStandardsIgnoreEnd
  7732. $this->assertHtml($expected, $result);
  7733. $result = $this->Form->control('0.comments.0.comment');
  7734. //@codingStandardsIgnoreStart
  7735. $expected = [
  7736. 'div' => ['class' => 'input textarea'],
  7737. 'label' => ['for' => '0-comments-0-comment'],
  7738. 'Comment',
  7739. '/label',
  7740. 'textarea' => [
  7741. 'name',
  7742. 'id' => '0-comments-0-comment',
  7743. 'rows' => 5
  7744. ],
  7745. 'Value',
  7746. '/textarea',
  7747. '/div'
  7748. ];
  7749. //@codingStandardsIgnoreEnd
  7750. $this->assertHtml($expected, $result);
  7751. $comment->setError('comment', ['Not valid']);
  7752. $result = $this->Form->control('0.comments.0.comment');
  7753. //@codingStandardsIgnoreStart
  7754. $expected = [
  7755. 'div' => ['class' => 'input textarea error'],
  7756. 'label' => ['for' => '0-comments-0-comment'],
  7757. 'Comment',
  7758. '/label',
  7759. 'textarea' => [
  7760. 'name',
  7761. 'class' => 'form-error',
  7762. 'id' => '0-comments-0-comment',
  7763. 'rows' => 5
  7764. ],
  7765. 'Value',
  7766. '/textarea',
  7767. ['div' => ['class' => 'error-message']],
  7768. 'Not valid',
  7769. '/div',
  7770. '/div'
  7771. ];
  7772. //@codingStandardsIgnoreEnd
  7773. $this->assertHtml($expected, $result);
  7774. $this->getTableLocator()->get('Comments')
  7775. ->getValidator('default')
  7776. ->allowEmpty('comment', false);
  7777. $result = $this->Form->control('0.comments.1.comment');
  7778. //@codingStandardsIgnoreStart
  7779. $expected = [
  7780. 'div' => ['class' => 'input textarea required'],
  7781. 'label' => ['for' => '0-comments-1-comment'],
  7782. 'Comment',
  7783. '/label',
  7784. 'textarea' => [
  7785. 'name',
  7786. 'required' => 'required',
  7787. 'id' => '0-comments-1-comment',
  7788. 'rows' => 5,
  7789. ],
  7790. '/textarea',
  7791. '/div'
  7792. ];
  7793. //@codingStandardsIgnoreEnd
  7794. $this->assertHtml($expected, $result);
  7795. }
  7796. /**
  7797. * testHtml5Controls method
  7798. *
  7799. * Test that some html5 inputs + FormHelper::__call() work.
  7800. *
  7801. * @return void
  7802. */
  7803. public function testHtml5Controls()
  7804. {
  7805. $result = $this->Form->email('User.email');
  7806. $expected = [
  7807. 'input' => ['type' => 'email', 'name' => 'User[email]'],
  7808. ];
  7809. $this->assertHtml($expected, $result);
  7810. $result = $this->Form->search('User.query');
  7811. $expected = [
  7812. 'input' => ['type' => 'search', 'name' => 'User[query]'],
  7813. ];
  7814. $this->assertHtml($expected, $result);
  7815. $result = $this->Form->search('User.query', ['value' => 'test']);
  7816. $expected = [
  7817. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test'],
  7818. ];
  7819. $this->assertHtml($expected, $result);
  7820. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7821. $expected = [
  7822. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test'],
  7823. ];
  7824. $this->assertHtml($expected, $result);
  7825. }
  7826. /**
  7827. * testHtml5ControlWithControl method
  7828. *
  7829. * Test accessing html5 inputs through control().
  7830. *
  7831. * @return void
  7832. */
  7833. public function testHtml5ControlWithControl()
  7834. {
  7835. $this->Form->create();
  7836. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7837. $result = $this->Form->control('website', [
  7838. 'type' => 'url',
  7839. 'val' => 'http://domain.tld',
  7840. 'label' => false,
  7841. ]);
  7842. $expected = [
  7843. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld'],
  7844. ];
  7845. $this->assertHtml($expected, $result);
  7846. }
  7847. /**
  7848. * testHtml5ControlException method
  7849. *
  7850. * Test errors when field name is missing.
  7851. *
  7852. * @return void
  7853. */
  7854. public function testHtml5ControlException()
  7855. {
  7856. $this->expectException(\Cake\Core\Exception\Exception::class);
  7857. $this->Form->email();
  7858. }
  7859. /**
  7860. * tests fields that are required use custom validation messages
  7861. *
  7862. * @return void
  7863. */
  7864. public function testHtml5ErrorMessage()
  7865. {
  7866. $this->Form->setConfig('autoSetCustomValidity', true);
  7867. $validator = (new \Cake\Validation\Validator())
  7868. ->requirePresence('email', true, 'Custom error message')
  7869. ->requirePresence('password')
  7870. ->alphaNumeric('password')
  7871. ->notBlank('phone');
  7872. $table = $this->getTableLocator()->get('Contacts', [
  7873. 'className' => __NAMESPACE__ . '\ContactsTable',
  7874. ]);
  7875. $table->setValidator('default', $validator);
  7876. $contact = new Entity();
  7877. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7878. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7879. $result = $this->Form->control('password');
  7880. $expected = [
  7881. 'label' => ['for' => 'password'],
  7882. 'Password',
  7883. '/label',
  7884. 'input' => [
  7885. 'id' => 'password',
  7886. 'name' => 'password',
  7887. 'type' => 'password',
  7888. 'value' => '',
  7889. 'required' => 'required',
  7890. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  7891. 'oninvalid' => 'this.setCustomValidity(&#039;This field is required&#039;)',
  7892. ],
  7893. ];
  7894. $this->assertHtml($expected, $result);
  7895. $result = $this->Form->control('phone');
  7896. $expected = [
  7897. 'label' => ['for' => 'phone'],
  7898. 'Phone',
  7899. '/label',
  7900. 'input' => [
  7901. 'id' => 'phone',
  7902. 'name' => 'phone',
  7903. 'type' => 'tel',
  7904. 'value' => '',
  7905. 'maxlength' => 255,
  7906. 'required' => 'required',
  7907. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  7908. 'oninvalid' => 'this.setCustomValidity(&#039;This field cannot be left empty&#039;)',
  7909. ],
  7910. ];
  7911. $this->assertHtml($expected, $result);
  7912. $result = $this->Form->control('email');
  7913. $expected = [
  7914. 'label' => ['for' => 'email'],
  7915. 'Email',
  7916. '/label',
  7917. 'input' => [
  7918. 'id' => 'email',
  7919. 'name' => 'email',
  7920. 'type' => 'email',
  7921. 'value' => '',
  7922. 'maxlength' => 255,
  7923. 'required' => 'required',
  7924. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  7925. 'oninvalid' => 'this.setCustomValidity(&#039;Custom error message&#039;)',
  7926. ],
  7927. ];
  7928. $this->assertHtml($expected, $result);
  7929. }
  7930. /**
  7931. * tests that custom validation messages are in templateVars
  7932. *
  7933. * @return void
  7934. */
  7935. public function testHtml5ErrorMessageInTemplateVars()
  7936. {
  7937. $validator = (new \Cake\Validation\Validator())
  7938. ->requirePresence('email', true, 'Custom error "message" & entities')
  7939. ->requirePresence('password')
  7940. ->alphaNumeric('password')
  7941. ->notBlank('phone');
  7942. $table = $this->getTableLocator()->get('Contacts', [
  7943. 'className' => __NAMESPACE__ . '\ContactsTable',
  7944. ]);
  7945. $table->setValidator('default', $validator);
  7946. $contact = new Entity();
  7947. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7948. $this->Form->setTemplates([
  7949. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  7950. 'inputContainer' => '{{content}}',
  7951. ]);
  7952. $result = $this->Form->control('password');
  7953. $expected = [
  7954. 'label' => ['for' => 'password'],
  7955. 'Password',
  7956. '/label',
  7957. 'input' => [
  7958. 'id' => 'password',
  7959. 'name' => 'password',
  7960. 'type' => 'password',
  7961. 'value' => '',
  7962. 'required' => 'required',
  7963. 'data-message' => 'This field is required',
  7964. ],
  7965. ];
  7966. $this->assertHtml($expected, $result);
  7967. $result = $this->Form->control('phone');
  7968. $expected = [
  7969. 'label' => ['for' => 'phone'],
  7970. 'Phone',
  7971. '/label',
  7972. 'input' => [
  7973. 'id' => 'phone',
  7974. 'name' => 'phone',
  7975. 'type' => 'tel',
  7976. 'value' => '',
  7977. 'maxlength' => 255,
  7978. 'required' => 'required',
  7979. 'data-message' => 'This field cannot be left empty',
  7980. ],
  7981. ];
  7982. $this->assertHtml($expected, $result);
  7983. $result = $this->Form->control('email', [
  7984. 'templateVars' => [
  7985. 'custom' => 'data-custom="1"',
  7986. ],
  7987. ]);
  7988. $expected = [
  7989. 'label' => ['for' => 'email'],
  7990. 'Email',
  7991. '/label',
  7992. 'input' => [
  7993. 'id' => 'email',
  7994. 'name' => 'email',
  7995. 'type' => 'email',
  7996. 'value' => '',
  7997. 'maxlength' => 255,
  7998. 'required' => 'required',
  7999. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  8000. 'data-custom' => '1',
  8001. ],
  8002. ];
  8003. $this->assertHtml($expected, $result);
  8004. }
  8005. /**
  8006. * testRequiredAttribute method
  8007. *
  8008. * Tests that formhelper sets required attributes.
  8009. *
  8010. * @return void
  8011. */
  8012. public function testRequiredAttribute()
  8013. {
  8014. $this->article['required'] = [
  8015. 'title' => true,
  8016. 'body' => false,
  8017. ];
  8018. $this->Form->create($this->article);
  8019. $result = $this->Form->control('title');
  8020. $expected = [
  8021. 'div' => ['class' => 'input text required'],
  8022. 'label' => ['for' => 'title'],
  8023. 'Title',
  8024. '/label',
  8025. 'input' => [
  8026. 'type' => 'text',
  8027. 'name' => 'title',
  8028. 'id' => 'title',
  8029. 'required' => 'required',
  8030. ],
  8031. '/div',
  8032. ];
  8033. $this->assertHtml($expected, $result);
  8034. $result = $this->Form->control('title', ['required' => false]);
  8035. $this->assertNotContains('required', $result);
  8036. $result = $this->Form->control('body');
  8037. $expected = [
  8038. 'div' => ['class' => 'input text'],
  8039. 'label' => ['for' => 'body'],
  8040. 'Body',
  8041. '/label',
  8042. 'input' => [
  8043. 'type' => 'text',
  8044. 'name' => 'body',
  8045. 'id' => 'body',
  8046. ],
  8047. '/div',
  8048. ];
  8049. $this->assertHtml($expected, $result);
  8050. $result = $this->Form->control('body', ['required' => true]);
  8051. $this->assertContains('required', $result);
  8052. }
  8053. /**
  8054. * testControlsNotNested method
  8055. *
  8056. * Tests that it is possible to put inputs outside of the label.
  8057. *
  8058. * @return void
  8059. */
  8060. public function testControlsNotNested()
  8061. {
  8062. $this->Form->setTemplates([
  8063. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  8064. 'formGroup' => '{{input}}{{label}}',
  8065. ]);
  8066. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  8067. $expected = [
  8068. 'div' => ['class' => 'input checkbox'],
  8069. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  8070. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  8071. 'label' => ['for' => 'foo'],
  8072. 'Foo',
  8073. '/label',
  8074. '/div',
  8075. ];
  8076. $this->assertHtml($expected, $result);
  8077. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  8078. $expected = [
  8079. 'div' => ['class' => 'input checkbox'],
  8080. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  8081. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  8082. '/div',
  8083. ];
  8084. $this->assertHtml($expected, $result);
  8085. $result = $this->Form->control('confirm', [
  8086. 'type' => 'radio',
  8087. 'options' => ['Y' => 'Yes', 'N' => 'No'],
  8088. ]);
  8089. $expected = [
  8090. 'div' => ['class' => 'input radio'],
  8091. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  8092. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  8093. ['label' => ['for' => 'confirm-y']],
  8094. 'Yes',
  8095. '/label',
  8096. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  8097. ['label' => ['for' => 'confirm-n']],
  8098. 'No',
  8099. '/label',
  8100. '<label',
  8101. 'Confirm',
  8102. '/label',
  8103. '/div',
  8104. ];
  8105. $this->assertHtml($expected, $result);
  8106. $result = $this->Form->select('category', ['1', '2'], [
  8107. 'multiple' => 'checkbox',
  8108. 'name' => 'fish',
  8109. ]);
  8110. $expected = [
  8111. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  8112. ['div' => ['class' => 'checkbox']],
  8113. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  8114. ['label' => ['for' => 'fish-0']],
  8115. '1',
  8116. '/label',
  8117. '/div',
  8118. ['div' => ['class' => 'checkbox']],
  8119. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  8120. ['label' => ['for' => 'fish-1']],
  8121. '2',
  8122. '/label',
  8123. '/div',
  8124. ];
  8125. $this->assertHtml($expected, $result);
  8126. }
  8127. /**
  8128. * testControlContainerTemplates method
  8129. *
  8130. * Test that *Container templates are used by input.
  8131. *
  8132. * @return void
  8133. */
  8134. public function testControlContainerTemplates()
  8135. {
  8136. $this->Form->setTemplates([
  8137. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  8138. 'radioContainer' => '<div class="rad">{{content}}</div>',
  8139. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  8140. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  8141. ]);
  8142. $this->article['errors'] = [
  8143. 'Article' => ['published' => 'error message'],
  8144. ];
  8145. $this->Form->create($this->article);
  8146. $result = $this->Form->control('accept', [
  8147. 'type' => 'checkbox',
  8148. ]);
  8149. $expected = [
  8150. 'div' => ['class' => 'check'],
  8151. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  8152. 'label' => ['for' => 'accept'],
  8153. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  8154. 'Accept',
  8155. '/label',
  8156. '/div',
  8157. ];
  8158. $this->assertHtml($expected, $result);
  8159. $result = $this->Form->control('accept', [
  8160. 'type' => 'radio',
  8161. 'options' => ['Y', 'N'],
  8162. ]);
  8163. $this->assertContains('<div class="rad">', $result);
  8164. $result = $this->Form->control('Article.published', [
  8165. 'type' => 'radio',
  8166. 'options' => ['Y', 'N'],
  8167. ]);
  8168. $this->assertContains('<div class="rad err">', $result);
  8169. $result = $this->Form->control('Article.created', [
  8170. 'type' => 'datetime',
  8171. ]);
  8172. $this->assertContains('<div class="dt">', $result);
  8173. }
  8174. /**
  8175. * testFormGroupTemplates method
  8176. *
  8177. * Test that *Container templates are used by input.
  8178. *
  8179. * @return void
  8180. */
  8181. public function testFormGroupTemplates()
  8182. {
  8183. $this->Form->setTemplates([
  8184. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  8185. ]);
  8186. $this->Form->create($this->article);
  8187. $result = $this->Form->control('accept', [
  8188. 'type' => 'radio',
  8189. 'options' => ['Y', 'N'],
  8190. ]);
  8191. $this->assertContains('<div class="radio">', $result);
  8192. }
  8193. /**
  8194. * testResetTemplates method
  8195. *
  8196. * Test resetting templates.
  8197. *
  8198. * @return void
  8199. */
  8200. public function testResetTemplates()
  8201. {
  8202. $this->Form->setTemplates(['input' => '<input/>']);
  8203. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  8204. $this->Form->resetTemplates();
  8205. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  8206. }
  8207. /**
  8208. * testContext method
  8209. *
  8210. * Test the context method.
  8211. *
  8212. * @return void
  8213. */
  8214. public function testContext()
  8215. {
  8216. $result = $this->Form->context();
  8217. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  8218. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  8219. $this->assertSame($mock, $this->Form->context($mock));
  8220. $this->assertSame($mock, $this->Form->context());
  8221. }
  8222. /**
  8223. * testAutoDomId method
  8224. *
  8225. * @return void
  8226. */
  8227. public function testAutoDomId()
  8228. {
  8229. $result = $this->Form->text('field', ['id' => true]);
  8230. $expected = [
  8231. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  8232. ];
  8233. $this->assertHtml($expected, $result);
  8234. // Ensure id => doesn't cause problem when multiple inputs are generated.
  8235. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  8236. $expected = [
  8237. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  8238. ['label' => ['for' => 'field-0']],
  8239. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  8240. 'option A',
  8241. '/label',
  8242. ['label' => ['for' => 'field-1']],
  8243. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  8244. 'option B',
  8245. '/label',
  8246. ];
  8247. $this->assertHtml($expected, $result);
  8248. $result = $this->Form->select(
  8249. 'multi_field',
  8250. ['first', 'second'],
  8251. ['multiple' => 'checkbox', 'id' => true]
  8252. );
  8253. $expected = [
  8254. 'input' => [
  8255. 'type' => 'hidden', 'name' => 'multi_field', 'value' => '',
  8256. ],
  8257. ['div' => ['class' => 'checkbox']],
  8258. ['label' => ['for' => 'multi-field-0']],
  8259. ['input' => [
  8260. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8261. 'value' => '0', 'id' => 'multi-field-0',
  8262. ]],
  8263. 'first',
  8264. '/label',
  8265. '/div',
  8266. ['div' => ['class' => 'checkbox']],
  8267. ['label' => ['for' => 'multi-field-1']],
  8268. ['input' => [
  8269. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8270. 'value' => '1', 'id' => 'multi-field-1',
  8271. ]],
  8272. 'second',
  8273. '/label',
  8274. '/div',
  8275. ];
  8276. $this->assertHtml($expected, $result);
  8277. }
  8278. /**
  8279. * Test the basic setters and getters for value sources
  8280. *
  8281. * @return void
  8282. */
  8283. public function testFormValueSourcesSettersGetters()
  8284. {
  8285. $this->View->setRequest($this->View->getRequest()
  8286. ->withData('id', '1')
  8287. ->withQueryParams(['id' => '2']));
  8288. $expected = ['context'];
  8289. $result = $this->Form->getValueSources();
  8290. $this->assertEquals($expected, $result);
  8291. $expected = ['query', 'data', 'context'];
  8292. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  8293. $result = $this->Form->getValueSources();
  8294. $this->assertEquals($expected, $result);
  8295. $this->Form->setValueSources(['context']);
  8296. $expected = '1';
  8297. $result = $this->Form->getSourceValue('id');
  8298. $this->assertEquals($expected, $result);
  8299. $this->Form->setValueSources('query');
  8300. $expected = ['query'];
  8301. $result = $this->Form->getValueSources();
  8302. $this->assertEquals($expected, $result);
  8303. $expected = '2';
  8304. $result = $this->Form->getSourceValue('id');
  8305. $this->assertEquals($expected, $result);
  8306. $this->Form->setValueSources(['data']);
  8307. $expected = '1';
  8308. $result = $this->Form->getSourceValue('id');
  8309. $this->assertEquals($expected, $result);
  8310. $this->Form->setValueSources(['query', 'data']);
  8311. $expected = '2';
  8312. $result = $this->Form->getSourceValue('id');
  8313. $this->assertEquals($expected, $result);
  8314. }
  8315. /**
  8316. * Tests the different input rendering values based on sources values switching
  8317. *
  8318. * @return void
  8319. */
  8320. public function testFormValueSourcesSingleSwitchRendering()
  8321. {
  8322. $this->loadFixtures('Articles');
  8323. $articles = $this->getTableLocator()->get('Articles');
  8324. $article = new Article();
  8325. $articles->patchEntity($article, ['id' => '3']);
  8326. $this->Form->create($article);
  8327. $this->Form->setValueSources(['context']);
  8328. $result = $this->Form->control('id');
  8329. $expected = [
  8330. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8331. ];
  8332. $this->assertHtml($expected, $result);
  8333. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  8334. $this->Form->setValueSources(['query']);
  8335. $result = $this->Form->control('id');
  8336. $expected = [
  8337. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8338. ];
  8339. $this->assertHtml($expected, $result);
  8340. $this->View->setRequest($this->View->getRequest()
  8341. ->withQueryParams(['id' => '5a'])
  8342. ->withData('id', '5b'));
  8343. $this->Form->setValueSources(['context']);
  8344. $this->Form->create($article);
  8345. $result = $this->Form->control('id');
  8346. $expected = [
  8347. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8348. ];
  8349. $this->assertHtml($expected, $result);
  8350. $this->Form->setValueSources(['data']);
  8351. $this->Form->create($article);
  8352. $result = $this->Form->control('id');
  8353. $expected = [
  8354. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8355. ];
  8356. $this->assertHtml($expected, $result);
  8357. $this->Form->setValueSources(['query']);
  8358. $result = $this->Form->control('id');
  8359. $expected = [
  8360. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  8361. ];
  8362. $this->assertHtml($expected, $result);
  8363. }
  8364. /**
  8365. * Tests the different input rendering values based on sources values switching while supplying
  8366. * an entity (base context) and multiple sources (such as data, query)
  8367. *
  8368. * @return void
  8369. */
  8370. public function testFormValueSourcesListSwitchRendering()
  8371. {
  8372. $this->loadFixtures('Articles');
  8373. $articles = $this->getTableLocator()->get('Articles');
  8374. $article = new Article();
  8375. $articles->patchEntity($article, ['id' => '3']);
  8376. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  8377. $this->Form->create($article);
  8378. $this->Form->setValueSources(['context', 'query']);
  8379. $result = $this->Form->control('id');
  8380. $expected = [
  8381. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8382. ];
  8383. $this->assertHtml($expected, $result);
  8384. $this->Form->setValueSources(['query', 'context']);
  8385. $result = $this->Form->control('id');
  8386. $expected = [
  8387. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8388. ];
  8389. $this->assertHtml($expected, $result);
  8390. $this->Form->setValueSources(['data', 'query', 'context']);
  8391. $result = $this->Form->control('id');
  8392. $expected = [
  8393. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8394. ];
  8395. $this->assertHtml($expected, $result);
  8396. $this->View->setRequest($this->View->getRequest()
  8397. ->withData('id', '8')
  8398. ->withQueryParams(['id' => '9']));
  8399. $this->Form->setValueSources(['data', 'query', 'context']);
  8400. $result = $this->Form->control('id');
  8401. $expected = [
  8402. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  8403. ];
  8404. $this->assertHtml($expected, $result);
  8405. }
  8406. /**
  8407. * Test the different form input renderings based on values sources switchings through form options
  8408. *
  8409. * @return void
  8410. */
  8411. public function testFormValueSourcesSwitchViaOptionsRendering()
  8412. {
  8413. $this->loadFixtures('Articles');
  8414. $articles = $this->getTableLocator()->get('Articles');
  8415. $article = new Article();
  8416. $articles->patchEntity($article, ['id' => '3']);
  8417. $this->View->setRequest(
  8418. $this->View->getRequest()->withData('id', 4)->withQueryParams(['id' => 5])
  8419. );
  8420. $this->Form->create($article, ['valueSources' => 'query']);
  8421. $result = $this->Form->control('id');
  8422. $expected = [
  8423. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8424. ];
  8425. $this->assertHtml($expected, $result);
  8426. $result = $this->Form->getSourceValue('id');
  8427. $this->assertEquals('5', $result);
  8428. $this->Form->setValueSources(['context']);
  8429. $this->Form->create($article, ['valueSources' => 'query']);
  8430. $result = $this->Form->control('id');
  8431. $expected = [
  8432. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8433. ];
  8434. $this->assertHtml($expected, $result);
  8435. $result = $this->Form->getSourceValue('id');
  8436. $this->assertEquals('5', $result);
  8437. $this->Form->setValueSources(['query']);
  8438. $this->Form->create($article, ['valueSources' => 'data']);
  8439. $result = $this->Form->control('id');
  8440. $expected = [
  8441. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8442. ];
  8443. $this->assertHtml($expected, $result);
  8444. $result = $this->Form->getSourceValue('id');
  8445. $this->assertEquals('4', $result);
  8446. $this->Form->setValueSources(['query']);
  8447. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8448. $result = $this->Form->control('id');
  8449. $expected = [
  8450. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8451. ];
  8452. $this->assertHtml($expected, $result);
  8453. $result = $this->Form->getSourceValue('id');
  8454. $this->assertEquals('4', $result);
  8455. }
  8456. /**
  8457. * Test the different form input renderings based on values sources switchings through form options
  8458. *
  8459. * @return void
  8460. */
  8461. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8462. {
  8463. $this->loadFixtures('Articles');
  8464. $articles = $this->getTableLocator()->get('Articles');
  8465. $article = new Article();
  8466. $articles->patchEntity($article, ['id' => '3']);
  8467. $this->View->setRequest(
  8468. $this->View->getRequest()->withData('id', 10)->withQueryParams(['id' => 11])
  8469. );
  8470. $this->Form->setValueSources(['context'])
  8471. ->create($article, ['valueSources' => ['query', 'data']]);
  8472. $result = $this->Form->control('id');
  8473. $expected = [
  8474. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8475. ];
  8476. $this->assertHtml($expected, $result);
  8477. $result = $this->Form->getSourceValue('id');
  8478. $this->assertEquals('11', $result);
  8479. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  8480. $this->Form->setValueSources(['context'])
  8481. ->create($article, ['valueSources' => ['query', 'data']]);
  8482. $result = $this->Form->control('id');
  8483. $expected = [
  8484. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8485. ];
  8486. $this->assertHtml($expected, $result);
  8487. $result = $this->Form->getSourceValue('id');
  8488. $this->assertEquals('10', $result);
  8489. }
  8490. /**
  8491. * Test the different form values sources resetting through From::end();
  8492. *
  8493. * @return void
  8494. */
  8495. public function testFormValueSourcesResetViaEnd()
  8496. {
  8497. $expected = ['context'];
  8498. $result = $this->Form->getValueSources();
  8499. $this->assertEquals($expected, $result);
  8500. $expected = ['query', 'context', 'data'];
  8501. $this->Form->setValueSources(['query', 'context', 'data']);
  8502. $result = $this->Form->getValueSources();
  8503. $this->assertEquals($expected, $result);
  8504. $this->Form->create();
  8505. $result = $this->Form->getValueSources();
  8506. $this->assertEquals($expected, $result);
  8507. $this->Form->end();
  8508. $result = $this->Form->getValueSources();
  8509. $this->assertEquals(['context'], $result);
  8510. }
  8511. /**
  8512. * Test sources values defaults handling
  8513. *
  8514. * @return void
  8515. */
  8516. public function testFormValueSourcesDefaults()
  8517. {
  8518. $this->View->setRequest(
  8519. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  8520. );
  8521. $this->Form->create();
  8522. $result = $this->Form->password('password');
  8523. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8524. $this->assertHtml($expected, $result);
  8525. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8526. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8527. $this->assertHtml($expected, $result);
  8528. $this->Form->setValueSources('query');
  8529. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8530. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8531. $this->assertHtml($expected, $result);
  8532. $this->Form->setValueSources('data');
  8533. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8534. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8535. $this->assertHtml($expected, $result);
  8536. }
  8537. /**
  8538. * Test sources values schema defaults handling
  8539. *
  8540. * @return void
  8541. */
  8542. public function testSourcesValueDoesntExistPassThrough()
  8543. {
  8544. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  8545. $articles = $this->getTableLocator()->get('Articles');
  8546. $entity = $articles->newEntity();
  8547. $this->Form->create($entity);
  8548. $this->Form->setValueSources(['query', 'context']);
  8549. $result = $this->Form->getSourceValue('category');
  8550. $this->assertEquals('sesame-cookies', $result);
  8551. $this->Form->setValueSources(['context', 'query']);
  8552. $result = $this->Form->getSourceValue('category');
  8553. $this->assertEquals('sesame-cookies', $result);
  8554. }
  8555. /**
  8556. * testNestedLabelInput method
  8557. *
  8558. * Test the `nestedInput` parameter
  8559. *
  8560. * @return void
  8561. */
  8562. public function testNestedLabelInput()
  8563. {
  8564. $result = $this->Form->control('foo', ['nestedInput' => true]);
  8565. $expected = [
  8566. 'div' => ['class' => 'input text'],
  8567. 'label' => ['for' => 'foo'],
  8568. ['input' => [
  8569. 'type' => 'text',
  8570. 'name' => 'foo',
  8571. 'id' => 'foo',
  8572. ]],
  8573. 'Foo',
  8574. '/label',
  8575. '/div',
  8576. ];
  8577. $this->assertHtml($expected, $result);
  8578. }
  8579. /**
  8580. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8581. *
  8582. * This test makes sure `false` excludes the label from the render
  8583. *
  8584. * @return void
  8585. */
  8586. public function testControlLabelManipulationDisableLabels()
  8587. {
  8588. $result = $this->Form->control('test', [
  8589. 'type' => 'radio',
  8590. 'options' => ['A', 'B'],
  8591. 'labelOptions' => false,
  8592. ]);
  8593. $expected = [
  8594. ['div' => ['class' => 'input radio']],
  8595. '<label',
  8596. 'Test',
  8597. '/label',
  8598. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8599. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8600. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8601. '/div',
  8602. ];
  8603. $this->assertHtml($expected, $result);
  8604. $result = $this->Form->control('checkbox1', [
  8605. 'label' => 'My checkboxes',
  8606. 'multiple' => 'checkbox',
  8607. 'type' => 'select',
  8608. 'options' => [
  8609. ['text' => 'First Checkbox', 'value' => 1],
  8610. ['text' => 'Second Checkbox', 'value' => 2],
  8611. ],
  8612. 'labelOptions' => false,
  8613. ]);
  8614. $expected = [
  8615. ['div' => ['class' => 'input select']],
  8616. ['label' => ['for' => 'checkbox1']],
  8617. 'My checkboxes',
  8618. '/label',
  8619. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8620. ['div' => ['class' => 'checkbox']],
  8621. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8622. '/div',
  8623. ['div' => ['class' => 'checkbox']],
  8624. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8625. '/div',
  8626. '/div',
  8627. ];
  8628. $this->assertHtml($expected, $result);
  8629. }
  8630. /**
  8631. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8632. *
  8633. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8634. * added to the class if checked.
  8635. *
  8636. * Also checks to make sure any custom attributes are rendered correctly
  8637. *
  8638. * @return void
  8639. */
  8640. public function testControlLabelManipulationRadios()
  8641. {
  8642. $result = $this->Form->control('test', [
  8643. 'type' => 'radio',
  8644. 'options' => ['A', 'B'],
  8645. 'labelOptions' => ['class' => 'custom-class'],
  8646. ]);
  8647. $expected = [
  8648. ['div' => ['class' => 'input radio']],
  8649. '<label',
  8650. 'Test',
  8651. '/label',
  8652. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8653. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8654. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8655. 'A',
  8656. '/label',
  8657. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8658. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8659. 'B',
  8660. '/label',
  8661. '/div',
  8662. ];
  8663. $this->assertHtml($expected, $result);
  8664. $result = $this->Form->control('test', [
  8665. 'type' => 'radio',
  8666. 'options' => ['A', 'B'],
  8667. 'value' => 1,
  8668. 'labelOptions' => ['class' => 'custom-class'],
  8669. ]);
  8670. $expected = [
  8671. ['div' => ['class' => 'input radio']],
  8672. '<label',
  8673. 'Test',
  8674. '/label',
  8675. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8676. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8677. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8678. 'A',
  8679. '/label',
  8680. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8681. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8682. 'B',
  8683. '/label',
  8684. '/div',
  8685. ];
  8686. $this->assertHtml($expected, $result);
  8687. $result = $this->Form->control('test', [
  8688. 'type' => 'radio',
  8689. 'options' => ['A', 'B'],
  8690. 'value' => 1,
  8691. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']],
  8692. ]);
  8693. $expected = [
  8694. ['div' => ['class' => 'input radio']],
  8695. '<label',
  8696. 'Test',
  8697. '/label',
  8698. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8699. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8700. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8701. 'A',
  8702. '/label',
  8703. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8704. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8705. 'B',
  8706. '/label',
  8707. '/div',
  8708. ];
  8709. $this->assertHtml($expected, $result);
  8710. $result = $this->Form->radio('test', ['A', 'B'], [
  8711. 'label' => [
  8712. 'class' => ['custom-class', 'another-class'],
  8713. 'data-name' => 'bob',
  8714. ],
  8715. 'value' => 1,
  8716. ]);
  8717. $expected = [
  8718. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8719. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8720. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8721. 'A',
  8722. '/label',
  8723. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8724. ['input' => [
  8725. 'type' => 'radio',
  8726. 'name' => 'test',
  8727. 'value' => '1',
  8728. 'id' => 'test-1',
  8729. 'checked' => 'checked',
  8730. ]],
  8731. 'B',
  8732. '/label',
  8733. ];
  8734. $this->assertHtml($expected, $result);
  8735. }
  8736. /**
  8737. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8738. *
  8739. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8740. * added to the class if checked.
  8741. *
  8742. * Also checks to make sure any custom attributes are rendered correctly
  8743. *
  8744. * @return void
  8745. */
  8746. public function testControlLabelManipulationCheckboxes()
  8747. {
  8748. $result = $this->Form->control('checkbox1', [
  8749. 'label' => 'My checkboxes',
  8750. 'multiple' => 'checkbox',
  8751. 'type' => 'select',
  8752. 'options' => [
  8753. ['text' => 'First Checkbox', 'value' => 1],
  8754. ['text' => 'Second Checkbox', 'value' => 2],
  8755. ],
  8756. 'labelOptions' => ['class' => 'custom-class'],
  8757. 'value' => ['1'],
  8758. ]);
  8759. $expected = [
  8760. ['div' => ['class' => 'input select']],
  8761. ['label' => ['for' => 'checkbox1']],
  8762. 'My checkboxes',
  8763. '/label',
  8764. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8765. ['div' => ['class' => 'checkbox']],
  8766. ['label' => [
  8767. 'class' => 'custom-class selected',
  8768. 'for' => 'checkbox1-1',
  8769. ]],
  8770. ['input' => [
  8771. 'type' => 'checkbox',
  8772. 'name' => 'checkbox1[]',
  8773. 'value' => '1',
  8774. 'id' => 'checkbox1-1',
  8775. 'checked' => 'checked',
  8776. ]],
  8777. 'First Checkbox',
  8778. '/label',
  8779. '/div',
  8780. ['div' => ['class' => 'checkbox']],
  8781. ['label' => [
  8782. 'class' => 'custom-class',
  8783. 'for' => 'checkbox1-2',
  8784. ]],
  8785. ['input' => [
  8786. 'type' => 'checkbox',
  8787. 'name' => 'checkbox1[]',
  8788. 'value' => '2',
  8789. 'id' => 'checkbox1-2',
  8790. ]],
  8791. 'Second Checkbox',
  8792. '/label',
  8793. '/div',
  8794. '/div',
  8795. ];
  8796. $this->assertHtml($expected, $result);
  8797. $result = $this->Form->control('checkbox1', [
  8798. 'label' => 'My checkboxes',
  8799. 'multiple' => 'checkbox',
  8800. 'type' => 'select',
  8801. 'options' => [
  8802. ['text' => 'First Checkbox', 'value' => 1],
  8803. ['text' => 'Second Checkbox', 'value' => 2],
  8804. ],
  8805. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8806. 'value' => ['1'],
  8807. ]);
  8808. $expected = [
  8809. ['div' => ['class' => 'input select']],
  8810. ['label' => ['for' => 'checkbox1']],
  8811. 'My checkboxes',
  8812. '/label',
  8813. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8814. ['div' => ['class' => 'checkbox']],
  8815. ['label' => [
  8816. 'class' => 'custom-class another-class selected',
  8817. 'data-name' => 'bob',
  8818. 'for' => 'checkbox1-1',
  8819. ]],
  8820. ['input' => [
  8821. 'type' => 'checkbox',
  8822. 'name' => 'checkbox1[]',
  8823. 'value' => '1',
  8824. 'id' => 'checkbox1-1',
  8825. 'checked' => 'checked',
  8826. ]],
  8827. 'First Checkbox',
  8828. '/label',
  8829. '/div',
  8830. ['div' => ['class' => 'checkbox']],
  8831. ['label' => [
  8832. 'class' => 'custom-class another-class',
  8833. 'data-name' => 'bob',
  8834. 'for' => 'checkbox1-2',
  8835. ]],
  8836. ['input' => [
  8837. 'type' => 'checkbox',
  8838. 'name' => 'checkbox1[]',
  8839. 'value' => '2',
  8840. 'id' => 'checkbox1-2',
  8841. ]],
  8842. 'Second Checkbox',
  8843. '/label',
  8844. '/div',
  8845. '/div',
  8846. ];
  8847. $this->assertHtml($expected, $result);
  8848. }
  8849. }