FormHelperTest.php 325 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Collection\Collection;
  17. use Cake\Core\Configure;
  18. use Cake\Form\Form;
  19. use Cake\Http\ServerRequest;
  20. use Cake\ORM\Entity;
  21. use Cake\ORM\Table;
  22. use Cake\Routing\Router;
  23. use Cake\TestSuite\TestCase;
  24. use Cake\Utility\Security;
  25. use Cake\Validation\Validator;
  26. use Cake\View\Form\EntityContext;
  27. use Cake\View\Helper\FormHelper;
  28. use Cake\View\View;
  29. use Cake\View\Widget\WidgetLocator;
  30. /**
  31. * Test stub.
  32. */
  33. class Article extends Entity
  34. {
  35. }
  36. /**
  37. * Contact class
  38. */
  39. class ContactsTable extends Table
  40. {
  41. /**
  42. * Default schema
  43. *
  44. * @var array
  45. */
  46. protected $_schema = [
  47. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  48. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  49. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  50. 'phone' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  51. 'password' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  52. 'published' => ['type' => 'date', 'null' => true, 'default' => null, 'length' => null],
  53. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  54. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  55. 'age' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => null],
  56. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  57. ];
  58. /**
  59. * Initializes the schema
  60. *
  61. * @param array $config
  62. * @return void
  63. */
  64. public function initialize(array $config)
  65. {
  66. $this->setSchema($this->_schema);
  67. }
  68. }
  69. /**
  70. * ValidateUser class
  71. */
  72. class ValidateUsersTable extends Table
  73. {
  74. /**
  75. * schema method
  76. *
  77. * @var array
  78. */
  79. protected $_schema = [
  80. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  81. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  82. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  83. 'balance' => ['type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2],
  84. 'cost_decimal' => ['type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3],
  85. 'null_decimal' => ['type' => 'decimal', 'null' => false, 'length' => null, 'precision' => null],
  86. 'ratio' => ['type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6],
  87. 'population' => ['type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0],
  88. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  89. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  90. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  91. ];
  92. /**
  93. * Initializes the schema
  94. *
  95. * @param array $config
  96. * @return void
  97. */
  98. public function initialize(array $config)
  99. {
  100. $this->setSchema($this->_schema);
  101. }
  102. }
  103. /**
  104. * FormHelperTest class
  105. *
  106. * @property \Cake\View\Helper\FormHelper $Form
  107. * @property \Cake\View\View $View
  108. */
  109. class FormHelperTest extends TestCase
  110. {
  111. /**
  112. * Fixtures to be used
  113. *
  114. * @var array
  115. */
  116. public $fixtures = ['core.Articles', 'core.Comments'];
  117. /**
  118. * Do not load the fixtures by default
  119. *
  120. * @var bool
  121. */
  122. public $autoFixtures = false;
  123. /**
  124. * @var array
  125. */
  126. protected $article = [];
  127. /**
  128. * setUp method
  129. *
  130. * @return void
  131. */
  132. public function setUp()
  133. {
  134. parent::setUp();
  135. Configure::write('Config.language', 'eng');
  136. Configure::write('App.base', '');
  137. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  138. $request = new ServerRequest([
  139. 'webroot' => '',
  140. 'base' => '',
  141. 'url' => '/articles/add',
  142. 'params' => [
  143. 'controller' => 'articles',
  144. 'action' => 'add',
  145. ],
  146. ]);
  147. $this->View = new View($request);
  148. $this->Form = new FormHelper($this->View);
  149. $this->dateRegex = [
  150. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  151. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  152. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  153. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  154. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  155. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  156. ];
  157. $this->article = [
  158. 'schema' => [
  159. 'id' => ['type' => 'integer'],
  160. 'author_id' => ['type' => 'integer', 'null' => true],
  161. 'title' => ['type' => 'string', 'null' => true],
  162. 'body' => 'text',
  163. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  164. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  165. ],
  166. 'required' => [
  167. 'author_id' => true,
  168. 'title' => true,
  169. ],
  170. ];
  171. Security::setSalt('foo!');
  172. Router::connect('/:controller', ['action' => 'index']);
  173. Router::connect('/:controller/:action/*');
  174. }
  175. /**
  176. * tearDown method
  177. *
  178. * @return void
  179. */
  180. public function tearDown()
  181. {
  182. parent::tearDown();
  183. unset($this->Form, $this->Controller, $this->View);
  184. $this->getTableLocator()->clear();
  185. }
  186. /**
  187. * Test construct() with the templates option.
  188. *
  189. * @return void
  190. */
  191. public function testConstructTemplatesFile()
  192. {
  193. $helper = new FormHelper($this->View, [
  194. 'templates' => 'htmlhelper_tags',
  195. ]);
  196. $result = $helper->control('name');
  197. $this->assertContains('<input', $result);
  198. }
  199. /**
  200. * Test that when specifying custom widgets the config array for that widget
  201. * is overwritten instead of merged.
  202. *
  203. * @return void
  204. */
  205. public function testConstructWithWidgets()
  206. {
  207. $config = [
  208. 'widgets' => [
  209. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select'],
  210. ],
  211. ];
  212. $helper = new FormHelper($this->View, $config);
  213. $locator = $helper->getWidgetLocator();
  214. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  215. }
  216. /**
  217. * Test that when specifying custom widgets config file and it should be
  218. * added to widgets array. WidgetLocator will load widgets in constructor.
  219. *
  220. * @return void
  221. */
  222. public function testConstructWithWidgetsConfig()
  223. {
  224. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  225. $locator = $helper->getWidgetLocator();
  226. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  227. }
  228. /**
  229. * Test setting the widget locator
  230. *
  231. * @return void
  232. */
  233. public function testSetAndGetWidgetLocator()
  234. {
  235. $helper = new FormHelper($this->View);
  236. $locator = new WidgetLocator($helper->templater(), $this->View);
  237. $helper->setWidgetLocator($locator);
  238. $this->assertSame($locator, $helper->getWidgetLocator());
  239. }
  240. /**
  241. * Test overridding grouped input types which controls generation of "for"
  242. * attribute of labels.
  243. *
  244. * @return void
  245. */
  246. public function testConstructWithGroupedInputTypes()
  247. {
  248. $helper = new FormHelper($this->View, [
  249. 'groupedInputTypes' => ['radio'],
  250. ]);
  251. $result = $helper->control('when', ['type' => 'datetime']);
  252. $this->assertContains('<label for="when">When</label>', $result);
  253. }
  254. /**
  255. * Test registering a new widget class and rendering it.
  256. *
  257. * @return void
  258. */
  259. public function testAddWidgetAndRenderWidget()
  260. {
  261. $data = [
  262. 'val' => 1,
  263. ];
  264. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  265. $this->Form->addWidget('test', $mock);
  266. $mock->expects($this->once())
  267. ->method('render')
  268. ->with($data)
  269. ->will($this->returnValue('HTML'));
  270. $result = $this->Form->widget('test', $data);
  271. $this->assertEquals('HTML', $result);
  272. }
  273. /**
  274. * Test that secureFields() of widget is called after calling render(),
  275. * not before.
  276. *
  277. * @return void
  278. */
  279. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  280. {
  281. $data = [
  282. 'val' => 1,
  283. 'name' => 'test',
  284. ];
  285. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  286. $this->Form->addWidget('test', $mock);
  287. $mock->expects($this->at(0))
  288. ->method('render')
  289. ->with($data)
  290. ->will($this->returnValue('HTML'));
  291. $mock->expects($this->at(1))
  292. ->method('secureFields')
  293. ->with($data)
  294. ->will($this->returnValue(['test']));
  295. $result = $this->Form->widget('test', $data + ['secure' => true]);
  296. $this->assertEquals('HTML', $result);
  297. }
  298. /**
  299. * Test that empty string is not added to secure fields list when
  300. * rendering input widget without name.
  301. *
  302. * @return void
  303. */
  304. public function testRenderingWidgetWithEmptyName()
  305. {
  306. $this->assertEquals([], $this->Form->fields);
  307. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  308. $this->assertEquals('<select name=""></select>', $result);
  309. $this->assertEquals([], $this->Form->fields);
  310. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  311. $this->assertEquals('<select name="0"></select>', $result);
  312. $this->assertEquals(['0'], $this->Form->fields);
  313. }
  314. /**
  315. * Test registering an invalid widget class.
  316. *
  317. * @return void
  318. */
  319. public function testAddWidgetInvalid()
  320. {
  321. $this->expectException(\RuntimeException::class);
  322. $mock = new \StdClass();
  323. $this->Form->addWidget('test', $mock);
  324. $this->Form->widget('test');
  325. }
  326. /**
  327. * Test adding a new context class.
  328. *
  329. * @return void
  330. */
  331. public function testAddContextProvider()
  332. {
  333. $context = 'My data';
  334. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  335. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  336. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  337. $this->assertEquals($context, $data['entity']);
  338. return $stub;
  339. });
  340. $this->Form->create($context);
  341. $result = $this->Form->context();
  342. $this->assertSame($stub, $result);
  343. }
  344. /**
  345. * Test replacing a context class.
  346. *
  347. * @return void
  348. */
  349. public function testAddContextProviderReplace()
  350. {
  351. $entity = new Article();
  352. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  353. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  354. return $stub;
  355. });
  356. $this->Form->create($entity);
  357. $result = $this->Form->context();
  358. $this->assertSame($stub, $result);
  359. }
  360. /**
  361. * Test overriding a context class.
  362. *
  363. * @return void
  364. */
  365. public function testAddContextProviderAdd()
  366. {
  367. $entity = new Article();
  368. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  369. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  370. if ($data['entity'] instanceof Entity) {
  371. return $stub;
  372. }
  373. });
  374. $this->Form->create($entity);
  375. $result = $this->Form->context();
  376. $this->assertSame($stub, $result);
  377. }
  378. /**
  379. * Test adding an invalid context class.
  380. *
  381. * @return void
  382. */
  383. public function testAddContextProviderInvalid()
  384. {
  385. $this->expectException(\RuntimeException::class);
  386. $this->expectExceptionMessage('Context providers must return object implementing Cake\View\Form\ContextInterface. Got "stdClass" instead.');
  387. $context = 'My data';
  388. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  389. return new \StdClass();
  390. });
  391. $this->Form->create($context);
  392. }
  393. /**
  394. * Provides context options for create().
  395. *
  396. * @return array
  397. */
  398. public function contextSelectionProvider()
  399. {
  400. $entity = new Article();
  401. $collection = new Collection([$entity]);
  402. $emptyCollection = new Collection([]);
  403. $arrayObject = new \ArrayObject([]);
  404. $data = [
  405. 'schema' => [
  406. 'title' => ['type' => 'string'],
  407. ],
  408. ];
  409. $form = new Form();
  410. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  411. return [
  412. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  413. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  414. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  415. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  416. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  417. 'form' => [$form, 'Cake\View\Form\FormContext'],
  418. 'none' => [null, 'Cake\View\Form\NullContext'],
  419. 'false' => [false, 'Cake\View\Form\NullContext'],
  420. 'custom' => [$custom, get_class($custom)],
  421. ];
  422. }
  423. /**
  424. * Test default context selection in create()
  425. *
  426. * @dataProvider contextSelectionProvider
  427. * @return void
  428. */
  429. public function testCreateContextSelectionBuiltIn($data, $class)
  430. {
  431. $this->loadFixtures('Articles');
  432. $this->Form->create($data);
  433. $this->assertInstanceOf($class, $this->Form->context());
  434. }
  435. /**
  436. * Data provider for type option.
  437. *
  438. * @return array
  439. */
  440. public static function requestTypeProvider()
  441. {
  442. return [
  443. // type, method, override
  444. ['post', 'post', 'POST'],
  445. ['put', 'post', 'PUT'],
  446. ['patch', 'post', 'PATCH'],
  447. ['delete', 'post', 'DELETE'],
  448. ];
  449. }
  450. /**
  451. * Test creating file forms.
  452. *
  453. * @return void
  454. */
  455. public function testCreateFile()
  456. {
  457. $encoding = strtolower(Configure::read('App.encoding'));
  458. $result = $this->Form->create(false, ['type' => 'file']);
  459. $expected = [
  460. 'form' => [
  461. 'method' => 'post', 'action' => '/articles/add',
  462. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data',
  463. ],
  464. 'div' => ['style' => 'display:none;'],
  465. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  466. '/div',
  467. ];
  468. $this->assertHtml($expected, $result);
  469. }
  470. /**
  471. * Test creating GET forms.
  472. *
  473. * @return void
  474. */
  475. public function testCreateGet()
  476. {
  477. $encoding = strtolower(Configure::read('App.encoding'));
  478. $result = $this->Form->create(false, ['type' => 'get']);
  479. $expected = ['form' => [
  480. 'method' => 'get', 'action' => '/articles/add',
  481. 'accept-charset' => $encoding,
  482. ]];
  483. $this->assertHtml($expected, $result);
  484. }
  485. /**
  486. * Test explicit method/enctype options.
  487. *
  488. * Explicit method overwrites inferred method from 'type'
  489. *
  490. * @return void
  491. */
  492. public function testCreateExplicitMethodEnctype()
  493. {
  494. $encoding = strtolower(Configure::read('App.encoding'));
  495. $result = $this->Form->create(false, [
  496. 'type' => 'get',
  497. 'method' => 'put',
  498. 'enctype' => 'multipart/form-data',
  499. ]);
  500. $expected = ['form' => [
  501. 'method' => 'put',
  502. 'action' => '/articles/add',
  503. 'enctype' => 'multipart/form-data',
  504. 'accept-charset' => $encoding,
  505. ]];
  506. $this->assertHtml($expected, $result);
  507. }
  508. /**
  509. * Test create() with the templates option.
  510. *
  511. * @return void
  512. */
  513. public function testCreateTemplatesArray()
  514. {
  515. $result = $this->Form->create($this->article, [
  516. 'templates' => [
  517. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  518. ],
  519. ]);
  520. $expected = [
  521. 'form' => [
  522. 'class' => 'form-horizontal',
  523. 'method' => 'post',
  524. 'action' => '/articles/add',
  525. 'accept-charset' => 'utf-8',
  526. ],
  527. ];
  528. $this->assertHtml($expected, $result);
  529. }
  530. /**
  531. * Test create() with the templates option.
  532. *
  533. * @return void
  534. */
  535. public function testCreateTemplatesFile()
  536. {
  537. $result = $this->Form->create($this->article, [
  538. 'templates' => 'htmlhelper_tags',
  539. ]);
  540. $expected = [
  541. 'start form',
  542. 'div' => ['class' => 'hidden'],
  543. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  544. '/div',
  545. ];
  546. $this->assertHtml($expected, $result);
  547. }
  548. /**
  549. * Test that create() and end() restore templates.
  550. *
  551. * @return void
  552. */
  553. public function testCreateEndRestoreTemplates()
  554. {
  555. $this->Form->create($this->article, [
  556. 'templates' => ['input' => 'custom input element'],
  557. ]);
  558. $this->Form->end();
  559. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  560. }
  561. /**
  562. * Test using template vars in various templates used by control() method.
  563. *
  564. * @return void
  565. */
  566. public function testControlTemplateVars()
  567. {
  568. $result = $this->Form->control('text', [
  569. 'templates' => [
  570. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  571. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  572. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  573. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  574. ],
  575. 'templateVars' => [
  576. 'forinput' => 'in-input',
  577. 'forlabel' => 'in-label',
  578. 'forgroup' => 'in-group',
  579. 'forcontainer' => 'in-container',
  580. ],
  581. ]);
  582. $expected = [
  583. 'div' => ['class'],
  584. 'label' => ['for'],
  585. 'Text in-label',
  586. '/label',
  587. 'in-group',
  588. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  589. 'in-container',
  590. '/div',
  591. ];
  592. $this->assertHtml($expected, $result);
  593. }
  594. /**
  595. * Test ensuring template variables work in template files loaded
  596. * during control().
  597. *
  598. * @return void
  599. */
  600. public function testControlTemplatesFromFile()
  601. {
  602. $result = $this->Form->control('title', [
  603. 'templates' => 'test_templates',
  604. 'templateVars' => [
  605. 'forcontainer' => 'container-data',
  606. ],
  607. ]);
  608. $expected = [
  609. 'div' => ['class'],
  610. 'label' => ['for'],
  611. 'Title',
  612. '/label',
  613. 'input' => ['name', 'type' => 'text', 'id'],
  614. 'container-data',
  615. '/div',
  616. ];
  617. $this->assertHtml($expected, $result);
  618. }
  619. /**
  620. * Test using template vars in inputSubmit and submitContainer template.
  621. *
  622. * @return void
  623. */
  624. public function testSubmitTemplateVars()
  625. {
  626. $this->Form->setTemplates([
  627. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  628. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>',
  629. ]);
  630. $result = $this->Form->submit('Submit', [
  631. 'templateVars' => [
  632. 'forinput' => 'in-input',
  633. 'forcontainer' => 'in-container',
  634. ],
  635. ]);
  636. $expected = [
  637. 'div' => ['class'],
  638. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  639. 'in-container',
  640. '/div',
  641. ];
  642. $this->assertHtml($expected, $result);
  643. }
  644. /**
  645. * test the create() method
  646. *
  647. * @dataProvider requestTypeProvider
  648. * @return void
  649. */
  650. public function testCreateTypeOptions($type, $method, $override)
  651. {
  652. $encoding = strtolower(Configure::read('App.encoding'));
  653. $result = $this->Form->create(false, ['type' => $type]);
  654. $expected = [
  655. 'form' => [
  656. 'method' => $method, 'action' => '/articles/add',
  657. 'accept-charset' => $encoding,
  658. ],
  659. 'div' => ['style' => 'display:none;'],
  660. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  661. '/div',
  662. ];
  663. $this->assertHtml($expected, $result);
  664. }
  665. /**
  666. * Test using template vars in Create (formStart template)
  667. *
  668. * @return void
  669. */
  670. public function testCreateTemplateVars()
  671. {
  672. $result = $this->Form->create($this->article, [
  673. 'templates' => [
  674. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  675. ],
  676. 'templateVars' => ['header' => 'headertext'],
  677. ]);
  678. $expected = [
  679. 'h4' => ['class'],
  680. 'headertext',
  681. '/h4',
  682. 'form' => [
  683. 'method' => 'post',
  684. 'action' => '/articles/add',
  685. 'accept-charset' => 'utf-8',
  686. ],
  687. ];
  688. $this->assertHtml($expected, $result);
  689. }
  690. /**
  691. * Test opening a form for an update operation.
  692. *
  693. * @return void
  694. */
  695. public function testCreateUpdateForm()
  696. {
  697. $encoding = strtolower(Configure::read('App.encoding'));
  698. $this->View->setRequest($this->View->getRequest()
  699. ->withRequestTarget('/articles/edit/1')
  700. ->withParam('action', 'edit'));
  701. $this->article['defaults']['id'] = 1;
  702. $result = $this->Form->create($this->article);
  703. $expected = [
  704. 'form' => [
  705. 'method' => 'post',
  706. 'action' => '/articles/edit/1',
  707. 'accept-charset' => $encoding,
  708. ],
  709. 'div' => ['style' => 'display:none;'],
  710. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  711. '/div',
  712. ];
  713. $this->assertHtml($expected, $result);
  714. }
  715. /**
  716. * test create() with automatic url generation
  717. *
  718. * @return void
  719. */
  720. public function testCreateAutoUrl()
  721. {
  722. $encoding = strtolower(Configure::read('App.encoding'));
  723. $this->View->setRequest($this->View->getRequest()
  724. ->withRequestTarget('/articles/delete/10')
  725. ->withParam('action', 'delete'));
  726. $result = $this->Form->create($this->article);
  727. $expected = [
  728. 'form' => [
  729. 'method' => 'post', 'action' => '/articles/delete/10',
  730. 'accept-charset' => $encoding,
  731. ],
  732. 'div' => ['style' => 'display:none;'],
  733. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  734. '/div',
  735. ];
  736. $this->assertHtml($expected, $result);
  737. $this->article['defaults'] = ['id' => 1];
  738. $this->View->setRequest($this->View->getRequest()
  739. ->withRequestTarget('/articles/edit/1')
  740. ->withParam('action', 'delete'));
  741. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  742. $expected = [
  743. 'form' => [
  744. 'method' => 'post',
  745. 'action' => '/articles/edit/1',
  746. 'accept-charset' => $encoding,
  747. ],
  748. 'div' => ['style' => 'display:none;'],
  749. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  750. '/div',
  751. ];
  752. $this->assertHtml($expected, $result);
  753. $this->View->setRequest($this->View->getRequest()
  754. ->withParam('action', 'add'));
  755. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  756. $expected = [
  757. 'form' => [
  758. 'method' => 'post',
  759. 'action' => '/articles/publish/1',
  760. 'accept-charset' => $encoding,
  761. ],
  762. 'div' => ['style' => 'display:none;'],
  763. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  764. '/div',
  765. ];
  766. $this->assertHtml($expected, $result);
  767. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  768. $expected = [
  769. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  770. 'div' => ['style' => 'display:none;'],
  771. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  772. '/div',
  773. ];
  774. $this->assertHtml($expected, $result);
  775. $this->View->setRequest($this->View->getRequest()
  776. ->withParam('controller', 'Pages'));
  777. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  778. $expected = [
  779. 'form' => [
  780. 'method' => 'post', 'action' => '/Pages/signup/1',
  781. 'accept-charset' => $encoding,
  782. ],
  783. 'div' => ['style' => 'display:none;'],
  784. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  785. '/div',
  786. ];
  787. $this->assertHtml($expected, $result);
  788. }
  789. /**
  790. * Test create() with no URL (no "action" attribute for <form> tag)
  791. *
  792. * @return void
  793. */
  794. public function testCreateNoUrl()
  795. {
  796. $result = $this->Form->create(false, ['url' => false]);
  797. $expected = [
  798. 'form' => [
  799. 'method' => 'post',
  800. 'accept-charset' => strtolower(Configure::read('App.encoding')),
  801. ],
  802. 'div' => ['style' => 'display:none;'],
  803. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  804. '/div',
  805. ];
  806. $this->assertHtml($expected, $result);
  807. }
  808. /**
  809. * test create() with a custom route
  810. *
  811. * @return void
  812. */
  813. public function testCreateCustomRoute()
  814. {
  815. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  816. $encoding = strtolower(Configure::read('App.encoding'));
  817. $this->View->setRequest($this->View->getRequest()
  818. ->withParam('controller', 'users'));
  819. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  820. $expected = [
  821. 'form' => [
  822. 'method' => 'post', 'action' => '/login',
  823. 'accept-charset' => $encoding,
  824. ],
  825. 'div' => ['style' => 'display:none;'],
  826. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  827. '/div',
  828. ];
  829. $this->assertHtml($expected, $result);
  830. Router::connect(
  831. '/new-article',
  832. ['controller' => 'articles', 'action' => 'myaction'],
  833. ['_name' => 'my-route']
  834. );
  835. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  836. $expected = [
  837. 'form' => [
  838. 'method' => 'post', 'action' => '/new-article',
  839. 'accept-charset' => $encoding,
  840. ],
  841. 'div' => ['style' => 'display:none;'],
  842. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  843. '/div',
  844. ];
  845. $this->assertHtml($expected, $result);
  846. }
  847. /**
  848. * test automatic accept-charset overriding
  849. *
  850. * @return void
  851. */
  852. public function testCreateWithAcceptCharset()
  853. {
  854. $result = $this->Form->create(
  855. $this->article,
  856. [
  857. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1',
  858. ]
  859. );
  860. $expected = [
  861. 'form' => [
  862. 'method' => 'post', 'action' => '/articles',
  863. 'accept-charset' => 'iso-8859-1',
  864. ],
  865. 'div' => ['style' => 'display:none;'],
  866. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  867. '/div',
  868. ];
  869. $this->assertHtml($expected, $result);
  870. }
  871. /**
  872. * Test base form URL when url param is passed with multiple parameters (&)
  873. *
  874. */
  875. public function testCreateQueryStringRequest()
  876. {
  877. $encoding = strtolower(Configure::read('App.encoding'));
  878. $result = $this->Form->create($this->article, [
  879. 'type' => 'post',
  880. 'escape' => false,
  881. 'url' => [
  882. 'controller' => 'controller',
  883. 'action' => 'action',
  884. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  885. ],
  886. ]);
  887. $expected = [
  888. 'form' => [
  889. 'method' => 'post',
  890. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  891. 'accept-charset' => $encoding,
  892. ],
  893. 'div' => ['style' => 'display:none;'],
  894. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  895. '/div',
  896. ];
  897. $this->assertHtml($expected, $result);
  898. $result = $this->Form->create($this->article, [
  899. 'type' => 'post',
  900. 'url' => [
  901. 'controller' => 'controller',
  902. 'action' => 'action',
  903. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  904. ],
  905. ]);
  906. $this->assertHtml($expected, $result);
  907. }
  908. /**
  909. * test that create() doesn't cause errors by multiple id's being in the primary key
  910. * as could happen with multiple select or checkboxes.
  911. *
  912. * @return void
  913. */
  914. public function testCreateWithMultipleIdInData()
  915. {
  916. $encoding = strtolower(Configure::read('App.encoding'));
  917. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  918. $result = $this->Form->create($this->article);
  919. $expected = [
  920. 'form' => [
  921. 'method' => 'post',
  922. 'action' => '/articles/add',
  923. 'accept-charset' => $encoding,
  924. ],
  925. 'div' => ['style' => 'display:none;'],
  926. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  927. '/div',
  928. ];
  929. $this->assertHtml($expected, $result);
  930. }
  931. /**
  932. * test that create() doesn't add in extra passed params.
  933. *
  934. * @return void
  935. */
  936. public function testCreatePassedArgs()
  937. {
  938. $encoding = strtolower(Configure::read('App.encoding'));
  939. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  940. $result = $this->Form->create($this->article, [
  941. 'type' => 'post',
  942. 'escape' => false,
  943. 'url' => [
  944. 'action' => 'edit',
  945. 'myparam',
  946. ],
  947. ]);
  948. $expected = [
  949. 'form' => [
  950. 'method' => 'post',
  951. 'action' => '/articles/edit/myparam',
  952. 'accept-charset' => $encoding,
  953. ],
  954. 'div' => ['style' => 'display:none;'],
  955. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  956. '/div',
  957. ];
  958. $this->assertHtml($expected, $result);
  959. }
  960. /**
  961. * test creating a get form, and get form inputs.
  962. *
  963. * @return void
  964. */
  965. public function testGetFormCreate()
  966. {
  967. $encoding = strtolower(Configure::read('App.encoding'));
  968. $result = $this->Form->create($this->article, ['type' => 'get']);
  969. $expected = ['form' => [
  970. 'method' => 'get', 'action' => '/articles/add',
  971. 'accept-charset' => $encoding,
  972. ]];
  973. $this->assertHtml($expected, $result);
  974. $result = $this->Form->text('title');
  975. $expected = ['input' => [
  976. 'name' => 'title', 'type' => 'text', 'required' => 'required',
  977. ]];
  978. $this->assertHtml($expected, $result);
  979. $result = $this->Form->password('password');
  980. $expected = ['input' => [
  981. 'name' => 'password', 'type' => 'password',
  982. ]];
  983. $this->assertHtml($expected, $result);
  984. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  985. $result = $this->Form->text('user_form');
  986. $expected = ['input' => [
  987. 'name' => 'user_form', 'type' => 'text',
  988. ]];
  989. $this->assertHtml($expected, $result);
  990. }
  991. /**
  992. * test get form, and inputs when the model param is false
  993. *
  994. * @return void
  995. */
  996. public function testGetFormWithFalseModel()
  997. {
  998. $encoding = strtolower(Configure::read('App.encoding'));
  999. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'contact_test'));
  1000. $result = $this->Form->create(false, [
  1001. 'type' => 'get', 'url' => ['controller' => 'contact_test'],
  1002. ]);
  1003. $expected = ['form' => [
  1004. 'method' => 'get', 'action' => '/contact_test/add',
  1005. 'accept-charset' => $encoding,
  1006. ]];
  1007. $this->assertHtml($expected, $result);
  1008. $result = $this->Form->text('reason');
  1009. $expected = [
  1010. 'input' => ['type' => 'text', 'name' => 'reason'],
  1011. ];
  1012. $this->assertHtml($expected, $result);
  1013. }
  1014. /**
  1015. * testFormCreateWithSecurity method
  1016. *
  1017. * Test form->create() with security key.
  1018. *
  1019. * @return void
  1020. */
  1021. public function testCreateWithSecurity()
  1022. {
  1023. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  1024. $encoding = strtolower(Configure::read('App.encoding'));
  1025. $result = $this->Form->create($this->article, [
  1026. 'url' => '/articles/publish',
  1027. ]);
  1028. $expected = [
  1029. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  1030. 'div' => ['style' => 'display:none;'],
  1031. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1032. ['input' => [
  1033. 'type' => 'hidden',
  1034. 'name' => '_csrfToken',
  1035. 'value' => 'testKey',
  1036. 'autocomplete' => 'off',
  1037. ]],
  1038. '/div',
  1039. ];
  1040. $this->assertHtml($expected, $result);
  1041. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1042. $expected['form']['id'] = 'MyForm';
  1043. $this->assertHtml($expected, $result);
  1044. }
  1045. /**
  1046. * testFormCreateGetNoSecurity method
  1047. *
  1048. * Test form->create() with no security key as its a get form
  1049. *
  1050. * @return void
  1051. */
  1052. public function testCreateEndGetNoSecurity()
  1053. {
  1054. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  1055. $article = new Article();
  1056. $result = $this->Form->create($article, [
  1057. 'type' => 'get',
  1058. 'url' => '/contacts/add',
  1059. ]);
  1060. $this->assertNotContains('testKey', $result);
  1061. $result = $this->Form->end();
  1062. $this->assertNotContains('testKey', $result);
  1063. }
  1064. /**
  1065. * test that create() clears the fields property so it starts fresh
  1066. *
  1067. * @return void
  1068. */
  1069. public function testCreateClearingFields()
  1070. {
  1071. $this->Form->fields = ['model_id'];
  1072. $this->Form->create($this->article);
  1073. $this->assertEquals([], $this->Form->fields);
  1074. }
  1075. /**
  1076. * Tests form hash generation with model-less data
  1077. *
  1078. * @return void
  1079. */
  1080. public function testValidateHashNoModel()
  1081. {
  1082. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1083. $fields = ['anything'];
  1084. $result = $this->Form->secure($fields);
  1085. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1086. $this->assertContains($hash, $result);
  1087. }
  1088. /**
  1089. * Tests that hidden fields generated for checkboxes don't get locked
  1090. *
  1091. * @return void
  1092. */
  1093. public function testNoCheckboxLocking()
  1094. {
  1095. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1096. $this->assertSame([], $this->Form->fields);
  1097. $this->Form->checkbox('check', ['value' => '1']);
  1098. $this->assertSame($this->Form->fields, ['check']);
  1099. }
  1100. /**
  1101. * testFormSecurityFields method
  1102. *
  1103. * Test generation of secure form hash generation.
  1104. *
  1105. * @return void
  1106. */
  1107. public function testFormSecurityFields()
  1108. {
  1109. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1110. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1111. $result = $this->Form->secure($fields);
  1112. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1113. $hash .= ':' . 'Model.valid';
  1114. $hash = urlencode($hash);
  1115. $tokenDebug = urlencode(json_encode([
  1116. '',
  1117. $fields,
  1118. [],
  1119. ]));
  1120. $expected = [
  1121. 'div' => ['style' => 'display:none;'],
  1122. ['input' => [
  1123. 'type' => 'hidden',
  1124. 'name' => '_Token[fields]',
  1125. 'value' => $hash,
  1126. 'autocomplete' => 'off',
  1127. ]],
  1128. ['input' => [
  1129. 'type' => 'hidden',
  1130. 'name' => '_Token[unlocked]',
  1131. 'value' => '',
  1132. 'autocomplete' => 'off',
  1133. ]],
  1134. ['input' => [
  1135. 'type' => 'hidden',
  1136. 'name' => '_Token[debug]',
  1137. 'value' => $tokenDebug,
  1138. 'autocomplete' => 'off',
  1139. ]],
  1140. '/div',
  1141. ];
  1142. $this->assertHtml($expected, $result);
  1143. }
  1144. /**
  1145. * testFormSecurityFields method
  1146. *
  1147. * Test debug token is not generated if debug is false
  1148. *
  1149. * @return void
  1150. */
  1151. public function testFormSecurityFieldsNoDebugMode()
  1152. {
  1153. Configure::write('debug', false);
  1154. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1155. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1156. $result = $this->Form->secure($fields);
  1157. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1158. $hash .= ':' . 'Model.valid';
  1159. $hash = urlencode($hash);
  1160. $expected = [
  1161. 'div' => ['style' => 'display:none;'],
  1162. ['input' => [
  1163. 'type' => 'hidden',
  1164. 'name' => '_Token[fields]',
  1165. 'autocomplete' => 'off',
  1166. 'value' => $hash,
  1167. ]],
  1168. ['input' => [
  1169. 'type' => 'hidden',
  1170. 'name' => '_Token[unlocked]',
  1171. 'autocomplete' => 'off',
  1172. 'value' => '',
  1173. ]],
  1174. '/div',
  1175. ];
  1176. $this->assertHtml($expected, $result);
  1177. }
  1178. /**
  1179. * Tests correct generation of number fields for smallint
  1180. *
  1181. * @return void
  1182. */
  1183. public function testTextFieldGenerationForSmallint()
  1184. {
  1185. $this->article['schema'] = [
  1186. 'foo' => [
  1187. 'type' => 'smallinteger',
  1188. 'null' => false,
  1189. 'default' => null,
  1190. 'length' => 10,
  1191. ],
  1192. ];
  1193. $this->Form->create($this->article);
  1194. $result = $this->Form->control('foo');
  1195. $this->assertContains('class="input number"', $result);
  1196. $this->assertContains('type="number"', $result);
  1197. }
  1198. /**
  1199. * Tests correct generation of number fields for tinyint
  1200. *
  1201. * @return void
  1202. */
  1203. public function testTextFieldGenerationForTinyint()
  1204. {
  1205. $this->article['schema'] = [
  1206. 'foo' => [
  1207. 'type' => 'tinyinteger',
  1208. 'null' => false,
  1209. 'default' => null,
  1210. 'length' => 10,
  1211. ],
  1212. ];
  1213. $this->Form->create($this->article);
  1214. $result = $this->Form->control('foo');
  1215. $this->assertContains('class="input number"', $result);
  1216. $this->assertContains('type="number"', $result);
  1217. }
  1218. /**
  1219. * Tests correct generation of number fields for double and float fields
  1220. *
  1221. * @return void
  1222. */
  1223. public function testTextFieldGenerationForFloats()
  1224. {
  1225. $this->article['schema'] = [
  1226. 'foo' => [
  1227. 'type' => 'float',
  1228. 'null' => false,
  1229. 'default' => null,
  1230. 'length' => 10,
  1231. ],
  1232. ];
  1233. $this->Form->create($this->article);
  1234. $result = $this->Form->control('foo');
  1235. $expected = [
  1236. 'div' => ['class' => 'input number'],
  1237. 'label' => ['for' => 'foo'],
  1238. 'Foo',
  1239. '/label',
  1240. ['input' => [
  1241. 'type' => 'number',
  1242. 'name' => 'foo',
  1243. 'id' => 'foo',
  1244. 'step' => 'any',
  1245. ]],
  1246. '/div',
  1247. ];
  1248. $this->assertHtml($expected, $result);
  1249. $result = $this->Form->control('foo', ['step' => 0.5]);
  1250. $expected = [
  1251. 'div' => ['class' => 'input number'],
  1252. 'label' => ['for' => 'foo'],
  1253. 'Foo',
  1254. '/label',
  1255. ['input' => [
  1256. 'type' => 'number',
  1257. 'name' => 'foo',
  1258. 'id' => 'foo',
  1259. 'step' => '0.5',
  1260. ]],
  1261. '/div',
  1262. ];
  1263. $this->assertHtml($expected, $result);
  1264. }
  1265. /**
  1266. * Tests correct generation of number fields for integer fields
  1267. *
  1268. * @return void
  1269. */
  1270. public function testTextFieldTypeNumberGenerationForIntegers()
  1271. {
  1272. $this->getTableLocator()->get('Contacts', [
  1273. 'className' => __NAMESPACE__ . '\ContactsTable',
  1274. ]);
  1275. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1276. $result = $this->Form->control('age');
  1277. $expected = [
  1278. 'div' => ['class' => 'input number'],
  1279. 'label' => ['for' => 'age'],
  1280. 'Age',
  1281. '/label',
  1282. ['input' => [
  1283. 'type' => 'number', 'name' => 'age',
  1284. 'id' => 'age',
  1285. ]],
  1286. '/div',
  1287. ];
  1288. $this->assertHtml($expected, $result);
  1289. }
  1290. /**
  1291. * Tests correct generation of file upload fields for binary fields
  1292. *
  1293. * @return void
  1294. */
  1295. public function testFileUploadFieldTypeGenerationForBinaries()
  1296. {
  1297. $table = $this->getTableLocator()->get('Contacts', [
  1298. 'className' => __NAMESPACE__ . '\ContactsTable',
  1299. ]);
  1300. $table->setSchema(['foo' => [
  1301. 'type' => 'binary',
  1302. 'null' => false,
  1303. 'default' => null,
  1304. 'length' => 1024,
  1305. ]]);
  1306. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1307. $result = $this->Form->control('foo');
  1308. $expected = [
  1309. 'div' => ['class' => 'input file'],
  1310. 'label' => ['for' => 'foo'],
  1311. 'Foo',
  1312. '/label',
  1313. ['input' => [
  1314. 'type' => 'file', 'name' => 'foo',
  1315. 'id' => 'foo',
  1316. ]],
  1317. '/div',
  1318. ];
  1319. $this->assertHtml($expected, $result);
  1320. }
  1321. /**
  1322. * testFormSecurityMultipleFields method
  1323. *
  1324. * Test secure() with multiple row form. Ensure hash is correct.
  1325. *
  1326. * @return void
  1327. */
  1328. public function testFormSecurityMultipleFields()
  1329. {
  1330. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1331. $fields = [
  1332. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1333. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1334. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0',
  1335. ];
  1336. $result = $this->Form->secure($fields);
  1337. $hash = '8670192c3f040bf58680479060b4755b7a5c3596' .
  1338. '%3AModel.0.hidden%7CModel.0.valid%7CModel.1.hidden%7CModel.1.valid';
  1339. $tokenDebug = urlencode(json_encode([
  1340. '',
  1341. $fields,
  1342. [],
  1343. ]));
  1344. $expected = [
  1345. 'div' => ['style' => 'display:none;'],
  1346. ['input' => [
  1347. 'type' => 'hidden',
  1348. 'name' => '_Token[fields]',
  1349. 'value' => $hash,
  1350. 'autocomplete' => 'off',
  1351. ]],
  1352. ['input' => [
  1353. 'type' => 'hidden',
  1354. 'name' => '_Token[unlocked]',
  1355. 'autocomplete' => 'off',
  1356. 'value' => '',
  1357. ]],
  1358. ['input' => [
  1359. 'type' => 'hidden',
  1360. 'name' => '_Token[debug]',
  1361. 'value' => $tokenDebug,
  1362. 'autocomplete' => 'off',
  1363. ]],
  1364. '/div',
  1365. ];
  1366. $this->assertHtml($expected, $result);
  1367. }
  1368. /**
  1369. * testFormSecurityMultipleSubmitButtons
  1370. *
  1371. * test form submit generation and ensure that _Token is only created on end()
  1372. *
  1373. * @return void
  1374. */
  1375. public function testFormSecurityMultipleSubmitButtons()
  1376. {
  1377. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1378. $this->Form->create($this->article);
  1379. $this->Form->text('Address.title');
  1380. $this->Form->text('Address.first_name');
  1381. $result = $this->Form->submit('Save', ['name' => 'save']);
  1382. $expected = [
  1383. 'div' => ['class' => 'submit'],
  1384. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1385. '/div',
  1386. ];
  1387. $this->assertHtml($expected, $result);
  1388. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1389. $expected = [
  1390. 'div' => ['class' => 'submit'],
  1391. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1392. '/div',
  1393. ];
  1394. $this->assertHtml($expected, $result);
  1395. $result = $this->Form->end();
  1396. $tokenDebug = urlencode(json_encode([
  1397. '/articles/add',
  1398. [
  1399. 'Address.title',
  1400. 'Address.first_name',
  1401. ],
  1402. ['save', 'cancel'],
  1403. ]));
  1404. $expected = [
  1405. 'div' => ['style' => 'display:none;'],
  1406. ['input' => [
  1407. 'type' => 'hidden',
  1408. 'name' => '_Token[fields]',
  1409. 'autocomplete',
  1410. 'value',
  1411. ]],
  1412. ['input' => [
  1413. 'type' => 'hidden',
  1414. 'name' => '_Token[unlocked]',
  1415. 'value' => 'cancel%7Csave',
  1416. 'autocomplete' => 'off',
  1417. ]],
  1418. ['input' => [
  1419. 'type' => 'hidden',
  1420. 'name' => '_Token[debug]',
  1421. 'value' => $tokenDebug,
  1422. 'autocomplete' => 'off',
  1423. ]],
  1424. '/div',
  1425. ];
  1426. $this->assertHtml($expected, $result);
  1427. }
  1428. /**
  1429. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1430. *
  1431. * @return void
  1432. */
  1433. public function testSecurityButtonNestedNamed()
  1434. {
  1435. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  1436. $this->Form->create('Addresses');
  1437. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1438. $result = $this->Form->unlockField();
  1439. $this->assertEquals(['Address.button'], $result);
  1440. }
  1441. /**
  1442. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1443. *
  1444. * @return void
  1445. */
  1446. public function testSecuritySubmitNestedNamed()
  1447. {
  1448. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1449. $this->Form->create($this->article);
  1450. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1451. $result = $this->Form->unlockField();
  1452. $this->assertEquals(['Address.button'], $result);
  1453. }
  1454. /**
  1455. * Test that the correct fields are unlocked for image submits with no names.
  1456. *
  1457. * @return void
  1458. */
  1459. public function testSecuritySubmitImageNoName()
  1460. {
  1461. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1462. $this->Form->create(false);
  1463. $result = $this->Form->submit('save.png');
  1464. $expected = [
  1465. 'div' => ['class' => 'submit'],
  1466. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1467. '/div',
  1468. ];
  1469. $this->assertHtml($expected, $result);
  1470. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1471. }
  1472. /**
  1473. * Test that the correct fields are unlocked for image submits with names.
  1474. *
  1475. * @return void
  1476. */
  1477. public function testSecuritySubmitImageName()
  1478. {
  1479. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1480. $this->Form->create(null);
  1481. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1482. $expected = [
  1483. 'div' => ['class' => 'submit'],
  1484. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1485. '/div',
  1486. ];
  1487. $this->assertHtml($expected, $result);
  1488. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1489. }
  1490. /**
  1491. * testFormSecurityMultipleControlFields method
  1492. *
  1493. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1494. *
  1495. * @return void
  1496. */
  1497. public function testFormSecurityMultipleControlFields()
  1498. {
  1499. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1500. $this->Form->create();
  1501. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1502. $this->Form->control('Addresses.0.title');
  1503. $this->Form->control('Addresses.0.first_name');
  1504. $this->Form->control('Addresses.0.last_name');
  1505. $this->Form->control('Addresses.0.address');
  1506. $this->Form->control('Addresses.0.city');
  1507. $this->Form->control('Addresses.0.phone');
  1508. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1509. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1510. $this->Form->control('Addresses.1.title');
  1511. $this->Form->control('Addresses.1.first_name');
  1512. $this->Form->control('Addresses.1.last_name');
  1513. $this->Form->control('Addresses.1.address');
  1514. $this->Form->control('Addresses.1.city');
  1515. $this->Form->control('Addresses.1.phone');
  1516. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1517. $result = $this->Form->secure($this->Form->fields);
  1518. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1519. $tokenDebug = urlencode(json_encode([
  1520. '/articles/add',
  1521. [
  1522. 'Addresses.0.id' => '123456',
  1523. 'Addresses.0.title',
  1524. 'Addresses.0.first_name',
  1525. 'Addresses.0.last_name',
  1526. 'Addresses.0.address',
  1527. 'Addresses.0.city',
  1528. 'Addresses.0.phone',
  1529. 'Addresses.0.primary',
  1530. 'Addresses.1.id' => '654321',
  1531. 'Addresses.1.title',
  1532. 'Addresses.1.first_name',
  1533. 'Addresses.1.last_name',
  1534. 'Addresses.1.address',
  1535. 'Addresses.1.city',
  1536. 'Addresses.1.phone',
  1537. 'Addresses.1.primary',
  1538. ],
  1539. [],
  1540. ]));
  1541. $expected = [
  1542. 'div' => ['style' => 'display:none;'],
  1543. ['input' => [
  1544. 'type' => 'hidden',
  1545. 'name' => '_Token[fields]',
  1546. 'value' => $hash,
  1547. 'autocomplete' => 'off',
  1548. ]],
  1549. ['input' => [
  1550. 'type' => 'hidden',
  1551. 'name' => '_Token[unlocked]',
  1552. 'autocomplete' => 'off',
  1553. 'value' => '',
  1554. ]],
  1555. ['input' => [
  1556. 'type' => 'hidden',
  1557. 'name' => '_Token[debug]',
  1558. 'value' => $tokenDebug,
  1559. 'autocomplete' => 'off',
  1560. ]],
  1561. '/div',
  1562. ];
  1563. $this->assertHtml($expected, $result);
  1564. }
  1565. /**
  1566. * testFormSecurityArrayFields method
  1567. *
  1568. * Test form security with Model.field.0 style inputs.
  1569. *
  1570. * @return void
  1571. */
  1572. public function testFormSecurityArrayFields()
  1573. {
  1574. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1575. $this->Form->create();
  1576. $this->Form->text('Address.primary.1');
  1577. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1578. $this->Form->text('Address.secondary.1.0');
  1579. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1580. }
  1581. /**
  1582. * testFormSecurityMultipleControlDisabledFields method
  1583. *
  1584. * Test secure form generation with multiple records and disabled fields.
  1585. *
  1586. * @return void
  1587. */
  1588. public function testFormSecurityMultipleControlDisabledFields()
  1589. {
  1590. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1591. 'unlockedFields' => ['first_name', 'address'],
  1592. ]));
  1593. $this->Form->create();
  1594. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1595. $this->Form->text('Addresses.0.title');
  1596. $this->Form->text('Addresses.0.first_name');
  1597. $this->Form->text('Addresses.0.last_name');
  1598. $this->Form->text('Addresses.0.address');
  1599. $this->Form->text('Addresses.0.city');
  1600. $this->Form->text('Addresses.0.phone');
  1601. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1602. $this->Form->text('Addresses.1.title');
  1603. $this->Form->text('Addresses.1.first_name');
  1604. $this->Form->text('Addresses.1.last_name');
  1605. $this->Form->text('Addresses.1.address');
  1606. $this->Form->text('Addresses.1.city');
  1607. $this->Form->text('Addresses.1.phone');
  1608. $result = $this->Form->secure($this->Form->fields);
  1609. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1610. $tokenDebug = urlencode(json_encode([
  1611. '/articles/add',
  1612. [
  1613. 'Addresses.0.id' => '123456',
  1614. 'Addresses.0.title',
  1615. 'Addresses.0.last_name',
  1616. 'Addresses.0.city',
  1617. 'Addresses.0.phone',
  1618. 'Addresses.1.id' => '654321',
  1619. 'Addresses.1.title',
  1620. 'Addresses.1.last_name',
  1621. 'Addresses.1.city',
  1622. 'Addresses.1.phone',
  1623. ],
  1624. [
  1625. 'first_name',
  1626. 'address',
  1627. ],
  1628. ]));
  1629. $expected = [
  1630. 'div' => ['style' => 'display:none;'],
  1631. ['input' => [
  1632. 'type' => 'hidden',
  1633. 'name' => '_Token[fields]',
  1634. 'autocomplete' => 'off',
  1635. 'value' => $hash,
  1636. ]],
  1637. ['input' => [
  1638. 'type' => 'hidden',
  1639. 'name' => '_Token[unlocked]',
  1640. 'autocomplete' => 'off',
  1641. 'value' => 'address%7Cfirst_name',
  1642. ]],
  1643. ['input' => [
  1644. 'type' => 'hidden',
  1645. 'name' => '_Token[debug]',
  1646. 'autocomplete' => 'off',
  1647. 'value' => $tokenDebug,
  1648. ]],
  1649. '/div',
  1650. ];
  1651. $this->assertHtml($expected, $result);
  1652. }
  1653. /**
  1654. * testFormSecurityControlDisabledFields method
  1655. *
  1656. * Test single record form with disabled fields.
  1657. *
  1658. * @return void
  1659. */
  1660. public function testFormSecurityControlUnlockedFields()
  1661. {
  1662. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1663. 'unlockedFields' => ['first_name', 'address'],
  1664. ]));
  1665. $this->Form->create();
  1666. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1667. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1668. $this->Form->text('Addresses.title');
  1669. $this->Form->text('Addresses.first_name');
  1670. $this->Form->text('Addresses.last_name');
  1671. $this->Form->text('Addresses.address');
  1672. $this->Form->text('Addresses.city');
  1673. $this->Form->text('Addresses.phone');
  1674. $result = $this->Form->fields;
  1675. $expected = [
  1676. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1677. 'Addresses.city', 'Addresses.phone',
  1678. ];
  1679. $this->assertEquals($expected, $result);
  1680. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1681. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1682. $tokenDebug = urlencode(json_encode([
  1683. '/articles/add',
  1684. [
  1685. 'Addresses.id' => '123456',
  1686. 'Addresses.title',
  1687. 'Addresses.last_name',
  1688. 'Addresses.city',
  1689. 'Addresses.phone',
  1690. ],
  1691. [
  1692. 'first_name',
  1693. 'address',
  1694. ],
  1695. ]));
  1696. $expected = [
  1697. 'div' => ['style' => 'display:none;'],
  1698. ['input' => [
  1699. 'type' => 'hidden',
  1700. 'name' => '_Token[fields]',
  1701. 'value' => $hash,
  1702. 'autocomplete' => 'off',
  1703. 'data-foo' => 'bar',
  1704. ]],
  1705. ['input' => [
  1706. 'type' => 'hidden',
  1707. 'name' => '_Token[unlocked]',
  1708. 'value' => 'address%7Cfirst_name',
  1709. 'autocomplete' => 'off',
  1710. 'data-foo' => 'bar',
  1711. ]],
  1712. ['input' => [
  1713. 'type' => 'hidden', 'name' => '_Token[debug]',
  1714. 'value' => $tokenDebug,
  1715. 'autocomplete' => 'off',
  1716. 'data-foo' => 'bar',
  1717. ]],
  1718. '/div',
  1719. ];
  1720. $this->assertHtml($expected, $result);
  1721. }
  1722. /**
  1723. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1724. *
  1725. * Test single record form with debugSecurity param.
  1726. *
  1727. * @return void
  1728. */
  1729. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1730. {
  1731. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1732. 'unlockedFields' => ['first_name', 'address'],
  1733. ]));
  1734. $this->Form->create();
  1735. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1736. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1737. $this->Form->text('Addresses.title');
  1738. $this->Form->text('Addresses.first_name');
  1739. $this->Form->text('Addresses.last_name');
  1740. $this->Form->text('Addresses.address');
  1741. $this->Form->text('Addresses.city');
  1742. $this->Form->text('Addresses.phone');
  1743. $result = $this->Form->fields;
  1744. $expected = [
  1745. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1746. 'Addresses.city', 'Addresses.phone',
  1747. ];
  1748. $this->assertEquals($expected, $result);
  1749. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1750. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1751. $tokenDebug = urlencode(json_encode([
  1752. '/articles/add',
  1753. [
  1754. 'Addresses.id' => '123456',
  1755. 'Addresses.title',
  1756. 'Addresses.last_name',
  1757. 'Addresses.city',
  1758. 'Addresses.phone',
  1759. ],
  1760. [
  1761. 'first_name',
  1762. 'address',
  1763. ],
  1764. ]));
  1765. $expected = [
  1766. 'div' => ['style' => 'display:none;'],
  1767. ['input' => [
  1768. 'type' => 'hidden',
  1769. 'name' => '_Token[fields]',
  1770. 'value' => $hash,
  1771. 'autocomplete' => 'off',
  1772. 'data-foo' => 'bar',
  1773. ]],
  1774. ['input' => [
  1775. 'type' => 'hidden',
  1776. 'name' => '_Token[unlocked]',
  1777. 'value' => 'address%7Cfirst_name',
  1778. 'autocomplete' => 'off',
  1779. 'data-foo' => 'bar',
  1780. ]],
  1781. ['input' => [
  1782. 'type' => 'hidden', 'name' => '_Token[debug]',
  1783. 'value' => $tokenDebug,
  1784. 'autocomplete' => 'off',
  1785. 'data-foo' => 'bar',
  1786. ]],
  1787. '/div',
  1788. ];
  1789. $this->assertHtml($expected, $result);
  1790. }
  1791. /**
  1792. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1793. *
  1794. * Debug is false, debugSecurity is true -> no debug
  1795. *
  1796. * @return void
  1797. */
  1798. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1799. {
  1800. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1801. 'unlockedFields' => ['first_name', 'address'],
  1802. ]));
  1803. $this->Form->create();
  1804. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1805. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1806. $this->Form->text('Addresses.title');
  1807. $this->Form->text('Addresses.first_name');
  1808. $this->Form->text('Addresses.last_name');
  1809. $this->Form->text('Addresses.address');
  1810. $this->Form->text('Addresses.city');
  1811. $this->Form->text('Addresses.phone');
  1812. $result = $this->Form->fields;
  1813. $expected = [
  1814. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1815. 'Addresses.city', 'Addresses.phone',
  1816. ];
  1817. $this->assertEquals($expected, $result);
  1818. Configure::write('debug', false);
  1819. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1820. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1821. $expected = [
  1822. 'div' => ['style' => 'display:none;'],
  1823. ['input' => [
  1824. 'type' => 'hidden',
  1825. 'name' => '_Token[fields]',
  1826. 'value' => $hash,
  1827. 'autocomplete' => 'off',
  1828. 'data-foo' => 'bar',
  1829. ]],
  1830. ['input' => [
  1831. 'type' => 'hidden',
  1832. 'name' => '_Token[unlocked]',
  1833. 'value' => 'address%7Cfirst_name',
  1834. 'autocomplete' => 'off',
  1835. 'data-foo' => 'bar',
  1836. ]],
  1837. '/div',
  1838. ];
  1839. $this->assertHtml($expected, $result);
  1840. }
  1841. /**
  1842. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1843. *
  1844. * Test single record form with debugSecurity param.
  1845. *
  1846. * @return void
  1847. */
  1848. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1849. {
  1850. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1851. 'unlockedFields' => ['first_name', 'address'],
  1852. ]));
  1853. $this->Form->create();
  1854. $this->assertEquals(
  1855. $this->View->getRequest()->getParam('_Token.unlockedFields'),
  1856. $this->Form->unlockField()
  1857. );
  1858. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1859. $this->Form->text('Addresses.title');
  1860. $this->Form->text('Addresses.first_name');
  1861. $this->Form->text('Addresses.last_name');
  1862. $this->Form->text('Addresses.address');
  1863. $this->Form->text('Addresses.city');
  1864. $this->Form->text('Addresses.phone');
  1865. $result = $this->Form->fields;
  1866. $expected = [
  1867. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1868. 'Addresses.city', 'Addresses.phone',
  1869. ];
  1870. $this->assertEquals($expected, $result);
  1871. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1872. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1873. $expected = [
  1874. 'div' => ['style' => 'display:none;'],
  1875. ['input' => [
  1876. 'type' => 'hidden',
  1877. 'name' => '_Token[fields]',
  1878. 'value' => $hash,
  1879. 'autocomplete' => 'off',
  1880. 'data-foo' => 'bar',
  1881. ]],
  1882. ['input' => [
  1883. 'type' => 'hidden',
  1884. 'name' => '_Token[unlocked]',
  1885. 'value' => 'address%7Cfirst_name',
  1886. 'autocomplete' => 'off',
  1887. 'data-foo' => 'bar',
  1888. ]],
  1889. '/div',
  1890. ];
  1891. $this->assertHtml($expected, $result);
  1892. }
  1893. /**
  1894. * testFormSecureWithCustomNameAttribute method
  1895. *
  1896. * Test securing inputs with custom name attributes.
  1897. *
  1898. * @return void
  1899. */
  1900. public function testFormSecureWithCustomNameAttribute()
  1901. {
  1902. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1903. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1904. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1905. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1906. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1907. }
  1908. /**
  1909. * testFormSecuredControl method
  1910. *
  1911. * Test generation of entire secure form, assertions made on control() output.
  1912. *
  1913. * @return void
  1914. */
  1915. public function testFormSecuredControl()
  1916. {
  1917. $this->View->setRequest($this->View->getRequest()
  1918. ->withParam('_Token', 'stuff')
  1919. ->withParam('_csrfToken', 'testKey'));
  1920. $this->article['schema'] = [
  1921. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1922. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1923. ];
  1924. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1925. $encoding = strtolower(Configure::read('App.encoding'));
  1926. $expected = [
  1927. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1928. 'div' => ['style' => 'display:none;'],
  1929. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1930. ['input' => [
  1931. 'type' => 'hidden',
  1932. 'name' => '_csrfToken',
  1933. 'value' => 'testKey',
  1934. 'autocomplete' => 'off',
  1935. ]],
  1936. '/div',
  1937. ];
  1938. $this->assertHtml($expected, $result);
  1939. $result = $this->Form->control('ratio');
  1940. $expected = [
  1941. 'div' => ['class'],
  1942. 'label' => ['for'],
  1943. 'Ratio',
  1944. '/label',
  1945. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1946. '/div',
  1947. ];
  1948. $this->assertHtml($expected, $result);
  1949. $result = $this->Form->control('population');
  1950. $expected = [
  1951. 'div' => ['class'],
  1952. 'label' => ['for'],
  1953. 'Population',
  1954. '/label',
  1955. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1956. '/div',
  1957. ];
  1958. $this->assertHtml($expected, $result);
  1959. $result = $this->Form->control('published', ['type' => 'text']);
  1960. $expected = [
  1961. 'div' => ['class' => 'input text'],
  1962. 'label' => ['for' => 'published'],
  1963. 'Published',
  1964. '/label',
  1965. ['input' => [
  1966. 'type' => 'text',
  1967. 'name' => 'published',
  1968. 'id' => 'published',
  1969. ]],
  1970. '/div',
  1971. ];
  1972. $this->assertHtml($expected, $result);
  1973. $result = $this->Form->control('other', ['type' => 'text']);
  1974. $expected = [
  1975. 'div' => ['class' => 'input text'],
  1976. 'label' => ['for' => 'other'],
  1977. 'Other',
  1978. '/label',
  1979. ['input' => [
  1980. 'type' => 'text',
  1981. 'name' => 'other',
  1982. 'id',
  1983. ]],
  1984. '/div',
  1985. ];
  1986. $this->assertHtml($expected, $result);
  1987. $result = $this->Form->hidden('stuff');
  1988. $expected = [
  1989. 'input' => [
  1990. 'type' => 'hidden',
  1991. 'name' => 'stuff',
  1992. ],
  1993. ];
  1994. $this->assertHtml($expected, $result);
  1995. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1996. $expected = ['input' => [
  1997. 'type' => 'hidden',
  1998. 'name' => 'hidden',
  1999. 'value' => '0',
  2000. ]];
  2001. $this->assertHtml($expected, $result);
  2002. $result = $this->Form->control('something', ['type' => 'checkbox']);
  2003. $expected = [
  2004. 'div' => ['class' => 'input checkbox'],
  2005. ['input' => [
  2006. 'type' => 'hidden',
  2007. 'name' => 'something',
  2008. 'value' => '0',
  2009. ]],
  2010. 'label' => ['for' => 'something'],
  2011. ['input' => [
  2012. 'type' => 'checkbox',
  2013. 'name' => 'something',
  2014. 'value' => '1',
  2015. 'id' => 'something',
  2016. ]],
  2017. 'Something',
  2018. '/label',
  2019. '/div',
  2020. ];
  2021. $this->assertHtml($expected, $result);
  2022. $result = $this->Form->fields;
  2023. $expectedFields = [
  2024. 'ratio',
  2025. 'population',
  2026. 'published',
  2027. 'other',
  2028. 'stuff' => '',
  2029. 'hidden' => '0',
  2030. 'something',
  2031. ];
  2032. $this->assertEquals($expectedFields, $result);
  2033. $result = $this->Form->secure($this->Form->fields);
  2034. $tokenDebug = urlencode(json_encode([
  2035. '/articles/add',
  2036. $expectedFields,
  2037. [],
  2038. ]));
  2039. $expected = [
  2040. 'div' => ['style' => 'display:none;'],
  2041. ['input' => [
  2042. 'type' => 'hidden',
  2043. 'name' => '_Token[fields]',
  2044. 'value',
  2045. 'autocomplete',
  2046. ]],
  2047. ['input' => [
  2048. 'type' => 'hidden',
  2049. 'name' => '_Token[unlocked]',
  2050. 'value' => '',
  2051. 'autocomplete' => 'off',
  2052. ]],
  2053. ['input' => [
  2054. 'type' => 'hidden', 'name' => '_Token[debug]',
  2055. 'value' => $tokenDebug,
  2056. 'autocomplete' => 'off',
  2057. ]],
  2058. '/div',
  2059. ];
  2060. $this->assertHtml($expected, $result);
  2061. }
  2062. /**
  2063. * testSecuredControlCustomName method
  2064. *
  2065. * Test secured inputs with custom names.
  2066. *
  2067. * @return void
  2068. */
  2069. public function testSecuredControlCustomName()
  2070. {
  2071. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2072. $this->assertEquals([], $this->Form->fields);
  2073. $this->Form->text('text_input', [
  2074. 'name' => 'Option[General.default_role]',
  2075. ]);
  2076. $expected = ['Option.General.default_role'];
  2077. $this->assertEquals($expected, $this->Form->fields);
  2078. $this->Form->select('select_box', [1, 2], [
  2079. 'name' => 'Option[General.select_role]',
  2080. ]);
  2081. $expected[] = 'Option.General.select_role';
  2082. $this->assertEquals($expected, $this->Form->fields);
  2083. $this->Form->text('other.things[]');
  2084. $expected[] = 'other.things';
  2085. $this->assertEquals($expected, $this->Form->fields);
  2086. }
  2087. /**
  2088. * testSecuredControlDuplicate method
  2089. *
  2090. * Test that a hidden field followed by a visible field
  2091. * undoes the hidden field locking.
  2092. *
  2093. * @return void
  2094. */
  2095. public function testSecuredControlDuplicate()
  2096. {
  2097. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2098. $this->assertEquals([], $this->Form->fields);
  2099. $this->Form->control('text_val', [
  2100. 'type' => 'hidden',
  2101. 'value' => 'some text',
  2102. ]);
  2103. $expected = ['text_val' => 'some text'];
  2104. $this->assertEquals($expected, $this->Form->fields);
  2105. $this->Form->control('text_val', [
  2106. 'type' => 'text',
  2107. ]);
  2108. $expected = ['text_val'];
  2109. $this->assertEquals($expected, $this->Form->fields);
  2110. }
  2111. /**
  2112. * testFormSecuredFileControl method
  2113. *
  2114. * Tests that the correct keys are added to the field hash index.
  2115. *
  2116. * @return void
  2117. */
  2118. public function testFormSecuredFileControl()
  2119. {
  2120. $this->assertEquals([], $this->Form->fields);
  2121. $this->Form->file('Attachment.file');
  2122. $expected = [
  2123. 'Attachment.file.name', 'Attachment.file.type',
  2124. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2125. 'Attachment.file.size',
  2126. ];
  2127. $this->assertEquals($expected, $this->Form->fields);
  2128. }
  2129. /**
  2130. * testFormSecuredMultipleSelect method
  2131. *
  2132. * Test that multiple selects keys are added to field hash.
  2133. *
  2134. * @return void
  2135. */
  2136. public function testFormSecuredMultipleSelect()
  2137. {
  2138. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  2139. $this->assertEquals([], $this->Form->fields);
  2140. $options = ['1' => 'one', '2' => 'two'];
  2141. $this->Form->select('Model.select', $options);
  2142. $expected = ['Model.select'];
  2143. $this->assertEquals($expected, $this->Form->fields);
  2144. $this->Form->fields = [];
  2145. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2146. $this->assertEquals($expected, $this->Form->fields);
  2147. }
  2148. /**
  2149. * testFormSecuredRadio method
  2150. *
  2151. * @return void
  2152. */
  2153. public function testFormSecuredRadio()
  2154. {
  2155. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2156. $this->assertEquals([], $this->Form->fields);
  2157. $options = ['1' => 'option1', '2' => 'option2'];
  2158. $this->Form->radio('Test.test', $options);
  2159. $expected = ['Test.test'];
  2160. $this->assertEquals($expected, $this->Form->fields);
  2161. $this->Form->radio('Test.all', $options, [
  2162. 'disabled' => ['option1', 'option2'],
  2163. ]);
  2164. $expected = ['Test.test', 'Test.all' => ''];
  2165. $this->assertEquals($expected, $this->Form->fields);
  2166. $this->Form->radio('Test.some', $options, [
  2167. 'disabled' => ['option1'],
  2168. ]);
  2169. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2170. $this->assertEquals($expected, $this->Form->fields);
  2171. }
  2172. /**
  2173. * testFormSecuredAndDisabledNotAssoc method
  2174. *
  2175. * Test that when disabled is in a list based attribute array it works.
  2176. *
  2177. * @return void
  2178. */
  2179. public function testFormSecuredAndDisabledNotAssoc()
  2180. {
  2181. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2182. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2183. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2184. $this->Form->text('Model.text', ['disabled']);
  2185. $this->Form->textarea('Model.textarea', ['disabled']);
  2186. $this->Form->password('Model.password', ['disabled']);
  2187. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2188. $expected = [
  2189. 'Model.radio' => '',
  2190. ];
  2191. $this->assertEquals($expected, $this->Form->fields);
  2192. }
  2193. /**
  2194. * testFormSecuredAndDisabled method
  2195. *
  2196. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2197. * hashing.
  2198. *
  2199. * @return void
  2200. */
  2201. public function testFormSecuredAndDisabled()
  2202. {
  2203. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2204. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2205. $this->Form->text('Model.text', ['disabled' => true]);
  2206. $this->Form->password('Model.text', ['disabled' => true]);
  2207. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2208. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2209. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2210. $this->Form->year('Model.year', ['disabled' => true]);
  2211. $this->Form->month('Model.month', ['disabled' => true]);
  2212. $this->Form->day('Model.day', ['disabled' => true]);
  2213. $this->Form->hour('Model.hour', ['disabled' => true]);
  2214. $this->Form->minute('Model.minute', ['disabled' => true]);
  2215. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2216. $expected = [
  2217. 'Model.radio' => '',
  2218. ];
  2219. $this->assertEquals($expected, $this->Form->fields);
  2220. }
  2221. /**
  2222. * testDisableSecurityUsingForm method
  2223. *
  2224. * @return void
  2225. */
  2226. public function testDisableSecurityUsingForm()
  2227. {
  2228. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2229. 'disabledFields' => [],
  2230. ]));
  2231. $this->Form->create();
  2232. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2233. $this->Form->text('Addresses.title');
  2234. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2235. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2236. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2237. $result = $this->Form->fields;
  2238. $expected = [
  2239. 'Addresses.id' => '123456', 'Addresses.title',
  2240. ];
  2241. $this->assertEquals($expected, $result);
  2242. }
  2243. /**
  2244. * testUnlockFieldAddsToList method
  2245. *
  2246. * Test disableField.
  2247. *
  2248. * @return void
  2249. */
  2250. public function testUnlockFieldAddsToList()
  2251. {
  2252. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2253. 'unlockedFields' => [],
  2254. ]));
  2255. $this->Form->unlockField('Contact.name');
  2256. $this->Form->text('Contact.name');
  2257. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2258. $this->assertEquals([], $this->Form->fields);
  2259. }
  2260. /**
  2261. * testUnlockFieldRemovingFromFields method
  2262. *
  2263. * Test unlockField removing from fields array.
  2264. *
  2265. * @return void
  2266. */
  2267. public function testUnlockFieldRemovingFromFields()
  2268. {
  2269. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2270. 'unlockedFields' => [],
  2271. ]));
  2272. $this->Form->create($this->article);
  2273. $this->Form->hidden('Article.id', ['value' => 1]);
  2274. $this->Form->text('Article.title');
  2275. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2276. $this->assertContains('Article.title', $this->Form->fields, 'Field should be secured.');
  2277. $this->Form->unlockField('Article.title');
  2278. $this->Form->unlockField('Article.id');
  2279. $this->assertEquals([], $this->Form->fields);
  2280. }
  2281. /**
  2282. * testResetUnlockFields method
  2283. *
  2284. * Test reset unlockFields, when create new form.
  2285. *
  2286. * @return void
  2287. */
  2288. public function testResetUnlockFields()
  2289. {
  2290. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2291. 'key' => 'testKey',
  2292. 'unlockedFields' => [],
  2293. ]));
  2294. $this->Form->unlockField('Contact.id');
  2295. $this->Form->create('Contact');
  2296. $this->Form->hidden('Contact.id', ['value' => 1]);
  2297. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2298. $this->Form->end();
  2299. $this->Form->create('Contact');
  2300. $this->Form->hidden('Contact.id', ['value' => 1]);
  2301. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2302. }
  2303. /**
  2304. * testSecuredFormUrlIgnoresHost method
  2305. *
  2306. * Test that only the path + query elements of a form's URL show up in their hash.
  2307. *
  2308. * @return void
  2309. */
  2310. public function testSecuredFormUrlIgnoresHost()
  2311. {
  2312. $this->View->setRequest($this->View->getRequest()->withParam('_Token', ['key' => 'testKey']));
  2313. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2314. $this->Form->create($this->article, [
  2315. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]],
  2316. ]);
  2317. $result = $this->Form->secure();
  2318. $this->assertContains($expected, $result);
  2319. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2320. $result = $this->Form->secure();
  2321. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2322. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2323. $result = $this->Form->secure();
  2324. $this->assertContains($expected, $result, 'URL path + query should work.');
  2325. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2326. $result = $this->Form->secure();
  2327. $this->assertNotContains($expected, $result, 'URL is different');
  2328. }
  2329. /**
  2330. * testSecuredFormUrlHasHtmlAndIdentifier method
  2331. *
  2332. * Test that URL, HTML and identifier show up in their hashes.
  2333. *
  2334. * @return void
  2335. */
  2336. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2337. {
  2338. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2339. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2340. $this->Form->create($this->article, [
  2341. 'url' => [
  2342. 'controller' => 'articles',
  2343. 'action' => 'view',
  2344. '?' => [
  2345. 'page' => 1,
  2346. 'limit' => 10,
  2347. 'html' => '<>"',
  2348. ],
  2349. '#' => 'result',
  2350. ],
  2351. ]);
  2352. $result = $this->Form->secure();
  2353. $this->assertContains($expected, $result);
  2354. $this->Form->create($this->article, [
  2355. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2356. ]);
  2357. $result = $this->Form->secure();
  2358. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2359. $this->Form->create($this->article, [
  2360. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2361. ]);
  2362. $result = $this->Form->secure();
  2363. $this->assertContains($expected, $result, 'URL path + query should work.');
  2364. }
  2365. /**
  2366. * testErrorMessageDisplay method
  2367. *
  2368. * Test error message display.
  2369. *
  2370. * @return void
  2371. */
  2372. public function testErrorMessageDisplay()
  2373. {
  2374. $this->article['errors'] = [
  2375. 'Article' => [
  2376. 'title' => 'error message',
  2377. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2378. ],
  2379. ];
  2380. $this->Form->create($this->article);
  2381. $result = $this->Form->control('Article.title');
  2382. $expected = [
  2383. 'div' => ['class' => 'input text error'],
  2384. 'label' => ['for' => 'article-title'],
  2385. 'Title',
  2386. '/label',
  2387. 'input' => [
  2388. 'type' => 'text', 'name' => 'Article[title]',
  2389. 'id' => 'article-title', 'class' => 'form-error',
  2390. ],
  2391. ['div' => ['class' => 'error-message']],
  2392. 'error message',
  2393. '/div',
  2394. '/div',
  2395. ];
  2396. $this->assertHtml($expected, $result);
  2397. $result = $this->Form->control('Article.title', [
  2398. 'templates' => [
  2399. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>',
  2400. ],
  2401. ]);
  2402. $expected = [
  2403. 'div' => ['class' => 'input text error'],
  2404. 'label' => ['for' => 'article-title'],
  2405. 'Title',
  2406. '/label',
  2407. 'input' => [
  2408. 'type' => 'text', 'name' => 'Article[title]',
  2409. 'id' => 'article-title', 'class' => 'form-error',
  2410. ],
  2411. '/div',
  2412. ];
  2413. $this->assertHtml($expected, $result);
  2414. $result = $this->Form->control('Article.content');
  2415. $expected = [
  2416. 'div' => ['class' => 'input text error'],
  2417. 'label' => ['for' => 'article-content'],
  2418. 'Content',
  2419. '/label',
  2420. 'input' => [
  2421. 'type' => 'text', 'name' => 'Article[content]',
  2422. 'id' => 'article-content', 'class' => 'form-error',
  2423. ],
  2424. ['div' => ['class' => 'error-message']],
  2425. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2426. '/div',
  2427. '/div',
  2428. ];
  2429. $this->assertHtml($expected, $result);
  2430. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2431. $expected = [
  2432. 'div' => ['class' => 'input text error'],
  2433. 'label' => ['for' => 'article-content'],
  2434. 'Content',
  2435. '/label',
  2436. 'input' => [
  2437. 'type' => 'text', 'name' => 'Article[content]',
  2438. 'id' => 'article-content', 'class' => 'form-error',
  2439. ],
  2440. ['div' => ['class' => 'error-message']],
  2441. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2442. '/div',
  2443. '/div',
  2444. ];
  2445. $this->assertHtml($expected, $result);
  2446. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2447. $expected = [
  2448. 'div' => ['class' => 'input text error'],
  2449. 'label' => ['for' => 'article-content'],
  2450. 'Content',
  2451. '/label',
  2452. 'input' => [
  2453. 'type' => 'text', 'name' => 'Article[content]',
  2454. 'id' => 'article-content', 'class' => 'form-error',
  2455. ],
  2456. ['div' => ['class' => 'error-message']],
  2457. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2458. '/div',
  2459. '/div',
  2460. ];
  2461. $this->assertHtml($expected, $result);
  2462. }
  2463. /**
  2464. * testEmptyErrorValidation method
  2465. *
  2466. * Test validation errors, when validation message is an empty string.
  2467. *
  2468. * @return void
  2469. */
  2470. public function testEmptyErrorValidation()
  2471. {
  2472. $this->article['errors'] = [
  2473. 'Article' => ['title' => ''],
  2474. ];
  2475. $this->Form->create($this->article);
  2476. $result = $this->Form->control('Article.title');
  2477. $expected = [
  2478. 'div' => ['class' => 'input text error'],
  2479. 'label' => ['for' => 'article-title'],
  2480. 'Title',
  2481. '/label',
  2482. 'input' => [
  2483. 'type' => 'text', 'name' => 'Article[title]',
  2484. 'id' => 'article-title', 'class' => 'form-error',
  2485. ],
  2486. ['div' => ['class' => 'error-message']],
  2487. [],
  2488. '/div',
  2489. '/div',
  2490. ];
  2491. $this->assertHtml($expected, $result);
  2492. }
  2493. /**
  2494. * testEmptyControlErrorValidation method
  2495. *
  2496. * Test validation errors, when calling control() overriding validation message by an empty string.
  2497. *
  2498. * @return void
  2499. */
  2500. public function testEmptyControlErrorValidation()
  2501. {
  2502. $this->article['errors'] = [
  2503. 'Article' => ['title' => 'error message'],
  2504. ];
  2505. $this->Form->create($this->article);
  2506. $result = $this->Form->control('Article.title', ['error' => '']);
  2507. $expected = [
  2508. 'div' => ['class' => 'input text error'],
  2509. 'label' => ['for' => 'article-title'],
  2510. 'Title',
  2511. '/label',
  2512. 'input' => [
  2513. 'type' => 'text', 'name' => 'Article[title]',
  2514. 'id' => 'article-title', 'class' => 'form-error',
  2515. ],
  2516. ['div' => ['class' => 'error-message']],
  2517. [],
  2518. '/div',
  2519. '/div',
  2520. ];
  2521. $this->assertHtml($expected, $result);
  2522. }
  2523. /**
  2524. * testControlErrorMessage method
  2525. *
  2526. * Test validation errors, when calling control() overriding validation messages.
  2527. *
  2528. * @return void
  2529. */
  2530. public function testControlErrorMessage()
  2531. {
  2532. $this->article['errors'] = [
  2533. 'title' => ['error message'],
  2534. ];
  2535. $this->Form->create($this->article);
  2536. $result = $this->Form->control('title', [
  2537. 'error' => 'Custom error!',
  2538. ]);
  2539. $expected = [
  2540. 'div' => ['class' => 'input text required error'],
  2541. 'label' => ['for' => 'title'],
  2542. 'Title',
  2543. '/label',
  2544. 'input' => [
  2545. 'type' => 'text', 'name' => 'title',
  2546. 'id' => 'title', 'class' => 'form-error',
  2547. 'required' => 'required',
  2548. ],
  2549. ['div' => ['class' => 'error-message']],
  2550. 'Custom error!',
  2551. '/div',
  2552. '/div',
  2553. ];
  2554. $this->assertHtml($expected, $result);
  2555. $result = $this->Form->control('title', [
  2556. 'error' => ['error message' => 'Custom error!'],
  2557. ]);
  2558. $expected = [
  2559. 'div' => ['class' => 'input text required error'],
  2560. 'label' => ['for' => 'title'],
  2561. 'Title',
  2562. '/label',
  2563. 'input' => [
  2564. 'type' => 'text',
  2565. 'name' => 'title',
  2566. 'id' => 'title',
  2567. 'class' => 'form-error',
  2568. 'required' => 'required',
  2569. ],
  2570. ['div' => ['class' => 'error-message']],
  2571. 'Custom error!',
  2572. '/div',
  2573. '/div',
  2574. ];
  2575. $this->assertHtml($expected, $result);
  2576. }
  2577. /**
  2578. * testFormValidationAssociated method
  2579. *
  2580. * Tests displaying errors for nested entities.
  2581. *
  2582. * @return void
  2583. */
  2584. public function testFormValidationAssociated()
  2585. {
  2586. $nested = new Entity(['foo' => 'bar']);
  2587. $nested->setError('foo', ['not a valid bar']);
  2588. $entity = new Entity(['nested' => $nested]);
  2589. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2590. $result = $this->Form->error('nested.foo');
  2591. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2592. }
  2593. /**
  2594. * testFormValidationAssociatedSecondLevel method
  2595. *
  2596. * Test form error display with associated model.
  2597. *
  2598. * @return void
  2599. */
  2600. public function testFormValidationAssociatedSecondLevel()
  2601. {
  2602. $inner = new Entity(['bar' => 'baz']);
  2603. $nested = new Entity(['foo' => $inner]);
  2604. $entity = new Entity(['nested' => $nested]);
  2605. $inner->setError('bar', ['not a valid one']);
  2606. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2607. $result = $this->Form->error('nested.foo.bar');
  2608. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2609. }
  2610. /**
  2611. * testFormValidationMultiRecord method
  2612. *
  2613. * Test form error display with multiple records.
  2614. *
  2615. * @return void
  2616. */
  2617. public function testFormValidationMultiRecord()
  2618. {
  2619. $one = new Entity();
  2620. $two = new Entity();
  2621. $this->getTableLocator()->get('Contacts', [
  2622. 'className' => __NAMESPACE__ . '\ContactsTable',
  2623. ]);
  2624. $one->set('email', '');
  2625. $one->setError('email', ['invalid email']);
  2626. $two->set('name', '');
  2627. $two->setError('name', ['This is wrong']);
  2628. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2629. $result = $this->Form->control('0.email');
  2630. $expected = [
  2631. 'div' => ['class' => 'input email error'],
  2632. 'label' => ['for' => '0-email'],
  2633. 'Email',
  2634. '/label',
  2635. 'input' => [
  2636. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2637. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2638. ],
  2639. ['div' => ['class' => 'error-message']],
  2640. 'invalid email',
  2641. '/div',
  2642. '/div',
  2643. ];
  2644. $this->assertHtml($expected, $result);
  2645. $result = $this->Form->control('1.name');
  2646. $expected = [
  2647. 'div' => ['class' => 'input text error'],
  2648. 'label' => ['for' => '1-name'],
  2649. 'Name',
  2650. '/label',
  2651. 'input' => [
  2652. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2653. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2654. ],
  2655. ['div' => ['class' => 'error-message']],
  2656. 'This is wrong',
  2657. '/div',
  2658. '/div',
  2659. ];
  2660. $this->assertHtml($expected, $result);
  2661. }
  2662. /**
  2663. * testControl method
  2664. *
  2665. * Test various incarnations of control().
  2666. *
  2667. * @return void
  2668. */
  2669. public function testControl()
  2670. {
  2671. $this->getTableLocator()->get('ValidateUsers', [
  2672. 'className' => __NAMESPACE__ . '\ValidateUsersTable',
  2673. ]);
  2674. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2675. $result = $this->Form->control('ValidateUsers.balance');
  2676. $expected = [
  2677. 'div' => ['class'],
  2678. 'label' => ['for'],
  2679. 'Balance',
  2680. '/label',
  2681. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2682. '/div',
  2683. ];
  2684. $this->assertHtml($expected, $result);
  2685. $result = $this->Form->control('ValidateUser.cost_decimal');
  2686. $expected = [
  2687. 'div' => ['class'],
  2688. 'label' => ['for'],
  2689. 'Cost Decimal',
  2690. '/label',
  2691. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2692. '/div',
  2693. ];
  2694. $this->assertHtml($expected, $result);
  2695. $result = $this->Form->control('ValidateUser.null_decimal');
  2696. $expected = [
  2697. 'div' => ['class'],
  2698. 'label' => ['for'],
  2699. 'Null Decimal',
  2700. '/label',
  2701. 'input' => ['name', 'type' => 'number', 'id'],
  2702. '/div',
  2703. ];
  2704. $this->assertHtml($expected, $result);
  2705. }
  2706. /**
  2707. * testControlCustomization method
  2708. *
  2709. * Tests the input method and passing custom options.
  2710. *
  2711. * @return void
  2712. */
  2713. public function testControlCustomization()
  2714. {
  2715. $this->getTableLocator()->get('Contacts', [
  2716. 'className' => __NAMESPACE__ . '\ContactsTable',
  2717. ]);
  2718. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2719. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2720. $expected = [
  2721. 'div' => ['class' => 'input email'],
  2722. 'label' => ['for' => 'custom'],
  2723. 'Email',
  2724. '/label',
  2725. ['input' => [
  2726. 'type' => 'email', 'name' => 'Contact[email]',
  2727. 'id' => 'custom', 'maxlength' => 255,
  2728. ]],
  2729. '/div',
  2730. ];
  2731. $this->assertHtml($expected, $result);
  2732. $result = $this->Form->control('Contact.email', [
  2733. 'templates' => ['inputContainer' => '<div>{{content}}</div>'],
  2734. ]);
  2735. $expected = [
  2736. '<div',
  2737. 'label' => ['for' => 'contact-email'],
  2738. 'Email',
  2739. '/label',
  2740. ['input' => [
  2741. 'type' => 'email', 'name' => 'Contact[email]',
  2742. 'id' => 'contact-email', 'maxlength' => 255,
  2743. ]],
  2744. '/div',
  2745. ];
  2746. $this->assertHtml($expected, $result);
  2747. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2748. $expected = [
  2749. 'div' => ['class' => 'input text'],
  2750. 'label' => ['for' => 'contact-email'],
  2751. 'Email',
  2752. '/label',
  2753. ['input' => [
  2754. 'type' => 'text', 'name' => 'Contact[email]',
  2755. 'id' => 'contact-email', 'maxlength' => '255',
  2756. ]],
  2757. '/div',
  2758. ];
  2759. $this->assertHtml($expected, $result);
  2760. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2761. $expected = [
  2762. 'div' => ['class' => 'input text'],
  2763. 'label' => ['for' => 'contact-5-email'],
  2764. 'Email',
  2765. '/label',
  2766. ['input' => [
  2767. 'type' => 'text', 'name' => 'Contact[5][email]',
  2768. 'id' => 'contact-5-email', 'maxlength' => '255',
  2769. ]],
  2770. '/div',
  2771. ];
  2772. $this->assertHtml($expected, $result);
  2773. $result = $this->Form->control('Contact.password');
  2774. $expected = [
  2775. 'div' => ['class' => 'input password'],
  2776. 'label' => ['for' => 'contact-password'],
  2777. 'Password',
  2778. '/label',
  2779. ['input' => [
  2780. 'type' => 'password', 'name' => 'Contact[password]',
  2781. 'id' => 'contact-password',
  2782. ]],
  2783. '/div',
  2784. ];
  2785. $this->assertHtml($expected, $result);
  2786. $result = $this->Form->control('Contact.email', [
  2787. 'type' => 'file', 'class' => 'textbox',
  2788. ]);
  2789. $expected = [
  2790. 'div' => ['class' => 'input file'],
  2791. 'label' => ['for' => 'contact-email'],
  2792. 'Email',
  2793. '/label',
  2794. ['input' => [
  2795. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2796. 'id' => 'contact-email',
  2797. ]],
  2798. '/div',
  2799. ];
  2800. $this->assertHtml($expected, $result);
  2801. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2802. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2803. $result = $this->Form->control('phone');
  2804. $expected = [
  2805. 'div' => ['class' => 'input tel'],
  2806. 'label' => ['for' => 'phone'],
  2807. 'Phone',
  2808. '/label',
  2809. ['input' => [
  2810. 'type' => 'tel', 'name' => 'phone',
  2811. 'value' => 'Hello &amp; World &gt; weird chars',
  2812. 'id' => 'phone', 'maxlength' => 255,
  2813. ]],
  2814. '/div',
  2815. ];
  2816. $this->assertHtml($expected, $result);
  2817. $this->View->setRequest(
  2818. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2819. );
  2820. $this->Form->create();
  2821. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2822. $expected = [
  2823. 'div' => ['class' => 'input text'],
  2824. 'label' => ['for' => 'myId'],
  2825. 'Field',
  2826. '/label',
  2827. 'input' => [
  2828. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2829. 'value' => 'My value', 'id' => 'myId',
  2830. ],
  2831. '/div',
  2832. ];
  2833. $this->assertHtml($expected, $result);
  2834. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2835. $this->Form->create();
  2836. $entity->setError('field', 'Badness!');
  2837. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2838. $result = $this->Form->control('field');
  2839. $expected = [
  2840. 'div' => ['class' => 'input text error'],
  2841. 'label' => ['for' => 'field'],
  2842. 'Field',
  2843. '/label',
  2844. 'input' => [
  2845. 'type' => 'text', 'name' => 'field',
  2846. 'id' => 'field', 'class' => 'form-error',
  2847. ],
  2848. ['div' => ['class' => 'error-message']],
  2849. 'Badness!',
  2850. '/div',
  2851. '/div',
  2852. ];
  2853. $this->assertHtml($expected, $result);
  2854. $result = $this->Form->control('field', [
  2855. 'templates' => [
  2856. 'inputContainerError' => '{{content}}{{error}}',
  2857. 'error' => '<span class="error-message">{{content}}</span>',
  2858. ],
  2859. ]);
  2860. $expected = [
  2861. 'label' => ['for' => 'field'],
  2862. 'Field',
  2863. '/label',
  2864. 'input' => [
  2865. 'type' => 'text', 'name' => 'field',
  2866. 'id' => 'field', 'class' => 'form-error',
  2867. ],
  2868. ['span' => ['class' => 'error-message']],
  2869. 'Badness!',
  2870. '/span',
  2871. ];
  2872. $this->assertHtml($expected, $result);
  2873. $entity->setError('field', ['minLength'], true);
  2874. $result = $this->Form->control('field', [
  2875. 'error' => [
  2876. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2877. 'maxLength' => 'login too large',
  2878. ],
  2879. ]);
  2880. $expected = [
  2881. 'div' => ['class' => 'input text error'],
  2882. 'label' => ['for' => 'field'],
  2883. 'Field',
  2884. '/label',
  2885. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2886. ['div' => ['class' => 'error-message']],
  2887. 'Le login doit contenir au moins 2 caractères',
  2888. '/div',
  2889. '/div',
  2890. ];
  2891. $this->assertHtml($expected, $result);
  2892. $entity->setError('field', ['maxLength'], true);
  2893. $result = $this->Form->control('field', [
  2894. 'error' => [
  2895. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2896. 'maxLength' => 'login too large',
  2897. ],
  2898. ]);
  2899. $expected = [
  2900. 'div' => ['class' => 'input text error'],
  2901. 'label' => ['for' => 'field'],
  2902. 'Field',
  2903. '/label',
  2904. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2905. ['div' => ['class' => 'error-message']],
  2906. 'login too large',
  2907. '/div',
  2908. '/div',
  2909. ];
  2910. $this->assertHtml($expected, $result);
  2911. }
  2912. /**
  2913. * testControlWithTemplateFile method
  2914. *
  2915. * Test that control() accepts a template file.
  2916. *
  2917. * @return void
  2918. */
  2919. public function testControlWithTemplateFile()
  2920. {
  2921. $result = $this->Form->control('field', [
  2922. 'templates' => 'htmlhelper_tags',
  2923. ]);
  2924. $expected = [
  2925. 'label' => ['for' => 'field'],
  2926. 'Field',
  2927. '/label',
  2928. 'input' => [
  2929. 'type' => 'text', 'name' => 'field',
  2930. 'id' => 'field',
  2931. ],
  2932. ];
  2933. $this->assertHtml($expected, $result);
  2934. }
  2935. /**
  2936. * testNestedControlsEndWithBrackets method
  2937. *
  2938. * Test that nested inputs end with brackets.
  2939. *
  2940. * @return void
  2941. */
  2942. public function testNestedControlsEndWithBrackets()
  2943. {
  2944. $result = $this->Form->text('nested.text[]');
  2945. $expected = [
  2946. 'input' => [
  2947. 'type' => 'text', 'name' => 'nested[text][]',
  2948. ],
  2949. ];
  2950. $this->assertHtml($expected, $result);
  2951. $result = $this->Form->file('nested.file[]');
  2952. $expected = [
  2953. 'input' => [
  2954. 'type' => 'file', 'name' => 'nested[file][]',
  2955. ],
  2956. ];
  2957. $this->assertHtml($expected, $result);
  2958. }
  2959. /**
  2960. * testCreateIdPrefix method
  2961. *
  2962. * Test id prefix.
  2963. *
  2964. * @return void
  2965. */
  2966. public function testCreateIdPrefix()
  2967. {
  2968. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2969. $result = $this->Form->control('field');
  2970. $expected = [
  2971. 'div' => ['class' => 'input text'],
  2972. 'label' => ['for' => 'prefix-field'],
  2973. 'Field',
  2974. '/label',
  2975. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2976. '/div',
  2977. ];
  2978. $this->assertHtml($expected, $result);
  2979. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2980. $expected = [
  2981. 'div' => ['class' => 'input text'],
  2982. 'label' => ['for' => 'custom-id'],
  2983. 'Field',
  2984. '/label',
  2985. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2986. '/div',
  2987. ];
  2988. $this->assertHtml($expected, $result);
  2989. $result = $this->Form->radio('Model.field', ['option A']);
  2990. $expected = [
  2991. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2992. 'label' => ['for' => 'prefix-model-field-0'],
  2993. ['input' => [
  2994. 'type' => 'radio',
  2995. 'name' => 'Model[field]',
  2996. 'value' => '0',
  2997. 'id' => 'prefix-model-field-0',
  2998. ]],
  2999. 'option A',
  3000. '/label',
  3001. ];
  3002. $this->assertHtml($expected, $result);
  3003. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  3004. $expected = [
  3005. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  3006. 'label' => ['for' => 'prefix-model-field-0'],
  3007. ['input' => [
  3008. 'type' => 'radio',
  3009. 'name' => 'Model[field]',
  3010. 'value' => '0',
  3011. 'id' => 'prefix-model-field-0',
  3012. ]],
  3013. 'option A',
  3014. '/label',
  3015. ];
  3016. $this->assertHtml($expected, $result);
  3017. $result = $this->Form->select(
  3018. 'Model.multi_field',
  3019. ['first'],
  3020. ['multiple' => 'checkbox']
  3021. );
  3022. $expected = [
  3023. 'input' => [
  3024. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  3025. ],
  3026. ['div' => ['class' => 'checkbox']],
  3027. ['label' => ['for' => 'prefix-model-multi-field-0']],
  3028. ['input' => [
  3029. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3030. 'value' => '0', 'id' => 'prefix-model-multi-field-0',
  3031. ]],
  3032. 'first',
  3033. '/label',
  3034. '/div',
  3035. ];
  3036. $this->assertHtml($expected, $result);
  3037. $this->Form->end();
  3038. $result = $this->Form->control('field');
  3039. $expected = [
  3040. 'div' => ['class' => 'input text'],
  3041. 'label' => ['for' => 'field'],
  3042. 'Field',
  3043. '/label',
  3044. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  3045. '/div',
  3046. ];
  3047. $this->assertHtml($expected, $result);
  3048. }
  3049. /**
  3050. * testControlZero method
  3051. *
  3052. * Test that inputs with 0 can be created.
  3053. *
  3054. * @return void
  3055. */
  3056. public function testControlZero()
  3057. {
  3058. $this->getTableLocator()->get('Contacts', [
  3059. 'className' => __NAMESPACE__ . '\ContactsTable',
  3060. ]);
  3061. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  3062. $result = $this->Form->control('0');
  3063. $expected = [
  3064. 'div' => ['class' => 'input text'],
  3065. 'label' => ['for' => '0'], '/label',
  3066. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  3067. '/div',
  3068. ];
  3069. $this->assertHtml($expected, $result);
  3070. }
  3071. /**
  3072. * testControlCheckbox method
  3073. *
  3074. * Test control() with checkbox creation.
  3075. *
  3076. * @return void
  3077. */
  3078. public function testControlCheckbox()
  3079. {
  3080. $articles = $this->getTableLocator()->get('Articles');
  3081. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3082. $article = $articles->newEntity();
  3083. $this->Form->create($article);
  3084. $result = $this->Form->control('Articles.active');
  3085. $expected = [
  3086. 'div' => ['class' => 'input checkbox'],
  3087. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3088. 'label' => ['for' => 'articles-active'],
  3089. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3090. 'Active',
  3091. '/label',
  3092. '/div',
  3093. ];
  3094. $this->assertHtml($expected, $result);
  3095. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3096. $expected = [
  3097. 'div' => ['class' => 'input checkbox'],
  3098. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3099. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3100. '/div',
  3101. ];
  3102. $this->assertHtml($expected, $result);
  3103. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3104. $expected = [
  3105. 'div' => ['class' => 'input checkbox'],
  3106. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3107. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3108. '/div',
  3109. ];
  3110. $this->assertHtml($expected, $result);
  3111. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3112. $expected = [
  3113. 'div' => ['class' => 'input checkbox'],
  3114. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3115. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3116. '/div',
  3117. ];
  3118. $this->assertHtml($expected, $result);
  3119. $result = $this->Form->control('Articles.disabled', [
  3120. 'label' => 'Disabled',
  3121. 'type' => 'checkbox',
  3122. 'data-foo' => 'disabled',
  3123. ]);
  3124. $expected = [
  3125. 'div' => ['class' => 'input checkbox'],
  3126. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3127. 'label' => ['for' => 'articles-disabled'],
  3128. ['input' => [
  3129. 'type' => 'checkbox',
  3130. 'name' => 'Articles[disabled]',
  3131. 'value' => '1',
  3132. 'id' => 'articles-disabled',
  3133. 'data-foo' => 'disabled',
  3134. ]],
  3135. 'Disabled',
  3136. '/label',
  3137. '/div',
  3138. ];
  3139. $this->assertHtml($expected, $result);
  3140. $result = $this->Form->control('Articles.confirm', [
  3141. 'label' => 'Confirm <b>me</b>!',
  3142. 'type' => 'checkbox',
  3143. 'escape' => false,
  3144. ]);
  3145. $expected = [
  3146. 'div' => ['class' => 'input checkbox'],
  3147. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3148. 'label' => ['for' => 'articles-confirm'],
  3149. ['input' => [
  3150. 'type' => 'checkbox',
  3151. 'name' => 'Articles[confirm]',
  3152. 'value' => '1',
  3153. 'id' => 'articles-confirm',
  3154. ]],
  3155. 'Confirm <b>me</b>!',
  3156. '/label',
  3157. '/div',
  3158. ];
  3159. $this->assertHtml($expected, $result);
  3160. }
  3161. /**
  3162. * testControlHidden method
  3163. *
  3164. * Test that control() does not create wrapping div and label tag for hidden fields.
  3165. *
  3166. * @return void
  3167. */
  3168. public function testControlHidden()
  3169. {
  3170. $this->getTableLocator()->get('ValidateUsers', [
  3171. 'className' => __NAMESPACE__ . '\ValidateUsersTable',
  3172. ]);
  3173. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3174. $result = $this->Form->control('ValidateUser.id');
  3175. $expected = [
  3176. 'input' => ['name', 'type' => 'hidden', 'id'],
  3177. ];
  3178. $this->assertHtml($expected, $result);
  3179. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3180. $expected = [
  3181. 'input' => ['name', 'type' => 'hidden', 'id'],
  3182. ];
  3183. $this->assertHtml($expected, $result);
  3184. }
  3185. /**
  3186. * testControlDatetime method
  3187. *
  3188. * Test form->control() with datetime.
  3189. *
  3190. * @return void
  3191. */
  3192. public function testControlDatetime()
  3193. {
  3194. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3195. ->setMethods(['datetime'])
  3196. ->setConstructorArgs([new View()])
  3197. ->getMock();
  3198. $this->Form->expects($this->once())->method('datetime')
  3199. ->with('prueba', [
  3200. 'type' => 'datetime',
  3201. 'timeFormat' => 24,
  3202. 'minYear' => 2008,
  3203. 'maxYear' => 2011,
  3204. 'interval' => 15,
  3205. 'options' => null,
  3206. 'empty' => false,
  3207. 'id' => 'prueba',
  3208. 'required' => false,
  3209. 'templateVars' => [],
  3210. ])
  3211. ->will($this->returnValue('This is it!'));
  3212. $result = $this->Form->control('prueba', [
  3213. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3214. 'maxYear' => 2011, 'interval' => 15,
  3215. ]);
  3216. $expected = [
  3217. 'div' => ['class' => 'input datetime'],
  3218. '<label',
  3219. 'Prueba',
  3220. '/label',
  3221. 'This is it!',
  3222. '/div',
  3223. ];
  3224. $this->assertHtml($expected, $result);
  3225. }
  3226. /**
  3227. * testControlDatetimeIdPrefix method
  3228. *
  3229. * Test form->control() with datetime with id prefix.
  3230. *
  3231. * @return void
  3232. */
  3233. public function testControlDatetimeIdPrefix()
  3234. {
  3235. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3236. ->setMethods(['datetime'])
  3237. ->setConstructorArgs([new View()])
  3238. ->getMock();
  3239. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3240. $this->Form->expects($this->once())->method('datetime')
  3241. ->with('prueba', [
  3242. 'type' => 'datetime',
  3243. 'timeFormat' => 24,
  3244. 'minYear' => 2008,
  3245. 'maxYear' => 2011,
  3246. 'interval' => 15,
  3247. 'options' => null,
  3248. 'empty' => false,
  3249. 'id' => 'prefix-prueba',
  3250. 'required' => false,
  3251. 'templateVars' => [],
  3252. ])
  3253. ->will($this->returnValue('This is it!'));
  3254. $result = $this->Form->control('prueba', [
  3255. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3256. 'maxYear' => 2011, 'interval' => 15,
  3257. ]);
  3258. $expected = [
  3259. 'div' => ['class' => 'input datetime'],
  3260. '<label',
  3261. 'Prueba',
  3262. '/label',
  3263. 'This is it!',
  3264. '/div',
  3265. ];
  3266. $this->assertHtml($expected, $result);
  3267. }
  3268. /**
  3269. * testControlCheckboxWithDisabledElements method
  3270. *
  3271. * Test generating checkboxes with disabled elements.
  3272. *
  3273. * @return void
  3274. */
  3275. public function testControlCheckboxWithDisabledElements()
  3276. {
  3277. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3278. $result = $this->Form->control('Contact.multiple', [
  3279. 'multiple' => 'checkbox',
  3280. 'disabled' => 'disabled',
  3281. 'options' => $options,
  3282. ]);
  3283. $expected = [
  3284. ['div' => ['class' => 'input select']],
  3285. ['label' => ['for' => 'contact-multiple']],
  3286. 'Multiple',
  3287. '/label',
  3288. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3289. ['div' => ['class' => 'checkbox']],
  3290. ['label' => ['for' => 'contact-multiple-1']],
  3291. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3292. 'One',
  3293. '/label',
  3294. '/div',
  3295. ['div' => ['class' => 'checkbox']],
  3296. ['label' => ['for' => 'contact-multiple-2']],
  3297. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3298. 'Two',
  3299. '/label',
  3300. '/div',
  3301. ['div' => ['class' => 'checkbox']],
  3302. ['label' => ['for' => 'contact-multiple-3']],
  3303. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3304. 'Three',
  3305. '/label',
  3306. '/div',
  3307. '/div',
  3308. ];
  3309. $this->assertHtml($expected, $result);
  3310. // make sure 50 does only disable 50, and not 50f5c0cf
  3311. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3312. $disabled = [50];
  3313. $expected = [
  3314. ['div' => ['class' => 'input select']],
  3315. ['label' => ['for' => 'contact-multiple']],
  3316. 'Multiple',
  3317. '/label',
  3318. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3319. ['div' => ['class' => 'checkbox']],
  3320. ['label' => ['for' => 'contact-multiple-50']],
  3321. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3322. 'Fifty',
  3323. '/label',
  3324. '/div',
  3325. ['div' => ['class' => 'checkbox']],
  3326. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3327. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3328. 'Stringy',
  3329. '/label',
  3330. '/div',
  3331. '/div',
  3332. ];
  3333. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3334. $this->assertHtml($expected, $result);
  3335. }
  3336. /**
  3337. * testControlWithLeadingInteger method
  3338. *
  3339. * Test input name with leading integer, ensure attributes are generated correctly.
  3340. *
  3341. * @return void
  3342. */
  3343. public function testControlWithLeadingInteger()
  3344. {
  3345. $result = $this->Form->text('0.Node.title');
  3346. $expected = [
  3347. 'input' => ['name' => '0[Node][title]', 'type' => 'text'],
  3348. ];
  3349. $this->assertHtml($expected, $result);
  3350. }
  3351. /**
  3352. * testControlSelectType method
  3353. *
  3354. * Test form->control() with select type inputs.
  3355. *
  3356. * @return void
  3357. */
  3358. public function testControlSelectType()
  3359. {
  3360. $result = $this->Form->control(
  3361. 'email',
  3362. [
  3363. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3364. );
  3365. $expected = [
  3366. 'div' => ['class' => 'input select'],
  3367. 'label' => ['for' => 'email'],
  3368. 'Email',
  3369. '/label',
  3370. ['select' => ['name' => 'email', 'id' => 'email']],
  3371. ['option' => ['value' => '']],
  3372. '/option',
  3373. ['option' => ['value' => 'è']],
  3374. 'Firést',
  3375. '/option',
  3376. ['option' => ['value' => 'é']],
  3377. 'Secoènd',
  3378. '/option',
  3379. '/select',
  3380. '/div',
  3381. ];
  3382. $this->assertHtml($expected, $result);
  3383. $result = $this->Form->control(
  3384. 'email',
  3385. [
  3386. 'options' => ['First', 'Second'], 'empty' => true]
  3387. );
  3388. $expected = [
  3389. 'div' => ['class' => 'input select'],
  3390. 'label' => ['for' => 'email'],
  3391. 'Email',
  3392. '/label',
  3393. ['select' => ['name' => 'email', 'id' => 'email']],
  3394. ['option' => ['value' => '']],
  3395. '/option',
  3396. ['option' => ['value' => '0']],
  3397. 'First',
  3398. '/option',
  3399. ['option' => ['value' => '1']],
  3400. 'Second',
  3401. '/option',
  3402. '/select',
  3403. '/div',
  3404. ];
  3405. $this->assertHtml($expected, $result);
  3406. $result = $this->Form->control('email', [
  3407. 'type' => 'select',
  3408. 'options' => new \ArrayObject(['First', 'Second']),
  3409. 'empty' => true,
  3410. ]);
  3411. $this->assertHtml($expected, $result);
  3412. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3413. $this->View->setRequest(
  3414. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3415. );
  3416. $this->Form->create();
  3417. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3418. $expected = [
  3419. 'div' => ['class' => 'input select'],
  3420. 'label' => ['for' => 'model-user-id'],
  3421. 'User',
  3422. '/label',
  3423. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3424. ['option' => ['value' => '']],
  3425. '/option',
  3426. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3427. 'good',
  3428. '/option',
  3429. ['option' => ['value' => 'other']],
  3430. 'bad',
  3431. '/option',
  3432. '/select',
  3433. '/div',
  3434. ];
  3435. $this->assertHtml($expected, $result);
  3436. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3437. $this->View->setRequest(
  3438. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3439. );
  3440. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3441. $expected = [
  3442. 'div' => ['class' => 'input select'],
  3443. 'label' => ['for' => 'thing-user-id'],
  3444. 'User',
  3445. '/label',
  3446. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3447. ['option' => ['value' => '']],
  3448. 'Some Empty',
  3449. '/option',
  3450. ['option' => ['value' => 'value']],
  3451. 'good',
  3452. '/option',
  3453. ['option' => ['value' => 'other']],
  3454. 'bad',
  3455. '/option',
  3456. '/select',
  3457. '/div',
  3458. ];
  3459. $this->assertHtml($expected, $result);
  3460. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3461. $this->View->setRequest(
  3462. $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
  3463. );
  3464. $this->Form->create();
  3465. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3466. $expected = [
  3467. 'div' => ['class' => 'input select'],
  3468. 'label' => ['for' => 'thing-user-id'],
  3469. 'User',
  3470. '/label',
  3471. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3472. ['option' => ['value' => '']],
  3473. 'Some Empty',
  3474. '/option',
  3475. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3476. 'good',
  3477. '/option',
  3478. ['option' => ['value' => 'other']],
  3479. 'bad',
  3480. '/option',
  3481. '/select',
  3482. '/div',
  3483. ];
  3484. $this->assertHtml($expected, $result);
  3485. $result = $this->Form->control('Publisher.id', [
  3486. 'label' => 'Publisher',
  3487. 'type' => 'select',
  3488. 'multiple' => 'checkbox',
  3489. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2'],
  3490. ]);
  3491. $expected = [
  3492. ['div' => ['class' => 'input select']],
  3493. ['label' => ['for' => 'publisher-id']],
  3494. 'Publisher',
  3495. '/label',
  3496. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3497. ['div' => ['class' => 'checkbox']],
  3498. ['label' => ['for' => 'publisher-id-value-1']],
  3499. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3500. 'Label 1',
  3501. '/label',
  3502. '/div',
  3503. ['div' => ['class' => 'checkbox']],
  3504. ['label' => ['for' => 'publisher-id-value-2']],
  3505. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3506. 'Label 2',
  3507. '/label',
  3508. '/div',
  3509. '/div',
  3510. ];
  3511. $this->assertHtml($expected, $result);
  3512. }
  3513. /**
  3514. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3515. *
  3516. * Test that control() and a non standard primary key makes a hidden input by default.
  3517. *
  3518. * @return void
  3519. */
  3520. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3521. {
  3522. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3523. $this->Form->create($this->article);
  3524. $result = $this->Form->control('title');
  3525. $expected = [
  3526. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3527. ];
  3528. $this->assertHtml($expected, $result);
  3529. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3530. $this->Form->create($this->article);
  3531. $result = $this->Form->control('title');
  3532. $expected = [
  3533. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3534. ];
  3535. $this->assertHtml($expected, $result);
  3536. $result = $this->Form->control('body');
  3537. $expected = [
  3538. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3539. ];
  3540. $this->assertHtml($expected, $result);
  3541. }
  3542. /**
  3543. * testControlOverridingMagicSelectType method
  3544. *
  3545. * Test that overriding the magic select type widget is possible.
  3546. *
  3547. * @return void
  3548. */
  3549. public function testControlOverridingMagicSelectType()
  3550. {
  3551. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3552. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3553. $expected = [
  3554. 'div' => ['class' => 'input text'],
  3555. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3556. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3557. '/div',
  3558. ];
  3559. $this->assertHtml($expected, $result);
  3560. //Check that magic types still work for plural/singular vars
  3561. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3562. $result = $this->Form->control('Model.type');
  3563. $expected = [
  3564. 'div' => ['class' => 'input select'],
  3565. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3566. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3567. ['option' => ['value' => 'value']], 'good', '/option',
  3568. ['option' => ['value' => 'other']], 'bad', '/option',
  3569. '/select',
  3570. '/div',
  3571. ];
  3572. $this->assertHtml($expected, $result);
  3573. }
  3574. /**
  3575. * testControlMagicTypeDoesNotOverride method
  3576. *
  3577. * Test that inferred types do not override developer input.
  3578. *
  3579. * @return void
  3580. */
  3581. public function testControlMagicTypeDoesNotOverride()
  3582. {
  3583. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3584. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3585. $expected = [
  3586. 'div' => ['class' => 'input checkbox'],
  3587. ['input' => [
  3588. 'type' => 'hidden',
  3589. 'name' => 'Model[user]',
  3590. 'value' => 0,
  3591. ]],
  3592. 'label' => ['for' => 'model-user'],
  3593. ['input' => [
  3594. 'name' => 'Model[user]',
  3595. 'type' => 'checkbox',
  3596. 'id' => 'model-user',
  3597. 'value' => 1,
  3598. ]],
  3599. 'User',
  3600. '/label',
  3601. '/div',
  3602. ];
  3603. $this->assertHtml($expected, $result);
  3604. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3605. $options = [
  3606. 1 => 'blue',
  3607. 2 => 'red',
  3608. ];
  3609. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3610. $expected = [
  3611. 'div' => ['class' => 'input select'],
  3612. 'label' => ['for' => 'tags-ids'],
  3613. 'Tags',
  3614. '/label',
  3615. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3616. ['div' => ['class' => 'checkbox']],
  3617. ['label' => ['for' => 'tags-ids-1']],
  3618. ['input' => [
  3619. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3620. 'value' => '1', 'name' => 'tags[_ids][]',
  3621. ]],
  3622. 'blue',
  3623. '/label',
  3624. '/div',
  3625. ['div' => ['class' => 'checkbox']],
  3626. ['label' => ['for' => 'tags-ids-2']],
  3627. ['input' => [
  3628. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3629. 'value' => '2', 'name' => 'tags[_ids][]',
  3630. ]],
  3631. 'red',
  3632. '/label',
  3633. '/div',
  3634. '/div',
  3635. ];
  3636. $this->assertHtml($expected, $result);
  3637. }
  3638. /**
  3639. * testControlMagicSelectForTypeNumber method
  3640. *
  3641. * Test that magic control() selects are created for type=number.
  3642. *
  3643. * @return void
  3644. */
  3645. public function testControlMagicSelectForTypeNumber()
  3646. {
  3647. $this->getTableLocator()->get('ValidateUsers', [
  3648. 'className' => __NAMESPACE__ . '\ValidateUsersTable',
  3649. ]);
  3650. $entity = new Entity(['balance' => 1]);
  3651. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3652. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3653. $result = $this->Form->control('balance');
  3654. $expected = [
  3655. 'div' => ['class' => 'input select'],
  3656. 'label' => ['for' => 'balance'],
  3657. 'Balance',
  3658. '/label',
  3659. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3660. ['option' => ['value' => '0']],
  3661. 'nothing',
  3662. '/option',
  3663. ['option' => ['value' => '1', 'selected' => 'selected']],
  3664. 'some',
  3665. '/option',
  3666. ['option' => ['value' => '100']],
  3667. 'a lot',
  3668. '/option',
  3669. '/select',
  3670. '/div',
  3671. ];
  3672. $this->assertHtml($expected, $result);
  3673. }
  3674. /**
  3675. * testInvalidControlTypeOption method
  3676. *
  3677. * Test invalid 'input' type option to control() function.
  3678. *
  3679. * @return void
  3680. */
  3681. public function testInvalidControlTypeOption()
  3682. {
  3683. $this->expectException(\RuntimeException::class);
  3684. $this->expectExceptionMessage('Invalid type \'input\' used for field \'text\'');
  3685. $this->Form->control('text', ['type' => 'input']);
  3686. }
  3687. /**
  3688. * testControlMagicSelectChangeToRadio method
  3689. *
  3690. * Test that magic control() selects can easily be converted into radio types without error.
  3691. *
  3692. * @return void
  3693. */
  3694. public function testControlMagicSelectChangeToRadio()
  3695. {
  3696. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3697. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3698. $this->assertContains('input type="radio"', $result);
  3699. }
  3700. /**
  3701. * testFormControlSubmit method
  3702. *
  3703. * Test correct results for form::control() and type submit.
  3704. *
  3705. * @return void
  3706. */
  3707. public function testFormControlSubmit()
  3708. {
  3709. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3710. $expected = [
  3711. 'div' => ['class' => 'submit'],
  3712. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3713. '/div',
  3714. ];
  3715. $this->assertHtml($expected, $result);
  3716. }
  3717. /**
  3718. * testFormControls method
  3719. *
  3720. * Test correct results from Form::controls().
  3721. *
  3722. * @return void
  3723. */
  3724. public function testFormControlsLegendFieldset()
  3725. {
  3726. $this->Form->create($this->article);
  3727. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3728. $expected = [
  3729. '<fieldset',
  3730. '<legend',
  3731. 'The Legend',
  3732. '/legend',
  3733. '*/fieldset',
  3734. ];
  3735. $this->assertHtml($expected, $result);
  3736. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3737. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3738. $this->assertContains('<fieldset>', $result);
  3739. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3740. $this->assertNotContains('<legend>', $result);
  3741. $this->assertNotContains('<fieldset>', $result);
  3742. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3743. $this->assertNotContains('<legend>', $result);
  3744. $this->assertNotContains('<fieldset>', $result);
  3745. $this->Form->create($this->article);
  3746. $this->View->setRequest($this->View->getRequest()
  3747. ->withParam('prefix', 'admin')
  3748. ->withParam('action', 'admin_edit')
  3749. ->withParam('controller', 'articles'));
  3750. $result = $this->Form->allControls();
  3751. $expected = [
  3752. '<fieldset',
  3753. '<legend',
  3754. 'New Article',
  3755. '/legend',
  3756. '*/fieldset',
  3757. ];
  3758. $this->assertHtml($expected, $result);
  3759. $this->Form->create($this->article);
  3760. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3761. $expected = [
  3762. '<fieldset',
  3763. '<legend',
  3764. 'The Legend',
  3765. '/legend',
  3766. '*/fieldset',
  3767. ];
  3768. $this->assertHtml($expected, $result);
  3769. $this->Form->create($this->article);
  3770. $result = $this->Form->allControls([], [
  3771. 'fieldset' => [
  3772. 'class' => 'some-class some-other-class',
  3773. 'disabled' => true,
  3774. 'data-param' => 'a-param',
  3775. ],
  3776. 'legend' => 'The Legend',
  3777. ]);
  3778. $expected = [
  3779. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3780. '<legend',
  3781. 'The Legend',
  3782. '/legend',
  3783. '*/fieldset',
  3784. ];
  3785. $this->assertHtml($expected, $result);
  3786. }
  3787. /**
  3788. * testFormControls method
  3789. *
  3790. * Test the controls() method.
  3791. *
  3792. * @return void
  3793. */
  3794. public function testFormControls()
  3795. {
  3796. $this->Form->create($this->article);
  3797. $result = $this->Form->allControls();
  3798. $expected = [
  3799. '<fieldset',
  3800. '<legend', 'New Article', '/legend',
  3801. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3802. ['div' => ['class' => 'input select required']],
  3803. '*/div',
  3804. ['div' => ['class' => 'input text required']],
  3805. '*/div',
  3806. ['div' => ['class' => 'input text']],
  3807. '*/div',
  3808. ['div' => ['class' => 'input text']],
  3809. '*/div',
  3810. '/fieldset',
  3811. ];
  3812. $this->assertHtml($expected, $result);
  3813. $result = $this->Form->allControls([
  3814. 'published' => ['type' => 'boolean'],
  3815. ]);
  3816. $expected = [
  3817. '<fieldset',
  3818. '<legend', 'New Article', '/legend',
  3819. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3820. ['div' => ['class' => 'input select required']],
  3821. '*/div',
  3822. ['div' => ['class' => 'input text required']],
  3823. '*/div',
  3824. ['div' => ['class' => 'input text']],
  3825. '*/div',
  3826. ['div' => ['class' => 'input boolean']],
  3827. '*/div',
  3828. '/fieldset',
  3829. ];
  3830. $this->assertHtml($expected, $result);
  3831. $this->Form->create($this->article);
  3832. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3833. $expected = [
  3834. 'fieldset' => [],
  3835. 'legend' => [],
  3836. 'Hello',
  3837. '/legend',
  3838. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3839. ['div' => ['class' => 'input select required']],
  3840. '*/div',
  3841. ['div' => ['class' => 'input text required']],
  3842. '*/div',
  3843. ['div' => ['class' => 'input text']],
  3844. '*/div',
  3845. ['div' => ['class' => 'input text']],
  3846. '*/div',
  3847. '/fieldset',
  3848. ];
  3849. $this->assertHtml($expected, $result);
  3850. $this->Form->create(false);
  3851. $expected = [
  3852. 'fieldset' => [],
  3853. ['div' => ['class' => 'input text']],
  3854. 'label' => ['for' => 'foo'],
  3855. 'Foo',
  3856. '/label',
  3857. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3858. '*/div',
  3859. '/fieldset',
  3860. ];
  3861. $result = $this->Form->allControls(
  3862. ['foo' => ['type' => 'text']],
  3863. ['legend' => false]
  3864. );
  3865. $this->assertHtml($expected, $result);
  3866. }
  3867. /**
  3868. * testFormControlsBlacklist method
  3869. *
  3870. * @return void
  3871. */
  3872. public function testFormControlsBlacklist()
  3873. {
  3874. $this->Form->create($this->article);
  3875. $result = $this->Form->allControls([
  3876. 'id' => false,
  3877. ]);
  3878. $expected = [
  3879. '<fieldset',
  3880. '<legend', 'New Article', '/legend',
  3881. ['div' => ['class' => 'input select required']],
  3882. '*/div',
  3883. ['div' => ['class' => 'input text required']],
  3884. '*/div',
  3885. ['div' => ['class' => 'input text']],
  3886. '*/div',
  3887. ['div' => ['class' => 'input text']],
  3888. '*/div',
  3889. '/fieldset',
  3890. ];
  3891. $this->assertHtml($expected, $result);
  3892. $this->Form->create($this->article);
  3893. $result = $this->Form->allControls([
  3894. 'id' => [],
  3895. ]);
  3896. $expected = [
  3897. '<fieldset',
  3898. '<legend', 'New Article', '/legend',
  3899. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3900. ['div' => ['class' => 'input select required']],
  3901. '*/div',
  3902. ['div' => ['class' => 'input text required']],
  3903. '*/div',
  3904. ['div' => ['class' => 'input text']],
  3905. '*/div',
  3906. ['div' => ['class' => 'input text']],
  3907. '*/div',
  3908. '/fieldset',
  3909. ];
  3910. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3911. }
  3912. /**
  3913. * testSelectAsCheckbox method
  3914. *
  3915. * Test multi-select widget with checkbox formatting.
  3916. *
  3917. * @return void
  3918. */
  3919. public function testSelectAsCheckbox()
  3920. {
  3921. $result = $this->Form->select(
  3922. 'Model.multi_field',
  3923. ['first', 'second', 'third'],
  3924. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3925. );
  3926. $expected = [
  3927. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3928. ['div' => ['class' => 'checkbox']],
  3929. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3930. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3931. 'first',
  3932. '/label',
  3933. '/div',
  3934. ['div' => ['class' => 'checkbox']],
  3935. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3936. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3937. 'second',
  3938. '/label',
  3939. '/div',
  3940. ['div' => ['class' => 'checkbox']],
  3941. ['label' => ['for' => 'model-multi-field-2']],
  3942. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3943. 'third',
  3944. '/label',
  3945. '/div',
  3946. ];
  3947. $this->assertHtml($expected, $result);
  3948. $result = $this->Form->select(
  3949. 'Model.multi_field',
  3950. ['1/2' => 'half'],
  3951. ['multiple' => 'checkbox']
  3952. );
  3953. $expected = [
  3954. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3955. ['div' => ['class' => 'checkbox']],
  3956. ['label' => ['for' => 'model-multi-field-1-2']],
  3957. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3958. 'half',
  3959. '/label',
  3960. '/div',
  3961. ];
  3962. $this->assertHtml($expected, $result);
  3963. }
  3964. /**
  3965. * testLabel method
  3966. *
  3967. * Test label generation.
  3968. *
  3969. * @return void
  3970. */
  3971. public function testLabel()
  3972. {
  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.name');
  3977. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3978. $this->assertHtml($expected, $result);
  3979. $result = $this->Form->label('Person.first_name');
  3980. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3981. $this->assertHtml($expected, $result);
  3982. $result = $this->Form->label('Person.first_name', 'Your first name');
  3983. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3984. $this->assertHtml($expected, $result);
  3985. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3986. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3987. $this->assertHtml($expected, $result);
  3988. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3989. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3990. $this->assertHtml($expected, $result);
  3991. $result = $this->Form->label('Person.first_name', '');
  3992. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3993. $this->assertHtml($expected, $result);
  3994. $result = $this->Form->label('Person.2.name', '');
  3995. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3996. $this->assertHtml($expected, $result);
  3997. }
  3998. /**
  3999. * testLabelContainControl method
  4000. *
  4001. * Test that label() can accept an input with the correct template vars.
  4002. *
  4003. * @return void
  4004. */
  4005. public function testLabelContainControl()
  4006. {
  4007. $this->Form->setTemplates([
  4008. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4009. ]);
  4010. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  4011. 'input' => '<input type="checkbox" name="accept_tos"/>',
  4012. ]);
  4013. $expected = [
  4014. 'label' => ['for' => 'person-accept-terms'],
  4015. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  4016. 'Accept',
  4017. '/label',
  4018. ];
  4019. $this->assertHtml($expected, $result);
  4020. }
  4021. /**
  4022. * testTextbox method
  4023. *
  4024. * Test textbox element generation.
  4025. *
  4026. * @return void
  4027. */
  4028. public function testTextbox()
  4029. {
  4030. $result = $this->Form->text('Model.field');
  4031. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  4032. $this->assertHtml($expected, $result);
  4033. $result = $this->Form->text('Model.field', ['type' => 'password']);
  4034. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  4035. $this->assertHtml($expected, $result);
  4036. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  4037. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  4038. $this->assertHtml($expected, $result);
  4039. }
  4040. /**
  4041. * testTextBoxDataAndError method
  4042. *
  4043. * Test that text() hooks up with request data and error fields.
  4044. *
  4045. * @return void
  4046. */
  4047. public function testTextBoxDataAndError()
  4048. {
  4049. $this->article['errors'] = [
  4050. 'Contact' => ['text' => 'wrong'],
  4051. ];
  4052. $this->View->setRequest($this->View->getRequest()
  4053. ->withData('Model.text', 'test <strong>HTML</strong> values')
  4054. ->withData('Contact.text', 'test'));
  4055. $this->Form->create($this->article);
  4056. $result = $this->Form->text('Model.text');
  4057. $expected = [
  4058. 'input' => [
  4059. 'type' => 'text',
  4060. 'name' => 'Model[text]',
  4061. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  4062. ],
  4063. ];
  4064. $this->assertHtml($expected, $result);
  4065. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  4066. $expected = [
  4067. 'input' => [
  4068. 'type' => 'text',
  4069. 'name' => 'Contact[text]',
  4070. 'value' => 'test',
  4071. 'id' => 'theID',
  4072. 'class' => 'form-error',
  4073. ],
  4074. ];
  4075. $this->assertHtml($expected, $result);
  4076. }
  4077. /**
  4078. * testDefaultValue method
  4079. *
  4080. * Test default value setting.
  4081. *
  4082. * @return void
  4083. */
  4084. public function testTextDefaultValue()
  4085. {
  4086. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  4087. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4088. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  4089. $this->assertHtml($expected, $result);
  4090. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4091. $this->Form->create();
  4092. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4093. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4094. $this->assertHtml($expected, $result);
  4095. $Articles = $this->getTableLocator()->get('Articles');
  4096. $title = $Articles->getSchema()->getColumn('title');
  4097. $Articles->getSchema()->addColumn(
  4098. 'title',
  4099. ['default' => 'default title'] + $title
  4100. );
  4101. $entity = $Articles->newEntity();
  4102. $this->Form->create($entity);
  4103. // Get default value from schema
  4104. $result = $this->Form->text('title');
  4105. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4106. $this->assertHtml($expected, $result);
  4107. // Don't get value from schema
  4108. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4109. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4110. $this->assertHtml($expected, $result);
  4111. // Custom default value overrides default value from schema
  4112. $result = $this->Form->text('title', ['default' => 'override default']);
  4113. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4114. $this->assertHtml($expected, $result);
  4115. // Default value from schema is used only for new entities.
  4116. $entity->isNew(false);
  4117. $result = $this->Form->text('title');
  4118. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4119. $this->assertHtml($expected, $result);
  4120. }
  4121. /**
  4122. * testError method
  4123. *
  4124. * Test field error generation.
  4125. *
  4126. * @return void
  4127. */
  4128. public function testError()
  4129. {
  4130. $this->article['errors'] = [
  4131. 'Article' => ['field' => 'email'],
  4132. ];
  4133. $this->Form->create($this->article);
  4134. $result = $this->Form->error('Article.field');
  4135. $expected = [
  4136. ['div' => ['class' => 'error-message']],
  4137. 'email',
  4138. '/div',
  4139. ];
  4140. $this->assertHtml($expected, $result);
  4141. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4142. $expected = [
  4143. ['div' => ['class' => 'error-message']],
  4144. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4145. '/div',
  4146. ];
  4147. $this->assertHtml($expected, $result);
  4148. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4149. $expected = [
  4150. ['div' => ['class' => 'error-message']],
  4151. '<strong', 'Badness!', '/strong',
  4152. '/div',
  4153. ];
  4154. $this->assertHtml($expected, $result);
  4155. }
  4156. /**
  4157. * testErrorRuleName method
  4158. *
  4159. * Test error translation can use rule names for translating.
  4160. *
  4161. * @return void
  4162. */
  4163. public function testErrorRuleName()
  4164. {
  4165. $this->article['errors'] = [
  4166. 'Article' => [
  4167. 'field' => ['email' => 'Your email was not good'],
  4168. ],
  4169. ];
  4170. $this->Form->create($this->article);
  4171. $result = $this->Form->error('Article.field');
  4172. $expected = [
  4173. ['div' => ['class' => 'error-message']],
  4174. 'Your email was not good',
  4175. '/div',
  4176. ];
  4177. $this->assertHtml($expected, $result);
  4178. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4179. $expected = [
  4180. ['div' => ['class' => 'error-message']],
  4181. 'Email in use',
  4182. '/div',
  4183. ];
  4184. $this->assertHtml($expected, $result);
  4185. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4186. $expected = [
  4187. ['div' => ['class' => 'error-message']],
  4188. 'Email in use',
  4189. '/div',
  4190. ];
  4191. $this->assertHtml($expected, $result);
  4192. $result = $this->Form->error('Article.field', [
  4193. 'email' => 'Key is preferred',
  4194. 'Your email was not good' => 'Email in use',
  4195. ]);
  4196. $expected = [
  4197. ['div' => ['class' => 'error-message']],
  4198. 'Key is preferred',
  4199. '/div',
  4200. ];
  4201. $this->assertHtml($expected, $result);
  4202. }
  4203. /**
  4204. * testErrorMessages method
  4205. *
  4206. * Test error with nested lists.
  4207. *
  4208. * @return void
  4209. */
  4210. public function testErrorMessages()
  4211. {
  4212. $this->article['errors'] = [
  4213. 'Article' => ['field' => 'email'],
  4214. ];
  4215. $this->Form->create($this->article);
  4216. $result = $this->Form->error('Article.field', [
  4217. 'email' => 'No good!',
  4218. ]);
  4219. $expected = [
  4220. 'div' => ['class' => 'error-message'],
  4221. 'No good!',
  4222. '/div',
  4223. ];
  4224. $this->assertHtml($expected, $result);
  4225. }
  4226. /**
  4227. * testErrorMultipleMessages method
  4228. *
  4229. * Test error() with multiple messages.
  4230. *
  4231. * @return void
  4232. */
  4233. public function testErrorMultipleMessages()
  4234. {
  4235. $this->article['errors'] = [
  4236. 'field' => ['notBlank', 'email', 'Something else'],
  4237. ];
  4238. $this->Form->create($this->article);
  4239. $result = $this->Form->error('field', [
  4240. 'notBlank' => 'Cannot be empty',
  4241. 'email' => 'No good!',
  4242. ]);
  4243. $expected = [
  4244. 'div' => ['class' => 'error-message'],
  4245. 'ul' => [],
  4246. '<li', 'Cannot be empty', '/li',
  4247. '<li', 'No good!', '/li',
  4248. '<li', 'Something else', '/li',
  4249. '/ul',
  4250. '/div',
  4251. ];
  4252. $this->assertHtml($expected, $result);
  4253. }
  4254. /**
  4255. * testPassword method
  4256. *
  4257. * Test password element generation.
  4258. *
  4259. * @return void
  4260. */
  4261. public function testPassword()
  4262. {
  4263. $this->article['errors'] = [
  4264. 'Contact' => [
  4265. 'passwd' => 1,
  4266. ],
  4267. ];
  4268. $this->Form->create($this->article);
  4269. $result = $this->Form->password('Contact.field');
  4270. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4271. $this->assertHtml($expected, $result);
  4272. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4273. $this->Form->create($this->article);
  4274. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4275. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4276. $this->assertHtml($expected, $result);
  4277. }
  4278. /**
  4279. * testRadio method
  4280. *
  4281. * Test radio element set generation.
  4282. *
  4283. * @return void
  4284. */
  4285. public function testRadio()
  4286. {
  4287. $result = $this->Form->radio('Model.field', ['option A']);
  4288. $expected = [
  4289. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4290. 'label' => ['for' => 'model-field-0'],
  4291. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4292. 'option A',
  4293. '/label',
  4294. ];
  4295. $this->assertHtml($expected, $result);
  4296. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4297. $this->assertHtml($expected, $result);
  4298. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4299. $expected = [
  4300. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4301. ['label' => ['for' => 'model-field-0']],
  4302. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4303. 'option A',
  4304. '/label',
  4305. ['label' => ['for' => 'model-field-1']],
  4306. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4307. 'option B',
  4308. '/label',
  4309. ];
  4310. $this->assertHtml($expected, $result);
  4311. $result = $this->Form->radio(
  4312. 'Employee.gender',
  4313. ['male' => 'Male', 'female' => 'Female'],
  4314. ['form' => 'my-form']
  4315. );
  4316. $expected = [
  4317. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4318. ['label' => ['for' => 'employee-gender-male']],
  4319. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4320. 'Male',
  4321. '/label',
  4322. ['label' => ['for' => 'employee-gender-female']],
  4323. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4324. 'Female',
  4325. '/label',
  4326. ];
  4327. $this->assertHtml($expected, $result);
  4328. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4329. $expected = [
  4330. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4331. ['label' => ['for' => 'model-custom-0']],
  4332. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4333. 'option A',
  4334. '/label',
  4335. ['label' => ['for' => 'model-custom-1']],
  4336. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4337. 'option B',
  4338. '/label',
  4339. ];
  4340. $this->assertHtml($expected, $result);
  4341. $result = $this->Form->radio(
  4342. 'Employee.gender',
  4343. [
  4344. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4345. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4346. ]
  4347. );
  4348. $expected = [
  4349. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4350. ['label' => ['for' => 'employee-gender-male']],
  4351. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4352. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4353. 'Male',
  4354. '/label',
  4355. ['label' => ['for' => 'employee-gender-female']],
  4356. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4357. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4358. 'Female',
  4359. '/label',
  4360. ];
  4361. $this->assertHtml($expected, $result);
  4362. }
  4363. /**
  4364. * Test radio with complex options and empty disabled data.
  4365. *
  4366. * @return void
  4367. */
  4368. public function testRadioComplexDisabled()
  4369. {
  4370. $options = [
  4371. ['value' => 'r', 'text' => 'red'],
  4372. ['value' => 'b', 'text' => 'blue'],
  4373. ];
  4374. $attrs = ['disabled' => []];
  4375. $result = $this->Form->radio('Model.field', $options, $attrs);
  4376. $expected = [
  4377. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4378. ['label' => ['for' => 'model-field-r']],
  4379. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4380. 'red',
  4381. '/label',
  4382. ['label' => ['for' => 'model-field-b']],
  4383. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4384. 'blue',
  4385. '/label',
  4386. ];
  4387. $this->assertHtml($expected, $result);
  4388. $attrs = ['disabled' => ['r']];
  4389. $result = $this->Form->radio('Model.field', $options, $attrs);
  4390. $this->assertContains('disabled="disabled"', $result);
  4391. }
  4392. /**
  4393. * testRadioDefaultValue method
  4394. *
  4395. * Test default value setting on radio() method.
  4396. *
  4397. * @return void
  4398. */
  4399. public function testRadioDefaultValue()
  4400. {
  4401. $Articles = $this->getTableLocator()->get('Articles');
  4402. $title = $Articles->getSchema()->getColumn('title');
  4403. $Articles->getSchema()->addColumn(
  4404. 'title',
  4405. ['default' => '1'] + $title
  4406. );
  4407. $this->Form->create($Articles->newEntity());
  4408. $result = $this->Form->radio('title', ['option A', 'option B']);
  4409. $expected = [
  4410. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4411. ['label' => ['for' => 'title-0']],
  4412. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4413. 'option A',
  4414. '/label',
  4415. ['label' => ['for' => 'title-1']],
  4416. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4417. 'option B',
  4418. '/label',
  4419. ];
  4420. $this->assertHtml($expected, $result);
  4421. }
  4422. /**
  4423. * Test setting a hiddenField value on radio buttons.
  4424. *
  4425. * @return void
  4426. */
  4427. public function testRadioHiddenFieldValue()
  4428. {
  4429. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4430. $expected = [
  4431. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4432. 'label' => ['for' => 'title-0'],
  4433. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4434. 'option A',
  4435. '/label',
  4436. ];
  4437. $this->assertHtml($expected, $result);
  4438. }
  4439. /**
  4440. * testControlRadio method
  4441. *
  4442. * Test that input works with radio types.
  4443. *
  4444. * @return void
  4445. */
  4446. public function testControlRadio()
  4447. {
  4448. $result = $this->Form->control('test', [
  4449. 'type' => 'radio',
  4450. 'options' => ['A', 'B'],
  4451. ]);
  4452. $expected = [
  4453. ['div' => ['class' => 'input radio']],
  4454. '<label',
  4455. 'Test',
  4456. '/label',
  4457. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4458. ['label' => ['for' => 'test-0']],
  4459. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4460. 'A',
  4461. '/label',
  4462. ['label' => ['for' => 'test-1']],
  4463. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4464. 'B',
  4465. '/label',
  4466. '/div',
  4467. ];
  4468. $this->assertHtml($expected, $result);
  4469. $result = $this->Form->control('test', [
  4470. 'type' => 'radio',
  4471. 'options' => ['A', 'B'],
  4472. 'value' => '0',
  4473. ]);
  4474. $expected = [
  4475. ['div' => ['class' => 'input radio']],
  4476. '<label',
  4477. 'Test',
  4478. '/label',
  4479. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4480. ['label' => ['for' => 'test-0']],
  4481. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4482. 'A',
  4483. '/label',
  4484. ['label' => ['for' => 'test-1']],
  4485. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4486. 'B',
  4487. '/label',
  4488. '/div',
  4489. ];
  4490. $this->assertHtml($expected, $result);
  4491. $result = $this->Form->control('test', [
  4492. 'type' => 'radio',
  4493. 'options' => ['A', 'B'],
  4494. 'label' => false,
  4495. ]);
  4496. $expected = [
  4497. ['div' => ['class' => 'input radio']],
  4498. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4499. ['label' => ['for' => 'test-0']],
  4500. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4501. 'A',
  4502. '/label',
  4503. ['label' => ['for' => 'test-1']],
  4504. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4505. 'B',
  4506. '/label',
  4507. '/div',
  4508. ];
  4509. $this->assertHtml($expected, $result);
  4510. }
  4511. /**
  4512. * testRadioNoLabel method
  4513. *
  4514. * Test that radio() works with label = false.
  4515. *
  4516. * @return void
  4517. */
  4518. public function testRadioNoLabel()
  4519. {
  4520. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4521. $expected = [
  4522. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4523. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4524. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4525. ];
  4526. $this->assertHtml($expected, $result);
  4527. }
  4528. /**
  4529. * testRadioControlInsideLabel method
  4530. *
  4531. * Test generating radio input inside label ala twitter bootstrap.
  4532. *
  4533. * @return void
  4534. */
  4535. public function testRadioControlInsideLabel()
  4536. {
  4537. $this->Form->setTemplates([
  4538. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4539. 'radioWrapper' => '{{label}}',
  4540. ]);
  4541. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4542. //@codingStandardsIgnoreStart
  4543. $expected = [
  4544. ['input' => [
  4545. 'type' => 'hidden',
  4546. 'name' => 'Model[field]',
  4547. 'value' => ''
  4548. ]],
  4549. ['label' => ['for' => 'model-field-0']],
  4550. ['input' => [
  4551. 'type' => 'radio',
  4552. 'name' => 'Model[field]',
  4553. 'value' => '0',
  4554. 'id' => 'model-field-0'
  4555. ]],
  4556. 'option A',
  4557. '/label',
  4558. ['label' => ['for' => 'model-field-1']],
  4559. ['input' => [
  4560. 'type' => 'radio',
  4561. 'name' => 'Model[field]',
  4562. 'value' => '1',
  4563. 'id' => 'model-field-1'
  4564. ]],
  4565. 'option B',
  4566. '/label',
  4567. ];
  4568. //@codingStandardsIgnoreEnd
  4569. $this->assertHtml($expected, $result);
  4570. }
  4571. /**
  4572. * testRadioHiddenControlDisabling method
  4573. *
  4574. * Test disabling the hidden input for radio buttons.
  4575. *
  4576. * @return void
  4577. */
  4578. public function testRadioHiddenControlDisabling()
  4579. {
  4580. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4581. $expected = [
  4582. 'label' => ['for' => 'model-1-field-0'],
  4583. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4584. 'option A',
  4585. '/label',
  4586. ];
  4587. $this->assertHtml($expected, $result);
  4588. }
  4589. /**
  4590. * testRadioOutOfRange method
  4591. *
  4592. * Test radio element set generation.
  4593. *
  4594. * @return void
  4595. */
  4596. public function testRadioOutOfRange()
  4597. {
  4598. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4599. $expected = [
  4600. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4601. 'label' => ['for' => 'model-field-v'],
  4602. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4603. 'value',
  4604. '/label',
  4605. ];
  4606. $this->assertHtml($expected, $result);
  4607. }
  4608. /**
  4609. * testSelect method
  4610. *
  4611. * Test select element generation.
  4612. *
  4613. * @return void
  4614. */
  4615. public function testSelect()
  4616. {
  4617. $result = $this->Form->select('Model.field', []);
  4618. $expected = [
  4619. 'select' => ['name' => 'Model[field]'],
  4620. '/select',
  4621. ];
  4622. $this->assertHtml($expected, $result);
  4623. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4624. $this->Form->create();
  4625. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4626. $expected = [
  4627. 'select' => ['name' => 'Model[field]'],
  4628. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4629. 'good',
  4630. '/option',
  4631. ['option' => ['value' => 'other']],
  4632. 'bad',
  4633. '/option',
  4634. '/select',
  4635. ];
  4636. $this->assertHtml($expected, $result);
  4637. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4638. $this->assertHtml($expected, $result);
  4639. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4640. $this->Form->create();
  4641. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4642. $expected = [
  4643. 'select' => ['name' => 'Model[field]'],
  4644. ['option' => ['value' => 'value']],
  4645. 'good',
  4646. '/option',
  4647. ['option' => ['value' => 'other']],
  4648. 'bad',
  4649. '/option',
  4650. '/select',
  4651. ];
  4652. $this->assertHtml($expected, $result);
  4653. $options = [
  4654. ['value' => 'first', 'text' => 'First'],
  4655. ['value' => 'first', 'text' => 'Another First'],
  4656. ];
  4657. $result = $this->Form->select(
  4658. 'Model.field',
  4659. $options,
  4660. ['escape' => false, 'empty' => false]
  4661. );
  4662. $expected = [
  4663. 'select' => ['name' => 'Model[field]'],
  4664. ['option' => ['value' => 'first']],
  4665. 'First',
  4666. '/option',
  4667. ['option' => ['value' => 'first']],
  4668. 'Another First',
  4669. '/option',
  4670. '/select',
  4671. ];
  4672. $this->assertHtml($expected, $result);
  4673. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4674. $this->Form->create();
  4675. $result = $this->Form->select(
  4676. 'Model.contact_id',
  4677. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4678. ['escape' => false, 'empty' => 'pick something']
  4679. );
  4680. $expected = [
  4681. 'select' => ['name' => 'Model[contact_id]'],
  4682. ['option' => ['value' => '']], 'pick something', '/option',
  4683. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4684. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4685. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4686. '/select',
  4687. ];
  4688. $this->assertHtml($expected, $result);
  4689. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4690. $this->Form->create();
  4691. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4692. $expected = [
  4693. 'select' => ['name' => 'Model[field]'],
  4694. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4695. ['option' => ['value' => '1']], 'Yes', '/option',
  4696. '/select',
  4697. ];
  4698. $this->assertHtml($expected, $result);
  4699. }
  4700. /**
  4701. * testSelectEscapeHtml method
  4702. *
  4703. * Test that select() escapes HTML.
  4704. *
  4705. * @return void
  4706. */
  4707. public function testSelectEscapeHtml()
  4708. {
  4709. $result = $this->Form->select(
  4710. 'Model.field',
  4711. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4712. ['empty' => false]
  4713. );
  4714. $expected = [
  4715. 'select' => ['name' => 'Model[field]'],
  4716. ['option' => ['value' => 'first']],
  4717. 'first &quot;html&quot; &lt;chars&gt;',
  4718. '/option',
  4719. ['option' => ['value' => 'second']],
  4720. 'value',
  4721. '/option',
  4722. '/select',
  4723. ];
  4724. $this->assertHtml($expected, $result);
  4725. $result = $this->Form->select(
  4726. 'Model.field',
  4727. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4728. ['escape' => false, 'empty' => false]
  4729. );
  4730. $expected = [
  4731. 'select' => ['name' => 'Model[field]'],
  4732. ['option' => ['value' => 'first']],
  4733. 'first "html" <chars>',
  4734. '/option',
  4735. ['option' => ['value' => 'second']],
  4736. 'value',
  4737. '/option',
  4738. '/select',
  4739. ];
  4740. $this->assertHtml($expected, $result);
  4741. }
  4742. /**
  4743. * testSelectRequired method
  4744. *
  4745. * Test select() with required and disabled attributes.
  4746. *
  4747. * @return void
  4748. */
  4749. public function testSelectRequired()
  4750. {
  4751. $this->article['required'] = [
  4752. 'user_id' => true,
  4753. ];
  4754. $this->Form->create($this->article);
  4755. $result = $this->Form->select('user_id', ['option A']);
  4756. $expected = [
  4757. 'select' => [
  4758. 'name' => 'user_id',
  4759. 'required' => 'required',
  4760. ],
  4761. ['option' => ['value' => '0']], 'option A', '/option',
  4762. '/select',
  4763. ];
  4764. $this->assertHtml($expected, $result);
  4765. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4766. $expected = [
  4767. 'select' => [
  4768. 'name' => 'user_id',
  4769. 'disabled' => 'disabled',
  4770. ],
  4771. ['option' => ['value' => '0']], 'option A', '/option',
  4772. '/select',
  4773. ];
  4774. $this->assertHtml($expected, $result);
  4775. }
  4776. /**
  4777. * testNestedSelect method
  4778. *
  4779. * Test select element generation with optgroups.
  4780. *
  4781. * @return void
  4782. */
  4783. public function testNestedSelect()
  4784. {
  4785. $result = $this->Form->select(
  4786. 'Model.field',
  4787. [1 => 'One', 2 => 'Two', 'Three' => [
  4788. 3 => 'Three', 4 => 'Four', 5 => 'Five',
  4789. ]],
  4790. ['empty' => false]
  4791. );
  4792. $expected = [
  4793. 'select' => ['name' => 'Model[field]'],
  4794. ['option' => ['value' => 1]],
  4795. 'One',
  4796. '/option',
  4797. ['option' => ['value' => 2]],
  4798. 'Two',
  4799. '/option',
  4800. ['optgroup' => ['label' => 'Three']],
  4801. ['option' => ['value' => 3]],
  4802. 'Three',
  4803. '/option',
  4804. ['option' => ['value' => 4]],
  4805. 'Four',
  4806. '/option',
  4807. ['option' => ['value' => 5]],
  4808. 'Five',
  4809. '/option',
  4810. '/optgroup',
  4811. '/select',
  4812. ];
  4813. $this->assertHtml($expected, $result);
  4814. }
  4815. /**
  4816. * testSelectMultiple method
  4817. *
  4818. * Test generation of multiple select elements.
  4819. *
  4820. * @return void
  4821. */
  4822. public function testSelectMultiple()
  4823. {
  4824. $options = ['first', 'second', 'third'];
  4825. $result = $this->Form->select(
  4826. 'Model.multi_field',
  4827. $options,
  4828. ['form' => 'my-form', 'multiple' => true]
  4829. );
  4830. $expected = [
  4831. 'input' => [
  4832. 'type' => 'hidden',
  4833. 'name' => 'Model[multi_field]',
  4834. 'value' => '',
  4835. 'form' => 'my-form',
  4836. ],
  4837. 'select' => [
  4838. 'name' => 'Model[multi_field][]',
  4839. 'multiple' => 'multiple',
  4840. 'form' => 'my-form',
  4841. ],
  4842. ['option' => ['value' => '0']],
  4843. 'first',
  4844. '/option',
  4845. ['option' => ['value' => '1']],
  4846. 'second',
  4847. '/option',
  4848. ['option' => ['value' => '2']],
  4849. 'third',
  4850. '/option',
  4851. '/select',
  4852. ];
  4853. $this->assertHtml($expected, $result);
  4854. $result = $this->Form->select(
  4855. 'Model.multi_field',
  4856. $options,
  4857. ['multiple' => 'multiple', 'form' => 'my-form']
  4858. );
  4859. $this->assertHtml($expected, $result);
  4860. $result = $this->Form->select(
  4861. 'Model.multi_field',
  4862. $options,
  4863. ['form' => 'my-form', 'multiple' => false]
  4864. );
  4865. $this->assertNotContains('multiple', $result);
  4866. }
  4867. /**
  4868. * testCheckboxZeroValue method
  4869. *
  4870. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4871. *
  4872. * @return void
  4873. */
  4874. public function testCheckboxZeroValue()
  4875. {
  4876. $result = $this->Form->control('User.get_spam', [
  4877. 'type' => 'checkbox',
  4878. 'value' => '0',
  4879. 'hiddenField' => '1',
  4880. ]);
  4881. $expected = [
  4882. 'div' => ['class' => 'input checkbox'],
  4883. 'label' => ['for' => 'user-get-spam'],
  4884. ['input' => [
  4885. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4886. 'value' => '1',
  4887. ]],
  4888. ['input' => [
  4889. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4890. 'value' => '0', 'id' => 'user-get-spam',
  4891. ]],
  4892. 'Get Spam',
  4893. '/label',
  4894. '/div',
  4895. ];
  4896. $this->assertHtml($expected, $result);
  4897. }
  4898. /**
  4899. * testHabtmSelectBox method
  4900. *
  4901. * Test generation of habtm select boxes.
  4902. *
  4903. * @return void
  4904. */
  4905. public function testHabtmSelectBox()
  4906. {
  4907. $this->loadFixtures('Articles');
  4908. $options = [
  4909. 1 => 'blue',
  4910. 2 => 'red',
  4911. 3 => 'green',
  4912. ];
  4913. $tags = [
  4914. new Entity(['id' => 1, 'name' => 'blue']),
  4915. new Entity(['id' => 3, 'name' => 'green']),
  4916. ];
  4917. $article = new Article(['tags' => $tags]);
  4918. $this->Form->create($article);
  4919. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4920. $expected = [
  4921. 'div' => ['class' => 'input select'],
  4922. 'label' => ['for' => 'tags-ids'],
  4923. 'Tags',
  4924. '/label',
  4925. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4926. 'select' => [
  4927. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4928. 'multiple' => 'multiple',
  4929. ],
  4930. ['option' => ['value' => '1', 'selected' => 'selected']],
  4931. 'blue',
  4932. '/option',
  4933. ['option' => ['value' => '2']],
  4934. 'red',
  4935. '/option',
  4936. ['option' => ['value' => '3', 'selected' => 'selected']],
  4937. 'green',
  4938. '/option',
  4939. '/select',
  4940. '/div',
  4941. ];
  4942. $this->assertHtml($expected, $result);
  4943. // make sure only 50 is selected, and not 50f5c0cf
  4944. $options = [
  4945. '1' => 'blue',
  4946. '50f5c0cf' => 'red',
  4947. '50' => 'green',
  4948. ];
  4949. $tags = [
  4950. new Entity(['id' => 1, 'name' => 'blue']),
  4951. new Entity(['id' => 50, 'name' => 'green']),
  4952. ];
  4953. $article = new Article(['tags' => $tags]);
  4954. $this->Form->create($article);
  4955. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4956. $expected = [
  4957. 'div' => ['class' => 'input select'],
  4958. 'label' => ['for' => 'tags-ids'],
  4959. 'Tags',
  4960. '/label',
  4961. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4962. 'select' => [
  4963. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4964. 'multiple' => 'multiple',
  4965. ],
  4966. ['option' => ['value' => '1', 'selected' => 'selected']],
  4967. 'blue',
  4968. '/option',
  4969. ['option' => ['value' => '50f5c0cf']],
  4970. 'red',
  4971. '/option',
  4972. ['option' => ['value' => '50', 'selected' => 'selected']],
  4973. 'green',
  4974. '/option',
  4975. '/select',
  4976. '/div',
  4977. ];
  4978. $this->assertHtml($expected, $result);
  4979. $spacecraft = [
  4980. 1 => 'Orion',
  4981. 2 => 'Helios',
  4982. ];
  4983. $this->View->viewVars['spacecraft'] = $spacecraft;
  4984. $this->Form->create();
  4985. $result = $this->Form->control('spacecraft._ids');
  4986. $expected = [
  4987. 'div' => ['class' => 'input select'],
  4988. 'label' => ['for' => 'spacecraft-ids'],
  4989. 'Spacecraft',
  4990. '/label',
  4991. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4992. 'select' => [
  4993. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4994. 'multiple' => 'multiple',
  4995. ],
  4996. ['option' => ['value' => '1']],
  4997. 'Orion',
  4998. '/option',
  4999. ['option' => ['value' => '2']],
  5000. 'Helios',
  5001. '/option',
  5002. '/select',
  5003. '/div',
  5004. ];
  5005. $this->assertHtml($expected, $result);
  5006. }
  5007. /**
  5008. * testErrorsForBelongsToManySelect method
  5009. *
  5010. * Tests that errors for belongsToMany select fields are being
  5011. * picked up properly.
  5012. *
  5013. * @return void
  5014. */
  5015. public function testErrorsForBelongsToManySelect()
  5016. {
  5017. $spacecraft = [
  5018. 1 => 'Orion',
  5019. 2 => 'Helios',
  5020. ];
  5021. $this->View->viewVars['spacecraft'] = $spacecraft;
  5022. $article = new Article();
  5023. $article->setError('spacecraft', ['Invalid']);
  5024. $this->Form->create($article);
  5025. $result = $this->Form->control('spacecraft._ids');
  5026. $expected = [
  5027. ['div' => ['class' => 'input select error']],
  5028. 'label' => ['for' => 'spacecraft-ids'],
  5029. 'Spacecraft',
  5030. '/label',
  5031. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  5032. 'select' => [
  5033. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  5034. 'multiple' => 'multiple',
  5035. ],
  5036. ['option' => ['value' => '1']],
  5037. 'Orion',
  5038. '/option',
  5039. ['option' => ['value' => '2']],
  5040. 'Helios',
  5041. '/option',
  5042. '/select',
  5043. ['div' => ['class' => 'error-message']],
  5044. 'Invalid',
  5045. '/div',
  5046. '/div',
  5047. ];
  5048. $this->assertHtml($expected, $result);
  5049. }
  5050. /**
  5051. * testSelectMultipleCheckboxes method
  5052. *
  5053. * Test generation of multi select elements in checkbox format.
  5054. *
  5055. * @return void
  5056. */
  5057. public function testSelectMultipleCheckboxes()
  5058. {
  5059. $result = $this->Form->select(
  5060. 'Model.multi_field',
  5061. ['first', 'second', 'third'],
  5062. ['multiple' => 'checkbox']
  5063. );
  5064. $expected = [
  5065. 'input' => [
  5066. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5067. ],
  5068. ['div' => ['class' => 'checkbox']],
  5069. ['label' => ['for' => 'model-multi-field-0']],
  5070. ['input' => [
  5071. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5072. 'value' => '0', 'id' => 'model-multi-field-0',
  5073. ]],
  5074. 'first',
  5075. '/label',
  5076. '/div',
  5077. ['div' => ['class' => 'checkbox']],
  5078. ['label' => ['for' => 'model-multi-field-1']],
  5079. ['input' => [
  5080. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5081. 'value' => '1', 'id' => 'model-multi-field-1',
  5082. ]],
  5083. 'second',
  5084. '/label',
  5085. '/div',
  5086. ['div' => ['class' => 'checkbox']],
  5087. ['label' => ['for' => 'model-multi-field-2']],
  5088. ['input' => [
  5089. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5090. 'value' => '2', 'id' => 'model-multi-field-2',
  5091. ]],
  5092. 'third',
  5093. '/label',
  5094. '/div',
  5095. ];
  5096. $this->assertHtml($expected, $result);
  5097. $result = $this->Form->select(
  5098. 'Model.multi_field',
  5099. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5100. ['multiple' => 'checkbox']
  5101. );
  5102. $expected = [
  5103. 'input' => [
  5104. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5105. ],
  5106. ['div' => ['class' => 'checkbox']],
  5107. ['label' => ['for' => 'model-multi-field-a+']],
  5108. ['input' => [
  5109. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5110. 'value' => 'a+', 'id' => 'model-multi-field-a+',
  5111. ]],
  5112. 'first',
  5113. '/label',
  5114. '/div',
  5115. ['div' => ['class' => 'checkbox']],
  5116. ['label' => ['for' => 'model-multi-field-a++']],
  5117. ['input' => [
  5118. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5119. 'value' => 'a++', 'id' => 'model-multi-field-a++',
  5120. ]],
  5121. 'second',
  5122. '/label',
  5123. '/div',
  5124. ['div' => ['class' => 'checkbox']],
  5125. ['label' => ['for' => 'model-multi-field-a+++']],
  5126. ['input' => [
  5127. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5128. 'value' => 'a+++', 'id' => 'model-multi-field-a+++',
  5129. ]],
  5130. 'third',
  5131. '/label',
  5132. '/div',
  5133. ];
  5134. $this->assertHtml($expected, $result);
  5135. $result = $this->Form->select(
  5136. 'Model.multi_field',
  5137. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5138. ['multiple' => 'checkbox']
  5139. );
  5140. $expected = [
  5141. 'input' => [
  5142. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5143. ],
  5144. ['div' => ['class' => 'checkbox']],
  5145. ['label' => ['for' => 'model-multi-field-a-b']],
  5146. ['input' => [
  5147. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5148. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b',
  5149. ]],
  5150. 'first',
  5151. '/label',
  5152. '/div',
  5153. ['div' => ['class' => 'checkbox']],
  5154. ['label' => ['for' => 'model-multi-field-a-b1']],
  5155. ['input' => [
  5156. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5157. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1',
  5158. ]],
  5159. 'second',
  5160. '/label',
  5161. '/div',
  5162. ['div' => ['class' => 'checkbox']],
  5163. ['label' => ['for' => 'model-multi-field-a-b2']],
  5164. ['input' => [
  5165. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5166. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2',
  5167. ]],
  5168. 'third',
  5169. '/label',
  5170. '/div',
  5171. ];
  5172. $this->assertHtml($expected, $result);
  5173. }
  5174. /**
  5175. * testSelectMultipleCheckboxRequestData method
  5176. *
  5177. * Ensure that multiCheckbox reads from the request data.
  5178. *
  5179. * @return void
  5180. */
  5181. public function testSelectMultipleCheckboxRequestData()
  5182. {
  5183. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  5184. $result = $this->Form->select(
  5185. 'Model.tags',
  5186. ['1' => 'first', 'Array' => 'Array'],
  5187. ['multiple' => 'checkbox']
  5188. );
  5189. $expected = [
  5190. 'input' => [
  5191. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '',
  5192. ],
  5193. ['div' => ['class' => 'checkbox']],
  5194. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5195. ['input' => [
  5196. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5197. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked',
  5198. ]],
  5199. 'first',
  5200. '/label',
  5201. '/div',
  5202. ['div' => ['class' => 'checkbox']],
  5203. ['label' => ['for' => 'model-tags-array']],
  5204. ['input' => [
  5205. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5206. 'value' => 'Array', 'id' => 'model-tags-array',
  5207. ]],
  5208. 'Array',
  5209. '/label',
  5210. '/div',
  5211. ];
  5212. $this->assertHtml($expected, $result);
  5213. }
  5214. /**
  5215. * testSelectMultipleCheckboxSecurity method
  5216. *
  5217. * Checks the security hash array generated for multiple-input checkbox elements.
  5218. *
  5219. * @return void
  5220. */
  5221. public function testSelectMultipleCheckboxSecurity()
  5222. {
  5223. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  5224. $this->assertEquals([], $this->Form->fields);
  5225. $this->Form->select(
  5226. 'Model.multi_field',
  5227. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5228. ['multiple' => 'checkbox']
  5229. );
  5230. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5231. $result = $this->Form->secure($this->Form->fields);
  5232. $key = '8af36fb34e6f2ef8ba0eb473bb4365ec232f3fe5%3A';
  5233. $this->assertContains('"' . $key . '"', $result);
  5234. }
  5235. /**
  5236. * testSelectMultipleSecureWithNoOptions method
  5237. *
  5238. * Multiple select elements should always be secured as they always participate
  5239. * in the POST data.
  5240. *
  5241. * @return void
  5242. */
  5243. public function testSelectMultipleSecureWithNoOptions()
  5244. {
  5245. $this->assertEquals([], $this->Form->fields);
  5246. $this->Form->select(
  5247. 'Model.select',
  5248. [],
  5249. ['multiple' => true]
  5250. );
  5251. $this->assertEquals(['Model.select'], $this->Form->fields);
  5252. }
  5253. /**
  5254. * testSelectNoSecureWithNoOptions method
  5255. *
  5256. * When a select box has no options it should not be added to the fields list
  5257. * as it always fail post validation.
  5258. *
  5259. * @return void
  5260. */
  5261. public function testSelectNoSecureWithNoOptions()
  5262. {
  5263. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testkey'));
  5264. $this->assertEquals([], $this->Form->fields);
  5265. $this->Form->select(
  5266. 'Model.select',
  5267. []
  5268. );
  5269. $this->assertEquals([], $this->Form->fields);
  5270. $this->Form->select(
  5271. 'Model.user_id',
  5272. [],
  5273. ['empty' => true]
  5274. );
  5275. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5276. }
  5277. /**
  5278. * testControlMultipleCheckboxes method
  5279. *
  5280. * Test control() resulting in multi select elements being generated.
  5281. *
  5282. * @return void
  5283. */
  5284. public function testControlMultipleCheckboxes()
  5285. {
  5286. $result = $this->Form->control('Model.multi_field', [
  5287. 'options' => ['first', 'second', 'third'],
  5288. 'multiple' => 'checkbox',
  5289. ]);
  5290. $expected = [
  5291. ['div' => ['class' => 'input select']],
  5292. ['label' => ['for' => 'model-multi-field']],
  5293. 'Multi Field',
  5294. '/label',
  5295. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5296. ['div' => ['class' => 'checkbox']],
  5297. ['label' => ['for' => 'model-multi-field-0']],
  5298. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5299. 'first',
  5300. '/label',
  5301. '/div',
  5302. ['div' => ['class' => 'checkbox']],
  5303. ['label' => ['for' => 'model-multi-field-1']],
  5304. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5305. 'second',
  5306. '/label',
  5307. '/div',
  5308. ['div' => ['class' => 'checkbox']],
  5309. ['label' => ['for' => 'model-multi-field-2']],
  5310. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5311. 'third',
  5312. '/label',
  5313. '/div',
  5314. '/div',
  5315. ];
  5316. $this->assertHtml($expected, $result);
  5317. $result = $this->Form->control('Model.multi_field', [
  5318. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5319. 'multiple' => 'checkbox',
  5320. ]);
  5321. $expected = [
  5322. ['div' => ['class' => 'input select']],
  5323. ['label' => ['for' => 'model-multi-field']],
  5324. 'Multi Field',
  5325. '/label',
  5326. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5327. ['div' => ['class' => 'checkbox']],
  5328. ['label' => ['for' => 'model-multi-field-a']],
  5329. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5330. 'first',
  5331. '/label',
  5332. '/div',
  5333. ['div' => ['class' => 'checkbox']],
  5334. ['label' => ['for' => 'model-multi-field-b']],
  5335. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5336. 'second',
  5337. '/label',
  5338. '/div',
  5339. ['div' => ['class' => 'checkbox']],
  5340. ['label' => ['for' => 'model-multi-field-c']],
  5341. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5342. 'third',
  5343. '/label',
  5344. '/div',
  5345. '/div',
  5346. ];
  5347. $this->assertHtml($expected, $result);
  5348. }
  5349. /**
  5350. * testSelectHiddenFieldOmission method
  5351. *
  5352. * Test that select() with 'hiddenField' => false omits the hidden field.
  5353. *
  5354. * @return void
  5355. */
  5356. public function testSelectHiddenFieldOmission()
  5357. {
  5358. $result = $this->Form->select(
  5359. 'Model.multi_field',
  5360. ['first', 'second'],
  5361. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5362. );
  5363. $this->assertNotContains('type="hidden"', $result);
  5364. }
  5365. /**
  5366. * testSelectCheckboxMultipleOverrideName method
  5367. *
  5368. * Test that select() with multiple = checkbox works with overriding name attribute.
  5369. *
  5370. * @return void
  5371. */
  5372. public function testSelectCheckboxMultipleOverrideName()
  5373. {
  5374. $result = $this->Form->select('category', ['1', '2'], [
  5375. 'multiple' => 'checkbox',
  5376. 'name' => 'fish',
  5377. ]);
  5378. $expected = [
  5379. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5380. ['div' => ['class' => 'checkbox']],
  5381. ['label' => ['for' => 'fish-0']],
  5382. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5383. '1',
  5384. '/label',
  5385. '/div',
  5386. ['div' => ['class' => 'checkbox']],
  5387. ['label' => ['for' => 'fish-1']],
  5388. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5389. '2',
  5390. '/label',
  5391. '/div',
  5392. ];
  5393. $this->assertHtml($expected, $result);
  5394. $result = $this->Form->multiCheckbox(
  5395. 'category',
  5396. new Collection(['1', '2']),
  5397. ['name' => 'fish']
  5398. );
  5399. $this->assertHtml($expected, $result);
  5400. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5401. 'name' => 'fish',
  5402. ]);
  5403. $this->assertHtml($expected, $result);
  5404. }
  5405. /**
  5406. * testControlMultiCheckbox method
  5407. *
  5408. * Test that control() works with multicheckbox.
  5409. *
  5410. * @return void
  5411. */
  5412. public function testControlMultiCheckbox()
  5413. {
  5414. $result = $this->Form->control('category', [
  5415. 'type' => 'multicheckbox',
  5416. 'options' => ['1', '2'],
  5417. ]);
  5418. $expected = [
  5419. ['div' => ['class' => 'input multicheckbox']],
  5420. '<label',
  5421. 'Category',
  5422. '/label',
  5423. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5424. ['div' => ['class' => 'checkbox']],
  5425. ['label' => ['for' => 'category-0']],
  5426. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5427. '1',
  5428. '/label',
  5429. '/div',
  5430. ['div' => ['class' => 'checkbox']],
  5431. ['label' => ['for' => 'category-1']],
  5432. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5433. '2',
  5434. '/label',
  5435. '/div',
  5436. '/div',
  5437. ];
  5438. $this->assertHtml($expected, $result);
  5439. }
  5440. /**
  5441. * testCheckbox method
  5442. *
  5443. * Test generation of checkboxes.
  5444. *
  5445. * @return void
  5446. */
  5447. public function testCheckbox()
  5448. {
  5449. $result = $this->Form->checkbox('Model.field');
  5450. $expected = [
  5451. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5452. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']],
  5453. ];
  5454. $this->assertHtml($expected, $result);
  5455. $result = $this->Form->checkbox('Model.field', [
  5456. 'id' => 'theID',
  5457. 'value' => 'myvalue',
  5458. 'form' => 'my-form',
  5459. ]);
  5460. $expected = [
  5461. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5462. ['input' => [
  5463. 'type' => 'checkbox', 'name' => 'Model[field]',
  5464. 'value' => 'myvalue', 'id' => 'theID',
  5465. 'form' => 'my-form',
  5466. ]],
  5467. ];
  5468. $this->assertHtml($expected, $result);
  5469. }
  5470. /**
  5471. * testCheckboxDefaultValue method
  5472. *
  5473. * Test default value setting on checkbox() method.
  5474. *
  5475. * @return void
  5476. */
  5477. public function testCheckboxDefaultValue()
  5478. {
  5479. $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
  5480. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5481. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5482. $this->assertHtml($expected, $result);
  5483. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5484. $this->Form->create();
  5485. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5486. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5487. $this->assertHtml($expected, $result);
  5488. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5489. $this->Form->create();
  5490. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5491. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5492. $this->assertHtml($expected, $result);
  5493. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5494. $this->Form->create();
  5495. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5496. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5497. $this->assertHtml($expected, $result);
  5498. $Articles = $this->getTableLocator()->get('Articles');
  5499. $Articles->getSchema()->addColumn(
  5500. 'published',
  5501. ['type' => 'boolean', 'null' => false, 'default' => true]
  5502. );
  5503. $this->Form->create($Articles->newEntity());
  5504. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5505. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5506. $this->assertHtml($expected, $result);
  5507. }
  5508. /**
  5509. * testCheckboxCheckedAndError method
  5510. *
  5511. * Test checkbox being checked or having errors.
  5512. *
  5513. * @return void
  5514. */
  5515. public function testCheckboxCheckedAndError()
  5516. {
  5517. $this->article['errors'] = [
  5518. 'published' => true,
  5519. ];
  5520. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5521. $this->Form->create($this->article);
  5522. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5523. $expected = [
  5524. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5525. ['input' => [
  5526. 'type' => 'checkbox',
  5527. 'name' => 'published',
  5528. 'value' => 'myvalue',
  5529. 'id' => 'theID',
  5530. 'checked' => 'checked',
  5531. 'class' => 'form-error',
  5532. ]],
  5533. ];
  5534. $this->assertHtml($expected, $result);
  5535. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5536. $this->Form->create($this->article);
  5537. $result = $this->Form->checkbox('published');
  5538. $expected = [
  5539. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5540. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']],
  5541. ];
  5542. $this->assertHtml($expected, $result);
  5543. }
  5544. /**
  5545. * testCheckboxCustomNameAttribute method
  5546. *
  5547. * Test checkbox() with a custom name attribute.
  5548. *
  5549. * @return void
  5550. */
  5551. public function testCheckboxCustomNameAttribute()
  5552. {
  5553. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5554. $expected = [
  5555. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5556. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']],
  5557. ];
  5558. $this->assertHtml($expected, $result);
  5559. }
  5560. /**
  5561. * testCheckboxHiddenField method
  5562. *
  5563. * Test that the hidden input for checkboxes can be omitted or set to a
  5564. * specific value.
  5565. *
  5566. * @return void
  5567. */
  5568. public function testCheckboxHiddenField()
  5569. {
  5570. $result = $this->Form->checkbox('UserForm.something', [
  5571. 'hiddenField' => false,
  5572. ]);
  5573. $expected = [
  5574. 'input' => [
  5575. 'type' => 'checkbox',
  5576. 'name' => 'UserForm[something]',
  5577. 'value' => '1',
  5578. ],
  5579. ];
  5580. $this->assertHtml($expected, $result);
  5581. $result = $this->Form->checkbox('UserForm.something', [
  5582. 'value' => 'Y',
  5583. 'hiddenField' => 'N',
  5584. ]);
  5585. $expected = [
  5586. ['input' => [
  5587. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5588. 'value' => 'N',
  5589. ]],
  5590. ['input' => [
  5591. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5592. 'value' => 'Y',
  5593. ]],
  5594. ];
  5595. $this->assertHtml($expected, $result);
  5596. }
  5597. /**
  5598. * testTime method
  5599. *
  5600. * Test the time type.
  5601. *
  5602. * @return void
  5603. */
  5604. public function testTime()
  5605. {
  5606. $result = $this->Form->time('start_time', [
  5607. 'timeFormat' => 12,
  5608. 'interval' => 5,
  5609. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm'],
  5610. ]);
  5611. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5612. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5613. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5614. $this->assertNotContains('year', $result);
  5615. $this->assertNotContains('month', $result);
  5616. $this->assertNotContains('day', $result);
  5617. $result = $this->Form->time('start_time', [
  5618. 'timeFormat' => 12,
  5619. 'interval' => 5,
  5620. 'value' => '2014-03-08 16:30:00',
  5621. ]);
  5622. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5623. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5624. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5625. $this->assertNotContains('year', $result);
  5626. $this->assertNotContains('month', $result);
  5627. $this->assertNotContains('day', $result);
  5628. }
  5629. /**
  5630. * testTimeFormat24NoMeridian method
  5631. *
  5632. * Ensure that timeFormat=24 has no merdian.
  5633. *
  5634. * @return void
  5635. */
  5636. public function testTimeFormat24NoMeridian()
  5637. {
  5638. $result = $this->Form->time('start_time', [
  5639. 'timeFormat' => 24,
  5640. 'interval' => 5,
  5641. 'value' => '2014-03-08 16:30:00',
  5642. ]);
  5643. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5644. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5645. $this->assertNotContains('meridian', $result);
  5646. $this->assertNotContains('pm', $result);
  5647. $this->assertNotContains('year', $result);
  5648. $this->assertNotContains('month', $result);
  5649. $this->assertNotContains('day', $result);
  5650. }
  5651. /**
  5652. * testDate method
  5653. *
  5654. * Test the date type.
  5655. *
  5656. * @return void
  5657. */
  5658. public function testDate()
  5659. {
  5660. $result = $this->Form->date('start_day', [
  5661. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08'],
  5662. ]);
  5663. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5664. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5665. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5666. $this->assertNotContains('hour', $result);
  5667. $this->assertNotContains('minute', $result);
  5668. $this->assertNotContains('second', $result);
  5669. $this->assertNotContains('meridian', $result);
  5670. }
  5671. /**
  5672. * testDateTime method
  5673. *
  5674. * Test generation of date/time select elements.
  5675. *
  5676. * @return void
  5677. */
  5678. public function testDateTime()
  5679. {
  5680. extract($this->dateRegex);
  5681. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5682. $now = strtotime('now');
  5683. $expected = [
  5684. ['select' => ['name' => 'Contact[date][year]']],
  5685. ['option' => ['value' => '']],
  5686. '/option',
  5687. $yearsRegex,
  5688. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5689. date('Y', $now),
  5690. '/option',
  5691. '*/select',
  5692. ['select' => ['name' => 'Contact[date][month]']],
  5693. ['option' => ['value' => '']],
  5694. '/option',
  5695. $monthsRegex,
  5696. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5697. date('F', $now),
  5698. '/option',
  5699. '*/select',
  5700. ['select' => ['name' => 'Contact[date][day]']],
  5701. ['option' => ['value' => '']],
  5702. '/option',
  5703. $daysRegex,
  5704. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5705. date('j', $now),
  5706. '/option',
  5707. '*/select',
  5708. ['select' => ['name' => 'Contact[date][hour]']],
  5709. ['option' => ['value' => '']],
  5710. '/option',
  5711. $hoursRegex,
  5712. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5713. date('G', $now),
  5714. '/option',
  5715. '*/select',
  5716. ['select' => ['name' => 'Contact[date][minute]']],
  5717. ['option' => ['value' => '']],
  5718. '/option',
  5719. $minutesRegex,
  5720. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5721. date('i', $now),
  5722. '/option',
  5723. '*/select',
  5724. ];
  5725. $this->assertHtml($expected, $result);
  5726. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5727. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5728. $now = strtotime('now');
  5729. $expected = [
  5730. ['select' => ['name' => 'Contact[date][year]']],
  5731. $yearsRegex,
  5732. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5733. date('Y', $now),
  5734. '/option',
  5735. '*/select',
  5736. ['select' => ['name' => 'Contact[date][month]']],
  5737. $monthsRegex,
  5738. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5739. date('F', $now),
  5740. '/option',
  5741. '*/select',
  5742. ['select' => ['name' => 'Contact[date][day]']],
  5743. $daysRegex,
  5744. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5745. date('j', $now),
  5746. '/option',
  5747. '*/select',
  5748. ['select' => ['name' => 'Contact[date][hour]']],
  5749. $hoursRegex,
  5750. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5751. date('G', $now),
  5752. '/option',
  5753. '*/select',
  5754. ['select' => ['name' => 'Contact[date][minute]']],
  5755. $minutesRegex,
  5756. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5757. date('i', $now),
  5758. '/option',
  5759. '*/select',
  5760. ];
  5761. $this->assertHtml($expected, $result);
  5762. }
  5763. /**
  5764. * testDateTimeSecured method
  5765. *
  5766. * Test that datetime fields are added to protected fields list.
  5767. *
  5768. * @return void
  5769. */
  5770. public function testDateTimeSecured()
  5771. {
  5772. $this->View->setRequest(
  5773. $this->View->getRequest()->withParam('_Token', ['unlockedFields' => []])
  5774. );
  5775. $this->Form->dateTime('Contact.date');
  5776. $expected = [
  5777. 'Contact.date.year',
  5778. 'Contact.date.month',
  5779. 'Contact.date.day',
  5780. 'Contact.date.hour',
  5781. 'Contact.date.minute',
  5782. ];
  5783. $this->assertEquals($expected, $this->Form->fields);
  5784. $this->Form->fields = [];
  5785. $this->Form->date('Contact.published');
  5786. $expected = [
  5787. 'Contact.published.year',
  5788. 'Contact.published.month',
  5789. 'Contact.published.day',
  5790. ];
  5791. $this->assertEquals($expected, $this->Form->fields);
  5792. }
  5793. /**
  5794. * testDateTimeSecuredDisabled method
  5795. *
  5796. * Test that datetime fields are added to protected fields list.
  5797. *
  5798. * @return void
  5799. */
  5800. public function testDateTimeSecuredDisabled()
  5801. {
  5802. $this->View->setRequest(
  5803. $this->View->getRequest()->withParam('_Token', ['unlockedFields' => []])
  5804. );
  5805. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5806. $expected = [];
  5807. $this->assertEquals($expected, $this->Form->fields);
  5808. $this->Form->fields = [];
  5809. $this->Form->date('Contact.published', ['secure' => false]);
  5810. $expected = [];
  5811. $this->assertEquals($expected, $this->Form->fields);
  5812. }
  5813. /**
  5814. * Test empty defaulting to true for datetime.
  5815. *
  5816. * @return void
  5817. */
  5818. public function testDatetimeEmpty()
  5819. {
  5820. extract($this->dateRegex);
  5821. $result = $this->Form->dateTime('Contact.date', [
  5822. 'timeFormat' => 12,
  5823. 'empty' => true,
  5824. 'default' => true,
  5825. ]);
  5826. $expected = [
  5827. ['select' => ['name' => 'Contact[date][year]']],
  5828. $yearsRegex,
  5829. ['option' => ['value' => '']],
  5830. '/option',
  5831. '*/select',
  5832. ['select' => ['name' => 'Contact[date][month]']],
  5833. $monthsRegex,
  5834. ['option' => ['value' => '']],
  5835. '/option',
  5836. '*/select',
  5837. ['select' => ['name' => 'Contact[date][day]']],
  5838. $daysRegex,
  5839. ['option' => ['value' => '']],
  5840. '/option',
  5841. '*/select',
  5842. ['select' => ['name' => 'Contact[date][hour]']],
  5843. $hoursRegex,
  5844. ['option' => ['value' => '']],
  5845. '/option',
  5846. '*/select',
  5847. ['select' => ['name' => 'Contact[date][minute]']],
  5848. $minutesRegex,
  5849. ['option' => ['value' => '']],
  5850. '/option',
  5851. '*/select',
  5852. ['select' => ['name' => 'Contact[date][meridian]']],
  5853. $meridianRegex,
  5854. ['option' => ['value' => '']],
  5855. '/option',
  5856. '*/select',
  5857. ];
  5858. $this->assertHtml($expected, $result);
  5859. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5860. }
  5861. /**
  5862. * Presence check for array form empty options
  5863. *
  5864. * @return void
  5865. */
  5866. public function testDateTimeEmptyAsArrayPresence()
  5867. {
  5868. $result = $this->Form->dateTime('Contact.date', [
  5869. 'empty' => [
  5870. 'day' => 'DAY',
  5871. 'month' => 'MONTH',
  5872. 'year' => 'YEAR',
  5873. 'hour' => 'HOUR',
  5874. 'minute' => 'MINUTE',
  5875. 'meridian' => false,
  5876. ],
  5877. 'default' => true,
  5878. ]);
  5879. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5880. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5881. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5882. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5883. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5884. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5885. }
  5886. /**
  5887. * Test datetime with array empty value, ensuring
  5888. * empty options aren't duplicated.
  5889. *
  5890. * @return void
  5891. */
  5892. public function testDatetimeEmptyArrayForm()
  5893. {
  5894. extract($this->dateRegex);
  5895. $result = $this->Form->dateTime('Contact.date', [
  5896. 'minYear' => '2017',
  5897. 'maxYear' => '2019',
  5898. 'empty' => [
  5899. 'year' => 'pick year',
  5900. 'month' => 'pick month',
  5901. ],
  5902. 'hour' => false,
  5903. 'minute' => false,
  5904. 'second' => false,
  5905. 'meridian' => false,
  5906. ]);
  5907. $expected = [
  5908. ['select' => ['name' => 'Contact[date][year]']],
  5909. ['option' => ['value' => '', 'selected' => 'selected']], 'pick year', '/option',
  5910. '*/select',
  5911. ['select' => ['name' => 'Contact[date][month]']],
  5912. ['option' => ['value' => '', 'selected' => 'selected']], 'pick month', '/option',
  5913. $monthsRegex,
  5914. '*/select',
  5915. ['select' => ['name' => 'Contact[date][day]']],
  5916. $daysRegex,
  5917. '*/select',
  5918. ];
  5919. $this->assertHtml($expected, $result);
  5920. }
  5921. /**
  5922. * testDatetimeMinuteInterval method
  5923. *
  5924. * Test datetime with interval option.
  5925. *
  5926. * @return void
  5927. */
  5928. public function testDatetimeMinuteInterval()
  5929. {
  5930. extract($this->dateRegex);
  5931. $result = $this->Form->dateTime('Contact.date', [
  5932. 'interval' => 5,
  5933. 'value' => '',
  5934. ]);
  5935. $expected = [
  5936. ['select' => ['name' => 'Contact[date][year]']],
  5937. $yearsRegex,
  5938. ['option' => ['selected' => 'selected', 'value' => '']],
  5939. '/option',
  5940. '*/select',
  5941. ['select' => ['name' => 'Contact[date][month]']],
  5942. $monthsRegex,
  5943. ['option' => ['selected' => 'selected', 'value' => '']],
  5944. '/option',
  5945. '*/select',
  5946. ['select' => ['name' => 'Contact[date][day]']],
  5947. $daysRegex,
  5948. ['option' => ['selected' => 'selected', 'value' => '']],
  5949. '/option',
  5950. '*/select',
  5951. ['select' => ['name' => 'Contact[date][hour]']],
  5952. $hoursRegex,
  5953. ['option' => ['selected' => 'selected', 'value' => '']],
  5954. '/option',
  5955. '*/select',
  5956. ['select' => ['name' => 'Contact[date][minute]']],
  5957. $minutesRegex,
  5958. ['option' => ['selected' => 'selected', 'value' => '']],
  5959. '/option',
  5960. ['option' => ['value' => '00']],
  5961. '00',
  5962. '/option',
  5963. ['option' => ['value' => '05']],
  5964. '05',
  5965. '/option',
  5966. ['option' => ['value' => '10']],
  5967. '10',
  5968. '/option',
  5969. '*/select',
  5970. ];
  5971. $this->assertHtml($expected, $result);
  5972. }
  5973. /**
  5974. * testDateTimeRounding method
  5975. *
  5976. * Test dateTime with rounding.
  5977. *
  5978. * @return void
  5979. */
  5980. public function testDateTimeRounding()
  5981. {
  5982. $this->View->setRequest($this->View->getRequest()->withData('Contact', [
  5983. 'date' => [
  5984. 'day' => '13',
  5985. 'month' => '12',
  5986. 'year' => '2010',
  5987. 'hour' => '04',
  5988. 'minute' => '19',
  5989. 'meridian' => 'AM',
  5990. ],
  5991. ]));
  5992. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5993. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5994. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5995. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5996. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5997. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5998. }
  5999. /**
  6000. * testDatetimeWithDefault method
  6001. *
  6002. * Test that datetime() and default values work.
  6003. *
  6004. * @return void
  6005. */
  6006. public function testDatetimeWithDefault()
  6007. {
  6008. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  6009. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  6010. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  6011. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  6012. $result = $this->Form->dateTime('Contact.updated', [
  6013. 'default' => '2009-06-01 11:15:30',
  6014. ]);
  6015. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  6016. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  6017. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  6018. }
  6019. /**
  6020. * testDateTimeAllZeros method
  6021. *
  6022. * Test datetime() with all zeros.
  6023. *
  6024. * @return void
  6025. */
  6026. public function testDateTimeAllZeros()
  6027. {
  6028. $result = $this->Form->dateTime('Contact.date', [
  6029. 'timeFormat' => false,
  6030. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  6031. 'value' => '0000-00-00',
  6032. ]);
  6033. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  6034. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  6035. }
  6036. /**
  6037. * testFormDateTimeMulti method
  6038. *
  6039. * Test multiple datetime element generation.
  6040. *
  6041. * @return void
  6042. */
  6043. public function testFormDateTimeMulti()
  6044. {
  6045. extract($this->dateRegex);
  6046. $result = $this->Form->dateTime('Contact.1.updated');
  6047. $this->assertContains('Contact[1][updated][month]', $result);
  6048. $this->assertContains('Contact[1][updated][day]', $result);
  6049. $this->assertContains('Contact[1][updated][year]', $result);
  6050. $this->assertContains('Contact[1][updated][hour]', $result);
  6051. $this->assertContains('Contact[1][updated][minute]', $result);
  6052. }
  6053. /**
  6054. * testDateTimeLabelIdMatchesFirstControl method
  6055. *
  6056. * When changing the date format, the label should always focus the first select box when
  6057. * clicked.
  6058. *
  6059. * @return void
  6060. */
  6061. public function testDateTimeLabelIdMatchesFirstControl()
  6062. {
  6063. $result = $this->Form->control('Model.date', ['type' => 'date']);
  6064. $this->assertContains('<label>Date</label>', $result);
  6065. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  6066. $this->assertContains('<label>Date</label>', $result);
  6067. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  6068. $this->assertContains('<label>Date</label>', $result);
  6069. }
  6070. /**
  6071. * testDateTimeSecondOptions method
  6072. *
  6073. * Test datetime second=true.
  6074. *
  6075. * @return void
  6076. */
  6077. public function testDateTimeSecondOptions()
  6078. {
  6079. $result = $this->Form->dateTime('updated', ['second' => true]);
  6080. $this->assertContains('updated[second]', $result, 'Should have seconds');
  6081. $result = $this->Form->dateTime('updated', ['second' => []]);
  6082. $this->assertContains('updated[second]', $result, 'Should have seconds');
  6083. $result = $this->Form->dateTime('updated', ['second' => null]);
  6084. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  6085. $result = $this->Form->dateTime('updated', ['second' => false]);
  6086. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  6087. }
  6088. /**
  6089. * testMonth method
  6090. *
  6091. * Test generation of a month input.
  6092. *
  6093. * @return void
  6094. */
  6095. public function testMonth()
  6096. {
  6097. $result = $this->Form->month('Model.field', ['value' => '']);
  6098. $expected = [
  6099. ['select' => ['name' => 'Model[field][month]']],
  6100. ['option' => ['value' => '', 'selected' => 'selected']],
  6101. '/option',
  6102. ['option' => ['value' => '01']],
  6103. date('F', strtotime('2008-01-01 00:00:00')),
  6104. '/option',
  6105. ['option' => ['value' => '02']],
  6106. date('F', strtotime('2008-02-01 00:00:00')),
  6107. '/option',
  6108. '*/select',
  6109. ];
  6110. $this->assertHtml($expected, $result);
  6111. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  6112. $expected = [
  6113. ['select' => ['name' => 'Model[field][month]']],
  6114. ['option' => ['selected' => 'selected', 'value' => '']],
  6115. '/option',
  6116. ['option' => ['value' => '01']],
  6117. date('F', strtotime('2008-01-01 00:00:00')),
  6118. '/option',
  6119. ['option' => ['value' => '02']],
  6120. date('F', strtotime('2008-02-01 00:00:00')),
  6121. '/option',
  6122. '*/select',
  6123. ];
  6124. $this->assertHtml($expected, $result);
  6125. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  6126. $expected = [
  6127. ['select' => ['name' => 'Model[field][month]']],
  6128. ['option' => ['selected' => 'selected', 'value' => '']],
  6129. '/option',
  6130. ['option' => ['value' => '01']],
  6131. '1',
  6132. '/option',
  6133. ['option' => ['value' => '02']],
  6134. '2',
  6135. '/option',
  6136. '*/select',
  6137. ];
  6138. $this->assertHtml($expected, $result);
  6139. $monthNames = [
  6140. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6141. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec',
  6142. ];
  6143. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  6144. $expected = [
  6145. ['select' => ['name' => 'Model[field][month]']],
  6146. ['option' => ['value' => '']],
  6147. '/option',
  6148. ['option' => ['value' => '01', 'selected' => 'selected']],
  6149. 'Jan',
  6150. '/option',
  6151. ['option' => ['value' => '02']],
  6152. 'Feb',
  6153. '/option',
  6154. '*/select',
  6155. ];
  6156. $this->assertHtml($expected, $result);
  6157. $this->View->setRequest(
  6158. $this->View->getRequest()->withData('Project.release', '2050-02-10')
  6159. );
  6160. $this->Form->create();
  6161. $result = $this->Form->month('Project.release');
  6162. $expected = [
  6163. ['select' => ['name' => 'Project[release][month]']],
  6164. ['option' => ['value' => '']],
  6165. '/option',
  6166. ['option' => ['value' => '01']],
  6167. 'January',
  6168. '/option',
  6169. ['option' => ['value' => '02', 'selected' => 'selected']],
  6170. 'February',
  6171. '/option',
  6172. '*/select',
  6173. ];
  6174. $this->assertHtml($expected, $result);
  6175. $result = $this->Form->month('Contact.published', [
  6176. 'empty' => 'Published on',
  6177. ]);
  6178. $this->assertContains('Published on', $result);
  6179. }
  6180. /**
  6181. * testDay method
  6182. *
  6183. * Test generation of a day input.
  6184. *
  6185. * @return void
  6186. */
  6187. public function testDay()
  6188. {
  6189. extract($this->dateRegex);
  6190. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  6191. $expected = [
  6192. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  6193. ['option' => ['selected' => 'selected', 'value' => '']],
  6194. '/option',
  6195. ['option' => ['value' => '01']],
  6196. '1',
  6197. '/option',
  6198. ['option' => ['value' => '02']],
  6199. '2',
  6200. '/option',
  6201. $daysRegex,
  6202. '/select',
  6203. ];
  6204. $this->assertHtml($expected, $result);
  6205. $this->View->setRequest(
  6206. $this->View->getRequest()->withData('Model.field', '2006-10-10 23:12:32')
  6207. );
  6208. $this->Form->create();
  6209. $result = $this->Form->day('Model.field');
  6210. $expected = [
  6211. ['select' => ['name' => 'Model[field][day]']],
  6212. ['option' => ['value' => '']],
  6213. '/option',
  6214. ['option' => ['value' => '01']],
  6215. '1',
  6216. '/option',
  6217. ['option' => ['value' => '02']],
  6218. '2',
  6219. '/option',
  6220. $daysRegex,
  6221. ['option' => ['value' => '10', 'selected' => 'selected']],
  6222. '10',
  6223. '/option',
  6224. $daysRegex,
  6225. '/select',
  6226. ];
  6227. $this->assertHtml($expected, $result);
  6228. $this->View->setRequest($this->View->getRequest()->withData('Model.field', ''));
  6229. $this->Form->create();
  6230. $result = $this->Form->day('Model.field', ['value' => '10']);
  6231. $expected = [
  6232. ['select' => ['name' => 'Model[field][day]']],
  6233. ['option' => ['value' => '']],
  6234. '/option',
  6235. ['option' => ['value' => '01']],
  6236. '1',
  6237. '/option',
  6238. ['option' => ['value' => '02']],
  6239. '2',
  6240. '/option',
  6241. $daysRegex,
  6242. ['option' => ['value' => '10', 'selected' => 'selected']],
  6243. '10',
  6244. '/option',
  6245. $daysRegex,
  6246. '/select',
  6247. ];
  6248. $this->assertHtml($expected, $result);
  6249. $this->View->setRequest(
  6250. $this->View->getRequest()->withData('Project.release', '2050-10-10')
  6251. );
  6252. $this->Form->create();
  6253. $result = $this->Form->day('Project.release');
  6254. $expected = [
  6255. ['select' => ['name' => 'Project[release][day]']],
  6256. ['option' => ['value' => '']],
  6257. '/option',
  6258. ['option' => ['value' => '01']],
  6259. '1',
  6260. '/option',
  6261. ['option' => ['value' => '02']],
  6262. '2',
  6263. '/option',
  6264. $daysRegex,
  6265. ['option' => ['value' => '10', 'selected' => 'selected']],
  6266. '10',
  6267. '/option',
  6268. $daysRegex,
  6269. '/select',
  6270. ];
  6271. $this->assertHtml($expected, $result);
  6272. $result = $this->Form->day('Contact.published', [
  6273. 'empty' => 'Published on',
  6274. ]);
  6275. $this->assertContains('Published on', $result);
  6276. }
  6277. /**
  6278. * testMinute method
  6279. *
  6280. * Test generation of a minute input.
  6281. *
  6282. * @return void
  6283. */
  6284. public function testMinute()
  6285. {
  6286. extract($this->dateRegex);
  6287. $result = $this->Form->minute('Model.field', ['value' => '']);
  6288. $expected = [
  6289. ['select' => ['name' => 'Model[field][minute]']],
  6290. ['option' => ['selected' => 'selected', 'value' => '']],
  6291. '/option',
  6292. ['option' => ['value' => '00']],
  6293. '00',
  6294. '/option',
  6295. ['option' => ['value' => '01']],
  6296. '01',
  6297. '/option',
  6298. ['option' => ['value' => '02']],
  6299. '02',
  6300. '/option',
  6301. $minutesRegex,
  6302. '/select',
  6303. ];
  6304. $this->assertHtml($expected, $result);
  6305. $this->View->setRequest(
  6306. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:12:32')
  6307. );
  6308. $this->Form->create();
  6309. $result = $this->Form->minute('Model.field');
  6310. $expected = [
  6311. ['select' => ['name' => 'Model[field][minute]']],
  6312. ['option' => ['value' => '']],
  6313. '/option',
  6314. ['option' => ['value' => '00']],
  6315. '00',
  6316. '/option',
  6317. ['option' => ['value' => '01']],
  6318. '01',
  6319. '/option',
  6320. ['option' => ['value' => '02']],
  6321. '02',
  6322. '/option',
  6323. $minutesRegex,
  6324. ['option' => ['value' => '12', 'selected' => 'selected']],
  6325. '12',
  6326. '/option',
  6327. $minutesRegex,
  6328. '/select',
  6329. ];
  6330. $this->assertHtml($expected, $result);
  6331. $this->View->setRequest($this->View->getRequest()->withData('Model.field', ''));
  6332. $this->Form->create();
  6333. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6334. $expected = [
  6335. ['select' => ['name' => 'Model[field][minute]']],
  6336. ['option' => ['selected' => 'selected', 'value' => '']],
  6337. '/option',
  6338. ['option' => ['value' => '00']],
  6339. '00',
  6340. '/option',
  6341. ['option' => ['value' => '05']],
  6342. '05',
  6343. '/option',
  6344. ['option' => ['value' => '10']],
  6345. '10',
  6346. '/option',
  6347. $minutesRegex,
  6348. '/select',
  6349. ];
  6350. $this->assertHtml($expected, $result);
  6351. $this->View->setRequest(
  6352. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:10:32')
  6353. );
  6354. $this->Form->create();
  6355. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6356. $expected = [
  6357. ['select' => ['name' => 'Model[field][minute]']],
  6358. ['option' => ['value' => '']],
  6359. '/option',
  6360. ['option' => ['value' => '00']],
  6361. '00',
  6362. '/option',
  6363. ['option' => ['value' => '05']],
  6364. '05',
  6365. '/option',
  6366. ['option' => ['value' => '10', 'selected' => 'selected']],
  6367. '10',
  6368. '/option',
  6369. $minutesRegex,
  6370. '/select',
  6371. ];
  6372. $this->assertHtml($expected, $result);
  6373. }
  6374. /**
  6375. * testMeridian method
  6376. *
  6377. * Test generating an input for the meridian.
  6378. *
  6379. * @return void
  6380. */
  6381. public function testMeridian()
  6382. {
  6383. extract($this->dateRegex);
  6384. $now = new \DateTime();
  6385. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6386. $expected = [
  6387. ['select' => ['name' => 'Model[field][meridian]']],
  6388. ['option' => ['value' => '']],
  6389. '/option',
  6390. $meridianRegex,
  6391. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6392. $now->format('a'),
  6393. '/option',
  6394. '*/select',
  6395. ];
  6396. $this->assertHtml($expected, $result);
  6397. }
  6398. /**
  6399. * testHour method
  6400. *
  6401. * Test generation of an hour input.
  6402. *
  6403. * @return void
  6404. */
  6405. public function testHour()
  6406. {
  6407. extract($this->dateRegex);
  6408. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6409. $expected = [
  6410. ['select' => ['name' => 'Model[field][hour]']],
  6411. ['option' => ['selected' => 'selected', 'value' => '']],
  6412. '/option',
  6413. ['option' => ['value' => '01']],
  6414. '1',
  6415. '/option',
  6416. ['option' => ['value' => '02']],
  6417. '2',
  6418. '/option',
  6419. $hoursRegex,
  6420. '/select',
  6421. ];
  6422. $this->assertHtml($expected, $result);
  6423. $this->View->setRequest(
  6424. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:12:32')
  6425. );
  6426. $this->Form->create();
  6427. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6428. $expected = [
  6429. ['select' => ['name' => 'Model[field][hour]']],
  6430. ['option' => ['value' => '']],
  6431. '/option',
  6432. ['option' => ['value' => '01']],
  6433. '1',
  6434. '/option',
  6435. ['option' => ['value' => '02']],
  6436. '2',
  6437. '/option',
  6438. $hoursRegex,
  6439. ['option' => ['value' => '12', 'selected' => 'selected']],
  6440. '12',
  6441. '/option',
  6442. '/select',
  6443. ];
  6444. $this->assertHtml($expected, $result);
  6445. $this->View->setRequest($this->View->getRequest()->withData('Model.field', ''));
  6446. $this->Form->create();
  6447. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6448. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6449. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6450. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6451. $this->View->setRequest(
  6452. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:12:32')
  6453. );
  6454. $this->Form->create();
  6455. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6456. $expected = [
  6457. ['select' => ['name' => 'Model[field][hour]']],
  6458. ['option' => ['value' => '']],
  6459. '/option',
  6460. ['option' => ['value' => '00', 'selected' => 'selected']],
  6461. '0',
  6462. '/option',
  6463. ['option' => ['value' => '01']],
  6464. '1',
  6465. '/option',
  6466. ['option' => ['value' => '02']],
  6467. '2',
  6468. '/option',
  6469. $hoursRegex,
  6470. '/select',
  6471. ];
  6472. $this->assertHtml($expected, $result);
  6473. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  6474. $this->Form->create();
  6475. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6476. $thisHour = date('H');
  6477. $optValue = date('G');
  6478. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6479. $this->View->setRequest(
  6480. $this->View->getRequest()->withData('Model.field', '2050-10-10 01:12:32')
  6481. );
  6482. $this->Form->create();
  6483. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6484. $expected = [
  6485. ['select' => ['name' => 'Model[field][hour]']],
  6486. ['option' => ['value' => '']],
  6487. '/option',
  6488. ['option' => ['value' => '00']],
  6489. '0',
  6490. '/option',
  6491. ['option' => ['value' => '01', 'selected' => 'selected']],
  6492. '1',
  6493. '/option',
  6494. ['option' => ['value' => '02']],
  6495. '2',
  6496. '/option',
  6497. $hoursRegex,
  6498. '/select',
  6499. ];
  6500. $this->assertHtml($expected, $result);
  6501. }
  6502. /**
  6503. * testYear method
  6504. *
  6505. * Test generation of a year input.
  6506. *
  6507. * @return void
  6508. */
  6509. public function testYear()
  6510. {
  6511. $this->View->setRequest(
  6512. $this->View->getRequest()->withData('Contact.published', '2006-10-10')
  6513. );
  6514. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6515. $expected = [
  6516. ['select' => ['name' => 'Model[field][year]']],
  6517. ['option' => ['selected' => 'selected', 'value' => '']],
  6518. '/option',
  6519. ['option' => ['value' => '2007']],
  6520. '2007',
  6521. '/option',
  6522. ['option' => ['value' => '2006']],
  6523. '2006',
  6524. '/option',
  6525. '/select',
  6526. ];
  6527. $this->assertHtml($expected, $result);
  6528. $result = $this->Form->year('Model.field', [
  6529. 'value' => '',
  6530. 'minYear' => 2006,
  6531. 'maxYear' => 2007,
  6532. 'orderYear' => 'asc',
  6533. ]);
  6534. $expected = [
  6535. ['select' => ['name' => 'Model[field][year]']],
  6536. ['option' => ['selected' => 'selected', 'value' => '']],
  6537. '/option',
  6538. ['option' => ['value' => '2006']],
  6539. '2006',
  6540. '/option',
  6541. ['option' => ['value' => '2007']],
  6542. '2007',
  6543. '/option',
  6544. '/select',
  6545. ];
  6546. $this->assertHtml($expected, $result);
  6547. $result = $this->Form->year('Contact.published', [
  6548. 'empty' => false,
  6549. 'minYear' => 2006,
  6550. 'maxYear' => 2007,
  6551. ]);
  6552. $expected = [
  6553. ['select' => ['name' => 'Contact[published][year]']],
  6554. ['option' => ['value' => '2007']],
  6555. '2007',
  6556. '/option',
  6557. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6558. '2006',
  6559. '/option',
  6560. '/select',
  6561. ];
  6562. $this->assertHtml($expected, $result);
  6563. $result = $this->Form->year('Contact.published', [
  6564. 'empty' => 'Published on',
  6565. ]);
  6566. $this->assertContains('Published on', $result);
  6567. }
  6568. /**
  6569. * testControlDatetimePreEpoch method
  6570. *
  6571. * Test minYear being prior to the unix epoch.
  6572. *
  6573. * @return void
  6574. */
  6575. public function testControlDatetimePreEpoch()
  6576. {
  6577. $start = date('Y') - 80;
  6578. $end = date('Y') - 18;
  6579. $result = $this->Form->control('birth_year', [
  6580. 'type' => 'date',
  6581. 'label' => 'Birth Year',
  6582. 'minYear' => $start,
  6583. 'maxYear' => $end,
  6584. 'month' => false,
  6585. 'day' => false,
  6586. ]);
  6587. $this->assertContains('value="' . $start . '">' . $start, $result);
  6588. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6589. $this->assertNotContains('value="00">00', $result);
  6590. }
  6591. /**
  6592. * test control() datetime & required attributes
  6593. *
  6594. * @return void
  6595. */
  6596. public function testControlDatetimeRequired()
  6597. {
  6598. $result = $this->Form->control('birthday', [
  6599. 'type' => 'date',
  6600. 'required' => true,
  6601. ]);
  6602. $this->assertContains(
  6603. '<select name="birthday[year]" required="required"',
  6604. $result
  6605. );
  6606. }
  6607. /**
  6608. * testYearAutoExpandRange method
  6609. *
  6610. * @return void
  6611. */
  6612. public function testYearAutoExpandRange()
  6613. {
  6614. $this->View->setRequest($this->View->getRequest()->withData('User.birthday', '1930-10-10'));
  6615. $result = $this->Form->year('User.birthday');
  6616. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6617. $result = $matches[1];
  6618. $expected = range(date('Y') + 5, 1930);
  6619. $this->assertEquals($expected, $result);
  6620. $this->View->setRequest($this->View->getRequest()->withData('Project.release', '2050-10-10'));
  6621. $this->Form->create();
  6622. $result = $this->Form->year('Project.release');
  6623. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6624. $result = $matches[1];
  6625. $expected = range(2050, date('Y') - 5);
  6626. $this->assertEquals($expected, $result);
  6627. $this->View->setRequest($this->View->getRequest()->withData('Project.release', '1881-10-10'));
  6628. $this->Form->create();
  6629. $result = $this->Form->year('Project.release', [
  6630. 'minYear' => 1890,
  6631. 'maxYear' => 1900,
  6632. ]);
  6633. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6634. $result = $matches[1];
  6635. $expected = range(1900, 1881);
  6636. $this->assertEquals($expected, $result);
  6637. }
  6638. /**
  6639. * testControlDate method
  6640. *
  6641. * Test that control() accepts the type of date and passes options in.
  6642. *
  6643. * @return void
  6644. */
  6645. public function testControlDate()
  6646. {
  6647. $this->View->setRequest($this->View->getRequest()->withParsedBody([
  6648. 'month_year' => ['month' => date('m')],
  6649. ]));
  6650. $this->Form->create($this->article);
  6651. $result = $this->Form->control('month_year', [
  6652. 'label' => false,
  6653. 'type' => 'date',
  6654. 'minYear' => 2006,
  6655. 'maxYear' => 2008,
  6656. ]);
  6657. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6658. $this->assertNotContains('value="2008" selected="selected"', $result);
  6659. }
  6660. /**
  6661. * testControlLabelFalse method
  6662. *
  6663. * Test the label option being set to false.
  6664. *
  6665. * @return void
  6666. */
  6667. public function testControlLabelFalse()
  6668. {
  6669. $this->Form->create($this->article);
  6670. $result = $this->Form->control('title', ['label' => false]);
  6671. $expected = [
  6672. 'div' => ['class' => 'input text required'],
  6673. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6674. '/div',
  6675. ];
  6676. $this->assertHtml($expected, $result);
  6677. }
  6678. /**
  6679. * testControlDateMaxYear method
  6680. *
  6681. * Let's say we want to only allow users born from 2006 to 2008 to register
  6682. * This being the first signup page, we still don't have any data.
  6683. *
  6684. * @return void
  6685. */
  6686. public function testControlDateMaxYear()
  6687. {
  6688. $this->Form->create($this->article);
  6689. $result = $this->Form->control('birthday', [
  6690. 'label' => false,
  6691. 'type' => 'date',
  6692. 'minYear' => 2006,
  6693. 'maxYear' => 2008,
  6694. 'default' => true,
  6695. ]);
  6696. $this->assertContains('value="2008" selected="selected"', $result);
  6697. $this->assertContains('value="2006"', $result);
  6698. $this->assertNotContains('value="2005"', $result);
  6699. $this->assertNotContains('value="2009"', $result);
  6700. }
  6701. /**
  6702. * testTextArea method
  6703. *
  6704. * Test generation of a textarea input.
  6705. *
  6706. * @return void
  6707. */
  6708. public function testTextArea()
  6709. {
  6710. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  6711. $result = $this->Form->textarea('field');
  6712. $expected = [
  6713. 'textarea' => ['name' => 'field', 'rows' => 5],
  6714. 'some test data',
  6715. '/textarea',
  6716. ];
  6717. $this->assertHtml($expected, $result);
  6718. $result = $this->Form->textarea('user.bio');
  6719. $expected = [
  6720. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6721. '/textarea',
  6722. ];
  6723. $this->assertHtml($expected, $result);
  6724. $this->View->setRequest($this->View->getRequest()
  6725. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6726. $this->Form->create();
  6727. $result = $this->Form->textarea('field');
  6728. $expected = [
  6729. 'textarea' => ['name' => 'field', 'rows' => 5],
  6730. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6731. '/textarea',
  6732. ];
  6733. $this->assertHtml($expected, $result);
  6734. $this->View->setRequest($this->View->getRequest()->withData(
  6735. 'Model.field',
  6736. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  6737. ));
  6738. $this->Form->create();
  6739. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6740. $expected = [
  6741. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6742. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6743. '/textarea',
  6744. ];
  6745. $this->assertHtml($expected, $result);
  6746. $result = $this->Form->textarea('0.OtherModel.field');
  6747. $expected = [
  6748. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6749. '/textarea',
  6750. ];
  6751. $this->assertHtml($expected, $result);
  6752. }
  6753. /**
  6754. * testTextAreaWithStupidCharacters method
  6755. *
  6756. * Test text area with non-ascii characters.
  6757. *
  6758. * @return void
  6759. */
  6760. public function testTextAreaWithStupidCharacters()
  6761. {
  6762. $result = $this->Form->textarea('Post.content', [
  6763. 'value' => 'GREAT®',
  6764. 'rows' => '15',
  6765. 'cols' => '75',
  6766. ]);
  6767. $expected = [
  6768. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6769. 'GREAT®',
  6770. '/textarea',
  6771. ];
  6772. $this->assertHtml($expected, $result);
  6773. }
  6774. /**
  6775. * testTextAreaMaxLength method
  6776. *
  6777. * Test textareas maxlength read from schema.
  6778. *
  6779. * @return void
  6780. */
  6781. public function testTextAreaMaxLength()
  6782. {
  6783. $this->Form->create([
  6784. 'schema' => [
  6785. 'stuff' => ['type' => 'string', 'length' => 10],
  6786. ],
  6787. ]);
  6788. $result = $this->Form->control('other', ['type' => 'textarea']);
  6789. $expected = [
  6790. 'div' => ['class' => 'input textarea'],
  6791. 'label' => ['for' => 'other'],
  6792. 'Other',
  6793. '/label',
  6794. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6795. '/textarea',
  6796. '/div',
  6797. ];
  6798. $this->assertHtml($expected, $result);
  6799. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6800. $expected = [
  6801. 'div' => ['class' => 'input textarea'],
  6802. 'label' => ['for' => 'stuff'],
  6803. 'Stuff',
  6804. '/label',
  6805. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6806. '/textarea',
  6807. '/div',
  6808. ];
  6809. $this->assertHtml($expected, $result);
  6810. }
  6811. /**
  6812. * testHiddenField method
  6813. *
  6814. * Test generation of a hidden input.
  6815. *
  6816. * @return void
  6817. */
  6818. public function testHiddenField()
  6819. {
  6820. $this->article['errors'] = [
  6821. 'field' => true,
  6822. ];
  6823. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  6824. $this->Form->create($this->article);
  6825. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6826. $expected = [
  6827. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6828. $this->assertHtml($expected, $result);
  6829. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6830. $expected = [
  6831. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value'],
  6832. ];
  6833. $this->assertHtml($expected, $result);
  6834. }
  6835. /**
  6836. * testFileUploadField method
  6837. *
  6838. * Test generation of a file upload input.
  6839. *
  6840. * @return void
  6841. */
  6842. public function testFileUploadField()
  6843. {
  6844. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6845. $result = $this->Form->file('Model.upload');
  6846. $this->assertHtml($expected, $result);
  6847. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  6848. 'name' => '', 'type' => '', 'tmp_name' => '',
  6849. 'error' => 4, 'size' => 0,
  6850. ]));
  6851. $result = $this->Form->file('Model.upload');
  6852. $this->assertHtml($expected, $result);
  6853. $this->View->setRequest(
  6854. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  6855. );
  6856. $result = $this->Form->file('Model.upload');
  6857. $this->assertHtml($expected, $result);
  6858. }
  6859. /**
  6860. * testFileUploadOnOtherModel method
  6861. *
  6862. * Test File upload input on a model not used in create().
  6863. *
  6864. * @return void
  6865. */
  6866. public function testFileUploadOnOtherModel()
  6867. {
  6868. $this->Form->create($this->article, ['type' => 'file']);
  6869. $result = $this->Form->file('ValidateProfile.city');
  6870. $expected = [
  6871. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]'],
  6872. ];
  6873. $this->assertHtml($expected, $result);
  6874. }
  6875. /**
  6876. * testButton method
  6877. *
  6878. * Test generation of a form button.
  6879. *
  6880. * @return void
  6881. */
  6882. public function testButton()
  6883. {
  6884. $result = $this->Form->button('Hi');
  6885. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6886. $this->assertHtml($expected, $result);
  6887. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6888. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6889. $this->assertHtml($expected, $result);
  6890. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6891. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6892. $this->assertHtml($expected, $result);
  6893. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6894. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6895. $this->assertHtml($expected, $result);
  6896. $result = $this->Form->button('No type', ['type' => false]);
  6897. $expected = ['button' => [], 'No type', '/button'];
  6898. $this->assertHtml($expected, $result);
  6899. $result = $this->Form->button('Upload Text', [
  6900. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6901. 'escape' => false,
  6902. ]);
  6903. $this->assertNotRegExp('/\&039/', $result);
  6904. }
  6905. /**
  6906. * testButtonUnlockedByDefault method
  6907. *
  6908. * Test that button() makes unlocked fields by default.
  6909. *
  6910. * @return void
  6911. */
  6912. public function testButtonUnlockedByDefault()
  6913. {
  6914. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'secured'));
  6915. $this->Form->button('Save', ['name' => 'save']);
  6916. $this->Form->button('Clear');
  6917. $result = $this->Form->unlockField();
  6918. $this->assertEquals(['save'], $result);
  6919. }
  6920. /**
  6921. * Test generation of a form button with confirm message.
  6922. *
  6923. * @return void
  6924. */
  6925. public function testButtonWithConfirm()
  6926. {
  6927. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6928. $expected = ['button' => ['type' => 'submit', 'onclick' => 'if (confirm(&quot;Confirm me!&quot;)) { return true; } return false;'], 'Hi', '/button'];
  6929. $this->assertHtml($expected, $result);
  6930. }
  6931. /**
  6932. * testPostButton method
  6933. *
  6934. * @return void
  6935. */
  6936. public function testPostButton()
  6937. {
  6938. $result = $this->Form->postButton('Hi', '/controller/action');
  6939. $expected = [
  6940. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6941. 'div' => ['style' => 'display:none;'],
  6942. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6943. '/div',
  6944. 'button' => ['type' => 'submit'],
  6945. 'Hi',
  6946. '/button',
  6947. '/form',
  6948. ];
  6949. $this->assertHtml($expected, $result);
  6950. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6951. $this->assertContains('<input type="hidden" name="extra" value="value"', $result);
  6952. }
  6953. /**
  6954. * testPostButtonMethodType method
  6955. *
  6956. * @return void
  6957. */
  6958. public function testPostButtonMethodType()
  6959. {
  6960. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6961. $expected = [
  6962. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6963. 'div' => ['style' => 'display:none;'],
  6964. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6965. '/div',
  6966. 'button' => ['type' => 'submit'],
  6967. 'Hi',
  6968. '/button',
  6969. '/form',
  6970. ];
  6971. $this->assertHtml($expected, $result);
  6972. }
  6973. /**
  6974. * testPostButtonFormOptions method
  6975. *
  6976. * @return void
  6977. */
  6978. public function testPostButtonFormOptions()
  6979. {
  6980. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6981. $expected = [
  6982. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6983. 'div' => ['style' => 'display:none;'],
  6984. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6985. '/div',
  6986. 'button' => ['type' => 'submit'],
  6987. 'Hi',
  6988. '/button',
  6989. '/form',
  6990. ];
  6991. $this->assertHtml($expected, $result);
  6992. }
  6993. /**
  6994. * testPostButtonNestedData method
  6995. *
  6996. * Test using postButton with N dimensional data.
  6997. *
  6998. * @return void
  6999. */
  7000. public function testPostButtonNestedData()
  7001. {
  7002. $data = [
  7003. 'one' => [
  7004. 'two' => [
  7005. 3, 4, 5,
  7006. ],
  7007. ],
  7008. ];
  7009. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  7010. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7011. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7012. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7013. }
  7014. /**
  7015. * testSecurePostButton method
  7016. *
  7017. * Test that postButton adds _Token fields.
  7018. *
  7019. * @return void
  7020. */
  7021. public function testSecurePostButton()
  7022. {
  7023. $this->View->setRequest($this->View->getRequest()
  7024. ->withParam('_csrfToken', 'testkey')
  7025. ->withParam('_Token.unlockedFields', []));
  7026. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  7027. $tokenDebug = urlencode(json_encode([
  7028. '/posts/delete/1',
  7029. [],
  7030. [],
  7031. ]));
  7032. $expected = [
  7033. 'form' => [
  7034. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  7035. ],
  7036. ['div' => ['style' => 'display:none;']],
  7037. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7038. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7039. '/div',
  7040. 'button' => ['type' => 'submit'],
  7041. 'Delete',
  7042. '/button',
  7043. ['div' => ['style' => 'display:none;']],
  7044. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7045. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7046. ['input' => [
  7047. 'type' => 'hidden',
  7048. 'name' => '_Token[debug]',
  7049. 'value' => $tokenDebug,
  7050. 'autocomplete' => 'off',
  7051. ]],
  7052. '/div',
  7053. '/form',
  7054. ];
  7055. $this->assertHtml($expected, $result);
  7056. }
  7057. /**
  7058. * testPostLink method
  7059. *
  7060. * @return void
  7061. */
  7062. public function testPostLink()
  7063. {
  7064. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7065. $expected = [
  7066. 'form' => [
  7067. 'method' => 'post', 'action' => '/posts/delete/1',
  7068. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7069. ],
  7070. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7071. '/form',
  7072. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7073. 'Delete',
  7074. '/a',
  7075. ];
  7076. $this->assertHtml($expected, $result);
  7077. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  7078. $expected = [
  7079. 'form' => [
  7080. 'method' => 'post', 'action' => '/posts/delete/1',
  7081. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7082. ],
  7083. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  7084. '/form',
  7085. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7086. 'Delete',
  7087. '/a',
  7088. ];
  7089. $this->assertHtml($expected, $result);
  7090. $result = $this->Form->postLink(
  7091. 'Delete',
  7092. '/posts/delete/1',
  7093. ['target' => '_blank', 'class' => 'btn btn-danger']
  7094. );
  7095. $expected = [
  7096. 'form' => [
  7097. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  7098. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7099. ],
  7100. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7101. '/form',
  7102. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7103. 'Delete',
  7104. '/a',
  7105. ];
  7106. $this->assertHtml($expected, $result);
  7107. }
  7108. /**
  7109. * testPostLinkWithConfirm method
  7110. *
  7111. * Test the confirm option for postLink().
  7112. *
  7113. * @return void
  7114. */
  7115. public function testPostLinkWithConfirm()
  7116. {
  7117. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  7118. $expected = [
  7119. 'form' => [
  7120. 'method' => 'post', 'action' => '/posts/delete/1',
  7121. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7122. ],
  7123. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7124. '/form',
  7125. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  7126. 'Delete',
  7127. '/a',
  7128. ];
  7129. $this->assertHtml($expected, $result);
  7130. $result = $this->Form->postLink(
  7131. 'Delete',
  7132. '/posts/delete/1',
  7133. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  7134. );
  7135. $expected = [
  7136. 'form' => [
  7137. 'method' => 'post', 'action' => '/posts/delete/1',
  7138. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7139. ],
  7140. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7141. '/form',
  7142. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  7143. 'Delete',
  7144. '/a',
  7145. ];
  7146. $this->assertHtml($expected, $result);
  7147. $this->Form->setTemplates(['confirmJs' => 'if (confirm({{confirmMessage}})) { $(\'form[name="{{formName}}"]\').submit();};']);
  7148. $result = $this->Form->postLink(
  7149. 'Delete',
  7150. '/posts/delete/1',
  7151. ['escape' => false, 'confirm' => 'Confirm this deletion?']
  7152. );
  7153. $expected = [
  7154. 'form' => [
  7155. 'method' => 'post', 'action' => '/posts/delete/1',
  7156. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7157. ],
  7158. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7159. '/form',
  7160. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\("Confirm this deletion\?"\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/'],
  7161. 'Delete',
  7162. '/a',
  7163. ];
  7164. $this->assertHtml($expected, $result);
  7165. }
  7166. /**
  7167. * testPostLinkWithQuery method
  7168. *
  7169. * Test postLink() with query string args.
  7170. *
  7171. * @return void
  7172. */
  7173. public function testPostLinkWithQuery()
  7174. {
  7175. $result = $this->Form->postLink(
  7176. 'Delete',
  7177. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  7178. );
  7179. $expected = [
  7180. 'form' => [
  7181. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  7182. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7183. ],
  7184. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7185. '/form',
  7186. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7187. 'Delete',
  7188. '/a',
  7189. ];
  7190. $this->assertHtml($expected, $result);
  7191. }
  7192. /**
  7193. * testPostLinkWithData method
  7194. *
  7195. * Test postLink with additional data.
  7196. *
  7197. * @return void
  7198. */
  7199. public function testPostLinkWithData()
  7200. {
  7201. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  7202. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  7203. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  7204. $this->Form->create($entity);
  7205. $this->Form->end();
  7206. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  7207. $this->assertContains(
  7208. '<input type="hidden" name="name" value="show"',
  7209. $result,
  7210. 'should not contain entity data.'
  7211. );
  7212. }
  7213. /**
  7214. * testPostLinkSecurityHash method
  7215. *
  7216. * Test that security hashes for postLink include the url.
  7217. *
  7218. * @return void
  7219. */
  7220. public function testPostLinkSecurityHash()
  7221. {
  7222. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7223. $hash .= '%3Aid';
  7224. $this->View->setRequest($this->View->getRequest()->withParam('_Token.key', 'test'));
  7225. $result = $this->Form->postLink(
  7226. 'Delete',
  7227. '/posts/delete/1',
  7228. ['data' => ['id' => 1]]
  7229. );
  7230. $tokenDebug = urlencode(json_encode([
  7231. '/posts/delete/1',
  7232. [
  7233. 'id' => 1,
  7234. ],
  7235. [],
  7236. ]));
  7237. $expected = [
  7238. 'form' => [
  7239. 'method' => 'post', 'action' => '/posts/delete/1',
  7240. 'name', 'style' => 'display:none;',
  7241. ],
  7242. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7243. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7244. 'div' => ['style' => 'display:none;'],
  7245. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7246. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7247. ['input' => [
  7248. 'type' => 'hidden',
  7249. 'name' => '_Token[debug]',
  7250. 'value' => $tokenDebug,
  7251. 'autocomplete' => 'off',
  7252. ]],
  7253. '/div',
  7254. '/form',
  7255. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7256. 'Delete',
  7257. '/a',
  7258. ];
  7259. $this->assertHtml($expected, $result);
  7260. }
  7261. /**
  7262. * testPostLinkSecurityHashBlockMode method
  7263. *
  7264. * Test that postLink doesn't modify the fields in the containing form.
  7265. *
  7266. * postLink() calls inside open forms should not modify the field list
  7267. * for the form.
  7268. *
  7269. * @return void
  7270. */
  7271. public function testPostLinkSecurityHashBlockMode()
  7272. {
  7273. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  7274. $hash .= '%3A';
  7275. $this->View->setRequest($this->View->getRequest()->withParam('_Token.key', 'test'));
  7276. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  7277. $this->Form->control('title');
  7278. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7279. $result = $this->View->fetch('postLink');
  7280. $this->assertEquals(['title'], $this->Form->fields);
  7281. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  7282. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  7283. }
  7284. /**
  7285. * testPostLinkSecurityHashNoDebugMode method
  7286. *
  7287. * Test that security does not include debug token if debug is false.
  7288. *
  7289. * @return void
  7290. */
  7291. public function testPostLinkSecurityHashNoDebugMode()
  7292. {
  7293. Configure::write('debug', false);
  7294. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7295. $hash .= '%3Aid';
  7296. $this->View->setRequest($this->View->getRequest()
  7297. ->withParam('_Token.key', 'test'));
  7298. $result = $this->Form->postLink(
  7299. 'Delete',
  7300. '/posts/delete/1',
  7301. ['data' => ['id' => 1]]
  7302. );
  7303. $expected = [
  7304. 'form' => [
  7305. 'method' => 'post', 'action' => '/posts/delete/1',
  7306. 'name', 'style' => 'display:none;',
  7307. ],
  7308. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7309. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7310. 'div' => ['style' => 'display:none;'],
  7311. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7312. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7313. '/div',
  7314. '/form',
  7315. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7316. 'Delete',
  7317. '/a',
  7318. ];
  7319. $this->assertHtml($expected, $result);
  7320. }
  7321. /**
  7322. * testPostLinkNestedData method
  7323. *
  7324. * Test using postLink with N dimensional data.
  7325. *
  7326. * @return void
  7327. */
  7328. public function testPostLinkNestedData()
  7329. {
  7330. $data = [
  7331. 'one' => [
  7332. 'two' => [
  7333. 3, 4, 5,
  7334. ],
  7335. ],
  7336. ];
  7337. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7338. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7339. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7340. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7341. }
  7342. /**
  7343. * testPostLinkAfterGetForm method
  7344. *
  7345. * Test creating postLinks after a GET form.
  7346. *
  7347. * @return void
  7348. */
  7349. public function testPostLinkAfterGetForm()
  7350. {
  7351. $this->View->setRequest($this->View->getRequest()
  7352. ->withParam('_csrfToken', 'testkey')
  7353. ->withParam('_Token', 'val'));
  7354. $this->Form->create($this->article, ['type' => 'get']);
  7355. $this->Form->end();
  7356. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7357. $tokenDebug = urlencode(json_encode([
  7358. '/posts/delete/1',
  7359. [],
  7360. [],
  7361. ]));
  7362. $expected = [
  7363. 'form' => [
  7364. 'method' => 'post', 'action' => '/posts/delete/1',
  7365. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7366. ],
  7367. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7368. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7369. 'div' => ['style' => 'display:none;'],
  7370. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7371. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7372. ['input' => [
  7373. 'type' => 'hidden', 'name' => '_Token[debug]',
  7374. 'value' => $tokenDebug,
  7375. 'autocomplete' => 'off',
  7376. ]],
  7377. '/div',
  7378. '/form',
  7379. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7380. 'Delete',
  7381. '/a',
  7382. ];
  7383. $this->assertHtml($expected, $result);
  7384. }
  7385. /**
  7386. * testPostLinkFormBuffer method
  7387. *
  7388. * Test that postLink adds form tags to view block.
  7389. *
  7390. * @return void
  7391. */
  7392. public function testPostLinkFormBuffer()
  7393. {
  7394. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7395. $expected = [
  7396. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7397. 'Delete',
  7398. '/a',
  7399. ];
  7400. $this->assertHtml($expected, $result);
  7401. $result = $this->View->fetch('postLink');
  7402. $expected = [
  7403. 'form' => [
  7404. 'method' => 'post', 'action' => '/posts/delete/1',
  7405. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7406. ],
  7407. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7408. '/form',
  7409. ];
  7410. $this->assertHtml($expected, $result);
  7411. $result = $this->Form->postLink(
  7412. 'Delete',
  7413. '/posts/delete/2',
  7414. ['block' => true, 'method' => 'DELETE']
  7415. );
  7416. $expected = [
  7417. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7418. 'Delete',
  7419. '/a',
  7420. ];
  7421. $this->assertHtml($expected, $result);
  7422. $result = $this->View->fetch('postLink');
  7423. $expected = [
  7424. 'form' => [
  7425. 'method' => 'post', 'action' => '/posts/delete/1',
  7426. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7427. ],
  7428. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7429. '/form',
  7430. [
  7431. 'form' => [
  7432. 'method' => 'post', 'action' => '/posts/delete/2',
  7433. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7434. ],
  7435. ],
  7436. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7437. '/form',
  7438. ];
  7439. $this->assertHtml($expected, $result);
  7440. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7441. $expected = [
  7442. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7443. 'Delete',
  7444. '/a',
  7445. ];
  7446. $this->assertHtml($expected, $result);
  7447. $result = $this->View->fetch('foobar');
  7448. $expected = [
  7449. 'form' => [
  7450. 'method' => 'post', 'action' => '/posts/delete/1',
  7451. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7452. ],
  7453. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7454. '/form',
  7455. ];
  7456. $this->assertHtml($expected, $result);
  7457. }
  7458. /**
  7459. * testSubmitButton method
  7460. *
  7461. * @return void
  7462. */
  7463. public function testSubmitButton()
  7464. {
  7465. $result = $this->Form->submit('');
  7466. $expected = [
  7467. 'div' => ['class' => 'submit'],
  7468. 'input' => ['type' => 'submit', 'value' => ''],
  7469. '/div',
  7470. ];
  7471. $this->assertHtml($expected, $result);
  7472. $result = $this->Form->submit('Test Submit');
  7473. $expected = [
  7474. 'div' => ['class' => 'submit'],
  7475. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7476. '/div',
  7477. ];
  7478. $this->assertHtml($expected, $result);
  7479. $result = $this->Form->submit('Next >');
  7480. $expected = [
  7481. 'div' => ['class' => 'submit'],
  7482. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7483. '/div',
  7484. ];
  7485. $this->assertHtml($expected, $result);
  7486. $result = $this->Form->submit('Next >', ['escape' => false]);
  7487. $expected = [
  7488. 'div' => ['class' => 'submit'],
  7489. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7490. '/div',
  7491. ];
  7492. $this->assertHtml($expected, $result);
  7493. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7494. $expected = [
  7495. 'div' => ['class' => 'submit'],
  7496. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7497. '/div',
  7498. ];
  7499. $this->assertHtml($expected, $result);
  7500. }
  7501. /**
  7502. * testSubmitImage method
  7503. *
  7504. * Test image submit types.
  7505. *
  7506. * @return void
  7507. */
  7508. public function testSubmitImage()
  7509. {
  7510. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7511. $expected = [
  7512. 'div' => ['class' => 'submit'],
  7513. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7514. '/div',
  7515. ];
  7516. $this->assertHtml($expected, $result);
  7517. $result = $this->Form->submit('/relative/cake.power.gif');
  7518. $expected = [
  7519. 'div' => ['class' => 'submit'],
  7520. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7521. '/div',
  7522. ];
  7523. $this->assertHtml($expected, $result);
  7524. $result = $this->Form->submit('cake.power.gif');
  7525. $expected = [
  7526. 'div' => ['class' => 'submit'],
  7527. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7528. '/div',
  7529. ];
  7530. $this->assertHtml($expected, $result);
  7531. $result = $this->Form->submit('Not.an.image');
  7532. $expected = [
  7533. 'div' => ['class' => 'submit'],
  7534. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7535. '/div',
  7536. ];
  7537. $this->assertHtml($expected, $result);
  7538. }
  7539. /**
  7540. * testSubmitUnlockedByDefault method
  7541. *
  7542. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7543. * be submitted at a time.
  7544. *
  7545. * @return void
  7546. */
  7547. public function testSubmitUnlockedByDefault()
  7548. {
  7549. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'secured'));
  7550. $this->Form->submit('Go go');
  7551. $this->Form->submit('Save', ['name' => 'save']);
  7552. $result = $this->Form->unlockField();
  7553. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7554. }
  7555. /**
  7556. * testSubmitImageTimestamp method
  7557. *
  7558. * Test submit image with timestamps.
  7559. *
  7560. * @return void
  7561. */
  7562. public function testSubmitImageTimestamp()
  7563. {
  7564. Configure::write('Asset.timestamp', 'force');
  7565. $result = $this->Form->submit('cake.power.gif');
  7566. $expected = [
  7567. 'div' => ['class' => 'submit'],
  7568. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7569. '/div',
  7570. ];
  7571. $this->assertHtml($expected, $result);
  7572. }
  7573. /**
  7574. * testDateTimeWithGetForms method
  7575. *
  7576. * Test that datetime() works with GET style forms.
  7577. *
  7578. * @return void
  7579. */
  7580. public function testDateTimeWithGetForms()
  7581. {
  7582. extract($this->dateRegex);
  7583. $this->Form->create($this->article, ['type' => 'get']);
  7584. $result = $this->Form->datetime('created');
  7585. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7586. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7587. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7588. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7589. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7590. }
  7591. /**
  7592. * testForMagicControlNonExistingNorValidated method
  7593. *
  7594. * @return void
  7595. */
  7596. public function testForMagicControlNonExistingNorValidated()
  7597. {
  7598. $this->Form->create($this->article);
  7599. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7600. $result = $this->Form->control('non_existing_nor_validated');
  7601. $expected = [
  7602. 'label' => ['for' => 'non-existing-nor-validated'],
  7603. 'Non Existing Nor Validated',
  7604. '/label',
  7605. 'input' => [
  7606. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7607. 'id' => 'non-existing-nor-validated',
  7608. ],
  7609. ];
  7610. $this->assertHtml($expected, $result);
  7611. $result = $this->Form->control('non_existing_nor_validated', [
  7612. 'val' => 'my value',
  7613. ]);
  7614. $expected = [
  7615. 'label' => ['for' => 'non-existing-nor-validated'],
  7616. 'Non Existing Nor Validated',
  7617. '/label',
  7618. 'input' => [
  7619. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7620. 'value' => 'my value', 'id' => 'non-existing-nor-validated',
  7621. ],
  7622. ];
  7623. $this->assertHtml($expected, $result);
  7624. $this->View->setRequest(
  7625. $this->View->getRequest()->withData('non_existing_nor_validated', 'CakePHP magic')
  7626. );
  7627. $this->Form->create($this->article);
  7628. $result = $this->Form->control('non_existing_nor_validated');
  7629. $expected = [
  7630. 'label' => ['for' => 'non-existing-nor-validated'],
  7631. 'Non Existing Nor Validated',
  7632. '/label',
  7633. 'input' => [
  7634. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7635. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated',
  7636. ],
  7637. ];
  7638. $this->assertHtml($expected, $result);
  7639. }
  7640. /**
  7641. * testFormMagicControlLabel method
  7642. *
  7643. * @return void
  7644. */
  7645. public function testFormMagicControlLabel()
  7646. {
  7647. $this->getTableLocator()->get('Contacts', [
  7648. 'className' => __NAMESPACE__ . '\ContactsTable',
  7649. ]);
  7650. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7651. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7652. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7653. $expected = [
  7654. 'label' => ['for' => 'contacts-name'],
  7655. 'My label',
  7656. '/label',
  7657. 'input' => [
  7658. 'type' => 'text', 'name' => 'Contacts[name]',
  7659. 'id' => 'contacts-name', 'maxlength' => '255',
  7660. ],
  7661. ];
  7662. $this->assertHtml($expected, $result);
  7663. $result = $this->Form->control('name', [
  7664. 'label' => ['class' => 'mandatory'],
  7665. ]);
  7666. $expected = [
  7667. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7668. 'Name',
  7669. '/label',
  7670. 'input' => [
  7671. 'type' => 'text', 'name' => 'name',
  7672. 'id' => 'name', 'maxlength' => '255',
  7673. ],
  7674. ];
  7675. $this->assertHtml($expected, $result);
  7676. $result = $this->Form->control('name', [
  7677. 'div' => false,
  7678. 'label' => ['class' => 'mandatory', 'text' => 'My label'],
  7679. ]);
  7680. $expected = [
  7681. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7682. 'My label',
  7683. '/label',
  7684. 'input' => [
  7685. 'type' => 'text', 'name' => 'name',
  7686. 'id' => 'name', 'maxlength' => '255',
  7687. ],
  7688. ];
  7689. $this->assertHtml($expected, $result);
  7690. $result = $this->Form->control('Contact.name', [
  7691. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id'],
  7692. ]);
  7693. $expected = [
  7694. 'label' => ['for' => 'my_id'],
  7695. 'Name',
  7696. '/label',
  7697. 'input' => [
  7698. 'type' => 'text', 'name' => 'Contact[name]',
  7699. 'id' => 'my_id', 'maxlength' => '255',
  7700. ],
  7701. ];
  7702. $this->assertHtml($expected, $result);
  7703. $result = $this->Form->control('1.id');
  7704. $expected = ['input' => [
  7705. 'type' => 'hidden', 'name' => '1[id]',
  7706. 'id' => '1-id',
  7707. ]];
  7708. $this->assertHtml($expected, $result);
  7709. $result = $this->Form->control('1.name');
  7710. $expected = [
  7711. 'label' => ['for' => '1-name'],
  7712. 'Name',
  7713. '/label',
  7714. 'input' => [
  7715. 'type' => 'text', 'name' => '1[name]',
  7716. 'id' => '1-name', 'maxlength' => '255',
  7717. ],
  7718. ];
  7719. $this->assertHtml($expected, $result);
  7720. }
  7721. /**
  7722. * testFormEnd method
  7723. *
  7724. * @return void
  7725. */
  7726. public function testFormEnd()
  7727. {
  7728. $this->assertEquals('</form>', $this->Form->end());
  7729. }
  7730. /**
  7731. * testMultiRecordForm method
  7732. *
  7733. * Test the generation of fields for a multi record form.
  7734. *
  7735. * @return void
  7736. */
  7737. public function testMultiRecordForm()
  7738. {
  7739. $this->loadFixtures('Articles', 'Comments');
  7740. $articles = $this->getTableLocator()->get('Articles');
  7741. $articles->hasMany('Comments');
  7742. $comment = new Entity(['comment' => 'Value']);
  7743. $article = new Article(['comments' => [$comment]]);
  7744. $this->Form->create([$article]);
  7745. $result = $this->Form->control('0.comments.1.comment');
  7746. //@codingStandardsIgnoreStart
  7747. $expected = [
  7748. 'div' => ['class' => 'input textarea'],
  7749. 'label' => ['for' => '0-comments-1-comment'],
  7750. 'Comment',
  7751. '/label',
  7752. 'textarea' => [
  7753. 'name',
  7754. 'id' => '0-comments-1-comment',
  7755. 'rows' => 5
  7756. ],
  7757. '/textarea',
  7758. '/div'
  7759. ];
  7760. //@codingStandardsIgnoreEnd
  7761. $this->assertHtml($expected, $result);
  7762. $result = $this->Form->control('0.comments.0.comment');
  7763. //@codingStandardsIgnoreStart
  7764. $expected = [
  7765. 'div' => ['class' => 'input textarea'],
  7766. 'label' => ['for' => '0-comments-0-comment'],
  7767. 'Comment',
  7768. '/label',
  7769. 'textarea' => [
  7770. 'name',
  7771. 'id' => '0-comments-0-comment',
  7772. 'rows' => 5
  7773. ],
  7774. 'Value',
  7775. '/textarea',
  7776. '/div'
  7777. ];
  7778. //@codingStandardsIgnoreEnd
  7779. $this->assertHtml($expected, $result);
  7780. $comment->setError('comment', ['Not valid']);
  7781. $result = $this->Form->control('0.comments.0.comment');
  7782. //@codingStandardsIgnoreStart
  7783. $expected = [
  7784. 'div' => ['class' => 'input textarea error'],
  7785. 'label' => ['for' => '0-comments-0-comment'],
  7786. 'Comment',
  7787. '/label',
  7788. 'textarea' => [
  7789. 'name',
  7790. 'class' => 'form-error',
  7791. 'id' => '0-comments-0-comment',
  7792. 'rows' => 5
  7793. ],
  7794. 'Value',
  7795. '/textarea',
  7796. ['div' => ['class' => 'error-message']],
  7797. 'Not valid',
  7798. '/div',
  7799. '/div'
  7800. ];
  7801. //@codingStandardsIgnoreEnd
  7802. $this->assertHtml($expected, $result);
  7803. $this->getTableLocator()->get('Comments')
  7804. ->getValidator('default')
  7805. ->allowEmpty('comment', false);
  7806. $result = $this->Form->control('0.comments.1.comment');
  7807. //@codingStandardsIgnoreStart
  7808. $expected = [
  7809. 'div' => ['class' => 'input textarea required'],
  7810. 'label' => ['for' => '0-comments-1-comment'],
  7811. 'Comment',
  7812. '/label',
  7813. 'textarea' => [
  7814. 'name',
  7815. 'required' => 'required',
  7816. 'id' => '0-comments-1-comment',
  7817. 'rows' => 5,
  7818. ],
  7819. '/textarea',
  7820. '/div'
  7821. ];
  7822. //@codingStandardsIgnoreEnd
  7823. $this->assertHtml($expected, $result);
  7824. }
  7825. /**
  7826. * testHtml5Controls method
  7827. *
  7828. * Test that some html5 inputs + FormHelper::__call() work.
  7829. *
  7830. * @return void
  7831. */
  7832. public function testHtml5Controls()
  7833. {
  7834. $result = $this->Form->email('User.email');
  7835. $expected = [
  7836. 'input' => ['type' => 'email', 'name' => 'User[email]'],
  7837. ];
  7838. $this->assertHtml($expected, $result);
  7839. $result = $this->Form->search('User.query');
  7840. $expected = [
  7841. 'input' => ['type' => 'search', 'name' => 'User[query]'],
  7842. ];
  7843. $this->assertHtml($expected, $result);
  7844. $result = $this->Form->search('User.query', ['value' => 'test']);
  7845. $expected = [
  7846. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test'],
  7847. ];
  7848. $this->assertHtml($expected, $result);
  7849. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7850. $expected = [
  7851. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test'],
  7852. ];
  7853. $this->assertHtml($expected, $result);
  7854. }
  7855. /**
  7856. * testHtml5ControlWithControl method
  7857. *
  7858. * Test accessing html5 inputs through control().
  7859. *
  7860. * @return void
  7861. */
  7862. public function testHtml5ControlWithControl()
  7863. {
  7864. $this->Form->create();
  7865. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7866. $result = $this->Form->control('website', [
  7867. 'type' => 'url',
  7868. 'val' => 'http://domain.tld',
  7869. 'label' => false,
  7870. ]);
  7871. $expected = [
  7872. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld'],
  7873. ];
  7874. $this->assertHtml($expected, $result);
  7875. }
  7876. /**
  7877. * testHtml5ControlException method
  7878. *
  7879. * Test errors when field name is missing.
  7880. *
  7881. * @return void
  7882. */
  7883. public function testHtml5ControlException()
  7884. {
  7885. $this->expectException(\Cake\Core\Exception\Exception::class);
  7886. $this->Form->email();
  7887. }
  7888. /**
  7889. * tests fields that are required use custom validation messages
  7890. *
  7891. * @return void
  7892. */
  7893. public function testHtml5ErrorMessage()
  7894. {
  7895. $this->Form->setConfig('autoSetCustomValidity', true);
  7896. $validator = (new \Cake\Validation\Validator())
  7897. ->requirePresence('email', true, 'Custom error message')
  7898. ->requirePresence('password')
  7899. ->alphaNumeric('password')
  7900. ->notBlank('phone');
  7901. $table = $this->getTableLocator()->get('Contacts', [
  7902. 'className' => __NAMESPACE__ . '\ContactsTable',
  7903. ]);
  7904. $table->setValidator('default', $validator);
  7905. $contact = new Entity();
  7906. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7907. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7908. $result = $this->Form->control('password');
  7909. $expected = [
  7910. 'label' => ['for' => 'password'],
  7911. 'Password',
  7912. '/label',
  7913. 'input' => [
  7914. 'id' => 'password',
  7915. 'name' => 'password',
  7916. 'type' => 'password',
  7917. 'value' => '',
  7918. 'required' => 'required',
  7919. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7920. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.validity.valid) this.setCustomValidity(&#039;This field is required&#039;)',
  7921. ],
  7922. ];
  7923. $this->assertHtml($expected, $result);
  7924. $result = $this->Form->control('phone');
  7925. $expected = [
  7926. 'label' => ['for' => 'phone'],
  7927. 'Phone',
  7928. '/label',
  7929. 'input' => [
  7930. 'id' => 'phone',
  7931. 'name' => 'phone',
  7932. 'type' => 'tel',
  7933. 'value' => '',
  7934. 'maxlength' => 255,
  7935. 'required' => 'required',
  7936. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7937. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.validity.valid) this.setCustomValidity(&#039;This field cannot be left empty&#039;)',
  7938. ],
  7939. ];
  7940. $this->assertHtml($expected, $result);
  7941. $result = $this->Form->control('email');
  7942. $expected = [
  7943. 'label' => ['for' => 'email'],
  7944. 'Email',
  7945. '/label',
  7946. 'input' => [
  7947. 'id' => 'email',
  7948. 'name' => 'email',
  7949. 'type' => 'email',
  7950. 'value' => '',
  7951. 'maxlength' => 255,
  7952. 'required' => 'required',
  7953. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7954. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.validity.valid) this.setCustomValidity(&#039;Custom error message&#039;)',
  7955. ],
  7956. ];
  7957. $this->assertHtml($expected, $result);
  7958. }
  7959. /**
  7960. * tests that custom validation messages are in templateVars
  7961. *
  7962. * @return void
  7963. */
  7964. public function testHtml5ErrorMessageInTemplateVars()
  7965. {
  7966. $validator = (new \Cake\Validation\Validator())
  7967. ->requirePresence('email', true, 'Custom error "message" & entities')
  7968. ->requirePresence('password')
  7969. ->alphaNumeric('password')
  7970. ->notBlank('phone');
  7971. $table = $this->getTableLocator()->get('Contacts', [
  7972. 'className' => __NAMESPACE__ . '\ContactsTable',
  7973. ]);
  7974. $table->setValidator('default', $validator);
  7975. $contact = new Entity();
  7976. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7977. $this->Form->setTemplates([
  7978. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  7979. 'inputContainer' => '{{content}}',
  7980. ]);
  7981. $result = $this->Form->control('password');
  7982. $expected = [
  7983. 'label' => ['for' => 'password'],
  7984. 'Password',
  7985. '/label',
  7986. 'input' => [
  7987. 'id' => 'password',
  7988. 'name' => 'password',
  7989. 'type' => 'password',
  7990. 'value' => '',
  7991. 'required' => 'required',
  7992. 'data-message' => 'This field is required',
  7993. ],
  7994. ];
  7995. $this->assertHtml($expected, $result);
  7996. $result = $this->Form->control('phone');
  7997. $expected = [
  7998. 'label' => ['for' => 'phone'],
  7999. 'Phone',
  8000. '/label',
  8001. 'input' => [
  8002. 'id' => 'phone',
  8003. 'name' => 'phone',
  8004. 'type' => 'tel',
  8005. 'value' => '',
  8006. 'maxlength' => 255,
  8007. 'required' => 'required',
  8008. 'data-message' => 'This field cannot be left empty',
  8009. ],
  8010. ];
  8011. $this->assertHtml($expected, $result);
  8012. $result = $this->Form->control('email', [
  8013. 'templateVars' => [
  8014. 'custom' => 'data-custom="1"',
  8015. ],
  8016. ]);
  8017. $expected = [
  8018. 'label' => ['for' => 'email'],
  8019. 'Email',
  8020. '/label',
  8021. 'input' => [
  8022. 'id' => 'email',
  8023. 'name' => 'email',
  8024. 'type' => 'email',
  8025. 'value' => '',
  8026. 'maxlength' => 255,
  8027. 'required' => 'required',
  8028. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  8029. 'data-custom' => '1',
  8030. ],
  8031. ];
  8032. $this->assertHtml($expected, $result);
  8033. }
  8034. /**
  8035. * testRequiredAttribute method
  8036. *
  8037. * Tests that formhelper sets required attributes.
  8038. *
  8039. * @return void
  8040. */
  8041. public function testRequiredAttribute()
  8042. {
  8043. $this->article['required'] = [
  8044. 'title' => true,
  8045. 'body' => false,
  8046. ];
  8047. $this->Form->create($this->article);
  8048. $result = $this->Form->control('title');
  8049. $expected = [
  8050. 'div' => ['class' => 'input text required'],
  8051. 'label' => ['for' => 'title'],
  8052. 'Title',
  8053. '/label',
  8054. 'input' => [
  8055. 'type' => 'text',
  8056. 'name' => 'title',
  8057. 'id' => 'title',
  8058. 'required' => 'required',
  8059. ],
  8060. '/div',
  8061. ];
  8062. $this->assertHtml($expected, $result);
  8063. $result = $this->Form->control('title', ['required' => false]);
  8064. $this->assertNotContains('required', $result);
  8065. $result = $this->Form->control('body');
  8066. $expected = [
  8067. 'div' => ['class' => 'input text'],
  8068. 'label' => ['for' => 'body'],
  8069. 'Body',
  8070. '/label',
  8071. 'input' => [
  8072. 'type' => 'text',
  8073. 'name' => 'body',
  8074. 'id' => 'body',
  8075. ],
  8076. '/div',
  8077. ];
  8078. $this->assertHtml($expected, $result);
  8079. $result = $this->Form->control('body', ['required' => true]);
  8080. $this->assertContains('required', $result);
  8081. }
  8082. /**
  8083. * testControlsNotNested method
  8084. *
  8085. * Tests that it is possible to put inputs outside of the label.
  8086. *
  8087. * @return void
  8088. */
  8089. public function testControlsNotNested()
  8090. {
  8091. $this->Form->setTemplates([
  8092. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  8093. 'formGroup' => '{{input}}{{label}}',
  8094. ]);
  8095. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  8096. $expected = [
  8097. 'div' => ['class' => 'input checkbox'],
  8098. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  8099. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  8100. 'label' => ['for' => 'foo'],
  8101. 'Foo',
  8102. '/label',
  8103. '/div',
  8104. ];
  8105. $this->assertHtml($expected, $result);
  8106. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  8107. $expected = [
  8108. 'div' => ['class' => 'input checkbox'],
  8109. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  8110. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  8111. '/div',
  8112. ];
  8113. $this->assertHtml($expected, $result);
  8114. $result = $this->Form->control('confirm', [
  8115. 'type' => 'radio',
  8116. 'options' => ['Y' => 'Yes', 'N' => 'No'],
  8117. ]);
  8118. $expected = [
  8119. 'div' => ['class' => 'input radio'],
  8120. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  8121. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  8122. ['label' => ['for' => 'confirm-y']],
  8123. 'Yes',
  8124. '/label',
  8125. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  8126. ['label' => ['for' => 'confirm-n']],
  8127. 'No',
  8128. '/label',
  8129. '<label',
  8130. 'Confirm',
  8131. '/label',
  8132. '/div',
  8133. ];
  8134. $this->assertHtml($expected, $result);
  8135. $result = $this->Form->select('category', ['1', '2'], [
  8136. 'multiple' => 'checkbox',
  8137. 'name' => 'fish',
  8138. ]);
  8139. $expected = [
  8140. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  8141. ['div' => ['class' => 'checkbox']],
  8142. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  8143. ['label' => ['for' => 'fish-0']],
  8144. '1',
  8145. '/label',
  8146. '/div',
  8147. ['div' => ['class' => 'checkbox']],
  8148. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  8149. ['label' => ['for' => 'fish-1']],
  8150. '2',
  8151. '/label',
  8152. '/div',
  8153. ];
  8154. $this->assertHtml($expected, $result);
  8155. }
  8156. /**
  8157. * testControlContainerTemplates method
  8158. *
  8159. * Test that *Container templates are used by input.
  8160. *
  8161. * @return void
  8162. */
  8163. public function testControlContainerTemplates()
  8164. {
  8165. $this->Form->setTemplates([
  8166. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  8167. 'radioContainer' => '<div class="rad">{{content}}</div>',
  8168. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  8169. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  8170. ]);
  8171. $this->article['errors'] = [
  8172. 'Article' => ['published' => 'error message'],
  8173. ];
  8174. $this->Form->create($this->article);
  8175. $result = $this->Form->control('accept', [
  8176. 'type' => 'checkbox',
  8177. ]);
  8178. $expected = [
  8179. 'div' => ['class' => 'check'],
  8180. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  8181. 'label' => ['for' => 'accept'],
  8182. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  8183. 'Accept',
  8184. '/label',
  8185. '/div',
  8186. ];
  8187. $this->assertHtml($expected, $result);
  8188. $result = $this->Form->control('accept', [
  8189. 'type' => 'radio',
  8190. 'options' => ['Y', 'N'],
  8191. ]);
  8192. $this->assertContains('<div class="rad">', $result);
  8193. $result = $this->Form->control('Article.published', [
  8194. 'type' => 'radio',
  8195. 'options' => ['Y', 'N'],
  8196. ]);
  8197. $this->assertContains('<div class="rad err">', $result);
  8198. $result = $this->Form->control('Article.created', [
  8199. 'type' => 'datetime',
  8200. ]);
  8201. $this->assertContains('<div class="dt">', $result);
  8202. }
  8203. /**
  8204. * testFormGroupTemplates method
  8205. *
  8206. * Test that *Container templates are used by input.
  8207. *
  8208. * @return void
  8209. */
  8210. public function testFormGroupTemplates()
  8211. {
  8212. $this->Form->setTemplates([
  8213. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  8214. ]);
  8215. $this->Form->create($this->article);
  8216. $result = $this->Form->control('accept', [
  8217. 'type' => 'radio',
  8218. 'options' => ['Y', 'N'],
  8219. ]);
  8220. $this->assertContains('<div class="radio">', $result);
  8221. }
  8222. /**
  8223. * testResetTemplates method
  8224. *
  8225. * Test resetting templates.
  8226. *
  8227. * @return void
  8228. */
  8229. public function testResetTemplates()
  8230. {
  8231. $this->Form->setTemplates(['input' => '<input/>']);
  8232. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  8233. $this->Form->resetTemplates();
  8234. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  8235. }
  8236. /**
  8237. * testContext method
  8238. *
  8239. * Test the context method.
  8240. *
  8241. * @return void
  8242. */
  8243. public function testContext()
  8244. {
  8245. $result = $this->Form->context();
  8246. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  8247. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  8248. $this->assertSame($mock, $this->Form->context($mock));
  8249. $this->assertSame($mock, $this->Form->context());
  8250. }
  8251. /**
  8252. * testAutoDomId method
  8253. *
  8254. * @return void
  8255. */
  8256. public function testAutoDomId()
  8257. {
  8258. $result = $this->Form->text('field', ['id' => true]);
  8259. $expected = [
  8260. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  8261. ];
  8262. $this->assertHtml($expected, $result);
  8263. // Ensure id => doesn't cause problem when multiple inputs are generated.
  8264. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  8265. $expected = [
  8266. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  8267. ['label' => ['for' => 'field-0']],
  8268. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  8269. 'option A',
  8270. '/label',
  8271. ['label' => ['for' => 'field-1']],
  8272. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  8273. 'option B',
  8274. '/label',
  8275. ];
  8276. $this->assertHtml($expected, $result);
  8277. $result = $this->Form->select(
  8278. 'multi_field',
  8279. ['first', 'second'],
  8280. ['multiple' => 'checkbox', 'id' => true]
  8281. );
  8282. $expected = [
  8283. 'input' => [
  8284. 'type' => 'hidden', 'name' => 'multi_field', 'value' => '',
  8285. ],
  8286. ['div' => ['class' => 'checkbox']],
  8287. ['label' => ['for' => 'multi-field-0']],
  8288. ['input' => [
  8289. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8290. 'value' => '0', 'id' => 'multi-field-0',
  8291. ]],
  8292. 'first',
  8293. '/label',
  8294. '/div',
  8295. ['div' => ['class' => 'checkbox']],
  8296. ['label' => ['for' => 'multi-field-1']],
  8297. ['input' => [
  8298. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8299. 'value' => '1', 'id' => 'multi-field-1',
  8300. ]],
  8301. 'second',
  8302. '/label',
  8303. '/div',
  8304. ];
  8305. $this->assertHtml($expected, $result);
  8306. }
  8307. /**
  8308. * Test the basic setters and getters for value sources
  8309. *
  8310. * @return void
  8311. */
  8312. public function testFormValueSourcesSettersGetters()
  8313. {
  8314. $this->View->setRequest($this->View->getRequest()
  8315. ->withData('id', '1')
  8316. ->withQueryParams(['id' => '2']));
  8317. $expected = ['context'];
  8318. $result = $this->Form->getValueSources();
  8319. $this->assertEquals($expected, $result);
  8320. $expected = ['query', 'data', 'context'];
  8321. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  8322. $result = $this->Form->getValueSources();
  8323. $this->assertEquals($expected, $result);
  8324. $this->Form->setValueSources(['context']);
  8325. $expected = '1';
  8326. $result = $this->Form->getSourceValue('id');
  8327. $this->assertEquals($expected, $result);
  8328. $this->Form->setValueSources('query');
  8329. $expected = ['query'];
  8330. $result = $this->Form->getValueSources();
  8331. $this->assertEquals($expected, $result);
  8332. $expected = '2';
  8333. $result = $this->Form->getSourceValue('id');
  8334. $this->assertEquals($expected, $result);
  8335. $this->Form->setValueSources(['data']);
  8336. $expected = '1';
  8337. $result = $this->Form->getSourceValue('id');
  8338. $this->assertEquals($expected, $result);
  8339. $this->Form->setValueSources(['query', 'data']);
  8340. $expected = '2';
  8341. $result = $this->Form->getSourceValue('id');
  8342. $this->assertEquals($expected, $result);
  8343. }
  8344. /**
  8345. * Tests the different input rendering values based on sources values switching
  8346. *
  8347. * @return void
  8348. */
  8349. public function testFormValueSourcesSingleSwitchRendering()
  8350. {
  8351. $this->loadFixtures('Articles');
  8352. $articles = $this->getTableLocator()->get('Articles');
  8353. $article = new Article();
  8354. $articles->patchEntity($article, ['id' => '3']);
  8355. $this->Form->create($article);
  8356. $this->Form->setValueSources(['context']);
  8357. $result = $this->Form->control('id');
  8358. $expected = [
  8359. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8360. ];
  8361. $this->assertHtml($expected, $result);
  8362. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  8363. $this->Form->setValueSources(['query']);
  8364. $result = $this->Form->control('id');
  8365. $expected = [
  8366. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8367. ];
  8368. $this->assertHtml($expected, $result);
  8369. $this->View->setRequest($this->View->getRequest()
  8370. ->withQueryParams(['id' => '5a'])
  8371. ->withData('id', '5b'));
  8372. $this->Form->setValueSources(['context']);
  8373. $this->Form->create($article);
  8374. $result = $this->Form->control('id');
  8375. $expected = [
  8376. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8377. ];
  8378. $this->assertHtml($expected, $result);
  8379. $this->Form->setValueSources(['data']);
  8380. $this->Form->create($article);
  8381. $result = $this->Form->control('id');
  8382. $expected = [
  8383. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8384. ];
  8385. $this->assertHtml($expected, $result);
  8386. $this->Form->setValueSources(['query']);
  8387. $result = $this->Form->control('id');
  8388. $expected = [
  8389. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  8390. ];
  8391. $this->assertHtml($expected, $result);
  8392. }
  8393. /**
  8394. * Tests the different input rendering values based on sources values switching while supplying
  8395. * an entity (base context) and multiple sources (such as data, query)
  8396. *
  8397. * @return void
  8398. */
  8399. public function testFormValueSourcesListSwitchRendering()
  8400. {
  8401. $this->loadFixtures('Articles');
  8402. $articles = $this->getTableLocator()->get('Articles');
  8403. $article = new Article();
  8404. $articles->patchEntity($article, ['id' => '3']);
  8405. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  8406. $this->Form->create($article);
  8407. $this->Form->setValueSources(['context', 'query']);
  8408. $result = $this->Form->control('id');
  8409. $expected = [
  8410. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8411. ];
  8412. $this->assertHtml($expected, $result);
  8413. $this->Form->setValueSources(['query', 'context']);
  8414. $result = $this->Form->control('id');
  8415. $expected = [
  8416. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8417. ];
  8418. $this->assertHtml($expected, $result);
  8419. $this->Form->setValueSources(['data', 'query', 'context']);
  8420. $result = $this->Form->control('id');
  8421. $expected = [
  8422. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8423. ];
  8424. $this->assertHtml($expected, $result);
  8425. $this->View->setRequest($this->View->getRequest()
  8426. ->withData('id', '8')
  8427. ->withQueryParams(['id' => '9']));
  8428. $this->Form->setValueSources(['data', 'query', 'context']);
  8429. $result = $this->Form->control('id');
  8430. $expected = [
  8431. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  8432. ];
  8433. $this->assertHtml($expected, $result);
  8434. }
  8435. /**
  8436. * Test the different form input renderings based on values sources switchings through form options
  8437. *
  8438. * @return void
  8439. */
  8440. public function testFormValueSourcesSwitchViaOptionsRendering()
  8441. {
  8442. $this->loadFixtures('Articles');
  8443. $articles = $this->getTableLocator()->get('Articles');
  8444. $article = new Article();
  8445. $articles->patchEntity($article, ['id' => '3']);
  8446. $this->View->setRequest(
  8447. $this->View->getRequest()->withData('id', 4)->withQueryParams(['id' => 5])
  8448. );
  8449. $this->Form->create($article, ['valueSources' => 'query']);
  8450. $result = $this->Form->control('id');
  8451. $expected = [
  8452. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8453. ];
  8454. $this->assertHtml($expected, $result);
  8455. $result = $this->Form->getSourceValue('id');
  8456. $this->assertEquals('5', $result);
  8457. $this->Form->setValueSources(['context']);
  8458. $this->Form->create($article, ['valueSources' => 'query']);
  8459. $result = $this->Form->control('id');
  8460. $expected = [
  8461. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8462. ];
  8463. $this->assertHtml($expected, $result);
  8464. $result = $this->Form->getSourceValue('id');
  8465. $this->assertEquals('5', $result);
  8466. $this->Form->setValueSources(['query']);
  8467. $this->Form->create($article, ['valueSources' => 'data']);
  8468. $result = $this->Form->control('id');
  8469. $expected = [
  8470. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8471. ];
  8472. $this->assertHtml($expected, $result);
  8473. $result = $this->Form->getSourceValue('id');
  8474. $this->assertEquals('4', $result);
  8475. $this->Form->setValueSources(['query']);
  8476. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8477. $result = $this->Form->control('id');
  8478. $expected = [
  8479. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8480. ];
  8481. $this->assertHtml($expected, $result);
  8482. $result = $this->Form->getSourceValue('id');
  8483. $this->assertEquals('4', $result);
  8484. }
  8485. /**
  8486. * Test the different form input renderings based on values sources switchings through form options
  8487. *
  8488. * @return void
  8489. */
  8490. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8491. {
  8492. $this->loadFixtures('Articles');
  8493. $articles = $this->getTableLocator()->get('Articles');
  8494. $article = new Article();
  8495. $articles->patchEntity($article, ['id' => '3']);
  8496. $this->View->setRequest(
  8497. $this->View->getRequest()->withData('id', 10)->withQueryParams(['id' => 11])
  8498. );
  8499. $this->Form->setValueSources(['context'])
  8500. ->create($article, ['valueSources' => ['query', 'data']]);
  8501. $result = $this->Form->control('id');
  8502. $expected = [
  8503. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8504. ];
  8505. $this->assertHtml($expected, $result);
  8506. $result = $this->Form->getSourceValue('id');
  8507. $this->assertEquals('11', $result);
  8508. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  8509. $this->Form->setValueSources(['context'])
  8510. ->create($article, ['valueSources' => ['query', 'data']]);
  8511. $result = $this->Form->control('id');
  8512. $expected = [
  8513. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8514. ];
  8515. $this->assertHtml($expected, $result);
  8516. $result = $this->Form->getSourceValue('id');
  8517. $this->assertEquals('10', $result);
  8518. }
  8519. /**
  8520. * Test the different form values sources resetting through From::end();
  8521. *
  8522. * @return void
  8523. */
  8524. public function testFormValueSourcesResetViaEnd()
  8525. {
  8526. $expected = ['context'];
  8527. $result = $this->Form->getValueSources();
  8528. $this->assertEquals($expected, $result);
  8529. $expected = ['query', 'context', 'data'];
  8530. $this->Form->setValueSources(['query', 'context', 'data']);
  8531. $result = $this->Form->getValueSources();
  8532. $this->assertEquals($expected, $result);
  8533. $this->Form->create();
  8534. $result = $this->Form->getValueSources();
  8535. $this->assertEquals($expected, $result);
  8536. $this->Form->end();
  8537. $result = $this->Form->getValueSources();
  8538. $this->assertEquals(['context'], $result);
  8539. }
  8540. /**
  8541. * Test sources values defaults handling
  8542. *
  8543. * @return void
  8544. */
  8545. public function testFormValueSourcesDefaults()
  8546. {
  8547. $this->View->setRequest(
  8548. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  8549. );
  8550. $this->Form->create();
  8551. $result = $this->Form->password('password');
  8552. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8553. $this->assertHtml($expected, $result);
  8554. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8555. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8556. $this->assertHtml($expected, $result);
  8557. $this->Form->setValueSources('query');
  8558. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8559. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8560. $this->assertHtml($expected, $result);
  8561. $this->Form->setValueSources('data');
  8562. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8563. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8564. $this->assertHtml($expected, $result);
  8565. }
  8566. /**
  8567. * Test sources values schema defaults handling
  8568. *
  8569. * @return void
  8570. */
  8571. public function testSourcesValueDoesntExistPassThrough()
  8572. {
  8573. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  8574. $articles = $this->getTableLocator()->get('Articles');
  8575. $entity = $articles->newEntity();
  8576. $this->Form->create($entity);
  8577. $this->Form->setValueSources(['query', 'context']);
  8578. $result = $this->Form->getSourceValue('category');
  8579. $this->assertEquals('sesame-cookies', $result);
  8580. $this->Form->setValueSources(['context', 'query']);
  8581. $result = $this->Form->getSourceValue('category');
  8582. $this->assertEquals('sesame-cookies', $result);
  8583. }
  8584. /**
  8585. * testNestedLabelInput method
  8586. *
  8587. * Test the `nestedInput` parameter
  8588. *
  8589. * @group deprecated
  8590. * @return void
  8591. */
  8592. public function testNestedLabelInput()
  8593. {
  8594. $this->deprecated(function () {
  8595. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8596. $expected = [
  8597. 'div' => ['class' => 'input text'],
  8598. 'label' => ['for' => 'foo'],
  8599. ['input' => [
  8600. 'type' => 'text',
  8601. 'name' => 'foo',
  8602. 'id' => 'foo',
  8603. ]],
  8604. 'Foo',
  8605. '/label',
  8606. '/div',
  8607. ];
  8608. $this->assertHtml($expected, $result);
  8609. });
  8610. }
  8611. /**
  8612. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8613. *
  8614. * This test makes sure `false` excludes the label from the render
  8615. *
  8616. * @return void
  8617. */
  8618. public function testControlLabelManipulationDisableLabels()
  8619. {
  8620. $result = $this->Form->control('test', [
  8621. 'type' => 'radio',
  8622. 'options' => ['A', 'B'],
  8623. 'labelOptions' => false,
  8624. ]);
  8625. $expected = [
  8626. ['div' => ['class' => 'input radio']],
  8627. '<label',
  8628. 'Test',
  8629. '/label',
  8630. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8631. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8632. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8633. '/div',
  8634. ];
  8635. $this->assertHtml($expected, $result);
  8636. $result = $this->Form->control('checkbox1', [
  8637. 'label' => 'My checkboxes',
  8638. 'multiple' => 'checkbox',
  8639. 'type' => 'select',
  8640. 'options' => [
  8641. ['text' => 'First Checkbox', 'value' => 1],
  8642. ['text' => 'Second Checkbox', 'value' => 2],
  8643. ],
  8644. 'labelOptions' => false,
  8645. ]);
  8646. $expected = [
  8647. ['div' => ['class' => 'input select']],
  8648. ['label' => ['for' => 'checkbox1']],
  8649. 'My checkboxes',
  8650. '/label',
  8651. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8652. ['div' => ['class' => 'checkbox']],
  8653. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8654. '/div',
  8655. ['div' => ['class' => 'checkbox']],
  8656. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8657. '/div',
  8658. '/div',
  8659. ];
  8660. $this->assertHtml($expected, $result);
  8661. }
  8662. /**
  8663. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8664. *
  8665. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8666. * added to the class if checked.
  8667. *
  8668. * Also checks to make sure any custom attributes are rendered correctly
  8669. *
  8670. * @return void
  8671. */
  8672. public function testControlLabelManipulationRadios()
  8673. {
  8674. $result = $this->Form->control('test', [
  8675. 'type' => 'radio',
  8676. 'options' => ['A', 'B'],
  8677. 'labelOptions' => ['class' => 'custom-class'],
  8678. ]);
  8679. $expected = [
  8680. ['div' => ['class' => 'input radio']],
  8681. '<label',
  8682. 'Test',
  8683. '/label',
  8684. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8685. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8686. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8687. 'A',
  8688. '/label',
  8689. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8690. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8691. 'B',
  8692. '/label',
  8693. '/div',
  8694. ];
  8695. $this->assertHtml($expected, $result);
  8696. $result = $this->Form->control('test', [
  8697. 'type' => 'radio',
  8698. 'options' => ['A', 'B'],
  8699. 'value' => 1,
  8700. 'labelOptions' => ['class' => 'custom-class'],
  8701. ]);
  8702. $expected = [
  8703. ['div' => ['class' => 'input radio']],
  8704. '<label',
  8705. 'Test',
  8706. '/label',
  8707. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8708. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8709. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8710. 'A',
  8711. '/label',
  8712. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8713. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8714. 'B',
  8715. '/label',
  8716. '/div',
  8717. ];
  8718. $this->assertHtml($expected, $result);
  8719. $result = $this->Form->control('test', [
  8720. 'type' => 'radio',
  8721. 'options' => ['A', 'B'],
  8722. 'value' => 1,
  8723. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']],
  8724. ]);
  8725. $expected = [
  8726. ['div' => ['class' => 'input radio']],
  8727. '<label',
  8728. 'Test',
  8729. '/label',
  8730. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8731. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8732. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8733. 'A',
  8734. '/label',
  8735. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8736. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8737. 'B',
  8738. '/label',
  8739. '/div',
  8740. ];
  8741. $this->assertHtml($expected, $result);
  8742. $result = $this->Form->radio('test', ['A', 'B'], [
  8743. 'label' => [
  8744. 'class' => ['custom-class', 'another-class'],
  8745. 'data-name' => 'bob',
  8746. ],
  8747. 'value' => 1,
  8748. ]);
  8749. $expected = [
  8750. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8751. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8752. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8753. 'A',
  8754. '/label',
  8755. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8756. ['input' => [
  8757. 'type' => 'radio',
  8758. 'name' => 'test',
  8759. 'value' => '1',
  8760. 'id' => 'test-1',
  8761. 'checked' => 'checked',
  8762. ]],
  8763. 'B',
  8764. '/label',
  8765. ];
  8766. $this->assertHtml($expected, $result);
  8767. }
  8768. /**
  8769. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8770. *
  8771. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8772. * added to the class if checked.
  8773. *
  8774. * Also checks to make sure any custom attributes are rendered correctly
  8775. *
  8776. * @return void
  8777. */
  8778. public function testControlLabelManipulationCheckboxes()
  8779. {
  8780. $result = $this->Form->control('checkbox1', [
  8781. 'label' => 'My checkboxes',
  8782. 'multiple' => 'checkbox',
  8783. 'type' => 'select',
  8784. 'options' => [
  8785. ['text' => 'First Checkbox', 'value' => 1],
  8786. ['text' => 'Second Checkbox', 'value' => 2],
  8787. ],
  8788. 'labelOptions' => ['class' => 'custom-class'],
  8789. 'value' => ['1'],
  8790. ]);
  8791. $expected = [
  8792. ['div' => ['class' => 'input select']],
  8793. ['label' => ['for' => 'checkbox1']],
  8794. 'My checkboxes',
  8795. '/label',
  8796. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8797. ['div' => ['class' => 'checkbox']],
  8798. ['label' => [
  8799. 'class' => 'custom-class selected',
  8800. 'for' => 'checkbox1-1',
  8801. ]],
  8802. ['input' => [
  8803. 'type' => 'checkbox',
  8804. 'name' => 'checkbox1[]',
  8805. 'value' => '1',
  8806. 'id' => 'checkbox1-1',
  8807. 'checked' => 'checked',
  8808. ]],
  8809. 'First Checkbox',
  8810. '/label',
  8811. '/div',
  8812. ['div' => ['class' => 'checkbox']],
  8813. ['label' => [
  8814. 'class' => 'custom-class',
  8815. 'for' => 'checkbox1-2',
  8816. ]],
  8817. ['input' => [
  8818. 'type' => 'checkbox',
  8819. 'name' => 'checkbox1[]',
  8820. 'value' => '2',
  8821. 'id' => 'checkbox1-2',
  8822. ]],
  8823. 'Second Checkbox',
  8824. '/label',
  8825. '/div',
  8826. '/div',
  8827. ];
  8828. $this->assertHtml($expected, $result);
  8829. $result = $this->Form->control('checkbox1', [
  8830. 'label' => 'My checkboxes',
  8831. 'multiple' => 'checkbox',
  8832. 'type' => 'select',
  8833. 'options' => [
  8834. ['text' => 'First Checkbox', 'value' => 1],
  8835. ['text' => 'Second Checkbox', 'value' => 2],
  8836. ],
  8837. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8838. 'value' => ['1'],
  8839. ]);
  8840. $expected = [
  8841. ['div' => ['class' => 'input select']],
  8842. ['label' => ['for' => 'checkbox1']],
  8843. 'My checkboxes',
  8844. '/label',
  8845. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8846. ['div' => ['class' => 'checkbox']],
  8847. ['label' => [
  8848. 'class' => 'custom-class another-class selected',
  8849. 'data-name' => 'bob',
  8850. 'for' => 'checkbox1-1',
  8851. ]],
  8852. ['input' => [
  8853. 'type' => 'checkbox',
  8854. 'name' => 'checkbox1[]',
  8855. 'value' => '1',
  8856. 'id' => 'checkbox1-1',
  8857. 'checked' => 'checked',
  8858. ]],
  8859. 'First Checkbox',
  8860. '/label',
  8861. '/div',
  8862. ['div' => ['class' => 'checkbox']],
  8863. ['label' => [
  8864. 'class' => 'custom-class another-class',
  8865. 'data-name' => 'bob',
  8866. 'for' => 'checkbox1-2',
  8867. ]],
  8868. ['input' => [
  8869. 'type' => 'checkbox',
  8870. 'name' => 'checkbox1[]',
  8871. 'value' => '2',
  8872. 'id' => 'checkbox1-2',
  8873. ]],
  8874. 'Second Checkbox',
  8875. '/label',
  8876. '/div',
  8877. '/div',
  8878. ];
  8879. $this->assertHtml($expected, $result);
  8880. }
  8881. /**
  8882. * testControlMaxLengthArrayContext method
  8883. *
  8884. * Test control() with maxlength attribute in Array Context.
  8885. *
  8886. * @return void
  8887. */
  8888. public function testControlMaxLengthArrayContext()
  8889. {
  8890. $this->article['schema'] = [
  8891. 'title' => ['length' => 10],
  8892. ];
  8893. $this->Form->create($this->article);
  8894. $result = $this->Form->control('title');
  8895. $expected = [
  8896. 'div' => ['class'],
  8897. 'label' => ['for'],
  8898. 'Title',
  8899. '/label',
  8900. 'input' => [
  8901. 'id',
  8902. 'name' => 'title',
  8903. 'type' => 'text',
  8904. 'required' => 'required',
  8905. 'maxlength' => 10,
  8906. ],
  8907. '/div',
  8908. ];
  8909. $this->assertHtml($expected, $result);
  8910. }
  8911. /**
  8912. * testControlMaxLengthEntityContext method
  8913. *
  8914. * Test control() with maxlength attribute in Entity Context.
  8915. *
  8916. * @return void
  8917. */
  8918. public function testControlMaxLengthEntityContext()
  8919. {
  8920. $this->article['schema']['title']['length'] = 45;
  8921. $validator = new Validator();
  8922. $validator->maxLength('title', 10);
  8923. $article = new EntityContext(
  8924. new ServerRequest(),
  8925. [
  8926. 'entity' => new Entity($this->article),
  8927. 'table' => new Table([
  8928. 'schema' => $this->article['schema'],
  8929. 'validator' => $validator,
  8930. ]),
  8931. ]
  8932. );
  8933. $this->Form->create($article);
  8934. $result = $this->Form->control('title');
  8935. $expected = [
  8936. 'div' => ['class'],
  8937. 'label' => ['for'],
  8938. 'Title',
  8939. '/label',
  8940. 'input' => [
  8941. 'id',
  8942. 'name' => 'title',
  8943. 'type' => 'text',
  8944. 'required' => 'required',
  8945. 'maxlength' => 10,
  8946. ],
  8947. '/div',
  8948. ];
  8949. $this->assertHtml($expected, $result);
  8950. $this->article['schema']['title']['length'] = 45;
  8951. $validator = new Validator();
  8952. $validator->maxLength('title', 55);
  8953. $article = new EntityContext(
  8954. new ServerRequest(),
  8955. [
  8956. 'entity' => new Entity($this->article),
  8957. 'table' => new Table([
  8958. 'schema' => $this->article['schema'],
  8959. 'validator' => $validator,
  8960. ]),
  8961. ]
  8962. );
  8963. $this->Form->create($article);
  8964. $result = $this->Form->control('title');
  8965. $expected = [
  8966. 'div' => ['class'],
  8967. 'label' => ['for'],
  8968. 'Title',
  8969. '/label',
  8970. 'input' => [
  8971. 'id',
  8972. 'name' => 'title',
  8973. 'type' => 'text',
  8974. 'required' => 'required',
  8975. 'maxlength' => 55, // Length set in validator should take precedence over schema.
  8976. ],
  8977. '/div',
  8978. ];
  8979. $this->assertHtml($expected, $result);
  8980. $this->article['schema']['title']['length'] = 45;
  8981. $validator = new Validator();
  8982. $validator->maxLength('title', 55);
  8983. $article = new EntityContext(
  8984. new ServerRequest(),
  8985. [
  8986. 'entity' => new Entity($this->article),
  8987. 'table' => new Table([
  8988. 'schema' => $this->article['schema'],
  8989. 'validator' => $validator,
  8990. ]),
  8991. ]
  8992. );
  8993. $this->Form->create($article);
  8994. $result = $this->Form->control('title', ['maxlength' => 10]);
  8995. $expected = [
  8996. 'div' => ['class'],
  8997. 'label' => ['for'],
  8998. 'Title',
  8999. '/label',
  9000. 'input' => [
  9001. 'id',
  9002. 'name' => 'title',
  9003. 'type' => 'text',
  9004. 'required' => 'required',
  9005. 'maxlength' => 10, // Length set in options should take highest precedence.
  9006. ],
  9007. '/div',
  9008. ];
  9009. $this->assertHtml($expected, $result);
  9010. }
  9011. /**
  9012. * testControlMinMaxLengthEntityContext method
  9013. *
  9014. * Test control() with maxlength attribute in Entity Context sets the minimum val.
  9015. *
  9016. * @return void
  9017. */
  9018. public function testControlMinMaxLengthEntityContext()
  9019. {
  9020. $validator = new Validator();
  9021. $validator->maxLength('title', 10);
  9022. $article = new EntityContext(
  9023. new ServerRequest(),
  9024. [
  9025. 'entity' => new Entity($this->article),
  9026. 'table' => new Table([
  9027. 'schema' => $this->article['schema'],
  9028. 'validator' => $validator,
  9029. ]),
  9030. ]
  9031. );
  9032. $this->Form->create($article);
  9033. $result = $this->Form->control('title');
  9034. $expected = [
  9035. 'div' => ['class'],
  9036. 'label' => ['for'],
  9037. 'Title',
  9038. '/label',
  9039. 'input' => [
  9040. 'id',
  9041. 'name' => 'title',
  9042. 'type' => 'text',
  9043. 'required' => 'required',
  9044. 'maxlength' => 10,
  9045. ],
  9046. '/div',
  9047. ];
  9048. $this->assertHtml($expected, $result);
  9049. }
  9050. /**
  9051. * testControlMaxLengthFormContext method
  9052. *
  9053. * Test control() with maxlength attribute in Form Context.
  9054. *
  9055. * @return void
  9056. */
  9057. public function testControlMaxLengthFormContext()
  9058. {
  9059. $validator = new Validator();
  9060. $validator->maxLength('title', 10);
  9061. $form = new Form();
  9062. $form->setValidator('default', $validator);
  9063. $this->Form->create($form);
  9064. $result = $this->Form->control('title');
  9065. $expected = [
  9066. 'div' => ['class'],
  9067. 'label' => ['for'],
  9068. 'Title',
  9069. '/label',
  9070. 'input' => [
  9071. 'id',
  9072. 'name' => 'title',
  9073. 'type' => 'text',
  9074. 'required' => 'required',
  9075. 'maxlength' => 10,
  9076. ],
  9077. '/div',
  9078. ];
  9079. $this->assertHtml($expected, $result);
  9080. }
  9081. }