FormHelperTest.php 303 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245
  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\ORM\TableRegistry;
  23. use Cake\Routing\Router;
  24. use Cake\TestSuite\TestCase;
  25. use Cake\Utility\Security;
  26. use Cake\View\Helper\FormHelper;
  27. use Cake\View\View;
  28. /**
  29. * Test stub.
  30. */
  31. class Article extends Entity
  32. {
  33. }
  34. /**
  35. * Contact class
  36. */
  37. class ContactsTable extends Table
  38. {
  39. /**
  40. * Default schema
  41. *
  42. * @var array
  43. */
  44. protected $_schema = [
  45. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  46. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  47. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  48. 'phone' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  49. 'password' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  50. 'published' => ['type' => 'date', 'null' => true, 'default' => null, 'length' => null],
  51. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  52. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  53. 'age' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => null],
  54. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  55. ];
  56. /**
  57. * Initializes the schema
  58. *
  59. * @return void
  60. */
  61. public function initialize(array $config)
  62. {
  63. $this->schema($this->_schema);
  64. }
  65. }
  66. /**
  67. * ValidateUser class
  68. */
  69. class ValidateUsersTable extends Table
  70. {
  71. /**
  72. * schema method
  73. *
  74. * @var array
  75. */
  76. protected $_schema = [
  77. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  78. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  79. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  80. 'balance' => ['type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2],
  81. 'cost_decimal' => ['type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3],
  82. 'null_decimal' => ['type' => 'decimal', 'null' => false, 'length' => null, 'precision' => null],
  83. 'ratio' => ['type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6],
  84. 'population' => ['type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0],
  85. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  86. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  87. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  88. ];
  89. /**
  90. * Initializes the schema
  91. *
  92. * @return void
  93. */
  94. public function initialize(array $config)
  95. {
  96. $this->schema($this->_schema);
  97. }
  98. }
  99. /**
  100. * FormHelperTest class
  101. *
  102. * @property \Cake\View\Helper\FormHelper $Form
  103. * @property \Cake\View\View $View
  104. */
  105. class FormHelperTest extends TestCase
  106. {
  107. /**
  108. * Fixtures to be used
  109. *
  110. * @var array
  111. */
  112. public $fixtures = ['core.articles', 'core.comments'];
  113. /**
  114. * Do not load the fixtures by default
  115. *
  116. * @var bool
  117. */
  118. public $autoFixtures = false;
  119. /**
  120. * @var array
  121. */
  122. protected $article = [];
  123. /**
  124. * setUp method
  125. *
  126. * @return void
  127. */
  128. public function setUp()
  129. {
  130. parent::setUp();
  131. Configure::write('Config.language', 'eng');
  132. Configure::write('App.base', '');
  133. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  134. $this->View = new View();
  135. $this->Form = new FormHelper($this->View);
  136. $request = new ServerRequest([
  137. 'webroot' => '',
  138. 'base' => '',
  139. 'url' => '/articles/add',
  140. 'params' => [
  141. 'controller' => 'articles',
  142. 'action' => 'add',
  143. ]
  144. ]);
  145. $this->Form->Url->request = $this->Form->request = $request;
  146. $this->dateRegex = [
  147. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  148. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  149. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  150. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  151. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  152. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  153. ];
  154. $this->article = [
  155. 'schema' => [
  156. 'id' => ['type' => 'integer'],
  157. 'author_id' => ['type' => 'integer', 'null' => true],
  158. 'title' => ['type' => 'string', 'null' => true],
  159. 'body' => 'text',
  160. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  161. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  162. ],
  163. 'required' => [
  164. 'author_id' => true,
  165. 'title' => true,
  166. ]
  167. ];
  168. Security::salt('foo!');
  169. Router::connect('/:controller', ['action' => 'index']);
  170. Router::connect('/:controller/:action/*');
  171. }
  172. /**
  173. * tearDown method
  174. *
  175. * @return void
  176. */
  177. public function tearDown()
  178. {
  179. parent::tearDown();
  180. unset($this->Form, $this->Controller, $this->View);
  181. TableRegistry::clear();
  182. }
  183. /**
  184. * Test construct() with the templates option.
  185. *
  186. * @return void
  187. */
  188. public function testConstructTemplatesFile()
  189. {
  190. $helper = new FormHelper($this->View, [
  191. 'templates' => 'htmlhelper_tags'
  192. ]);
  193. $result = $helper->control('name');
  194. $this->assertContains('<input', $result);
  195. }
  196. /**
  197. * Test that when specifying custom widgets the config array for that widget
  198. * is overwritten instead of merged.
  199. *
  200. * @return void
  201. */
  202. public function testConstructWithWidgets()
  203. {
  204. $config = [
  205. 'widgets' => [
  206. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
  207. ]
  208. ];
  209. $helper = new FormHelper($this->View, $config);
  210. $registry = $helper->widgetRegistry();
  211. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('datetime'));
  212. }
  213. /**
  214. * Test that when specifying custom widgets config file and it should be
  215. * added to widgets array. WidgetRegistry will load widgets in constructor.
  216. *
  217. * @return void
  218. */
  219. public function testConstructWithWidgetsConfig()
  220. {
  221. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  222. $registry = $helper->widgetRegistry();
  223. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('text'));
  224. }
  225. /**
  226. * Test registering a new widget class and rendering it.
  227. *
  228. * @return void
  229. */
  230. public function testAddWidgetAndRenderWidget()
  231. {
  232. $data = [
  233. 'val' => 1
  234. ];
  235. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  236. $this->Form->addWidget('test', $mock);
  237. $mock->expects($this->once())
  238. ->method('render')
  239. ->with($data)
  240. ->will($this->returnValue('HTML'));
  241. $result = $this->Form->widget('test', $data);
  242. $this->assertEquals('HTML', $result);
  243. }
  244. /**
  245. * Test that secureFields() of widget is called after calling render(),
  246. * not before.
  247. *
  248. * @return void
  249. */
  250. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  251. {
  252. $data = [
  253. 'val' => 1,
  254. 'name' => 'test'
  255. ];
  256. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  257. $this->Form->addWidget('test', $mock);
  258. $mock->expects($this->at(0))
  259. ->method('render')
  260. ->with($data)
  261. ->will($this->returnValue('HTML'));
  262. $mock->expects($this->at(1))
  263. ->method('secureFields')
  264. ->with($data)
  265. ->will($this->returnValue(['test']));
  266. $result = $this->Form->widget('test', $data + ['secure' => true]);
  267. $this->assertEquals('HTML', $result);
  268. }
  269. /**
  270. * Test that empty string is not added to secure fields list when
  271. * rendering input widget without name.
  272. *
  273. * @return void
  274. */
  275. public function testRenderingWidgetWithEmptyName()
  276. {
  277. $this->assertEquals([], $this->Form->fields);
  278. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  279. $this->assertEquals('<select name=""></select>', $result);
  280. $this->assertEquals([], $this->Form->fields);
  281. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  282. $this->assertEquals('<select name="0"></select>', $result);
  283. $this->assertEquals(['0'], $this->Form->fields);
  284. }
  285. /**
  286. * Test registering an invalid widget class.
  287. *
  288. * @expectedException \RuntimeException
  289. * @return void
  290. */
  291. public function testAddWidgetInvalid()
  292. {
  293. $mock = new \StdClass();
  294. $this->Form->addWidget('test', $mock);
  295. $this->Form->widget('test');
  296. }
  297. /**
  298. * Test adding a new context class.
  299. *
  300. * @return void
  301. */
  302. public function testAddContextProvider()
  303. {
  304. $context = 'My data';
  305. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  306. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  307. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  308. $this->assertEquals($context, $data['entity']);
  309. return $stub;
  310. });
  311. $this->Form->create($context);
  312. $result = $this->Form->context();
  313. $this->assertSame($stub, $result);
  314. }
  315. /**
  316. * Test replacing a context class.
  317. *
  318. * @return void
  319. */
  320. public function testAddContextProviderReplace()
  321. {
  322. $entity = new Article();
  323. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  324. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  325. return $stub;
  326. });
  327. $this->Form->create($entity);
  328. $result = $this->Form->context();
  329. $this->assertSame($stub, $result);
  330. }
  331. /**
  332. * Test overriding a context class.
  333. *
  334. * @return void
  335. */
  336. public function testAddContextProviderAdd()
  337. {
  338. $entity = new Article();
  339. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  340. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  341. if ($data['entity'] instanceof Entity) {
  342. return $stub;
  343. }
  344. });
  345. $this->Form->create($entity);
  346. $result = $this->Form->context();
  347. $this->assertSame($stub, $result);
  348. }
  349. /**
  350. * Test adding an invalid context class.
  351. *
  352. * @expectedException \RuntimeException
  353. * @expectedExceptionMessage Context providers must return object implementing Cake\View\Form\ContextInterface. Got "stdClass" instead.
  354. * @return void
  355. */
  356. public function testAddContextProviderInvalid()
  357. {
  358. $context = 'My data';
  359. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  360. return new \StdClass();
  361. });
  362. $this->Form->create($context);
  363. }
  364. /**
  365. * Provides context options for create().
  366. *
  367. * @return array
  368. */
  369. public function contextSelectionProvider()
  370. {
  371. $entity = new Article();
  372. $collection = new Collection([$entity]);
  373. $emptyCollection = new Collection([]);
  374. $arrayObject = new \ArrayObject([]);
  375. $data = [
  376. 'schema' => [
  377. 'title' => ['type' => 'string']
  378. ]
  379. ];
  380. $form = new Form();
  381. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  382. return [
  383. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  384. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  385. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  386. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  387. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  388. 'form' => [$form, 'Cake\View\Form\FormContext'],
  389. 'none' => [null, 'Cake\View\Form\NullContext'],
  390. 'false' => [false, 'Cake\View\Form\NullContext'],
  391. 'custom' => [$custom, get_class($custom)]
  392. ];
  393. }
  394. /**
  395. * Test default context selection in create()
  396. *
  397. * @dataProvider contextSelectionProvider
  398. * @return void
  399. */
  400. public function testCreateContextSelectionBuiltIn($data, $class)
  401. {
  402. $this->loadFixtures('Articles');
  403. $this->Form->create($data);
  404. $this->assertInstanceOf($class, $this->Form->context());
  405. }
  406. /**
  407. * Data provider for type option.
  408. *
  409. * @return array
  410. */
  411. public static function requestTypeProvider()
  412. {
  413. return [
  414. // type, method, override
  415. ['post', 'post', 'POST'],
  416. ['put', 'post', 'PUT'],
  417. ['patch', 'post', 'PATCH'],
  418. ['delete', 'post', 'DELETE'],
  419. ];
  420. }
  421. /**
  422. * Test creating file forms.
  423. *
  424. * @return void
  425. */
  426. public function testCreateFile()
  427. {
  428. $encoding = strtolower(Configure::read('App.encoding'));
  429. $result = $this->Form->create(false, ['type' => 'file']);
  430. $expected = [
  431. 'form' => [
  432. 'method' => 'post', 'action' => '/articles/add',
  433. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  434. ],
  435. 'div' => ['style' => 'display:none;'],
  436. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  437. '/div'
  438. ];
  439. $this->assertHtml($expected, $result);
  440. }
  441. /**
  442. * Test creating GET forms.
  443. *
  444. * @return void
  445. */
  446. public function testCreateGet()
  447. {
  448. $encoding = strtolower(Configure::read('App.encoding'));
  449. $result = $this->Form->create(false, ['type' => 'get']);
  450. $expected = ['form' => [
  451. 'method' => 'get', 'action' => '/articles/add',
  452. 'accept-charset' => $encoding
  453. ]];
  454. $this->assertHtml($expected, $result);
  455. }
  456. /**
  457. * Test explicit method/enctype options.
  458. *
  459. * Explicit method overwrites inferred method from 'type'
  460. *
  461. * @return void
  462. */
  463. public function testCreateExplicitMethodEnctype()
  464. {
  465. $encoding = strtolower(Configure::read('App.encoding'));
  466. $result = $this->Form->create(false, [
  467. 'type' => 'get',
  468. 'method' => 'put',
  469. 'enctype' => 'multipart/form-data'
  470. ]);
  471. $expected = ['form' => [
  472. 'method' => 'put',
  473. 'action' => '/articles/add',
  474. 'enctype' => 'multipart/form-data',
  475. 'accept-charset' => $encoding
  476. ]];
  477. $this->assertHtml($expected, $result);
  478. }
  479. /**
  480. * Test create() with the templates option.
  481. *
  482. * @return void
  483. */
  484. public function testCreateTemplatesArray()
  485. {
  486. $result = $this->Form->create($this->article, [
  487. 'templates' => [
  488. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  489. ]
  490. ]);
  491. $expected = [
  492. 'form' => [
  493. 'class' => 'form-horizontal',
  494. 'method' => 'post',
  495. 'action' => '/articles/add',
  496. 'accept-charset' => 'utf-8'
  497. ]
  498. ];
  499. $this->assertHtml($expected, $result);
  500. }
  501. /**
  502. * Test create() with the templates option.
  503. *
  504. * @return void
  505. */
  506. public function testCreateTemplatesFile()
  507. {
  508. $result = $this->Form->create($this->article, [
  509. 'templates' => 'htmlhelper_tags',
  510. ]);
  511. $expected = [
  512. 'start form',
  513. 'div' => ['class' => 'hidden'],
  514. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  515. '/div'
  516. ];
  517. $this->assertHtml($expected, $result);
  518. }
  519. /**
  520. * Test that create() and end() restore templates.
  521. *
  522. * @return void
  523. */
  524. public function testCreateEndRestoreTemplates()
  525. {
  526. $this->Form->create($this->article, [
  527. 'templates' => ['input' => 'custom input element']
  528. ]);
  529. $this->Form->end();
  530. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  531. }
  532. /**
  533. * Test using template vars in various templates used by control() method.
  534. *
  535. * @return void
  536. */
  537. public function testControlTemplateVars()
  538. {
  539. $result = $this->Form->control('text', [
  540. 'templates' => [
  541. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  542. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  543. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  544. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  545. ],
  546. 'templateVars' => [
  547. 'forinput' => 'in-input',
  548. 'forlabel' => 'in-label',
  549. 'forgroup' => 'in-group',
  550. 'forcontainer' => 'in-container'
  551. ]
  552. ]);
  553. $expected = [
  554. 'div' => ['class'],
  555. 'label' => ['for'],
  556. 'Text in-label',
  557. '/label',
  558. 'in-group',
  559. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  560. 'in-container',
  561. '/div',
  562. ];
  563. $this->assertHtml($expected, $result);
  564. }
  565. /**
  566. * Test ensuring template variables work in template files loaded
  567. * during control().
  568. *
  569. * @return void
  570. */
  571. public function testControlTemplatesFromFile()
  572. {
  573. $result = $this->Form->control('title', [
  574. 'templates' => 'test_templates',
  575. 'templateVars' => [
  576. 'forcontainer' => 'container-data'
  577. ]
  578. ]);
  579. $expected = [
  580. 'div' => ['class'],
  581. 'label' => ['for'],
  582. 'Title',
  583. '/label',
  584. 'input' => ['name', 'type' => 'text', 'id'],
  585. 'container-data',
  586. '/div',
  587. ];
  588. $this->assertHtml($expected, $result);
  589. }
  590. /**
  591. * Test using template vars in inputSubmit and submitContainer template.
  592. *
  593. * @return void
  594. */
  595. public function testSubmitTemplateVars()
  596. {
  597. $this->Form->templates([
  598. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  599. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  600. ]);
  601. $result = $this->Form->submit('Submit', [
  602. 'templateVars' => [
  603. 'forinput' => 'in-input',
  604. 'forcontainer' => 'in-container'
  605. ]
  606. ]);
  607. $expected = [
  608. 'div' => ['class'],
  609. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  610. 'in-container',
  611. '/div',
  612. ];
  613. $this->assertHtml($expected, $result);
  614. }
  615. /**
  616. * test the create() method
  617. *
  618. * @dataProvider requestTypeProvider
  619. * @return void
  620. */
  621. public function testCreateTypeOptions($type, $method, $override)
  622. {
  623. $encoding = strtolower(Configure::read('App.encoding'));
  624. $result = $this->Form->create(false, ['type' => $type]);
  625. $expected = [
  626. 'form' => [
  627. 'method' => $method, 'action' => '/articles/add',
  628. 'accept-charset' => $encoding
  629. ],
  630. 'div' => ['style' => 'display:none;'],
  631. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  632. '/div'
  633. ];
  634. $this->assertHtml($expected, $result);
  635. }
  636. /**
  637. * Test using template vars in Create (formStart template)
  638. *
  639. * @return void
  640. */
  641. public function testCreateTemplateVars()
  642. {
  643. $result = $this->Form->create($this->article, [
  644. 'templates' => [
  645. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  646. ],
  647. 'templateVars' => ['header' => 'headertext']
  648. ]);
  649. $expected = [
  650. 'h4' => ['class'],
  651. 'headertext',
  652. '/h4',
  653. 'form' => [
  654. 'method' => 'post',
  655. 'action' => '/articles/add',
  656. 'accept-charset' => 'utf-8'
  657. ]
  658. ];
  659. $this->assertHtml($expected, $result);
  660. }
  661. /**
  662. * Test opening a form for an update operation.
  663. *
  664. * @return void
  665. */
  666. public function testCreateUpdateForm()
  667. {
  668. $encoding = strtolower(Configure::read('App.encoding'));
  669. $this->Form->request->here = '/articles/edit/1';
  670. $this->Form->request->params['action'] = 'edit';
  671. $this->article['defaults']['id'] = 1;
  672. $result = $this->Form->create($this->article);
  673. $expected = [
  674. 'form' => [
  675. 'method' => 'post',
  676. 'action' => '/articles/edit/1',
  677. 'accept-charset' => $encoding
  678. ],
  679. 'div' => ['style' => 'display:none;'],
  680. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  681. '/div'
  682. ];
  683. $this->assertHtml($expected, $result);
  684. }
  685. /**
  686. * test create() with automatic url generation
  687. *
  688. * @return void
  689. */
  690. public function testCreateAutoUrl()
  691. {
  692. $encoding = strtolower(Configure::read('App.encoding'));
  693. $this->Form->request->params['action'] = 'delete';
  694. $this->Form->request->here = '/articles/delete/10';
  695. $this->Form->request->base = '';
  696. $result = $this->Form->create($this->article);
  697. $expected = [
  698. 'form' => [
  699. 'method' => 'post', 'action' => '/articles/delete/10',
  700. 'accept-charset' => $encoding
  701. ],
  702. 'div' => ['style' => 'display:none;'],
  703. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  704. '/div'
  705. ];
  706. $this->assertHtml($expected, $result);
  707. $this->article['defaults'] = ['id' => 1];
  708. $this->Form->request->here = '/articles/edit/1';
  709. $this->Form->request->params['action'] = 'delete';
  710. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  711. $expected = [
  712. 'form' => [
  713. 'method' => 'post',
  714. 'action' => '/articles/edit/1',
  715. 'accept-charset' => $encoding
  716. ],
  717. 'div' => ['style' => 'display:none;'],
  718. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  719. '/div'
  720. ];
  721. $this->assertHtml($expected, $result);
  722. $this->Form->request->params['action'] = 'add';
  723. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  724. $expected = [
  725. 'form' => [
  726. 'method' => 'post',
  727. 'action' => '/articles/publish/1',
  728. 'accept-charset' => $encoding
  729. ],
  730. 'div' => ['style' => 'display:none;'],
  731. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  732. '/div'
  733. ];
  734. $this->assertHtml($expected, $result);
  735. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  736. $expected = [
  737. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  738. 'div' => ['style' => 'display:none;'],
  739. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  740. '/div'
  741. ];
  742. $this->assertHtml($expected, $result);
  743. $this->Form->request->params['controller'] = 'Pages';
  744. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  745. $expected = [
  746. 'form' => [
  747. 'method' => 'post', 'action' => '/Pages/signup/1',
  748. 'accept-charset' => $encoding
  749. ],
  750. 'div' => ['style' => 'display:none;'],
  751. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  752. '/div'
  753. ];
  754. $this->assertHtml($expected, $result);
  755. }
  756. /**
  757. * Test create() with no URL (no "action" attribute for <form> tag)
  758. *
  759. * @return void
  760. */
  761. public function testCreateNoUrl()
  762. {
  763. $result = $this->Form->create(false, ['url' => false]);
  764. $expected = [
  765. 'form' => [
  766. 'method' => 'post',
  767. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  768. ],
  769. 'div' => ['style' => 'display:none;'],
  770. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  771. '/div'
  772. ];
  773. $this->assertHtml($expected, $result);
  774. }
  775. /**
  776. * test create() with a custom route
  777. *
  778. * @return void
  779. */
  780. public function testCreateCustomRoute()
  781. {
  782. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  783. $encoding = strtolower(Configure::read('App.encoding'));
  784. $this->Form->request->params['controller'] = 'users';
  785. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  786. $expected = [
  787. 'form' => [
  788. 'method' => 'post', 'action' => '/login',
  789. 'accept-charset' => $encoding
  790. ],
  791. 'div' => ['style' => 'display:none;'],
  792. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  793. '/div'
  794. ];
  795. $this->assertHtml($expected, $result);
  796. Router::connect(
  797. '/new-article',
  798. ['controller' => 'articles', 'action' => 'myaction'],
  799. ['_name' => 'my-route']
  800. );
  801. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  802. $expected = [
  803. 'form' => [
  804. 'method' => 'post', 'action' => '/new-article',
  805. 'accept-charset' => $encoding,
  806. ],
  807. 'div' => ['style' => 'display:none;'],
  808. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  809. '/div'
  810. ];
  811. $this->assertHtml($expected, $result);
  812. }
  813. /**
  814. * test automatic accept-charset overriding
  815. *
  816. * @return void
  817. */
  818. public function testCreateWithAcceptCharset()
  819. {
  820. $result = $this->Form->create(
  821. $this->article,
  822. [
  823. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  824. ]
  825. );
  826. $expected = [
  827. 'form' => [
  828. 'method' => 'post', 'action' => '/articles',
  829. 'accept-charset' => 'iso-8859-1'
  830. ],
  831. 'div' => ['style' => 'display:none;'],
  832. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  833. '/div'
  834. ];
  835. $this->assertHtml($expected, $result);
  836. }
  837. /**
  838. * Test base form URL when url param is passed with multiple parameters (&)
  839. *
  840. */
  841. public function testCreateQueryStringRequest()
  842. {
  843. $encoding = strtolower(Configure::read('App.encoding'));
  844. $result = $this->Form->create($this->article, [
  845. 'type' => 'post',
  846. 'escape' => false,
  847. 'url' => [
  848. 'controller' => 'controller',
  849. 'action' => 'action',
  850. '?' => ['param1' => 'value1', 'param2' => 'value2']
  851. ]
  852. ]);
  853. $expected = [
  854. 'form' => [
  855. 'method' => 'post',
  856. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  857. 'accept-charset' => $encoding
  858. ],
  859. 'div' => ['style' => 'display:none;'],
  860. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  861. '/div'
  862. ];
  863. $this->assertHtml($expected, $result);
  864. $result = $this->Form->create($this->article, [
  865. 'type' => 'post',
  866. 'url' => [
  867. 'controller' => 'controller',
  868. 'action' => 'action',
  869. '?' => ['param1' => 'value1', 'param2' => 'value2']
  870. ]
  871. ]);
  872. $this->assertHtml($expected, $result);
  873. }
  874. /**
  875. * test that create() doesn't cause errors by multiple id's being in the primary key
  876. * as could happen with multiple select or checkboxes.
  877. *
  878. * @return void
  879. */
  880. public function testCreateWithMultipleIdInData()
  881. {
  882. $encoding = strtolower(Configure::read('App.encoding'));
  883. $this->Form->request->data['Article']['id'] = [1, 2];
  884. $result = $this->Form->create($this->article);
  885. $expected = [
  886. 'form' => [
  887. 'method' => 'post',
  888. 'action' => '/articles/add',
  889. 'accept-charset' => $encoding
  890. ],
  891. 'div' => ['style' => 'display:none;'],
  892. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  893. '/div'
  894. ];
  895. $this->assertHtml($expected, $result);
  896. }
  897. /**
  898. * test that create() doesn't add in extra passed params.
  899. *
  900. * @return void
  901. */
  902. public function testCreatePassedArgs()
  903. {
  904. $encoding = strtolower(Configure::read('App.encoding'));
  905. $this->Form->request->data['Article']['id'] = 1;
  906. $result = $this->Form->create($this->article, [
  907. 'type' => 'post',
  908. 'escape' => false,
  909. 'url' => [
  910. 'action' => 'edit',
  911. 'myparam'
  912. ]
  913. ]);
  914. $expected = [
  915. 'form' => [
  916. 'method' => 'post',
  917. 'action' => '/articles/edit/myparam',
  918. 'accept-charset' => $encoding
  919. ],
  920. 'div' => ['style' => 'display:none;'],
  921. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  922. '/div'
  923. ];
  924. $this->assertHtml($expected, $result);
  925. }
  926. /**
  927. * test creating a get form, and get form inputs.
  928. *
  929. * @return void
  930. */
  931. public function testGetFormCreate()
  932. {
  933. $encoding = strtolower(Configure::read('App.encoding'));
  934. $result = $this->Form->create($this->article, ['type' => 'get']);
  935. $expected = ['form' => [
  936. 'method' => 'get', 'action' => '/articles/add',
  937. 'accept-charset' => $encoding
  938. ]];
  939. $this->assertHtml($expected, $result);
  940. $result = $this->Form->text('title');
  941. $expected = ['input' => [
  942. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  943. ]];
  944. $this->assertHtml($expected, $result);
  945. $result = $this->Form->password('password');
  946. $expected = ['input' => [
  947. 'name' => 'password', 'type' => 'password'
  948. ]];
  949. $this->assertHtml($expected, $result);
  950. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  951. $result = $this->Form->text('user_form');
  952. $expected = ['input' => [
  953. 'name' => 'user_form', 'type' => 'text'
  954. ]];
  955. $this->assertHtml($expected, $result);
  956. }
  957. /**
  958. * test get form, and inputs when the model param is false
  959. *
  960. * @return void
  961. */
  962. public function testGetFormWithFalseModel()
  963. {
  964. $encoding = strtolower(Configure::read('App.encoding'));
  965. $this->Form->request->params['controller'] = 'contact_test';
  966. $result = $this->Form->create(false, [
  967. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  968. ]);
  969. $expected = ['form' => [
  970. 'method' => 'get', 'action' => '/contact_test/add',
  971. 'accept-charset' => $encoding
  972. ]];
  973. $this->assertHtml($expected, $result);
  974. $result = $this->Form->text('reason');
  975. $expected = [
  976. 'input' => ['type' => 'text', 'name' => 'reason']
  977. ];
  978. $this->assertHtml($expected, $result);
  979. }
  980. /**
  981. * testFormCreateWithSecurity method
  982. *
  983. * Test form->create() with security key.
  984. *
  985. * @return void
  986. */
  987. public function testCreateWithSecurity()
  988. {
  989. $this->Form->request->params['_csrfToken'] = 'testKey';
  990. $encoding = strtolower(Configure::read('App.encoding'));
  991. $result = $this->Form->create($this->article, [
  992. 'url' => '/articles/publish',
  993. ]);
  994. $expected = [
  995. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  996. 'div' => ['style' => 'display:none;'],
  997. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  998. ['input' => [
  999. 'type' => 'hidden',
  1000. 'name' => '_csrfToken',
  1001. 'value' => 'testKey',
  1002. 'autocomplete' => 'off',
  1003. ]],
  1004. '/div'
  1005. ];
  1006. $this->assertHtml($expected, $result);
  1007. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1008. $expected['form']['id'] = 'MyForm';
  1009. $this->assertHtml($expected, $result);
  1010. }
  1011. /**
  1012. * testFormCreateGetNoSecurity method
  1013. *
  1014. * Test form->create() with no security key as its a get form
  1015. *
  1016. * @return void
  1017. */
  1018. public function testCreateEndGetNoSecurity()
  1019. {
  1020. $this->Form->request->params['_csrfToken'] = 'testKey';
  1021. $article = new Article();
  1022. $result = $this->Form->create($article, [
  1023. 'type' => 'get',
  1024. 'url' => '/contacts/add'
  1025. ]);
  1026. $this->assertNotContains('testKey', $result);
  1027. $result = $this->Form->end();
  1028. $this->assertNotContains('testKey', $result);
  1029. }
  1030. /**
  1031. * test that create() clears the fields property so it starts fresh
  1032. *
  1033. * @return void
  1034. */
  1035. public function testCreateClearingFields()
  1036. {
  1037. $this->Form->fields = ['model_id'];
  1038. $this->Form->create($this->article);
  1039. $this->assertEquals([], $this->Form->fields);
  1040. }
  1041. /**
  1042. * Tests form hash generation with model-less data
  1043. *
  1044. * @return void
  1045. */
  1046. public function testValidateHashNoModel()
  1047. {
  1048. $this->Form->request->params['_Token'] = 'foo';
  1049. $result = $this->Form->secure(['anything']);
  1050. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  1051. }
  1052. /**
  1053. * Tests that hidden fields generated for checkboxes don't get locked
  1054. *
  1055. * @return void
  1056. */
  1057. public function testNoCheckboxLocking()
  1058. {
  1059. $this->Form->request->params['_Token'] = 'foo';
  1060. $this->assertSame([], $this->Form->fields);
  1061. $this->Form->checkbox('check', ['value' => '1']);
  1062. $this->assertSame($this->Form->fields, ['check']);
  1063. }
  1064. /**
  1065. * testFormSecurityFields method
  1066. *
  1067. * Test generation of secure form hash generation.
  1068. *
  1069. * @return void
  1070. */
  1071. public function testFormSecurityFields()
  1072. {
  1073. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1074. $this->Form->request->params['_Token'] = 'testKey';
  1075. $result = $this->Form->secure($fields);
  1076. $hash = Security::hash(serialize($fields) . Security::salt());
  1077. $hash .= ':' . 'Model.valid';
  1078. $hash = urlencode($hash);
  1079. $tokenDebug = urlencode(json_encode([
  1080. '',
  1081. $fields,
  1082. []
  1083. ]));
  1084. $expected = [
  1085. 'div' => ['style' => 'display:none;'],
  1086. ['input' => [
  1087. 'type' => 'hidden',
  1088. 'name' => '_Token[fields]',
  1089. 'value' => $hash,
  1090. 'autocomplete' => 'off',
  1091. ]],
  1092. ['input' => [
  1093. 'type' => 'hidden',
  1094. 'name' => '_Token[unlocked]',
  1095. 'value' => '',
  1096. 'autocomplete' => 'off',
  1097. ]],
  1098. ['input' => [
  1099. 'type' => 'hidden',
  1100. 'name' => '_Token[debug]',
  1101. 'value' => $tokenDebug,
  1102. 'autocomplete' => 'off',
  1103. ]],
  1104. '/div'
  1105. ];
  1106. $this->assertHtml($expected, $result);
  1107. }
  1108. /**
  1109. * testFormSecurityFields method
  1110. *
  1111. * Test debug token is not generated if debug is false
  1112. *
  1113. * @return void
  1114. */
  1115. public function testFormSecurityFieldsNoDebugMode()
  1116. {
  1117. Configure::write('debug', false);
  1118. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1119. $this->Form->request->params['_Token'] = 'testKey';
  1120. $result = $this->Form->secure($fields);
  1121. $hash = Security::hash(serialize($fields) . Security::salt());
  1122. $hash .= ':' . 'Model.valid';
  1123. $hash = urlencode($hash);
  1124. $expected = [
  1125. 'div' => ['style' => 'display:none;'],
  1126. ['input' => [
  1127. 'type' => 'hidden',
  1128. 'name' => '_Token[fields]',
  1129. 'autocomplete' => 'off',
  1130. 'value' => $hash
  1131. ]],
  1132. ['input' => [
  1133. 'type' => 'hidden',
  1134. 'name' => '_Token[unlocked]',
  1135. 'autocomplete' => 'off',
  1136. 'value' => '',
  1137. ]],
  1138. '/div'
  1139. ];
  1140. $this->assertHtml($expected, $result);
  1141. }
  1142. /**
  1143. * Tests correct generation of number fields for smallint
  1144. *
  1145. * @return void
  1146. */
  1147. public function testTextFieldGenerationForSmallint()
  1148. {
  1149. $this->article['schema'] = [
  1150. 'foo' => [
  1151. 'type' => 'smallinteger',
  1152. 'null' => false,
  1153. 'default' => null,
  1154. 'length' => 10
  1155. ]
  1156. ];
  1157. $this->Form->create($this->article);
  1158. $result = $this->Form->control('foo');
  1159. $this->assertContains('class="input number"', $result);
  1160. $this->assertContains('type="number"', $result);
  1161. }
  1162. /**
  1163. * Tests correct generation of number fields for tinyint
  1164. *
  1165. * @return void
  1166. */
  1167. public function testTextFieldGenerationForTinyint()
  1168. {
  1169. $this->article['schema'] = [
  1170. 'foo' => [
  1171. 'type' => 'tinyinteger',
  1172. 'null' => false,
  1173. 'default' => null,
  1174. 'length' => 10
  1175. ]
  1176. ];
  1177. $this->Form->create($this->article);
  1178. $result = $this->Form->control('foo');
  1179. $this->assertContains('class="input number"', $result);
  1180. $this->assertContains('type="number"', $result);
  1181. }
  1182. /**
  1183. * Tests correct generation of number fields for double and float fields
  1184. *
  1185. * @return void
  1186. */
  1187. public function testTextFieldGenerationForFloats()
  1188. {
  1189. $this->article['schema'] = [
  1190. 'foo' => [
  1191. 'type' => 'float',
  1192. 'null' => false,
  1193. 'default' => null,
  1194. 'length' => 10
  1195. ]
  1196. ];
  1197. $this->Form->create($this->article);
  1198. $result = $this->Form->control('foo');
  1199. $expected = [
  1200. 'div' => ['class' => 'input number'],
  1201. 'label' => ['for' => 'foo'],
  1202. 'Foo',
  1203. '/label',
  1204. ['input' => [
  1205. 'type' => 'number',
  1206. 'name' => 'foo',
  1207. 'id' => 'foo',
  1208. 'step' => 'any'
  1209. ]],
  1210. '/div'
  1211. ];
  1212. $this->assertHtml($expected, $result);
  1213. $result = $this->Form->control('foo', ['step' => 0.5]);
  1214. $expected = [
  1215. 'div' => ['class' => 'input number'],
  1216. 'label' => ['for' => 'foo'],
  1217. 'Foo',
  1218. '/label',
  1219. ['input' => [
  1220. 'type' => 'number',
  1221. 'name' => 'foo',
  1222. 'id' => 'foo',
  1223. 'step' => '0.5'
  1224. ]],
  1225. '/div'
  1226. ];
  1227. $this->assertHtml($expected, $result);
  1228. }
  1229. /**
  1230. * Tests correct generation of number fields for integer fields
  1231. *
  1232. * @return void
  1233. */
  1234. public function testTextFieldTypeNumberGenerationForIntegers()
  1235. {
  1236. TableRegistry::get('Contacts', [
  1237. 'className' => __NAMESPACE__ . '\ContactsTable'
  1238. ]);
  1239. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1240. $result = $this->Form->control('age');
  1241. $expected = [
  1242. 'div' => ['class' => 'input number'],
  1243. 'label' => ['for' => 'age'],
  1244. 'Age',
  1245. '/label',
  1246. ['input' => [
  1247. 'type' => 'number', 'name' => 'age',
  1248. 'id' => 'age'
  1249. ]],
  1250. '/div'
  1251. ];
  1252. $this->assertHtml($expected, $result);
  1253. }
  1254. /**
  1255. * Tests correct generation of file upload fields for binary fields
  1256. *
  1257. * @return void
  1258. */
  1259. public function testFileUploadFieldTypeGenerationForBinaries()
  1260. {
  1261. $table = TableRegistry::get('Contacts', [
  1262. 'className' => __NAMESPACE__ . '\ContactsTable'
  1263. ]);
  1264. $table->schema(['foo' => [
  1265. 'type' => 'binary',
  1266. 'null' => false,
  1267. 'default' => null,
  1268. 'length' => 1024
  1269. ]]);
  1270. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1271. $result = $this->Form->control('foo');
  1272. $expected = [
  1273. 'div' => ['class' => 'input file'],
  1274. 'label' => ['for' => 'foo'],
  1275. 'Foo',
  1276. '/label',
  1277. ['input' => [
  1278. 'type' => 'file', 'name' => 'foo',
  1279. 'id' => 'foo'
  1280. ]],
  1281. '/div'
  1282. ];
  1283. $this->assertHtml($expected, $result);
  1284. }
  1285. /**
  1286. * testFormSecurityMultipleFields method
  1287. *
  1288. * Test secure() with multiple row form. Ensure hash is correct.
  1289. *
  1290. * @return void
  1291. */
  1292. public function testFormSecurityMultipleFields()
  1293. {
  1294. $this->Form->request->params['_Token'] = 'foo';
  1295. $fields = [
  1296. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1297. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1298. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1299. ];
  1300. $result = $this->Form->secure($fields);
  1301. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  1302. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  1303. $tokenDebug = urlencode(json_encode([
  1304. '',
  1305. $fields,
  1306. []
  1307. ]));
  1308. $expected = [
  1309. 'div' => ['style' => 'display:none;'],
  1310. ['input' => [
  1311. 'type' => 'hidden',
  1312. 'name' => '_Token[fields]',
  1313. 'value' => $hash,
  1314. 'autocomplete' => 'off',
  1315. ]],
  1316. ['input' => [
  1317. 'type' => 'hidden',
  1318. 'name' => '_Token[unlocked]',
  1319. 'autocomplete' => 'off',
  1320. 'value' => ''
  1321. ]],
  1322. ['input' => [
  1323. 'type' => 'hidden',
  1324. 'name' => '_Token[debug]',
  1325. 'value' => $tokenDebug,
  1326. 'autocomplete' => 'off',
  1327. ]],
  1328. '/div'
  1329. ];
  1330. $this->assertHtml($expected, $result);
  1331. }
  1332. /**
  1333. * testFormSecurityMultipleSubmitButtons
  1334. *
  1335. * test form submit generation and ensure that _Token is only created on end()
  1336. *
  1337. * @return void
  1338. */
  1339. public function testFormSecurityMultipleSubmitButtons()
  1340. {
  1341. $this->Form->request->params['_Token'] = 'testKey';
  1342. $this->Form->create($this->article);
  1343. $this->Form->text('Address.title');
  1344. $this->Form->text('Address.first_name');
  1345. $result = $this->Form->submit('Save', ['name' => 'save']);
  1346. $expected = [
  1347. 'div' => ['class' => 'submit'],
  1348. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1349. '/div',
  1350. ];
  1351. $this->assertHtml($expected, $result);
  1352. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1353. $expected = [
  1354. 'div' => ['class' => 'submit'],
  1355. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1356. '/div',
  1357. ];
  1358. $this->assertHtml($expected, $result);
  1359. $result = $this->Form->end();
  1360. $tokenDebug = urlencode(json_encode([
  1361. '/articles/add',
  1362. [
  1363. 'Address.title',
  1364. 'Address.first_name',
  1365. ],
  1366. ['save', 'cancel']
  1367. ]));
  1368. $expected = [
  1369. 'div' => ['style' => 'display:none;'],
  1370. ['input' => [
  1371. 'type' => 'hidden',
  1372. 'name' => '_Token[fields]',
  1373. 'autocomplete',
  1374. 'value'
  1375. ]],
  1376. ['input' => [
  1377. 'type' => 'hidden',
  1378. 'name' => '_Token[unlocked]',
  1379. 'value' => 'cancel%7Csave',
  1380. 'autocomplete' => 'off',
  1381. ]],
  1382. ['input' => [
  1383. 'type' => 'hidden',
  1384. 'name' => '_Token[debug]',
  1385. 'value' => $tokenDebug,
  1386. 'autocomplete' => 'off',
  1387. ]],
  1388. '/div'
  1389. ];
  1390. $this->assertHtml($expected, $result);
  1391. }
  1392. /**
  1393. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1394. *
  1395. * @return void
  1396. */
  1397. public function testSecurityButtonNestedNamed()
  1398. {
  1399. $key = 'testKey';
  1400. $this->Form->request->params['_csrfToken'] = $key;
  1401. $this->Form->create('Addresses');
  1402. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1403. $result = $this->Form->unlockField();
  1404. $this->assertEquals(['Address.button'], $result);
  1405. }
  1406. /**
  1407. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1408. *
  1409. * @return void
  1410. */
  1411. public function testSecuritySubmitNestedNamed()
  1412. {
  1413. $this->Form->request->params['_Token'] = 'testKey';
  1414. $this->Form->create($this->article);
  1415. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1416. $result = $this->Form->unlockField();
  1417. $this->assertEquals(['Address.button'], $result);
  1418. }
  1419. /**
  1420. * Test that the correct fields are unlocked for image submits with no names.
  1421. *
  1422. * @return void
  1423. */
  1424. public function testSecuritySubmitImageNoName()
  1425. {
  1426. $this->Form->request->params['_Token'] = 'testKey';
  1427. $this->Form->create(false);
  1428. $result = $this->Form->submit('save.png');
  1429. $expected = [
  1430. 'div' => ['class' => 'submit'],
  1431. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1432. '/div'
  1433. ];
  1434. $this->assertHtml($expected, $result);
  1435. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1436. }
  1437. /**
  1438. * Test that the correct fields are unlocked for image submits with names.
  1439. *
  1440. * @return void
  1441. */
  1442. public function testSecuritySubmitImageName()
  1443. {
  1444. $this->Form->request->params['_Token'] = 'testKey';
  1445. $this->Form->create(null);
  1446. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1447. $expected = [
  1448. 'div' => ['class' => 'submit'],
  1449. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1450. '/div'
  1451. ];
  1452. $this->assertHtml($expected, $result);
  1453. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1454. }
  1455. /**
  1456. * testFormSecurityMultipleControlFields method
  1457. *
  1458. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1459. *
  1460. * @return void
  1461. */
  1462. public function testFormSecurityMultipleControlFields()
  1463. {
  1464. $this->Form->request->params['_Token'] = 'testKey';
  1465. $this->Form->create();
  1466. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1467. $this->Form->control('Addresses.0.title');
  1468. $this->Form->control('Addresses.0.first_name');
  1469. $this->Form->control('Addresses.0.last_name');
  1470. $this->Form->control('Addresses.0.address');
  1471. $this->Form->control('Addresses.0.city');
  1472. $this->Form->control('Addresses.0.phone');
  1473. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1474. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1475. $this->Form->control('Addresses.1.title');
  1476. $this->Form->control('Addresses.1.first_name');
  1477. $this->Form->control('Addresses.1.last_name');
  1478. $this->Form->control('Addresses.1.address');
  1479. $this->Form->control('Addresses.1.city');
  1480. $this->Form->control('Addresses.1.phone');
  1481. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1482. $result = $this->Form->secure($this->Form->fields);
  1483. $hash = '8bd3911b07b507408b1a969b31ee90c47b7d387e%3AAddresses.0.id%7CAddresses.1.id';
  1484. $tokenDebug = urlencode(json_encode([
  1485. '/articles/add',
  1486. [
  1487. 'Addresses.0.id' => '123456',
  1488. 'Addresses.0.title',
  1489. 'Addresses.0.first_name',
  1490. 'Addresses.0.last_name',
  1491. 'Addresses.0.address',
  1492. 'Addresses.0.city',
  1493. 'Addresses.0.phone',
  1494. 'Addresses.0.primary',
  1495. 'Addresses.1.id' => '654321',
  1496. 'Addresses.1.title',
  1497. 'Addresses.1.first_name',
  1498. 'Addresses.1.last_name',
  1499. 'Addresses.1.address',
  1500. 'Addresses.1.city',
  1501. 'Addresses.1.phone',
  1502. 'Addresses.1.primary',
  1503. ],
  1504. []
  1505. ]));
  1506. $expected = [
  1507. 'div' => ['style' => 'display:none;'],
  1508. ['input' => [
  1509. 'type' => 'hidden',
  1510. 'name' => '_Token[fields]',
  1511. 'value' => $hash,
  1512. 'autocomplete' => 'off',
  1513. ]],
  1514. ['input' => [
  1515. 'type' => 'hidden',
  1516. 'name' => '_Token[unlocked]',
  1517. 'autocomplete' => 'off',
  1518. 'value' => ''
  1519. ]],
  1520. ['input' => [
  1521. 'type' => 'hidden',
  1522. 'name' => '_Token[debug]',
  1523. 'value' => $tokenDebug,
  1524. 'autocomplete' => 'off',
  1525. ]],
  1526. '/div'
  1527. ];
  1528. $this->assertHtml($expected, $result);
  1529. }
  1530. /**
  1531. * testFormSecurityArrayFields method
  1532. *
  1533. * Test form security with Model.field.0 style inputs.
  1534. *
  1535. * @return void
  1536. */
  1537. public function testFormSecurityArrayFields()
  1538. {
  1539. $this->Form->request->params['_Token'] = 'testKey';
  1540. $this->Form->create();
  1541. $this->Form->text('Address.primary.1');
  1542. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1543. $this->Form->text('Address.secondary.1.0');
  1544. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1545. }
  1546. /**
  1547. * testFormSecurityMultipleControlDisabledFields method
  1548. *
  1549. * Test secure form generation with multiple records and disabled fields.
  1550. *
  1551. * @return void
  1552. */
  1553. public function testFormSecurityMultipleControlDisabledFields()
  1554. {
  1555. $this->Form->request->params['_Token'] = [
  1556. 'unlockedFields' => ['first_name', 'address']
  1557. ];
  1558. $this->Form->create();
  1559. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1560. $this->Form->text('Addresses.0.title');
  1561. $this->Form->text('Addresses.0.first_name');
  1562. $this->Form->text('Addresses.0.last_name');
  1563. $this->Form->text('Addresses.0.address');
  1564. $this->Form->text('Addresses.0.city');
  1565. $this->Form->text('Addresses.0.phone');
  1566. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1567. $this->Form->text('Addresses.1.title');
  1568. $this->Form->text('Addresses.1.first_name');
  1569. $this->Form->text('Addresses.1.last_name');
  1570. $this->Form->text('Addresses.1.address');
  1571. $this->Form->text('Addresses.1.city');
  1572. $this->Form->text('Addresses.1.phone');
  1573. $result = $this->Form->secure($this->Form->fields);
  1574. $hash = '4fb10b46873df4ddd4ef5c3a19944a2f29b38991%3AAddresses.0.id%7CAddresses.1.id';
  1575. $tokenDebug = urlencode(json_encode([
  1576. '/articles/add',
  1577. [
  1578. 'Addresses.0.id' => '123456',
  1579. 'Addresses.0.title',
  1580. 'Addresses.0.last_name',
  1581. 'Addresses.0.city',
  1582. 'Addresses.0.phone',
  1583. 'Addresses.1.id' => '654321',
  1584. 'Addresses.1.title',
  1585. 'Addresses.1.last_name',
  1586. 'Addresses.1.city',
  1587. 'Addresses.1.phone'
  1588. ],
  1589. [
  1590. 'first_name',
  1591. 'address'
  1592. ]
  1593. ]));
  1594. $expected = [
  1595. 'div' => ['style' => 'display:none;'],
  1596. ['input' => [
  1597. 'type' => 'hidden',
  1598. 'name' => '_Token[fields]',
  1599. 'autocomplete' => 'off',
  1600. 'value' => $hash
  1601. ]],
  1602. ['input' => [
  1603. 'type' => 'hidden',
  1604. 'name' => '_Token[unlocked]',
  1605. 'autocomplete' => 'off',
  1606. 'value' => 'address%7Cfirst_name',
  1607. ]],
  1608. ['input' => [
  1609. 'type' => 'hidden',
  1610. 'name' => '_Token[debug]',
  1611. 'autocomplete' => 'off',
  1612. 'value' => $tokenDebug,
  1613. ]],
  1614. '/div'
  1615. ];
  1616. $this->assertHtml($expected, $result);
  1617. }
  1618. /**
  1619. * testFormSecurityControlDisabledFields method
  1620. *
  1621. * Test single record form with disabled fields.
  1622. *
  1623. * @return void
  1624. */
  1625. public function testFormSecurityControlUnlockedFields()
  1626. {
  1627. $this->Form->request->params['_Token'] = [
  1628. 'unlockedFields' => ['first_name', 'address']
  1629. ];
  1630. $this->Form->create();
  1631. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1632. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1633. $this->Form->text('Addresses.title');
  1634. $this->Form->text('Addresses.first_name');
  1635. $this->Form->text('Addresses.last_name');
  1636. $this->Form->text('Addresses.address');
  1637. $this->Form->text('Addresses.city');
  1638. $this->Form->text('Addresses.phone');
  1639. $result = $this->Form->fields;
  1640. $expected = [
  1641. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1642. 'Addresses.city', 'Addresses.phone'
  1643. ];
  1644. $this->assertEquals($expected, $result);
  1645. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1646. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1647. $tokenDebug = urlencode(json_encode([
  1648. '/articles/add',
  1649. [
  1650. 'Addresses.id' => '123456',
  1651. 'Addresses.title',
  1652. 'Addresses.last_name',
  1653. 'Addresses.city',
  1654. 'Addresses.phone'
  1655. ],
  1656. [
  1657. 'first_name',
  1658. 'address'
  1659. ]
  1660. ]));
  1661. $expected = [
  1662. 'div' => ['style' => 'display:none;'],
  1663. ['input' => [
  1664. 'type' => 'hidden',
  1665. 'name' => '_Token[fields]',
  1666. 'value' => $hash,
  1667. 'autocomplete' => 'off',
  1668. 'data-foo' => 'bar',
  1669. ]],
  1670. ['input' => [
  1671. 'type' => 'hidden',
  1672. 'name' => '_Token[unlocked]',
  1673. 'value' => 'address%7Cfirst_name',
  1674. 'autocomplete' => 'off',
  1675. 'data-foo' => 'bar',
  1676. ]],
  1677. ['input' => [
  1678. 'type' => 'hidden', 'name' => '_Token[debug]',
  1679. 'value' => $tokenDebug,
  1680. 'autocomplete' => 'off',
  1681. 'data-foo' => 'bar'
  1682. ]],
  1683. '/div'
  1684. ];
  1685. $this->assertHtml($expected, $result);
  1686. }
  1687. /**
  1688. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1689. *
  1690. * Test single record form with debugSecurity param.
  1691. *
  1692. * @return void
  1693. */
  1694. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1695. {
  1696. $this->Form->request->params['_Token'] = [
  1697. 'unlockedFields' => ['first_name', 'address']
  1698. ];
  1699. $this->Form->create();
  1700. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1701. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1702. $this->Form->text('Addresses.title');
  1703. $this->Form->text('Addresses.first_name');
  1704. $this->Form->text('Addresses.last_name');
  1705. $this->Form->text('Addresses.address');
  1706. $this->Form->text('Addresses.city');
  1707. $this->Form->text('Addresses.phone');
  1708. $result = $this->Form->fields;
  1709. $expected = [
  1710. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1711. 'Addresses.city', 'Addresses.phone'
  1712. ];
  1713. $this->assertEquals($expected, $result);
  1714. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1715. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1716. $tokenDebug = urlencode(json_encode([
  1717. '/articles/add',
  1718. [
  1719. 'Addresses.id' => '123456',
  1720. 'Addresses.title',
  1721. 'Addresses.last_name',
  1722. 'Addresses.city',
  1723. 'Addresses.phone'
  1724. ],
  1725. [
  1726. 'first_name',
  1727. 'address'
  1728. ]
  1729. ]));
  1730. $expected = [
  1731. 'div' => ['style' => 'display:none;'],
  1732. ['input' => [
  1733. 'type' => 'hidden',
  1734. 'name' => '_Token[fields]',
  1735. 'value' => $hash,
  1736. 'autocomplete' => 'off',
  1737. 'data-foo' => 'bar',
  1738. ]],
  1739. ['input' => [
  1740. 'type' => 'hidden',
  1741. 'name' => '_Token[unlocked]',
  1742. 'value' => 'address%7Cfirst_name',
  1743. 'autocomplete' => 'off',
  1744. 'data-foo' => 'bar',
  1745. ]],
  1746. ['input' => [
  1747. 'type' => 'hidden', 'name' => '_Token[debug]',
  1748. 'value' => $tokenDebug,
  1749. 'autocomplete' => 'off',
  1750. 'data-foo' => 'bar'
  1751. ]],
  1752. '/div'
  1753. ];
  1754. $this->assertHtml($expected, $result);
  1755. }
  1756. /**
  1757. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1758. *
  1759. * Debug is false, debugSecurity is true -> no debug
  1760. *
  1761. * @return void
  1762. */
  1763. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1764. {
  1765. $this->Form->request->params['_Token'] = [
  1766. 'unlockedFields' => ['first_name', 'address']
  1767. ];
  1768. $this->Form->create();
  1769. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1770. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1771. $this->Form->text('Addresses.title');
  1772. $this->Form->text('Addresses.first_name');
  1773. $this->Form->text('Addresses.last_name');
  1774. $this->Form->text('Addresses.address');
  1775. $this->Form->text('Addresses.city');
  1776. $this->Form->text('Addresses.phone');
  1777. $result = $this->Form->fields;
  1778. $expected = [
  1779. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1780. 'Addresses.city', 'Addresses.phone'
  1781. ];
  1782. $this->assertEquals($expected, $result);
  1783. Configure::write('debug', false);
  1784. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1785. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1786. $expected = [
  1787. 'div' => ['style' => 'display:none;'],
  1788. ['input' => [
  1789. 'type' => 'hidden',
  1790. 'name' => '_Token[fields]',
  1791. 'value' => $hash,
  1792. 'autocomplete' => 'off',
  1793. 'data-foo' => 'bar',
  1794. ]],
  1795. ['input' => [
  1796. 'type' => 'hidden',
  1797. 'name' => '_Token[unlocked]',
  1798. 'value' => 'address%7Cfirst_name',
  1799. 'autocomplete' => 'off',
  1800. 'data-foo' => 'bar',
  1801. ]],
  1802. '/div'
  1803. ];
  1804. $this->assertHtml($expected, $result);
  1805. }
  1806. /**
  1807. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1808. *
  1809. * Test single record form with debugSecurity param.
  1810. *
  1811. * @return void
  1812. */
  1813. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1814. {
  1815. $this->Form->request->params['_Token'] = [
  1816. 'unlockedFields' => ['first_name', 'address']
  1817. ];
  1818. $this->Form->create();
  1819. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1820. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1821. $this->Form->text('Addresses.title');
  1822. $this->Form->text('Addresses.first_name');
  1823. $this->Form->text('Addresses.last_name');
  1824. $this->Form->text('Addresses.address');
  1825. $this->Form->text('Addresses.city');
  1826. $this->Form->text('Addresses.phone');
  1827. $result = $this->Form->fields;
  1828. $expected = [
  1829. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1830. 'Addresses.city', 'Addresses.phone'
  1831. ];
  1832. $this->assertEquals($expected, $result);
  1833. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1834. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1835. $expected = [
  1836. 'div' => ['style' => 'display:none;'],
  1837. ['input' => [
  1838. 'type' => 'hidden',
  1839. 'name' => '_Token[fields]',
  1840. 'value' => $hash,
  1841. 'autocomplete' => 'off',
  1842. 'data-foo' => 'bar',
  1843. ]],
  1844. ['input' => [
  1845. 'type' => 'hidden',
  1846. 'name' => '_Token[unlocked]',
  1847. 'value' => 'address%7Cfirst_name',
  1848. 'autocomplete' => 'off',
  1849. 'data-foo' => 'bar',
  1850. ]],
  1851. '/div'
  1852. ];
  1853. $this->assertHtml($expected, $result);
  1854. }
  1855. /**
  1856. * testFormSecureWithCustomNameAttribute method
  1857. *
  1858. * Test securing inputs with custom name attributes.
  1859. *
  1860. * @return void
  1861. */
  1862. public function testFormSecureWithCustomNameAttribute()
  1863. {
  1864. $this->Form->request->params['_Token'] = 'testKey';
  1865. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1866. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1867. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1868. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1869. }
  1870. /**
  1871. * testFormSecuredControl method
  1872. *
  1873. * Test generation of entire secure form, assertions made on control() output.
  1874. *
  1875. * @return void
  1876. */
  1877. public function testFormSecuredControl()
  1878. {
  1879. $this->Form->request->params['_csrfToken'] = 'testKey';
  1880. $this->Form->request->params['_Token'] = 'stuff';
  1881. $this->article['schema'] = [
  1882. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1883. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1884. ];
  1885. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1886. $encoding = strtolower(Configure::read('App.encoding'));
  1887. $expected = [
  1888. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1889. 'div' => ['style' => 'display:none;'],
  1890. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1891. ['input' => [
  1892. 'type' => 'hidden',
  1893. 'name' => '_csrfToken',
  1894. 'value' => 'testKey',
  1895. 'autocomplete' => 'off'
  1896. ]],
  1897. '/div'
  1898. ];
  1899. $this->assertHtml($expected, $result);
  1900. $result = $this->Form->control('ratio');
  1901. $expected = [
  1902. 'div' => ['class'],
  1903. 'label' => ['for'],
  1904. 'Ratio',
  1905. '/label',
  1906. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1907. '/div',
  1908. ];
  1909. $this->assertHtml($expected, $result);
  1910. $result = $this->Form->control('population');
  1911. $expected = [
  1912. 'div' => ['class'],
  1913. 'label' => ['for'],
  1914. 'Population',
  1915. '/label',
  1916. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1917. '/div',
  1918. ];
  1919. $this->assertHtml($expected, $result);
  1920. $result = $this->Form->control('published', ['type' => 'text']);
  1921. $expected = [
  1922. 'div' => ['class' => 'input text'],
  1923. 'label' => ['for' => 'published'],
  1924. 'Published',
  1925. '/label',
  1926. ['input' => [
  1927. 'type' => 'text',
  1928. 'name' => 'published',
  1929. 'id' => 'published'
  1930. ]],
  1931. '/div'
  1932. ];
  1933. $this->assertHtml($expected, $result);
  1934. $result = $this->Form->control('other', ['type' => 'text']);
  1935. $expected = [
  1936. 'div' => ['class' => 'input text'],
  1937. 'label' => ['for' => 'other'],
  1938. 'Other',
  1939. '/label',
  1940. ['input' => [
  1941. 'type' => 'text',
  1942. 'name' => 'other',
  1943. 'id',
  1944. ]],
  1945. '/div'
  1946. ];
  1947. $this->assertHtml($expected, $result);
  1948. $result = $this->Form->hidden('stuff');
  1949. $expected = [
  1950. 'input' => [
  1951. 'type' => 'hidden',
  1952. 'name' => 'stuff'
  1953. ]
  1954. ];
  1955. $this->assertHtml($expected, $result);
  1956. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1957. $expected = ['input' => [
  1958. 'type' => 'hidden',
  1959. 'name' => 'hidden',
  1960. 'value' => '0'
  1961. ]];
  1962. $this->assertHtml($expected, $result);
  1963. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1964. $expected = [
  1965. 'div' => ['class' => 'input checkbox'],
  1966. ['input' => [
  1967. 'type' => 'hidden',
  1968. 'name' => 'something',
  1969. 'value' => '0'
  1970. ]],
  1971. 'label' => ['for' => 'something'],
  1972. ['input' => [
  1973. 'type' => 'checkbox',
  1974. 'name' => 'something',
  1975. 'value' => '1',
  1976. 'id' => 'something'
  1977. ]],
  1978. 'Something',
  1979. '/label',
  1980. '/div'
  1981. ];
  1982. $this->assertHtml($expected, $result);
  1983. $result = $this->Form->fields;
  1984. $expectedFields = [
  1985. 'ratio',
  1986. 'population',
  1987. 'published',
  1988. 'other',
  1989. 'stuff' => '',
  1990. 'hidden' => '0',
  1991. 'something'
  1992. ];
  1993. $this->assertEquals($expectedFields, $result);
  1994. $result = $this->Form->secure($this->Form->fields);
  1995. $tokenDebug = urlencode(json_encode([
  1996. '/articles/add',
  1997. $expectedFields,
  1998. []
  1999. ]));
  2000. $expected = [
  2001. 'div' => ['style' => 'display:none;'],
  2002. ['input' => [
  2003. 'type' => 'hidden',
  2004. 'name' => '_Token[fields]',
  2005. 'value',
  2006. 'autocomplete'
  2007. ]],
  2008. ['input' => [
  2009. 'type' => 'hidden',
  2010. 'name' => '_Token[unlocked]',
  2011. 'value' => '',
  2012. 'autocomplete' => 'off'
  2013. ]],
  2014. ['input' => [
  2015. 'type' => 'hidden', 'name' => '_Token[debug]',
  2016. 'value' => $tokenDebug,
  2017. 'autocomplete' => 'off'
  2018. ]],
  2019. '/div'
  2020. ];
  2021. $this->assertHtml($expected, $result);
  2022. }
  2023. /**
  2024. * testSecuredControlCustomName method
  2025. *
  2026. * Test secured inputs with custom names.
  2027. *
  2028. * @return void
  2029. */
  2030. public function testSecuredControlCustomName()
  2031. {
  2032. $this->Form->request->params['_Token'] = 'testKey';
  2033. $this->assertEquals([], $this->Form->fields);
  2034. $this->Form->text('text_input', [
  2035. 'name' => 'Option[General.default_role]',
  2036. ]);
  2037. $expected = ['Option.General.default_role'];
  2038. $this->assertEquals($expected, $this->Form->fields);
  2039. $this->Form->select('select_box', [1, 2], [
  2040. 'name' => 'Option[General.select_role]',
  2041. ]);
  2042. $expected[] = 'Option.General.select_role';
  2043. $this->assertEquals($expected, $this->Form->fields);
  2044. $this->Form->text('other.things[]');
  2045. $expected[] = 'other.things';
  2046. $this->assertEquals($expected, $this->Form->fields);
  2047. }
  2048. /**
  2049. * testSecuredControlDuplicate method
  2050. *
  2051. * Test that a hidden field followed by a visible field
  2052. * undoes the hidden field locking.
  2053. *
  2054. * @return void
  2055. */
  2056. public function testSecuredControlDuplicate()
  2057. {
  2058. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2059. $this->assertEquals([], $this->Form->fields);
  2060. $this->Form->control('text_val', [
  2061. 'type' => 'hidden',
  2062. 'value' => 'some text',
  2063. ]);
  2064. $expected = ['text_val' => 'some text'];
  2065. $this->assertEquals($expected, $this->Form->fields);
  2066. $this->Form->control('text_val', [
  2067. 'type' => 'text',
  2068. ]);
  2069. $expected = ['text_val'];
  2070. $this->assertEquals($expected, $this->Form->fields);
  2071. }
  2072. /**
  2073. * testFormSecuredFileControl method
  2074. *
  2075. * Tests that the correct keys are added to the field hash index.
  2076. *
  2077. * @return void
  2078. */
  2079. public function testFormSecuredFileControl()
  2080. {
  2081. $this->assertEquals([], $this->Form->fields);
  2082. $this->Form->file('Attachment.file');
  2083. $expected = [
  2084. 'Attachment.file.name', 'Attachment.file.type',
  2085. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2086. 'Attachment.file.size'
  2087. ];
  2088. $this->assertEquals($expected, $this->Form->fields);
  2089. }
  2090. /**
  2091. * testFormSecuredMultipleSelect method
  2092. *
  2093. * Test that multiple selects keys are added to field hash.
  2094. *
  2095. * @return void
  2096. */
  2097. public function testFormSecuredMultipleSelect()
  2098. {
  2099. $this->Form->request->params['_csrfToken'] = 'testKey';
  2100. $this->assertEquals([], $this->Form->fields);
  2101. $options = ['1' => 'one', '2' => 'two'];
  2102. $this->Form->select('Model.select', $options);
  2103. $expected = ['Model.select'];
  2104. $this->assertEquals($expected, $this->Form->fields);
  2105. $this->Form->fields = [];
  2106. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2107. $this->assertEquals($expected, $this->Form->fields);
  2108. }
  2109. /**
  2110. * testFormSecuredRadio method
  2111. *
  2112. * @return void
  2113. */
  2114. public function testFormSecuredRadio()
  2115. {
  2116. $this->Form->request->params['_Token'] = 'testKey';
  2117. $this->assertEquals([], $this->Form->fields);
  2118. $options = ['1' => 'option1', '2' => 'option2'];
  2119. $this->Form->radio('Test.test', $options);
  2120. $expected = ['Test.test'];
  2121. $this->assertEquals($expected, $this->Form->fields);
  2122. $this->Form->radio('Test.all', $options, [
  2123. 'disabled' => ['option1', 'option2']
  2124. ]);
  2125. $expected = ['Test.test', 'Test.all' => ''];
  2126. $this->assertEquals($expected, $this->Form->fields);
  2127. $this->Form->radio('Test.some', $options, [
  2128. 'disabled' => ['option1']
  2129. ]);
  2130. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2131. $this->assertEquals($expected, $this->Form->fields);
  2132. }
  2133. /**
  2134. * testFormSecuredAndDisabledNotAssoc method
  2135. *
  2136. * Test that when disabled is in a list based attribute array it works.
  2137. *
  2138. * @return void
  2139. */
  2140. public function testFormSecuredAndDisabledNotAssoc()
  2141. {
  2142. $this->Form->request->params['_Token'] = 'testKey';
  2143. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2144. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2145. $this->Form->text('Model.text', ['disabled']);
  2146. $this->Form->textarea('Model.textarea', ['disabled']);
  2147. $this->Form->password('Model.password', ['disabled']);
  2148. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2149. $expected = [
  2150. 'Model.radio' => ''
  2151. ];
  2152. $this->assertEquals($expected, $this->Form->fields);
  2153. }
  2154. /**
  2155. * testFormSecuredAndDisabled method
  2156. *
  2157. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2158. * hashing.
  2159. *
  2160. * @return void
  2161. */
  2162. public function testFormSecuredAndDisabled()
  2163. {
  2164. $this->Form->request->params['_Token'] = 'testKey';
  2165. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2166. $this->Form->text('Model.text', ['disabled' => true]);
  2167. $this->Form->password('Model.text', ['disabled' => true]);
  2168. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2169. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2170. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2171. $this->Form->year('Model.year', ['disabled' => true]);
  2172. $this->Form->month('Model.month', ['disabled' => true]);
  2173. $this->Form->day('Model.day', ['disabled' => true]);
  2174. $this->Form->hour('Model.hour', ['disabled' => true]);
  2175. $this->Form->minute('Model.minute', ['disabled' => true]);
  2176. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2177. $expected = [
  2178. 'Model.radio' => ''
  2179. ];
  2180. $this->assertEquals($expected, $this->Form->fields);
  2181. }
  2182. /**
  2183. * testDisableSecurityUsingForm method
  2184. *
  2185. * @return void
  2186. */
  2187. public function testDisableSecurityUsingForm()
  2188. {
  2189. $this->Form->request->params['_Token'] = [
  2190. 'disabledFields' => []
  2191. ];
  2192. $this->Form->create();
  2193. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2194. $this->Form->text('Addresses.title');
  2195. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2196. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2197. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2198. $result = $this->Form->fields;
  2199. $expected = [
  2200. 'Addresses.id' => '123456', 'Addresses.title',
  2201. ];
  2202. $this->assertEquals($expected, $result);
  2203. }
  2204. /**
  2205. * testUnlockFieldAddsToList method
  2206. *
  2207. * Test disableField.
  2208. *
  2209. * @return void
  2210. */
  2211. public function testUnlockFieldAddsToList()
  2212. {
  2213. $this->Form->request->params['_Token'] = [
  2214. 'unlockedFields' => []
  2215. ];
  2216. $this->Form->unlockField('Contact.name');
  2217. $this->Form->text('Contact.name');
  2218. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2219. $this->assertEquals([], $this->Form->fields);
  2220. }
  2221. /**
  2222. * testUnlockFieldRemovingFromFields method
  2223. *
  2224. * Test unlockField removing from fields array.
  2225. *
  2226. * @return void
  2227. */
  2228. public function testUnlockFieldRemovingFromFields()
  2229. {
  2230. $this->Form->request->params['_Token'] = [
  2231. 'unlockedFields' => []
  2232. ];
  2233. $this->Form->create($this->article);
  2234. $this->Form->hidden('Article.id', ['value' => 1]);
  2235. $this->Form->text('Article.title');
  2236. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2237. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  2238. $this->Form->unlockField('Article.title');
  2239. $this->Form->unlockField('Article.id');
  2240. $this->assertEquals([], $this->Form->fields);
  2241. }
  2242. /**
  2243. * testResetUnlockFields method
  2244. *
  2245. * Test reset unlockFields, when create new form.
  2246. *
  2247. * @return void
  2248. */
  2249. public function testResetUnlockFields()
  2250. {
  2251. $this->Form->request->params['_Token'] = [
  2252. 'key' => 'testKey',
  2253. 'unlockedFields' => []
  2254. ];
  2255. $this->Form->unlockField('Contact.id');
  2256. $this->Form->create('Contact');
  2257. $this->Form->hidden('Contact.id', ['value' => 1]);
  2258. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2259. $this->Form->end();
  2260. $this->Form->create('Contact');
  2261. $this->Form->hidden('Contact.id', ['value' => 1]);
  2262. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2263. }
  2264. /**
  2265. * testSecuredFormUrlIgnoresHost method
  2266. *
  2267. * Test that only the path + query elements of a form's URL show up in their hash.
  2268. *
  2269. * @return void
  2270. */
  2271. public function testSecuredFormUrlIgnoresHost()
  2272. {
  2273. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2274. $expected = '0ff0c85cd70584d8fd18fa136846d22c66c21e2d%3A';
  2275. $this->Form->create($this->article, [
  2276. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2277. ]);
  2278. $result = $this->Form->secure();
  2279. $this->assertContains($expected, $result);
  2280. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2281. $result = $this->Form->secure();
  2282. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2283. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2284. $result = $this->Form->secure();
  2285. $this->assertContains($expected, $result, 'URL path + query should work.');
  2286. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2287. $result = $this->Form->secure();
  2288. $this->assertNotContains($expected, $result, 'URL is different');
  2289. }
  2290. /**
  2291. * testSecuredFormUrlHasHtmlAndIdentifier method
  2292. *
  2293. * Test that URL, HTML and identifier show up in their hashes.
  2294. *
  2295. * @return void
  2296. */
  2297. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2298. {
  2299. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2300. $expected = 'ece0693fb1b19ca116133db1832ac29baaf41ce5%3A';
  2301. $this->Form->create($this->article, [
  2302. 'url' => [
  2303. 'controller' => 'articles',
  2304. 'action' => 'view',
  2305. '?' => [
  2306. 'page' => 1,
  2307. 'limit' => 10,
  2308. 'html' => '<>"',
  2309. ],
  2310. '#' => 'result',
  2311. ],
  2312. ]);
  2313. $result = $this->Form->secure();
  2314. $this->assertContains($expected, $result);
  2315. $this->Form->create($this->article, [
  2316. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2317. ]);
  2318. $result = $this->Form->secure();
  2319. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2320. $this->Form->create($this->article, [
  2321. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2322. ]);
  2323. $result = $this->Form->secure();
  2324. $this->assertContains($expected, $result, 'URL path + query should work.');
  2325. }
  2326. /**
  2327. * testErrorMessageDisplay method
  2328. *
  2329. * Test error message display.
  2330. *
  2331. * @return void
  2332. */
  2333. public function testErrorMessageDisplay()
  2334. {
  2335. $this->article['errors'] = [
  2336. 'Article' => [
  2337. 'title' => 'error message',
  2338. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2339. ]
  2340. ];
  2341. $this->Form->create($this->article);
  2342. $result = $this->Form->control('Article.title');
  2343. $expected = [
  2344. 'div' => ['class' => 'input text error'],
  2345. 'label' => ['for' => 'article-title'],
  2346. 'Title',
  2347. '/label',
  2348. 'input' => [
  2349. 'type' => 'text', 'name' => 'Article[title]',
  2350. 'id' => 'article-title', 'class' => 'form-error'
  2351. ],
  2352. ['div' => ['class' => 'error-message']],
  2353. 'error message',
  2354. '/div',
  2355. '/div'
  2356. ];
  2357. $this->assertHtml($expected, $result);
  2358. $result = $this->Form->control('Article.title', [
  2359. 'templates' => [
  2360. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2361. ]
  2362. ]);
  2363. $expected = [
  2364. 'div' => ['class' => 'input text error'],
  2365. 'label' => ['for' => 'article-title'],
  2366. 'Title',
  2367. '/label',
  2368. 'input' => [
  2369. 'type' => 'text', 'name' => 'Article[title]',
  2370. 'id' => 'article-title', 'class' => 'form-error'
  2371. ],
  2372. '/div'
  2373. ];
  2374. $this->assertHtml($expected, $result);
  2375. $result = $this->Form->control('Article.content');
  2376. $expected = [
  2377. 'div' => ['class' => 'input text error'],
  2378. 'label' => ['for' => 'article-content'],
  2379. 'Content',
  2380. '/label',
  2381. 'input' => [
  2382. 'type' => 'text', 'name' => 'Article[content]',
  2383. 'id' => 'article-content', 'class' => 'form-error'
  2384. ],
  2385. ['div' => ['class' => 'error-message']],
  2386. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2387. '/div',
  2388. '/div'
  2389. ];
  2390. $this->assertHtml($expected, $result);
  2391. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2392. $expected = [
  2393. 'div' => ['class' => 'input text error'],
  2394. 'label' => ['for' => 'article-content'],
  2395. 'Content',
  2396. '/label',
  2397. 'input' => [
  2398. 'type' => 'text', 'name' => 'Article[content]',
  2399. 'id' => 'article-content', 'class' => 'form-error'
  2400. ],
  2401. ['div' => ['class' => 'error-message']],
  2402. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2403. '/div',
  2404. '/div'
  2405. ];
  2406. $this->assertHtml($expected, $result);
  2407. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2408. $expected = [
  2409. 'div' => ['class' => 'input text error'],
  2410. 'label' => ['for' => 'article-content'],
  2411. 'Content',
  2412. '/label',
  2413. 'input' => [
  2414. 'type' => 'text', 'name' => 'Article[content]',
  2415. 'id' => 'article-content', 'class' => 'form-error'
  2416. ],
  2417. ['div' => ['class' => 'error-message']],
  2418. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2419. '/div',
  2420. '/div'
  2421. ];
  2422. $this->assertHtml($expected, $result);
  2423. }
  2424. /**
  2425. * testEmptyErrorValidation method
  2426. *
  2427. * Test validation errors, when validation message is an empty string.
  2428. *
  2429. * @return void
  2430. */
  2431. public function testEmptyErrorValidation()
  2432. {
  2433. $this->article['errors'] = [
  2434. 'Article' => ['title' => '']
  2435. ];
  2436. $this->Form->create($this->article);
  2437. $result = $this->Form->control('Article.title');
  2438. $expected = [
  2439. 'div' => ['class' => 'input text error'],
  2440. 'label' => ['for' => 'article-title'],
  2441. 'Title',
  2442. '/label',
  2443. 'input' => [
  2444. 'type' => 'text', 'name' => 'Article[title]',
  2445. 'id' => 'article-title', 'class' => 'form-error'
  2446. ],
  2447. ['div' => ['class' => 'error-message']],
  2448. [],
  2449. '/div',
  2450. '/div'
  2451. ];
  2452. $this->assertHtml($expected, $result);
  2453. }
  2454. /**
  2455. * testEmptyControlErrorValidation method
  2456. *
  2457. * Test validation errors, when calling control() overriding validation message by an empty string.
  2458. *
  2459. * @return void
  2460. */
  2461. public function testEmptyControlErrorValidation()
  2462. {
  2463. $this->article['errors'] = [
  2464. 'Article' => ['title' => 'error message']
  2465. ];
  2466. $this->Form->create($this->article);
  2467. $result = $this->Form->control('Article.title', ['error' => '']);
  2468. $expected = [
  2469. 'div' => ['class' => 'input text error'],
  2470. 'label' => ['for' => 'article-title'],
  2471. 'Title',
  2472. '/label',
  2473. 'input' => [
  2474. 'type' => 'text', 'name' => 'Article[title]',
  2475. 'id' => 'article-title', 'class' => 'form-error'
  2476. ],
  2477. ['div' => ['class' => 'error-message']],
  2478. [],
  2479. '/div',
  2480. '/div'
  2481. ];
  2482. $this->assertHtml($expected, $result);
  2483. }
  2484. /**
  2485. * testControlErrorMessage method
  2486. *
  2487. * Test validation errors, when calling control() overriding validation messages.
  2488. *
  2489. * @return void
  2490. */
  2491. public function testControlErrorMessage()
  2492. {
  2493. $this->article['errors'] = [
  2494. 'title' => ['error message']
  2495. ];
  2496. $this->Form->create($this->article);
  2497. $result = $this->Form->control('title', [
  2498. 'error' => 'Custom error!'
  2499. ]);
  2500. $expected = [
  2501. 'div' => ['class' => 'input text required error'],
  2502. 'label' => ['for' => 'title'],
  2503. 'Title',
  2504. '/label',
  2505. 'input' => [
  2506. 'type' => 'text', 'name' => 'title',
  2507. 'id' => 'title', 'class' => 'form-error',
  2508. 'required' => 'required',
  2509. ],
  2510. ['div' => ['class' => 'error-message']],
  2511. 'Custom error!',
  2512. '/div',
  2513. '/div'
  2514. ];
  2515. $this->assertHtml($expected, $result);
  2516. $result = $this->Form->control('title', [
  2517. 'error' => ['error message' => 'Custom error!']
  2518. ]);
  2519. $expected = [
  2520. 'div' => ['class' => 'input text required error'],
  2521. 'label' => ['for' => 'title'],
  2522. 'Title',
  2523. '/label',
  2524. 'input' => [
  2525. 'type' => 'text',
  2526. 'name' => 'title',
  2527. 'id' => 'title',
  2528. 'class' => 'form-error',
  2529. 'required' => 'required'
  2530. ],
  2531. ['div' => ['class' => 'error-message']],
  2532. 'Custom error!',
  2533. '/div',
  2534. '/div'
  2535. ];
  2536. $this->assertHtml($expected, $result);
  2537. }
  2538. /**
  2539. * testFormValidationAssociated method
  2540. *
  2541. * Tests displaying errors for nested entities.
  2542. *
  2543. * @return void
  2544. */
  2545. public function testFormValidationAssociated()
  2546. {
  2547. $nested = new Entity(['foo' => 'bar']);
  2548. $nested->errors('foo', ['not a valid bar']);
  2549. $entity = new Entity(['nested' => $nested]);
  2550. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2551. $result = $this->Form->error('nested.foo');
  2552. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2553. }
  2554. /**
  2555. * testFormValidationAssociatedSecondLevel method
  2556. *
  2557. * Test form error display with associated model.
  2558. *
  2559. * @return void
  2560. */
  2561. public function testFormValidationAssociatedSecondLevel()
  2562. {
  2563. $inner = new Entity(['bar' => 'baz']);
  2564. $nested = new Entity(['foo' => $inner]);
  2565. $entity = new Entity(['nested' => $nested]);
  2566. $inner->errors('bar', ['not a valid one']);
  2567. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2568. $result = $this->Form->error('nested.foo.bar');
  2569. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2570. }
  2571. /**
  2572. * testFormValidationMultiRecord method
  2573. *
  2574. * Test form error display with multiple records.
  2575. *
  2576. * @return void
  2577. */
  2578. public function testFormValidationMultiRecord()
  2579. {
  2580. $one = new Entity();
  2581. $two = new Entity();
  2582. TableRegistry::get('Contacts', [
  2583. 'className' => __NAMESPACE__ . '\ContactsTable'
  2584. ]);
  2585. $one->set('email', '');
  2586. $one->errors('email', ['invalid email']);
  2587. $two->set('name', '');
  2588. $two->errors('name', ['This is wrong']);
  2589. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2590. $result = $this->Form->control('0.email');
  2591. $expected = [
  2592. 'div' => ['class' => 'input email error'],
  2593. 'label' => ['for' => '0-email'],
  2594. 'Email',
  2595. '/label',
  2596. 'input' => [
  2597. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2598. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2599. ],
  2600. ['div' => ['class' => 'error-message']],
  2601. 'invalid email',
  2602. '/div',
  2603. '/div'
  2604. ];
  2605. $this->assertHtml($expected, $result);
  2606. $result = $this->Form->control('1.name');
  2607. $expected = [
  2608. 'div' => ['class' => 'input text error'],
  2609. 'label' => ['for' => '1-name'],
  2610. 'Name',
  2611. '/label',
  2612. 'input' => [
  2613. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2614. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2615. ],
  2616. ['div' => ['class' => 'error-message']],
  2617. 'This is wrong',
  2618. '/div',
  2619. '/div'
  2620. ];
  2621. $this->assertHtml($expected, $result);
  2622. }
  2623. /**
  2624. * testControl method
  2625. *
  2626. * Test various incarnations of control().
  2627. *
  2628. * @return void
  2629. */
  2630. public function testControl()
  2631. {
  2632. TableRegistry::get('ValidateUsers', [
  2633. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2634. ]);
  2635. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2636. $result = $this->Form->control('ValidateUsers.balance');
  2637. $expected = [
  2638. 'div' => ['class'],
  2639. 'label' => ['for'],
  2640. 'Balance',
  2641. '/label',
  2642. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2643. '/div',
  2644. ];
  2645. $this->assertHtml($expected, $result);
  2646. $result = $this->Form->control('ValidateUser.cost_decimal');
  2647. $expected = [
  2648. 'div' => ['class'],
  2649. 'label' => ['for'],
  2650. 'Cost Decimal',
  2651. '/label',
  2652. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2653. '/div',
  2654. ];
  2655. $this->assertHtml($expected, $result);
  2656. $result = $this->Form->control('ValidateUser.null_decimal');
  2657. $expected = [
  2658. 'div' => ['class'],
  2659. 'label' => ['for'],
  2660. 'Null Decimal',
  2661. '/label',
  2662. 'input' => ['name', 'type' => 'number', 'id'],
  2663. '/div',
  2664. ];
  2665. $this->assertHtml($expected, $result);
  2666. }
  2667. /**
  2668. * testControlCustomization method
  2669. *
  2670. * Tests the input method and passing custom options.
  2671. *
  2672. * @return void
  2673. */
  2674. public function testControlCustomization()
  2675. {
  2676. TableRegistry::get('Contacts', [
  2677. 'className' => __NAMESPACE__ . '\ContactsTable'
  2678. ]);
  2679. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2680. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2681. $expected = [
  2682. 'div' => ['class' => 'input email'],
  2683. 'label' => ['for' => 'custom'],
  2684. 'Email',
  2685. '/label',
  2686. ['input' => [
  2687. 'type' => 'email', 'name' => 'Contact[email]',
  2688. 'id' => 'custom', 'maxlength' => 255
  2689. ]],
  2690. '/div'
  2691. ];
  2692. $this->assertHtml($expected, $result);
  2693. $result = $this->Form->control('Contact.email', [
  2694. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2695. ]);
  2696. $expected = [
  2697. '<div',
  2698. 'label' => ['for' => 'contact-email'],
  2699. 'Email',
  2700. '/label',
  2701. ['input' => [
  2702. 'type' => 'email', 'name' => 'Contact[email]',
  2703. 'id' => 'contact-email', 'maxlength' => 255
  2704. ]],
  2705. '/div'
  2706. ];
  2707. $this->assertHtml($expected, $result);
  2708. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2709. $expected = [
  2710. 'div' => ['class' => 'input text'],
  2711. 'label' => ['for' => 'contact-email'],
  2712. 'Email',
  2713. '/label',
  2714. ['input' => [
  2715. 'type' => 'text', 'name' => 'Contact[email]',
  2716. 'id' => 'contact-email', 'maxlength' => '255'
  2717. ]],
  2718. '/div'
  2719. ];
  2720. $this->assertHtml($expected, $result);
  2721. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2722. $expected = [
  2723. 'div' => ['class' => 'input text'],
  2724. 'label' => ['for' => 'contact-5-email'],
  2725. 'Email',
  2726. '/label',
  2727. ['input' => [
  2728. 'type' => 'text', 'name' => 'Contact[5][email]',
  2729. 'id' => 'contact-5-email', 'maxlength' => '255'
  2730. ]],
  2731. '/div'
  2732. ];
  2733. $this->assertHtml($expected, $result);
  2734. $result = $this->Form->control('Contact.password');
  2735. $expected = [
  2736. 'div' => ['class' => 'input password'],
  2737. 'label' => ['for' => 'contact-password'],
  2738. 'Password',
  2739. '/label',
  2740. ['input' => [
  2741. 'type' => 'password', 'name' => 'Contact[password]',
  2742. 'id' => 'contact-password'
  2743. ]],
  2744. '/div'
  2745. ];
  2746. $this->assertHtml($expected, $result);
  2747. $result = $this->Form->control('Contact.email', [
  2748. 'type' => 'file', 'class' => 'textbox'
  2749. ]);
  2750. $expected = [
  2751. 'div' => ['class' => 'input file'],
  2752. 'label' => ['for' => 'contact-email'],
  2753. 'Email',
  2754. '/label',
  2755. ['input' => [
  2756. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2757. 'id' => 'contact-email'
  2758. ]],
  2759. '/div'
  2760. ];
  2761. $this->assertHtml($expected, $result);
  2762. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2763. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2764. $result = $this->Form->control('phone');
  2765. $expected = [
  2766. 'div' => ['class' => 'input tel'],
  2767. 'label' => ['for' => 'phone'],
  2768. 'Phone',
  2769. '/label',
  2770. ['input' => [
  2771. 'type' => 'tel', 'name' => 'phone',
  2772. 'value' => 'Hello &amp; World &gt; weird chars',
  2773. 'id' => 'phone', 'maxlength' => 255
  2774. ]],
  2775. '/div'
  2776. ];
  2777. $this->assertHtml($expected, $result);
  2778. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2779. $this->Form->create();
  2780. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2781. $expected = [
  2782. 'div' => ['class' => 'input text'],
  2783. 'label' => ['for' => 'myId'],
  2784. 'Field',
  2785. '/label',
  2786. 'input' => [
  2787. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2788. 'value' => 'My value', 'id' => 'myId'
  2789. ],
  2790. '/div'
  2791. ];
  2792. $this->assertHtml($expected, $result);
  2793. $this->Form->request->data = [];
  2794. $entity->errors('field', 'Badness!');
  2795. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2796. $result = $this->Form->control('field');
  2797. $expected = [
  2798. 'div' => ['class' => 'input text error'],
  2799. 'label' => ['for' => 'field'],
  2800. 'Field',
  2801. '/label',
  2802. 'input' => [
  2803. 'type' => 'text', 'name' => 'field',
  2804. 'id' => 'field', 'class' => 'form-error'
  2805. ],
  2806. ['div' => ['class' => 'error-message']],
  2807. 'Badness!',
  2808. '/div',
  2809. '/div'
  2810. ];
  2811. $this->assertHtml($expected, $result);
  2812. $result = $this->Form->control('field', [
  2813. 'templates' => [
  2814. 'inputContainerError' => '{{content}}{{error}}',
  2815. 'error' => '<span class="error-message">{{content}}</span>'
  2816. ]
  2817. ]);
  2818. $expected = [
  2819. 'label' => ['for' => 'field'],
  2820. 'Field',
  2821. '/label',
  2822. 'input' => [
  2823. 'type' => 'text', 'name' => 'field',
  2824. 'id' => 'field', 'class' => 'form-error'
  2825. ],
  2826. ['span' => ['class' => 'error-message']],
  2827. 'Badness!',
  2828. '/span'
  2829. ];
  2830. $this->assertHtml($expected, $result);
  2831. $entity->errors('field', ['minLength'], true);
  2832. $result = $this->Form->control('field', [
  2833. 'error' => [
  2834. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2835. 'maxLength' => 'login too large'
  2836. ]
  2837. ]);
  2838. $expected = [
  2839. 'div' => ['class' => 'input text error'],
  2840. 'label' => ['for' => 'field'],
  2841. 'Field',
  2842. '/label',
  2843. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2844. ['div' => ['class' => 'error-message']],
  2845. 'Le login doit contenir au moins 2 caractères',
  2846. '/div',
  2847. '/div'
  2848. ];
  2849. $this->assertHtml($expected, $result);
  2850. $entity->errors('field', ['maxLength'], true);
  2851. $result = $this->Form->control('field', [
  2852. 'error' => [
  2853. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2854. 'maxLength' => 'login too large',
  2855. ]
  2856. ]);
  2857. $expected = [
  2858. 'div' => ['class' => 'input text error'],
  2859. 'label' => ['for' => 'field'],
  2860. 'Field',
  2861. '/label',
  2862. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2863. ['div' => ['class' => 'error-message']],
  2864. 'login too large',
  2865. '/div',
  2866. '/div'
  2867. ];
  2868. $this->assertHtml($expected, $result);
  2869. }
  2870. /**
  2871. * testControlWithTemplateFile method
  2872. *
  2873. * Test that control() accepts a template file.
  2874. *
  2875. * @return void
  2876. */
  2877. public function testControlWithTemplateFile()
  2878. {
  2879. $result = $this->Form->control('field', [
  2880. 'templates' => 'htmlhelper_tags'
  2881. ]);
  2882. $expected = [
  2883. 'label' => ['for' => 'field'],
  2884. 'Field',
  2885. '/label',
  2886. 'input' => [
  2887. 'type' => 'text', 'name' => 'field',
  2888. 'id' => 'field'
  2889. ],
  2890. ];
  2891. $this->assertHtml($expected, $result);
  2892. }
  2893. /**
  2894. * testNestedControlsEndWithBrackets method
  2895. *
  2896. * Test that nested inputs end with brackets.
  2897. *
  2898. * @return void
  2899. */
  2900. public function testNestedControlsEndWithBrackets()
  2901. {
  2902. $result = $this->Form->text('nested.text[]');
  2903. $expected = [
  2904. 'input' => [
  2905. 'type' => 'text', 'name' => 'nested[text][]'
  2906. ],
  2907. ];
  2908. $this->assertHtml($expected, $result);
  2909. $result = $this->Form->file('nested.file[]');
  2910. $expected = [
  2911. 'input' => [
  2912. 'type' => 'file', 'name' => 'nested[file][]'
  2913. ],
  2914. ];
  2915. $this->assertHtml($expected, $result);
  2916. }
  2917. /**
  2918. * testCreateIdPrefix method
  2919. *
  2920. * Test id prefix.
  2921. *
  2922. * @return void
  2923. */
  2924. public function testCreateIdPrefix()
  2925. {
  2926. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2927. $result = $this->Form->control('field');
  2928. $expected = [
  2929. 'div' => ['class' => 'input text'],
  2930. 'label' => ['for' => 'prefix-field'],
  2931. 'Field',
  2932. '/label',
  2933. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2934. '/div'
  2935. ];
  2936. $this->assertHtml($expected, $result);
  2937. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2938. $expected = [
  2939. 'div' => ['class' => 'input text'],
  2940. 'label' => ['for' => 'custom-id'],
  2941. 'Field',
  2942. '/label',
  2943. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2944. '/div'
  2945. ];
  2946. $this->assertHtml($expected, $result);
  2947. $result = $this->Form->radio('Model.field', ['option A']);
  2948. $expected = [
  2949. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2950. 'label' => ['for' => 'prefix-model-field-0'],
  2951. ['input' => [
  2952. 'type' => 'radio',
  2953. 'name' => 'Model[field]',
  2954. 'value' => '0',
  2955. 'id' => 'prefix-model-field-0'
  2956. ]],
  2957. 'option A',
  2958. '/label'
  2959. ];
  2960. $this->assertHtml($expected, $result);
  2961. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2962. $expected = [
  2963. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2964. 'label' => ['for' => 'prefix-model-field-0'],
  2965. ['input' => [
  2966. 'type' => 'radio',
  2967. 'name' => 'Model[field]',
  2968. 'value' => '0',
  2969. 'id' => 'prefix-model-field-0'
  2970. ]],
  2971. 'option A',
  2972. '/label'
  2973. ];
  2974. $this->assertHtml($expected, $result);
  2975. $result = $this->Form->select(
  2976. 'Model.multi_field',
  2977. ['first'],
  2978. ['multiple' => 'checkbox']
  2979. );
  2980. $expected = [
  2981. 'input' => [
  2982. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2983. ],
  2984. ['div' => ['class' => 'checkbox']],
  2985. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2986. ['input' => [
  2987. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2988. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2989. ]],
  2990. 'first',
  2991. '/label',
  2992. '/div',
  2993. ];
  2994. $this->assertHtml($expected, $result);
  2995. $this->Form->end();
  2996. $result = $this->Form->control('field');
  2997. $expected = [
  2998. 'div' => ['class' => 'input text'],
  2999. 'label' => ['for' => 'field'],
  3000. 'Field',
  3001. '/label',
  3002. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  3003. '/div'
  3004. ];
  3005. $this->assertHtml($expected, $result);
  3006. }
  3007. /**
  3008. * testControlZero method
  3009. *
  3010. * Test that inputs with 0 can be created.
  3011. *
  3012. * @return void
  3013. */
  3014. public function testControlZero()
  3015. {
  3016. TableRegistry::get('Contacts', [
  3017. 'className' => __NAMESPACE__ . '\ContactsTable'
  3018. ]);
  3019. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  3020. $result = $this->Form->control('0');
  3021. $expected = [
  3022. 'div' => ['class' => 'input text'],
  3023. 'label' => ['for' => '0'], '/label',
  3024. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  3025. '/div'
  3026. ];
  3027. $this->assertHtml($expected, $result);
  3028. }
  3029. /**
  3030. * testControlCheckbox method
  3031. *
  3032. * Test control() with checkbox creation.
  3033. *
  3034. * @return void
  3035. */
  3036. public function testControlCheckbox()
  3037. {
  3038. $articles = TableRegistry::get('Articles');
  3039. $articles->schema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3040. $article = $articles->newEntity();
  3041. $this->Form->create($article);
  3042. $result = $this->Form->control('Articles.active');
  3043. $expected = [
  3044. 'div' => ['class' => 'input checkbox'],
  3045. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3046. 'label' => ['for' => 'articles-active'],
  3047. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3048. 'Active',
  3049. '/label',
  3050. '/div'
  3051. ];
  3052. $this->assertHtml($expected, $result);
  3053. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3054. $expected = [
  3055. 'div' => ['class' => 'input checkbox'],
  3056. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3057. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3058. '/div'
  3059. ];
  3060. $this->assertHtml($expected, $result);
  3061. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3062. $expected = [
  3063. 'div' => ['class' => 'input checkbox'],
  3064. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3065. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3066. '/div'
  3067. ];
  3068. $this->assertHtml($expected, $result);
  3069. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3070. $expected = [
  3071. 'div' => ['class' => 'input checkbox'],
  3072. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3073. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3074. '/div'
  3075. ];
  3076. $this->assertHtml($expected, $result);
  3077. $result = $this->Form->control('Articles.disabled', [
  3078. 'label' => 'Disabled',
  3079. 'type' => 'checkbox',
  3080. 'data-foo' => 'disabled'
  3081. ]);
  3082. $expected = [
  3083. 'div' => ['class' => 'input checkbox'],
  3084. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3085. 'label' => ['for' => 'articles-disabled'],
  3086. ['input' => [
  3087. 'type' => 'checkbox',
  3088. 'name' => 'Articles[disabled]',
  3089. 'value' => '1',
  3090. 'id' => 'articles-disabled',
  3091. 'data-foo' => 'disabled'
  3092. ]],
  3093. 'Disabled',
  3094. '/label',
  3095. '/div'
  3096. ];
  3097. $this->assertHtml($expected, $result);
  3098. $result = $this->Form->control('Articles.confirm', [
  3099. 'label' => 'Confirm <b>me</b>!',
  3100. 'type' => 'checkbox',
  3101. 'escape' => false
  3102. ]);
  3103. $expected = [
  3104. 'div' => ['class' => 'input checkbox'],
  3105. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3106. 'label' => ['for' => 'articles-confirm'],
  3107. ['input' => [
  3108. 'type' => 'checkbox',
  3109. 'name' => 'Articles[confirm]',
  3110. 'value' => '1',
  3111. 'id' => 'articles-confirm',
  3112. ]],
  3113. 'Confirm <b>me</b>!',
  3114. '/label',
  3115. '/div'
  3116. ];
  3117. $this->assertHtml($expected, $result);
  3118. }
  3119. /**
  3120. * testControlHidden method
  3121. *
  3122. * Test that control() does not create wrapping div and label tag for hidden fields.
  3123. *
  3124. * @return void
  3125. */
  3126. public function testControlHidden()
  3127. {
  3128. TableRegistry::get('ValidateUsers', [
  3129. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3130. ]);
  3131. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3132. $result = $this->Form->control('ValidateUser.id');
  3133. $expected = [
  3134. 'input' => ['name', 'type' => 'hidden', 'id']
  3135. ];
  3136. $this->assertHtml($expected, $result);
  3137. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3138. $expected = [
  3139. 'input' => ['name', 'type' => 'hidden', 'id']
  3140. ];
  3141. $this->assertHtml($expected, $result);
  3142. }
  3143. /**
  3144. * testControlDatetime method
  3145. *
  3146. * Test form->control() with datetime.
  3147. *
  3148. * @return void
  3149. */
  3150. public function testControlDatetime()
  3151. {
  3152. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3153. ->setMethods(['datetime'])
  3154. ->setConstructorArgs([new View()])
  3155. ->getMock();
  3156. $this->Form->expects($this->once())->method('datetime')
  3157. ->with('prueba', [
  3158. 'type' => 'datetime',
  3159. 'timeFormat' => 24,
  3160. 'minYear' => 2008,
  3161. 'maxYear' => 2011,
  3162. 'interval' => 15,
  3163. 'options' => null,
  3164. 'empty' => false,
  3165. 'id' => 'prueba',
  3166. 'required' => false,
  3167. 'templateVars' => []
  3168. ])
  3169. ->will($this->returnValue('This is it!'));
  3170. $result = $this->Form->control('prueba', [
  3171. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3172. 'maxYear' => 2011, 'interval' => 15
  3173. ]);
  3174. $expected = [
  3175. 'div' => ['class' => 'input datetime'],
  3176. '<label',
  3177. 'Prueba',
  3178. '/label',
  3179. 'This is it!',
  3180. '/div'
  3181. ];
  3182. $this->assertHtml($expected, $result);
  3183. }
  3184. /**
  3185. * testControlDatetimeIdPrefix method
  3186. *
  3187. * Test form->control() with datetime with id prefix.
  3188. *
  3189. * @return void
  3190. */
  3191. public function testControlDatetimeIdPrefix()
  3192. {
  3193. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3194. ->setMethods(['datetime'])
  3195. ->setConstructorArgs([new View()])
  3196. ->getMock();
  3197. $this->Form->create(false, ['idPrefix' => 'prefix']);
  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' => 'prefix-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. * testControlCheckboxWithDisabledElements method
  3228. *
  3229. * Test generating checkboxes with disabled elements.
  3230. *
  3231. * @return void
  3232. */
  3233. public function testControlCheckboxWithDisabledElements()
  3234. {
  3235. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3236. $result = $this->Form->control('Contact.multiple', [
  3237. 'multiple' => 'checkbox',
  3238. 'disabled' => 'disabled',
  3239. 'options' => $options
  3240. ]);
  3241. $expected = [
  3242. ['div' => ['class' => 'input select']],
  3243. ['label' => ['for' => 'contact-multiple']],
  3244. 'Multiple',
  3245. '/label',
  3246. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3247. ['div' => ['class' => 'checkbox']],
  3248. ['label' => ['for' => 'contact-multiple-1']],
  3249. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3250. 'One',
  3251. '/label',
  3252. '/div',
  3253. ['div' => ['class' => 'checkbox']],
  3254. ['label' => ['for' => 'contact-multiple-2']],
  3255. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3256. 'Two',
  3257. '/label',
  3258. '/div',
  3259. ['div' => ['class' => 'checkbox']],
  3260. ['label' => ['for' => 'contact-multiple-3']],
  3261. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3262. 'Three',
  3263. '/label',
  3264. '/div',
  3265. '/div'
  3266. ];
  3267. $this->assertHtml($expected, $result);
  3268. // make sure 50 does only disable 50, and not 50f5c0cf
  3269. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3270. $disabled = [50];
  3271. $expected = [
  3272. ['div' => ['class' => 'input select']],
  3273. ['label' => ['for' => 'contact-multiple']],
  3274. 'Multiple',
  3275. '/label',
  3276. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3277. ['div' => ['class' => 'checkbox']],
  3278. ['label' => ['for' => 'contact-multiple-50']],
  3279. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3280. 'Fifty',
  3281. '/label',
  3282. '/div',
  3283. ['div' => ['class' => 'checkbox']],
  3284. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3285. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3286. 'Stringy',
  3287. '/label',
  3288. '/div',
  3289. '/div'
  3290. ];
  3291. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3292. $this->assertHtml($expected, $result);
  3293. }
  3294. /**
  3295. * testControlWithLeadingInteger method
  3296. *
  3297. * Test input name with leading integer, ensure attributes are generated correctly.
  3298. *
  3299. * @return void
  3300. */
  3301. public function testControlWithLeadingInteger()
  3302. {
  3303. $result = $this->Form->text('0.Node.title');
  3304. $expected = [
  3305. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3306. ];
  3307. $this->assertHtml($expected, $result);
  3308. }
  3309. /**
  3310. * testControlSelectType method
  3311. *
  3312. * Test form->control() with select type inputs.
  3313. *
  3314. * @return void
  3315. */
  3316. public function testControlSelectType()
  3317. {
  3318. $result = $this->Form->control(
  3319. 'email',
  3320. [
  3321. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3322. );
  3323. $expected = [
  3324. 'div' => ['class' => 'input select'],
  3325. 'label' => ['for' => 'email'],
  3326. 'Email',
  3327. '/label',
  3328. ['select' => ['name' => 'email', 'id' => 'email']],
  3329. ['option' => ['value' => '']],
  3330. '/option',
  3331. ['option' => ['value' => 'è']],
  3332. 'Firést',
  3333. '/option',
  3334. ['option' => ['value' => 'é']],
  3335. 'Secoènd',
  3336. '/option',
  3337. '/select',
  3338. '/div'
  3339. ];
  3340. $this->assertHtml($expected, $result);
  3341. $result = $this->Form->control(
  3342. 'email',
  3343. [
  3344. 'options' => ['First', 'Second'], 'empty' => true]
  3345. );
  3346. $expected = [
  3347. 'div' => ['class' => 'input select'],
  3348. 'label' => ['for' => 'email'],
  3349. 'Email',
  3350. '/label',
  3351. ['select' => ['name' => 'email', 'id' => 'email']],
  3352. ['option' => ['value' => '']],
  3353. '/option',
  3354. ['option' => ['value' => '0']],
  3355. 'First',
  3356. '/option',
  3357. ['option' => ['value' => '1']],
  3358. 'Second',
  3359. '/option',
  3360. '/select',
  3361. '/div'
  3362. ];
  3363. $this->assertHtml($expected, $result);
  3364. $result = $this->Form->control('email', [
  3365. 'type' => 'select',
  3366. 'options' => new \ArrayObject(['First', 'Second']),
  3367. 'empty' => true
  3368. ]);
  3369. $this->assertHtml($expected, $result);
  3370. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3371. $this->Form->request->data = ['Model' => ['user_id' => 'value']];
  3372. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3373. $expected = [
  3374. 'div' => ['class' => 'input select'],
  3375. 'label' => ['for' => 'model-user-id'],
  3376. 'User',
  3377. '/label',
  3378. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3379. ['option' => ['value' => '']],
  3380. '/option',
  3381. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3382. 'good',
  3383. '/option',
  3384. ['option' => ['value' => 'other']],
  3385. 'bad',
  3386. '/option',
  3387. '/select',
  3388. '/div'
  3389. ];
  3390. $this->assertHtml($expected, $result);
  3391. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3392. $this->Form->request->data = ['Thing' => ['user_id' => null]];
  3393. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3394. $expected = [
  3395. 'div' => ['class' => 'input select'],
  3396. 'label' => ['for' => 'thing-user-id'],
  3397. 'User',
  3398. '/label',
  3399. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3400. ['option' => ['value' => '']],
  3401. 'Some Empty',
  3402. '/option',
  3403. ['option' => ['value' => 'value']],
  3404. 'good',
  3405. '/option',
  3406. ['option' => ['value' => 'other']],
  3407. 'bad',
  3408. '/option',
  3409. '/select',
  3410. '/div'
  3411. ];
  3412. $this->assertHtml($expected, $result);
  3413. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3414. $this->Form->request->data = ['Thing' => ['user_id' => 'value']];
  3415. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3416. $expected = [
  3417. 'div' => ['class' => 'input select'],
  3418. 'label' => ['for' => 'thing-user-id'],
  3419. 'User',
  3420. '/label',
  3421. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3422. ['option' => ['value' => '']],
  3423. 'Some Empty',
  3424. '/option',
  3425. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3426. 'good',
  3427. '/option',
  3428. ['option' => ['value' => 'other']],
  3429. 'bad',
  3430. '/option',
  3431. '/select',
  3432. '/div'
  3433. ];
  3434. $this->assertHtml($expected, $result);
  3435. $this->Form->data = [];
  3436. $result = $this->Form->control('Publisher.id', [
  3437. 'label' => 'Publisher',
  3438. 'type' => 'select',
  3439. 'multiple' => 'checkbox',
  3440. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3441. ]);
  3442. $expected = [
  3443. ['div' => ['class' => 'input select']],
  3444. ['label' => ['for' => 'publisher-id']],
  3445. 'Publisher',
  3446. '/label',
  3447. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3448. ['div' => ['class' => 'checkbox']],
  3449. ['label' => ['for' => 'publisher-id-value-1']],
  3450. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3451. 'Label 1',
  3452. '/label',
  3453. '/div',
  3454. ['div' => ['class' => 'checkbox']],
  3455. ['label' => ['for' => 'publisher-id-value-2']],
  3456. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3457. 'Label 2',
  3458. '/label',
  3459. '/div',
  3460. '/div'
  3461. ];
  3462. $this->assertHtml($expected, $result);
  3463. }
  3464. /**
  3465. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3466. *
  3467. * Test that control() and a non standard primary key makes a hidden input by default.
  3468. *
  3469. * @return void
  3470. */
  3471. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3472. {
  3473. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3474. $this->Form->create($this->article);
  3475. $result = $this->Form->control('title');
  3476. $expected = [
  3477. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3478. ];
  3479. $this->assertHtml($expected, $result);
  3480. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3481. $this->Form->create($this->article);
  3482. $result = $this->Form->control('title');
  3483. $expected = [
  3484. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3485. ];
  3486. $this->assertHtml($expected, $result);
  3487. $result = $this->Form->control('body');
  3488. $expected = [
  3489. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3490. ];
  3491. $this->assertHtml($expected, $result);
  3492. }
  3493. /**
  3494. * testControlOverridingMagicSelectType method
  3495. *
  3496. * Test that overriding the magic select type widget is possible.
  3497. *
  3498. * @return void
  3499. */
  3500. public function testControlOverridingMagicSelectType()
  3501. {
  3502. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3503. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3504. $expected = [
  3505. 'div' => ['class' => 'input text'],
  3506. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3507. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3508. '/div'
  3509. ];
  3510. $this->assertHtml($expected, $result);
  3511. //Check that magic types still work for plural/singular vars
  3512. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3513. $result = $this->Form->control('Model.type');
  3514. $expected = [
  3515. 'div' => ['class' => 'input select'],
  3516. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3517. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3518. ['option' => ['value' => 'value']], 'good', '/option',
  3519. ['option' => ['value' => 'other']], 'bad', '/option',
  3520. '/select',
  3521. '/div'
  3522. ];
  3523. $this->assertHtml($expected, $result);
  3524. }
  3525. /**
  3526. * testControlMagicTypeDoesNotOverride method
  3527. *
  3528. * Test that inferred types do not override developer input.
  3529. *
  3530. * @return void
  3531. */
  3532. public function testControlMagicTypeDoesNotOverride()
  3533. {
  3534. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3535. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3536. $expected = [
  3537. 'div' => ['class' => 'input checkbox'],
  3538. ['input' => [
  3539. 'type' => 'hidden',
  3540. 'name' => 'Model[user]',
  3541. 'value' => 0,
  3542. ]],
  3543. 'label' => ['for' => 'model-user'],
  3544. ['input' => [
  3545. 'name' => 'Model[user]',
  3546. 'type' => 'checkbox',
  3547. 'id' => 'model-user',
  3548. 'value' => 1
  3549. ]],
  3550. 'User',
  3551. '/label',
  3552. '/div'
  3553. ];
  3554. $this->assertHtml($expected, $result);
  3555. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3556. $options = [
  3557. 1 => 'blue',
  3558. 2 => 'red'
  3559. ];
  3560. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3561. $expected = [
  3562. 'div' => ['class' => 'input select'],
  3563. 'label' => ['for' => 'tags-ids'],
  3564. 'Tags',
  3565. '/label',
  3566. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3567. ['div' => ['class' => 'checkbox']],
  3568. ['label' => ['for' => 'tags-ids-1']],
  3569. ['input' => [
  3570. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3571. 'value' => '1', 'name' => 'tags[_ids][]'
  3572. ]],
  3573. 'blue',
  3574. '/label',
  3575. '/div',
  3576. ['div' => ['class' => 'checkbox']],
  3577. ['label' => ['for' => 'tags-ids-2']],
  3578. ['input' => [
  3579. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3580. 'value' => '2', 'name' => 'tags[_ids][]'
  3581. ]],
  3582. 'red',
  3583. '/label',
  3584. '/div',
  3585. '/div'
  3586. ];
  3587. $this->assertHtml($expected, $result);
  3588. }
  3589. /**
  3590. * testControlMagicSelectForTypeNumber method
  3591. *
  3592. * Test that magic control() selects are created for type=number.
  3593. *
  3594. * @return void
  3595. */
  3596. public function testControlMagicSelectForTypeNumber()
  3597. {
  3598. TableRegistry::get('ValidateUsers', [
  3599. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3600. ]);
  3601. $entity = new Entity(['balance' => 1]);
  3602. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3603. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3604. $result = $this->Form->control('balance');
  3605. $expected = [
  3606. 'div' => ['class' => 'input select'],
  3607. 'label' => ['for' => 'balance'],
  3608. 'Balance',
  3609. '/label',
  3610. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3611. ['option' => ['value' => '0']],
  3612. 'nothing',
  3613. '/option',
  3614. ['option' => ['value' => '1', 'selected' => 'selected']],
  3615. 'some',
  3616. '/option',
  3617. ['option' => ['value' => '100']],
  3618. 'a lot',
  3619. '/option',
  3620. '/select',
  3621. '/div'
  3622. ];
  3623. $this->assertHtml($expected, $result);
  3624. }
  3625. /**
  3626. * testInvalidControlTypeOption method
  3627. *
  3628. * Test invalid 'input' type option to control() function.
  3629. *
  3630. * @expectedException \RuntimeException
  3631. * @expectedExceptionMessage Invalid type 'input' used for field 'text'
  3632. * @return void
  3633. */
  3634. public function testInvalidControlTypeOption()
  3635. {
  3636. $this->Form->control('text', ['type' => 'input']);
  3637. }
  3638. /**
  3639. * testControlMagicSelectChangeToRadio method
  3640. *
  3641. * Test that magic control() selects can easily be converted into radio types without error.
  3642. *
  3643. * @return void
  3644. */
  3645. public function testControlMagicSelectChangeToRadio()
  3646. {
  3647. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3648. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3649. $this->assertContains('input type="radio"', $result);
  3650. }
  3651. /**
  3652. * testFormControlSubmit method
  3653. *
  3654. * Test correct results for form::control() and type submit.
  3655. *
  3656. * @return void
  3657. */
  3658. public function testFormControlSubmit()
  3659. {
  3660. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3661. $expected = [
  3662. 'div' => ['class' => 'submit'],
  3663. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3664. '/div'
  3665. ];
  3666. $this->assertHtml($expected, $result);
  3667. }
  3668. /**
  3669. * testFormControls method
  3670. *
  3671. * Test correct results from Form::controls().
  3672. *
  3673. * @return void
  3674. */
  3675. public function testFormControlsLegendFieldset()
  3676. {
  3677. $this->Form->create($this->article);
  3678. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3679. $expected = [
  3680. '<fieldset',
  3681. '<legend',
  3682. 'The Legend',
  3683. '/legend',
  3684. '*/fieldset',
  3685. ];
  3686. $this->assertHtml($expected, $result);
  3687. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3688. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3689. $this->assertContains('<fieldset>', $result);
  3690. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3691. $this->assertNotContains('<legend>', $result);
  3692. $this->assertNotContains('<fieldset>', $result);
  3693. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3694. $this->assertNotContains('<legend>', $result);
  3695. $this->assertNotContains('<fieldset>', $result);
  3696. $this->Form->create($this->article);
  3697. $this->Form->request->params['prefix'] = 'admin';
  3698. $this->Form->request->params['action'] = 'admin_edit';
  3699. $this->Form->request->params['controller'] = 'articles';
  3700. $result = $this->Form->allControls();
  3701. $expected = [
  3702. '<fieldset',
  3703. '<legend',
  3704. 'New Article',
  3705. '/legend',
  3706. '*/fieldset',
  3707. ];
  3708. $this->assertHtml($expected, $result);
  3709. $this->Form->create($this->article);
  3710. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3711. $expected = [
  3712. '<fieldset',
  3713. '<legend',
  3714. 'The Legend',
  3715. '/legend',
  3716. '*/fieldset',
  3717. ];
  3718. $this->assertHtml($expected, $result);
  3719. $this->Form->create($this->article);
  3720. $result = $this->Form->allControls([], [
  3721. 'fieldset' => [
  3722. 'class' => 'some-class some-other-class',
  3723. 'disabled' => true,
  3724. 'data-param' => 'a-param'
  3725. ],
  3726. 'legend' => 'The Legend'
  3727. ]);
  3728. $expected = [
  3729. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3730. '<legend',
  3731. 'The Legend',
  3732. '/legend',
  3733. '*/fieldset',
  3734. ];
  3735. $this->assertHtml($expected, $result);
  3736. }
  3737. /**
  3738. * testFormControls method
  3739. *
  3740. * Test the controls() method.
  3741. *
  3742. * @return void
  3743. */
  3744. public function testFormControls()
  3745. {
  3746. $this->Form->create($this->article);
  3747. $result = $this->Form->allControls();
  3748. $expected = [
  3749. '<fieldset',
  3750. '<legend', 'New Article', '/legend',
  3751. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3752. ['div' => ['class' => 'input select required']],
  3753. '*/div',
  3754. ['div' => ['class' => 'input text required']],
  3755. '*/div',
  3756. ['div' => ['class' => 'input text']],
  3757. '*/div',
  3758. ['div' => ['class' => 'input text']],
  3759. '*/div',
  3760. '/fieldset',
  3761. ];
  3762. $this->assertHtml($expected, $result);
  3763. $result = $this->Form->allControls([
  3764. 'published' => ['type' => 'boolean']
  3765. ]);
  3766. $expected = [
  3767. '<fieldset',
  3768. '<legend', 'New Article', '/legend',
  3769. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3770. ['div' => ['class' => 'input select required']],
  3771. '*/div',
  3772. ['div' => ['class' => 'input text required']],
  3773. '*/div',
  3774. ['div' => ['class' => 'input text']],
  3775. '*/div',
  3776. ['div' => ['class' => 'input boolean']],
  3777. '*/div',
  3778. '/fieldset',
  3779. ];
  3780. $this->assertHtml($expected, $result);
  3781. $this->Form->create($this->article);
  3782. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3783. $expected = [
  3784. 'fieldset' => [],
  3785. 'legend' => [],
  3786. 'Hello',
  3787. '/legend',
  3788. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3789. ['div' => ['class' => 'input select required']],
  3790. '*/div',
  3791. ['div' => ['class' => 'input text required']],
  3792. '*/div',
  3793. ['div' => ['class' => 'input text']],
  3794. '*/div',
  3795. ['div' => ['class' => 'input text']],
  3796. '*/div',
  3797. '/fieldset'
  3798. ];
  3799. $this->assertHtml($expected, $result);
  3800. $this->Form->create(false);
  3801. $expected = [
  3802. 'fieldset' => [],
  3803. ['div' => ['class' => 'input text']],
  3804. 'label' => ['for' => 'foo'],
  3805. 'Foo',
  3806. '/label',
  3807. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3808. '*/div',
  3809. '/fieldset'
  3810. ];
  3811. $result = $this->Form->allControls(
  3812. ['foo' => ['type' => 'text']],
  3813. ['legend' => false]
  3814. );
  3815. $this->assertHtml($expected, $result);
  3816. }
  3817. /**
  3818. * testFormControlsBlacklist method
  3819. *
  3820. * @return void
  3821. */
  3822. public function testFormControlsBlacklist()
  3823. {
  3824. $this->Form->create($this->article);
  3825. $result = $this->Form->allControls([
  3826. 'id' => false
  3827. ]);
  3828. $expected = [
  3829. '<fieldset',
  3830. '<legend', 'New Article', '/legend',
  3831. ['div' => ['class' => 'input select required']],
  3832. '*/div',
  3833. ['div' => ['class' => 'input text required']],
  3834. '*/div',
  3835. ['div' => ['class' => 'input text']],
  3836. '*/div',
  3837. ['div' => ['class' => 'input text']],
  3838. '*/div',
  3839. '/fieldset',
  3840. ];
  3841. $this->assertHtml($expected, $result);
  3842. $this->Form->create($this->article);
  3843. $result = $this->Form->allControls([
  3844. 'id' => []
  3845. ]);
  3846. $expected = [
  3847. '<fieldset',
  3848. '<legend', 'New Article', '/legend',
  3849. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3850. ['div' => ['class' => 'input select required']],
  3851. '*/div',
  3852. ['div' => ['class' => 'input text required']],
  3853. '*/div',
  3854. ['div' => ['class' => 'input text']],
  3855. '*/div',
  3856. ['div' => ['class' => 'input text']],
  3857. '*/div',
  3858. '/fieldset',
  3859. ];
  3860. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3861. }
  3862. /**
  3863. * testSelectAsCheckbox method
  3864. *
  3865. * Test multi-select widget with checkbox formatting.
  3866. *
  3867. * @return void
  3868. */
  3869. public function testSelectAsCheckbox()
  3870. {
  3871. $result = $this->Form->select(
  3872. 'Model.multi_field',
  3873. ['first', 'second', 'third'],
  3874. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3875. );
  3876. $expected = [
  3877. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3878. ['div' => ['class' => 'checkbox']],
  3879. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3880. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3881. 'first',
  3882. '/label',
  3883. '/div',
  3884. ['div' => ['class' => 'checkbox']],
  3885. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3886. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3887. 'second',
  3888. '/label',
  3889. '/div',
  3890. ['div' => ['class' => 'checkbox']],
  3891. ['label' => ['for' => 'model-multi-field-2']],
  3892. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3893. 'third',
  3894. '/label',
  3895. '/div',
  3896. ];
  3897. $this->assertHtml($expected, $result);
  3898. $result = $this->Form->select(
  3899. 'Model.multi_field',
  3900. ['1/2' => 'half'],
  3901. ['multiple' => 'checkbox']
  3902. );
  3903. $expected = [
  3904. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3905. ['div' => ['class' => 'checkbox']],
  3906. ['label' => ['for' => 'model-multi-field-1-2']],
  3907. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3908. 'half',
  3909. '/label',
  3910. '/div',
  3911. ];
  3912. $this->assertHtml($expected, $result);
  3913. }
  3914. /**
  3915. * testLabel method
  3916. *
  3917. * Test label generation.
  3918. *
  3919. * @return void
  3920. */
  3921. public function testLabel()
  3922. {
  3923. $result = $this->Form->label('Person.name');
  3924. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3925. $this->assertHtml($expected, $result);
  3926. $result = $this->Form->label('Person.name');
  3927. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3928. $this->assertHtml($expected, $result);
  3929. $result = $this->Form->label('Person.first_name');
  3930. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3931. $this->assertHtml($expected, $result);
  3932. $result = $this->Form->label('Person.first_name', 'Your first name');
  3933. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3934. $this->assertHtml($expected, $result);
  3935. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3936. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3937. $this->assertHtml($expected, $result);
  3938. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3939. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3940. $this->assertHtml($expected, $result);
  3941. $result = $this->Form->label('Person.first_name', '');
  3942. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3943. $this->assertHtml($expected, $result);
  3944. $result = $this->Form->label('Person.2.name', '');
  3945. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3946. $this->assertHtml($expected, $result);
  3947. }
  3948. /**
  3949. * testLabelContainControl method
  3950. *
  3951. * Test that label() can accept an input with the correct template vars.
  3952. *
  3953. * @return void
  3954. */
  3955. public function testLabelContainControl()
  3956. {
  3957. $this->Form->templates([
  3958. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3959. ]);
  3960. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3961. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3962. ]);
  3963. $expected = [
  3964. 'label' => ['for' => 'person-accept-terms'],
  3965. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3966. 'Accept',
  3967. '/label',
  3968. ];
  3969. $this->assertHtml($expected, $result);
  3970. }
  3971. /**
  3972. * testTextbox method
  3973. *
  3974. * Test textbox element generation.
  3975. *
  3976. * @return void
  3977. */
  3978. public function testTextbox()
  3979. {
  3980. $result = $this->Form->text('Model.field');
  3981. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3982. $this->assertHtml($expected, $result);
  3983. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3984. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3985. $this->assertHtml($expected, $result);
  3986. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3987. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3988. $this->assertHtml($expected, $result);
  3989. }
  3990. /**
  3991. * testTextBoxDataAndError method
  3992. *
  3993. * Test that text() hooks up with request data and error fields.
  3994. *
  3995. * @return void
  3996. */
  3997. public function testTextBoxDataAndError()
  3998. {
  3999. $this->article['errors'] = [
  4000. 'Contact' => ['text' => 'wrong']
  4001. ];
  4002. $this->Form->create($this->article);
  4003. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  4004. $result = $this->Form->text('Model.text');
  4005. $expected = [
  4006. 'input' => [
  4007. 'type' => 'text',
  4008. 'name' => 'Model[text]',
  4009. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  4010. ]
  4011. ];
  4012. $this->assertHtml($expected, $result);
  4013. $this->Form->request->data['Contact']['text'] = 'test';
  4014. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  4015. $expected = [
  4016. 'input' => [
  4017. 'type' => 'text',
  4018. 'name' => 'Contact[text]',
  4019. 'value' => 'test',
  4020. 'id' => 'theID',
  4021. 'class' => 'form-error'
  4022. ]
  4023. ];
  4024. $this->assertHtml($expected, $result);
  4025. }
  4026. /**
  4027. * testDefaultValue method
  4028. *
  4029. * Test default value setting.
  4030. *
  4031. * @return void
  4032. */
  4033. public function testTextDefaultValue()
  4034. {
  4035. $this->Form->request->data['Model']['field'] = 'test';
  4036. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4037. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  4038. $this->assertHtml($expected, $result);
  4039. unset($this->Form->request->data['Model']['field']);
  4040. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4041. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4042. $this->assertHtml($expected, $result);
  4043. $Articles = TableRegistry::get('Articles');
  4044. $title = $Articles->schema()->column('title');
  4045. $Articles->schema()->addColumn(
  4046. 'title',
  4047. ['default' => 'default title'] + $title
  4048. );
  4049. $entity = $Articles->newEntity();
  4050. $this->Form->create($entity);
  4051. // Get default value from schema
  4052. $result = $this->Form->text('title');
  4053. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4054. $this->assertHtml($expected, $result);
  4055. // Don't get value from schema
  4056. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4057. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4058. $this->assertHtml($expected, $result);
  4059. // Custom default value overrides default value from schema
  4060. $result = $this->Form->text('title', ['default' => 'override default']);
  4061. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4062. $this->assertHtml($expected, $result);
  4063. // Default value from schema is used only for new entities.
  4064. $entity->isNew(false);
  4065. $result = $this->Form->text('title');
  4066. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4067. $this->assertHtml($expected, $result);
  4068. }
  4069. /**
  4070. * testError method
  4071. *
  4072. * Test field error generation.
  4073. *
  4074. * @return void
  4075. */
  4076. public function testError()
  4077. {
  4078. $this->article['errors'] = [
  4079. 'Article' => ['field' => 'email']
  4080. ];
  4081. $this->Form->create($this->article);
  4082. $result = $this->Form->error('Article.field');
  4083. $expected = [
  4084. ['div' => ['class' => 'error-message']],
  4085. 'email',
  4086. '/div',
  4087. ];
  4088. $this->assertHtml($expected, $result);
  4089. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4090. $expected = [
  4091. ['div' => ['class' => 'error-message']],
  4092. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4093. '/div',
  4094. ];
  4095. $this->assertHtml($expected, $result);
  4096. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4097. $expected = [
  4098. ['div' => ['class' => 'error-message']],
  4099. '<strong', 'Badness!', '/strong',
  4100. '/div',
  4101. ];
  4102. $this->assertHtml($expected, $result);
  4103. }
  4104. /**
  4105. * testErrorRuleName method
  4106. *
  4107. * Test error translation can use rule names for translating.
  4108. *
  4109. * @return void
  4110. */
  4111. public function testErrorRuleName()
  4112. {
  4113. $this->article['errors'] = [
  4114. 'Article' => [
  4115. 'field' => ['email' => 'Your email was not good']
  4116. ]
  4117. ];
  4118. $this->Form->create($this->article);
  4119. $result = $this->Form->error('Article.field');
  4120. $expected = [
  4121. ['div' => ['class' => 'error-message']],
  4122. 'Your email was not good',
  4123. '/div',
  4124. ];
  4125. $this->assertHtml($expected, $result);
  4126. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4127. $expected = [
  4128. ['div' => ['class' => 'error-message']],
  4129. 'Email in use',
  4130. '/div',
  4131. ];
  4132. $this->assertHtml($expected, $result);
  4133. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4134. $expected = [
  4135. ['div' => ['class' => 'error-message']],
  4136. 'Email in use',
  4137. '/div',
  4138. ];
  4139. $this->assertHtml($expected, $result);
  4140. $result = $this->Form->error('Article.field', [
  4141. 'email' => 'Key is preferred',
  4142. 'Your email was not good' => 'Email in use'
  4143. ]);
  4144. $expected = [
  4145. ['div' => ['class' => 'error-message']],
  4146. 'Key is preferred',
  4147. '/div',
  4148. ];
  4149. $this->assertHtml($expected, $result);
  4150. }
  4151. /**
  4152. * testErrorMessages method
  4153. *
  4154. * Test error with nested lists.
  4155. *
  4156. * @return void
  4157. */
  4158. public function testErrorMessages()
  4159. {
  4160. $this->article['errors'] = [
  4161. 'Article' => ['field' => 'email']
  4162. ];
  4163. $this->Form->create($this->article);
  4164. $result = $this->Form->error('Article.field', [
  4165. 'email' => 'No good!'
  4166. ]);
  4167. $expected = [
  4168. 'div' => ['class' => 'error-message'],
  4169. 'No good!',
  4170. '/div'
  4171. ];
  4172. $this->assertHtml($expected, $result);
  4173. }
  4174. /**
  4175. * testErrorMultipleMessages method
  4176. *
  4177. * Test error() with multiple messages.
  4178. *
  4179. * @return void
  4180. */
  4181. public function testErrorMultipleMessages()
  4182. {
  4183. $this->article['errors'] = [
  4184. 'field' => ['notBlank', 'email', 'Something else']
  4185. ];
  4186. $this->Form->create($this->article);
  4187. $result = $this->Form->error('field', [
  4188. 'notBlank' => 'Cannot be empty',
  4189. 'email' => 'No good!'
  4190. ]);
  4191. $expected = [
  4192. 'div' => ['class' => 'error-message'],
  4193. 'ul' => [],
  4194. '<li', 'Cannot be empty', '/li',
  4195. '<li', 'No good!', '/li',
  4196. '<li', 'Something else', '/li',
  4197. '/ul',
  4198. '/div'
  4199. ];
  4200. $this->assertHtml($expected, $result);
  4201. }
  4202. /**
  4203. * testPassword method
  4204. *
  4205. * Test password element generation.
  4206. *
  4207. * @return void
  4208. */
  4209. public function testPassword()
  4210. {
  4211. $this->article['errors'] = [
  4212. 'Contact' => [
  4213. 'passwd' => 1
  4214. ]
  4215. ];
  4216. $this->Form->create($this->article);
  4217. $result = $this->Form->password('Contact.field');
  4218. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4219. $this->assertHtml($expected, $result);
  4220. $this->Form->request->data['Contact']['passwd'] = 'test';
  4221. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4222. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4223. $this->assertHtml($expected, $result);
  4224. }
  4225. /**
  4226. * testRadio method
  4227. *
  4228. * Test radio element set generation.
  4229. *
  4230. * @return void
  4231. */
  4232. public function testRadio()
  4233. {
  4234. $result = $this->Form->radio('Model.field', ['option A']);
  4235. $expected = [
  4236. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4237. 'label' => ['for' => 'model-field-0'],
  4238. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4239. 'option A',
  4240. '/label'
  4241. ];
  4242. $this->assertHtml($expected, $result);
  4243. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4244. $this->assertHtml($expected, $result);
  4245. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4246. $expected = [
  4247. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4248. ['label' => ['for' => 'model-field-0']],
  4249. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4250. 'option A',
  4251. '/label',
  4252. ['label' => ['for' => 'model-field-1']],
  4253. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4254. 'option B',
  4255. '/label',
  4256. ];
  4257. $this->assertHtml($expected, $result);
  4258. $result = $this->Form->radio(
  4259. 'Employee.gender',
  4260. ['male' => 'Male', 'female' => 'Female'],
  4261. ['form' => 'my-form']
  4262. );
  4263. $expected = [
  4264. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4265. ['label' => ['for' => 'employee-gender-male']],
  4266. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4267. 'Male',
  4268. '/label',
  4269. ['label' => ['for' => 'employee-gender-female']],
  4270. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4271. 'Female',
  4272. '/label',
  4273. ];
  4274. $this->assertHtml($expected, $result);
  4275. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4276. $expected = [
  4277. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4278. ['label' => ['for' => 'model-custom-0']],
  4279. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4280. 'option A',
  4281. '/label',
  4282. ['label' => ['for' => 'model-custom-1']],
  4283. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4284. 'option B',
  4285. '/label',
  4286. ];
  4287. $this->assertHtml($expected, $result);
  4288. $result = $this->Form->radio(
  4289. 'Employee.gender',
  4290. [
  4291. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4292. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4293. ]
  4294. );
  4295. $expected = [
  4296. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4297. ['label' => ['for' => 'employee-gender-male']],
  4298. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4299. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4300. 'Male',
  4301. '/label',
  4302. ['label' => ['for' => 'employee-gender-female']],
  4303. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4304. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4305. 'Female',
  4306. '/label',
  4307. ];
  4308. $this->assertHtml($expected, $result);
  4309. }
  4310. /**
  4311. * Test radio with complex options and empty disabled data.
  4312. *
  4313. * @return void
  4314. */
  4315. public function testRadioComplexDisabled()
  4316. {
  4317. $options = [
  4318. ['value' => 'r', 'text' => 'red'],
  4319. ['value' => 'b', 'text' => 'blue'],
  4320. ];
  4321. $attrs = ['disabled' => []];
  4322. $result = $this->Form->radio('Model.field', $options, $attrs);
  4323. $expected = [
  4324. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4325. ['label' => ['for' => 'model-field-r']],
  4326. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4327. 'red',
  4328. '/label',
  4329. ['label' => ['for' => 'model-field-b']],
  4330. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4331. 'blue',
  4332. '/label',
  4333. ];
  4334. $this->assertHtml($expected, $result);
  4335. $attrs = ['disabled' => ['r']];
  4336. $result = $this->Form->radio('Model.field', $options, $attrs);
  4337. $this->assertContains('disabled="disabled"', $result);
  4338. }
  4339. /**
  4340. * testRadioDefaultValue method
  4341. *
  4342. * Test default value setting on radio() method.
  4343. *
  4344. * @return void
  4345. */
  4346. public function testRadioDefaultValue()
  4347. {
  4348. $Articles = TableRegistry::get('Articles');
  4349. $title = $Articles->schema()->column('title');
  4350. $Articles->schema()->addColumn(
  4351. 'title',
  4352. ['default' => '1'] + $title
  4353. );
  4354. $this->Form->create($Articles->newEntity());
  4355. $result = $this->Form->radio('title', ['option A', 'option B']);
  4356. $expected = [
  4357. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4358. ['label' => ['for' => 'title-0']],
  4359. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4360. 'option A',
  4361. '/label',
  4362. ['label' => ['for' => 'title-1']],
  4363. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4364. 'option B',
  4365. '/label',
  4366. ];
  4367. $this->assertHtml($expected, $result);
  4368. }
  4369. /**
  4370. * testControlRadio method
  4371. *
  4372. * Test that input works with radio types.
  4373. *
  4374. * @return void
  4375. */
  4376. public function testControlRadio()
  4377. {
  4378. $result = $this->Form->control('test', [
  4379. 'type' => 'radio',
  4380. 'options' => ['A', 'B'],
  4381. ]);
  4382. $expected = [
  4383. ['div' => ['class' => 'input radio']],
  4384. '<label',
  4385. 'Test',
  4386. '/label',
  4387. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4388. ['label' => ['for' => 'test-0']],
  4389. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4390. 'A',
  4391. '/label',
  4392. ['label' => ['for' => 'test-1']],
  4393. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4394. 'B',
  4395. '/label',
  4396. '/div',
  4397. ];
  4398. $this->assertHtml($expected, $result);
  4399. $result = $this->Form->control('test', [
  4400. 'type' => 'radio',
  4401. 'options' => ['A', 'B'],
  4402. 'value' => '0'
  4403. ]);
  4404. $expected = [
  4405. ['div' => ['class' => 'input radio']],
  4406. '<label',
  4407. 'Test',
  4408. '/label',
  4409. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4410. ['label' => ['for' => 'test-0']],
  4411. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4412. 'A',
  4413. '/label',
  4414. ['label' => ['for' => 'test-1']],
  4415. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4416. 'B',
  4417. '/label',
  4418. '/div',
  4419. ];
  4420. $this->assertHtml($expected, $result);
  4421. $result = $this->Form->control('test', [
  4422. 'type' => 'radio',
  4423. 'options' => ['A', 'B'],
  4424. 'label' => false
  4425. ]);
  4426. $expected = [
  4427. ['div' => ['class' => 'input radio']],
  4428. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4429. ['label' => ['for' => 'test-0']],
  4430. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4431. 'A',
  4432. '/label',
  4433. ['label' => ['for' => 'test-1']],
  4434. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4435. 'B',
  4436. '/label',
  4437. '/div',
  4438. ];
  4439. $this->assertHtml($expected, $result);
  4440. }
  4441. /**
  4442. * testRadioNoLabel method
  4443. *
  4444. * Test that radio() works with label = false.
  4445. *
  4446. * @return void
  4447. */
  4448. public function testRadioNoLabel()
  4449. {
  4450. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4451. $expected = [
  4452. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4453. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4454. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4455. ];
  4456. $this->assertHtml($expected, $result);
  4457. }
  4458. /**
  4459. * testRadioControlInsideLabel method
  4460. *
  4461. * Test generating radio input inside label ala twitter bootstrap.
  4462. *
  4463. * @return void
  4464. */
  4465. public function testRadioControlInsideLabel()
  4466. {
  4467. $this->Form->templates([
  4468. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4469. 'radioWrapper' => '{{label}}'
  4470. ]);
  4471. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4472. //@codingStandardsIgnoreStart
  4473. $expected = [
  4474. ['input' => [
  4475. 'type' => 'hidden',
  4476. 'name' => 'Model[field]',
  4477. 'value' => ''
  4478. ]],
  4479. ['label' => ['for' => 'model-field-0']],
  4480. ['input' => [
  4481. 'type' => 'radio',
  4482. 'name' => 'Model[field]',
  4483. 'value' => '0',
  4484. 'id' => 'model-field-0'
  4485. ]],
  4486. 'option A',
  4487. '/label',
  4488. ['label' => ['for' => 'model-field-1']],
  4489. ['input' => [
  4490. 'type' => 'radio',
  4491. 'name' => 'Model[field]',
  4492. 'value' => '1',
  4493. 'id' => 'model-field-1'
  4494. ]],
  4495. 'option B',
  4496. '/label',
  4497. ];
  4498. //@codingStandardsIgnoreEnd
  4499. $this->assertHtml($expected, $result);
  4500. }
  4501. /**
  4502. * testRadioHiddenControlDisabling method
  4503. *
  4504. * Test disabling the hidden input for radio buttons.
  4505. *
  4506. * @return void
  4507. */
  4508. public function testRadioHiddenControlDisabling()
  4509. {
  4510. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4511. $expected = [
  4512. 'label' => ['for' => 'model-1-field-0'],
  4513. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4514. 'option A',
  4515. '/label'
  4516. ];
  4517. $this->assertHtml($expected, $result);
  4518. }
  4519. /**
  4520. * testRadioOutOfRange method
  4521. *
  4522. * Test radio element set generation.
  4523. *
  4524. * @return void
  4525. */
  4526. public function testRadioOutOfRange()
  4527. {
  4528. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4529. $expected = [
  4530. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4531. 'label' => ['for' => 'model-field-v'],
  4532. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4533. 'value',
  4534. '/label'
  4535. ];
  4536. $this->assertHtml($expected, $result);
  4537. }
  4538. /**
  4539. * testSelect method
  4540. *
  4541. * Test select element generation.
  4542. *
  4543. * @return void
  4544. */
  4545. public function testSelect()
  4546. {
  4547. $result = $this->Form->select('Model.field', []);
  4548. $expected = [
  4549. 'select' => ['name' => 'Model[field]'],
  4550. '/select'
  4551. ];
  4552. $this->assertHtml($expected, $result);
  4553. $this->Form->request->data = ['Model' => ['field' => 'value']];
  4554. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4555. $expected = [
  4556. 'select' => ['name' => 'Model[field]'],
  4557. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4558. 'good',
  4559. '/option',
  4560. ['option' => ['value' => 'other']],
  4561. 'bad',
  4562. '/option',
  4563. '/select'
  4564. ];
  4565. $this->assertHtml($expected, $result);
  4566. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4567. $this->assertHtml($expected, $result);
  4568. $this->Form->request->data = [];
  4569. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4570. $expected = [
  4571. 'select' => ['name' => 'Model[field]'],
  4572. ['option' => ['value' => 'value']],
  4573. 'good',
  4574. '/option',
  4575. ['option' => ['value' => 'other']],
  4576. 'bad',
  4577. '/option',
  4578. '/select'
  4579. ];
  4580. $this->assertHtml($expected, $result);
  4581. $options = [
  4582. ['value' => 'first', 'text' => 'First'],
  4583. ['value' => 'first', 'text' => 'Another First'],
  4584. ];
  4585. $result = $this->Form->select(
  4586. 'Model.field',
  4587. $options,
  4588. ['escape' => false, 'empty' => false]
  4589. );
  4590. $expected = [
  4591. 'select' => ['name' => 'Model[field]'],
  4592. ['option' => ['value' => 'first']],
  4593. 'First',
  4594. '/option',
  4595. ['option' => ['value' => 'first']],
  4596. 'Another First',
  4597. '/option',
  4598. '/select'
  4599. ];
  4600. $this->assertHtml($expected, $result);
  4601. $this->Form->request->data = ['Model' => ['contact_id' => 228]];
  4602. $result = $this->Form->select(
  4603. 'Model.contact_id',
  4604. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4605. ['escape' => false, 'empty' => 'pick something']
  4606. );
  4607. $expected = [
  4608. 'select' => ['name' => 'Model[contact_id]'],
  4609. ['option' => ['value' => '']], 'pick something', '/option',
  4610. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4611. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4612. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4613. '/select'
  4614. ];
  4615. $this->assertHtml($expected, $result);
  4616. $this->Form->request->data['Model']['field'] = 0;
  4617. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4618. $expected = [
  4619. 'select' => ['name' => 'Model[field]'],
  4620. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4621. ['option' => ['value' => '1']], 'Yes', '/option',
  4622. '/select'
  4623. ];
  4624. $this->assertHtml($expected, $result);
  4625. }
  4626. /**
  4627. * testSelectEscapeHtml method
  4628. *
  4629. * Test that select() escapes HTML.
  4630. *
  4631. * @return void
  4632. */
  4633. public function testSelectEscapeHtml()
  4634. {
  4635. $result = $this->Form->select(
  4636. 'Model.field',
  4637. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4638. ['empty' => false]
  4639. );
  4640. $expected = [
  4641. 'select' => ['name' => 'Model[field]'],
  4642. ['option' => ['value' => 'first']],
  4643. 'first &quot;html&quot; &lt;chars&gt;',
  4644. '/option',
  4645. ['option' => ['value' => 'second']],
  4646. 'value',
  4647. '/option',
  4648. '/select'
  4649. ];
  4650. $this->assertHtml($expected, $result);
  4651. $result = $this->Form->select(
  4652. 'Model.field',
  4653. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4654. ['escape' => false, 'empty' => false]
  4655. );
  4656. $expected = [
  4657. 'select' => ['name' => 'Model[field]'],
  4658. ['option' => ['value' => 'first']],
  4659. 'first "html" <chars>',
  4660. '/option',
  4661. ['option' => ['value' => 'second']],
  4662. 'value',
  4663. '/option',
  4664. '/select'
  4665. ];
  4666. $this->assertHtml($expected, $result);
  4667. }
  4668. /**
  4669. * testSelectRequired method
  4670. *
  4671. * Test select() with required and disabled attributes.
  4672. *
  4673. * @return void
  4674. */
  4675. public function testSelectRequired()
  4676. {
  4677. $this->article['required'] = [
  4678. 'user_id' => true
  4679. ];
  4680. $this->Form->create($this->article);
  4681. $result = $this->Form->select('user_id', ['option A']);
  4682. $expected = [
  4683. 'select' => [
  4684. 'name' => 'user_id',
  4685. 'required' => 'required'
  4686. ],
  4687. ['option' => ['value' => '0']], 'option A', '/option',
  4688. '/select'
  4689. ];
  4690. $this->assertHtml($expected, $result);
  4691. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4692. $expected = [
  4693. 'select' => [
  4694. 'name' => 'user_id',
  4695. 'disabled' => 'disabled'
  4696. ],
  4697. ['option' => ['value' => '0']], 'option A', '/option',
  4698. '/select'
  4699. ];
  4700. $this->assertHtml($expected, $result);
  4701. }
  4702. /**
  4703. * testNestedSelect method
  4704. *
  4705. * Test select element generation with optgroups.
  4706. *
  4707. * @return void
  4708. */
  4709. public function testNestedSelect()
  4710. {
  4711. $result = $this->Form->select(
  4712. 'Model.field',
  4713. [1 => 'One', 2 => 'Two', 'Three' => [
  4714. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4715. ]],
  4716. ['empty' => false]
  4717. );
  4718. $expected = [
  4719. 'select' => ['name' => 'Model[field]'],
  4720. ['option' => ['value' => 1]],
  4721. 'One',
  4722. '/option',
  4723. ['option' => ['value' => 2]],
  4724. 'Two',
  4725. '/option',
  4726. ['optgroup' => ['label' => 'Three']],
  4727. ['option' => ['value' => 3]],
  4728. 'Three',
  4729. '/option',
  4730. ['option' => ['value' => 4]],
  4731. 'Four',
  4732. '/option',
  4733. ['option' => ['value' => 5]],
  4734. 'Five',
  4735. '/option',
  4736. '/optgroup',
  4737. '/select'
  4738. ];
  4739. $this->assertHtml($expected, $result);
  4740. }
  4741. /**
  4742. * testSelectMultiple method
  4743. *
  4744. * Test generation of multiple select elements.
  4745. *
  4746. * @return void
  4747. */
  4748. public function testSelectMultiple()
  4749. {
  4750. $options = ['first', 'second', 'third'];
  4751. $result = $this->Form->select(
  4752. 'Model.multi_field',
  4753. $options,
  4754. ['form' => 'my-form', 'multiple' => true]
  4755. );
  4756. $expected = [
  4757. 'input' => [
  4758. 'type' => 'hidden',
  4759. 'name' => 'Model[multi_field]',
  4760. 'value' => '',
  4761. 'form' => 'my-form',
  4762. ],
  4763. 'select' => [
  4764. 'name' => 'Model[multi_field][]',
  4765. 'multiple' => 'multiple',
  4766. 'form' => 'my-form',
  4767. ],
  4768. ['option' => ['value' => '0']],
  4769. 'first',
  4770. '/option',
  4771. ['option' => ['value' => '1']],
  4772. 'second',
  4773. '/option',
  4774. ['option' => ['value' => '2']],
  4775. 'third',
  4776. '/option',
  4777. '/select'
  4778. ];
  4779. $this->assertHtml($expected, $result);
  4780. $result = $this->Form->select(
  4781. 'Model.multi_field',
  4782. $options,
  4783. ['multiple' => 'multiple', 'form' => 'my-form']
  4784. );
  4785. $this->assertHtml($expected, $result);
  4786. }
  4787. /**
  4788. * testCheckboxZeroValue method
  4789. *
  4790. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4791. *
  4792. * @return void
  4793. */
  4794. public function testCheckboxZeroValue()
  4795. {
  4796. $result = $this->Form->control('User.get_spam', [
  4797. 'type' => 'checkbox',
  4798. 'value' => '0',
  4799. 'hiddenField' => '1',
  4800. ]);
  4801. $expected = [
  4802. 'div' => ['class' => 'input checkbox'],
  4803. 'label' => ['for' => 'user-get-spam'],
  4804. ['input' => [
  4805. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4806. 'value' => '1'
  4807. ]],
  4808. ['input' => [
  4809. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4810. 'value' => '0', 'id' => 'user-get-spam'
  4811. ]],
  4812. 'Get Spam',
  4813. '/label',
  4814. '/div'
  4815. ];
  4816. $this->assertHtml($expected, $result);
  4817. }
  4818. /**
  4819. * testHabtmSelectBox method
  4820. *
  4821. * Test generation of habtm select boxes.
  4822. *
  4823. * @return void
  4824. */
  4825. public function testHabtmSelectBox()
  4826. {
  4827. $this->loadFixtures('Articles');
  4828. $options = [
  4829. 1 => 'blue',
  4830. 2 => 'red',
  4831. 3 => 'green'
  4832. ];
  4833. $tags = [
  4834. new Entity(['id' => 1, 'name' => 'blue']),
  4835. new Entity(['id' => 3, 'name' => 'green'])
  4836. ];
  4837. $article = new Article(['tags' => $tags]);
  4838. $this->Form->create($article);
  4839. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4840. $expected = [
  4841. 'div' => ['class' => 'input select'],
  4842. 'label' => ['for' => 'tags-ids'],
  4843. 'Tags',
  4844. '/label',
  4845. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4846. 'select' => [
  4847. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4848. 'multiple' => 'multiple'
  4849. ],
  4850. ['option' => ['value' => '1', 'selected' => 'selected']],
  4851. 'blue',
  4852. '/option',
  4853. ['option' => ['value' => '2']],
  4854. 'red',
  4855. '/option',
  4856. ['option' => ['value' => '3', 'selected' => 'selected']],
  4857. 'green',
  4858. '/option',
  4859. '/select',
  4860. '/div'
  4861. ];
  4862. $this->assertHtml($expected, $result);
  4863. // make sure only 50 is selected, and not 50f5c0cf
  4864. $options = [
  4865. '1' => 'blue',
  4866. '50f5c0cf' => 'red',
  4867. '50' => 'green'
  4868. ];
  4869. $tags = [
  4870. new Entity(['id' => 1, 'name' => 'blue']),
  4871. new Entity(['id' => 50, 'name' => 'green'])
  4872. ];
  4873. $article = new Article(['tags' => $tags]);
  4874. $this->Form->create($article);
  4875. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4876. $expected = [
  4877. 'div' => ['class' => 'input select'],
  4878. 'label' => ['for' => 'tags-ids'],
  4879. 'Tags',
  4880. '/label',
  4881. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4882. 'select' => [
  4883. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4884. 'multiple' => 'multiple'
  4885. ],
  4886. ['option' => ['value' => '1', 'selected' => 'selected']],
  4887. 'blue',
  4888. '/option',
  4889. ['option' => ['value' => '50f5c0cf']],
  4890. 'red',
  4891. '/option',
  4892. ['option' => ['value' => '50', 'selected' => 'selected']],
  4893. 'green',
  4894. '/option',
  4895. '/select',
  4896. '/div'
  4897. ];
  4898. $this->assertHtml($expected, $result);
  4899. $spacecraft = [
  4900. 1 => 'Orion',
  4901. 2 => 'Helios'
  4902. ];
  4903. $this->View->viewVars['spacecraft'] = $spacecraft;
  4904. $this->Form->create();
  4905. $result = $this->Form->control('spacecraft._ids');
  4906. $expected = [
  4907. 'div' => ['class' => 'input select'],
  4908. 'label' => ['for' => 'spacecraft-ids'],
  4909. 'Spacecraft',
  4910. '/label',
  4911. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4912. 'select' => [
  4913. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4914. 'multiple' => 'multiple'
  4915. ],
  4916. ['option' => ['value' => '1']],
  4917. 'Orion',
  4918. '/option',
  4919. ['option' => ['value' => '2']],
  4920. 'Helios',
  4921. '/option',
  4922. '/select',
  4923. '/div'
  4924. ];
  4925. $this->assertHtml($expected, $result);
  4926. }
  4927. /**
  4928. * testErrorsForBelongsToManySelect method
  4929. *
  4930. * Tests that errors for belongsToMany select fields are being
  4931. * picked up properly.
  4932. *
  4933. * @return void
  4934. */
  4935. public function testErrorsForBelongsToManySelect()
  4936. {
  4937. $spacecraft = [
  4938. 1 => 'Orion',
  4939. 2 => 'Helios'
  4940. ];
  4941. $this->View->viewVars['spacecraft'] = $spacecraft;
  4942. $article = new Article();
  4943. $article->errors('spacecraft', ['Invalid']);
  4944. $this->Form->create($article);
  4945. $result = $this->Form->control('spacecraft._ids');
  4946. $expected = [
  4947. ['div' => ['class' => 'input select error']],
  4948. 'label' => ['for' => 'spacecraft-ids'],
  4949. 'Spacecraft',
  4950. '/label',
  4951. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4952. 'select' => [
  4953. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4954. 'multiple' => 'multiple'
  4955. ],
  4956. ['option' => ['value' => '1']],
  4957. 'Orion',
  4958. '/option',
  4959. ['option' => ['value' => '2']],
  4960. 'Helios',
  4961. '/option',
  4962. '/select',
  4963. ['div' => ['class' => 'error-message']],
  4964. 'Invalid',
  4965. '/div',
  4966. '/div'
  4967. ];
  4968. $this->assertHtml($expected, $result);
  4969. }
  4970. /**
  4971. * testSelectMultipleCheckboxes method
  4972. *
  4973. * Test generation of multi select elements in checkbox format.
  4974. *
  4975. * @return void
  4976. */
  4977. public function testSelectMultipleCheckboxes()
  4978. {
  4979. $result = $this->Form->select(
  4980. 'Model.multi_field',
  4981. ['first', 'second', 'third'],
  4982. ['multiple' => 'checkbox']
  4983. );
  4984. $expected = [
  4985. 'input' => [
  4986. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4987. ],
  4988. ['div' => ['class' => 'checkbox']],
  4989. ['label' => ['for' => 'model-multi-field-0']],
  4990. ['input' => [
  4991. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4992. 'value' => '0', 'id' => 'model-multi-field-0'
  4993. ]],
  4994. 'first',
  4995. '/label',
  4996. '/div',
  4997. ['div' => ['class' => 'checkbox']],
  4998. ['label' => ['for' => 'model-multi-field-1']],
  4999. ['input' => [
  5000. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5001. 'value' => '1', 'id' => 'model-multi-field-1'
  5002. ]],
  5003. 'second',
  5004. '/label',
  5005. '/div',
  5006. ['div' => ['class' => 'checkbox']],
  5007. ['label' => ['for' => 'model-multi-field-2']],
  5008. ['input' => [
  5009. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5010. 'value' => '2', 'id' => 'model-multi-field-2'
  5011. ]],
  5012. 'third',
  5013. '/label',
  5014. '/div'
  5015. ];
  5016. $this->assertHtml($expected, $result);
  5017. $result = $this->Form->select(
  5018. 'Model.multi_field',
  5019. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5020. ['multiple' => 'checkbox']
  5021. );
  5022. $expected = [
  5023. 'input' => [
  5024. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5025. ],
  5026. ['div' => ['class' => 'checkbox']],
  5027. ['label' => ['for' => 'model-multi-field-a+']],
  5028. ['input' => [
  5029. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5030. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  5031. ]],
  5032. 'first',
  5033. '/label',
  5034. '/div',
  5035. ['div' => ['class' => 'checkbox']],
  5036. ['label' => ['for' => 'model-multi-field-a++']],
  5037. ['input' => [
  5038. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5039. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  5040. ]],
  5041. 'second',
  5042. '/label',
  5043. '/div',
  5044. ['div' => ['class' => 'checkbox']],
  5045. ['label' => ['for' => 'model-multi-field-a+++']],
  5046. ['input' => [
  5047. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5048. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  5049. ]],
  5050. 'third',
  5051. '/label',
  5052. '/div'
  5053. ];
  5054. $this->assertHtml($expected, $result);
  5055. $result = $this->Form->select(
  5056. 'Model.multi_field',
  5057. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5058. ['multiple' => 'checkbox']
  5059. );
  5060. $expected = [
  5061. 'input' => [
  5062. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5063. ],
  5064. ['div' => ['class' => 'checkbox']],
  5065. ['label' => ['for' => 'model-multi-field-a-b']],
  5066. ['input' => [
  5067. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5068. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  5069. ]],
  5070. 'first',
  5071. '/label',
  5072. '/div',
  5073. ['div' => ['class' => 'checkbox']],
  5074. ['label' => ['for' => 'model-multi-field-a-b1']],
  5075. ['input' => [
  5076. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5077. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  5078. ]],
  5079. 'second',
  5080. '/label',
  5081. '/div',
  5082. ['div' => ['class' => 'checkbox']],
  5083. ['label' => ['for' => 'model-multi-field-a-b2']],
  5084. ['input' => [
  5085. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5086. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  5087. ]],
  5088. 'third',
  5089. '/label',
  5090. '/div'
  5091. ];
  5092. $this->assertHtml($expected, $result);
  5093. }
  5094. /**
  5095. * testSelectMultipleCheckboxRequestData method
  5096. *
  5097. * Ensure that multiCheckbox reads from the request data.
  5098. *
  5099. * @return void
  5100. */
  5101. public function testSelectMultipleCheckboxRequestData()
  5102. {
  5103. $this->Form->request->data = ['Model' => ['tags' => [1]]];
  5104. $result = $this->Form->select(
  5105. 'Model.tags',
  5106. ['1' => 'first', 'Array' => 'Array'],
  5107. ['multiple' => 'checkbox']
  5108. );
  5109. $expected = [
  5110. 'input' => [
  5111. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  5112. ],
  5113. ['div' => ['class' => 'checkbox']],
  5114. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5115. ['input' => [
  5116. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5117. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  5118. ]],
  5119. 'first',
  5120. '/label',
  5121. '/div',
  5122. ['div' => ['class' => 'checkbox']],
  5123. ['label' => ['for' => 'model-tags-array']],
  5124. ['input' => [
  5125. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5126. 'value' => 'Array', 'id' => 'model-tags-array'
  5127. ]],
  5128. 'Array',
  5129. '/label',
  5130. '/div'
  5131. ];
  5132. $this->assertHtml($expected, $result);
  5133. }
  5134. /**
  5135. * testSelectMultipleCheckboxSecurity method
  5136. *
  5137. * Checks the security hash array generated for multiple-input checkbox elements.
  5138. *
  5139. * @return void
  5140. */
  5141. public function testSelectMultipleCheckboxSecurity()
  5142. {
  5143. $this->Form->request->params['_Token'] = 'testKey';
  5144. $this->assertEquals([], $this->Form->fields);
  5145. $this->Form->select(
  5146. 'Model.multi_field',
  5147. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5148. ['multiple' => 'checkbox']
  5149. );
  5150. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5151. $result = $this->Form->secure($this->Form->fields);
  5152. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  5153. $this->assertRegExp('/"' . $key . '"/', $result);
  5154. }
  5155. /**
  5156. * testSelectMultipleSecureWithNoOptions method
  5157. *
  5158. * Multiple select elements should always be secured as they always participate
  5159. * in the POST data.
  5160. *
  5161. * @return void
  5162. */
  5163. public function testSelectMultipleSecureWithNoOptions()
  5164. {
  5165. $this->assertEquals([], $this->Form->fields);
  5166. $this->Form->select(
  5167. 'Model.select',
  5168. [],
  5169. ['multiple' => true]
  5170. );
  5171. $this->assertEquals(['Model.select'], $this->Form->fields);
  5172. }
  5173. /**
  5174. * testSelectNoSecureWithNoOptions method
  5175. *
  5176. * When a select box has no options it should not be added to the fields list
  5177. * as it always fail post validation.
  5178. *
  5179. * @return void
  5180. */
  5181. public function testSelectNoSecureWithNoOptions()
  5182. {
  5183. $this->Form->request->params['_Token'] = 'testkey';
  5184. $this->assertEquals([], $this->Form->fields);
  5185. $this->Form->select(
  5186. 'Model.select',
  5187. []
  5188. );
  5189. $this->assertEquals([], $this->Form->fields);
  5190. $this->Form->select(
  5191. 'Model.user_id',
  5192. [],
  5193. ['empty' => true]
  5194. );
  5195. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5196. }
  5197. /**
  5198. * testControlMultipleCheckboxes method
  5199. *
  5200. * Test control() resulting in multi select elements being generated.
  5201. *
  5202. * @return void
  5203. */
  5204. public function testControlMultipleCheckboxes()
  5205. {
  5206. $result = $this->Form->control('Model.multi_field', [
  5207. 'options' => ['first', 'second', 'third'],
  5208. 'multiple' => 'checkbox'
  5209. ]);
  5210. $expected = [
  5211. ['div' => ['class' => 'input select']],
  5212. ['label' => ['for' => 'model-multi-field']],
  5213. 'Multi Field',
  5214. '/label',
  5215. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5216. ['div' => ['class' => 'checkbox']],
  5217. ['label' => ['for' => 'model-multi-field-0']],
  5218. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5219. 'first',
  5220. '/label',
  5221. '/div',
  5222. ['div' => ['class' => 'checkbox']],
  5223. ['label' => ['for' => 'model-multi-field-1']],
  5224. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5225. 'second',
  5226. '/label',
  5227. '/div',
  5228. ['div' => ['class' => 'checkbox']],
  5229. ['label' => ['for' => 'model-multi-field-2']],
  5230. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5231. 'third',
  5232. '/label',
  5233. '/div',
  5234. '/div'
  5235. ];
  5236. $this->assertHtml($expected, $result);
  5237. $result = $this->Form->control('Model.multi_field', [
  5238. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5239. 'multiple' => 'checkbox'
  5240. ]);
  5241. $expected = [
  5242. ['div' => ['class' => 'input select']],
  5243. ['label' => ['for' => 'model-multi-field']],
  5244. 'Multi Field',
  5245. '/label',
  5246. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5247. ['div' => ['class' => 'checkbox']],
  5248. ['label' => ['for' => 'model-multi-field-a']],
  5249. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5250. 'first',
  5251. '/label',
  5252. '/div',
  5253. ['div' => ['class' => 'checkbox']],
  5254. ['label' => ['for' => 'model-multi-field-b']],
  5255. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5256. 'second',
  5257. '/label',
  5258. '/div',
  5259. ['div' => ['class' => 'checkbox']],
  5260. ['label' => ['for' => 'model-multi-field-c']],
  5261. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5262. 'third',
  5263. '/label',
  5264. '/div',
  5265. '/div'
  5266. ];
  5267. $this->assertHtml($expected, $result);
  5268. }
  5269. /**
  5270. * testSelectHiddenFieldOmission method
  5271. *
  5272. * Test that select() with 'hiddenField' => false omits the hidden field.
  5273. *
  5274. * @return void
  5275. */
  5276. public function testSelectHiddenFieldOmission()
  5277. {
  5278. $result = $this->Form->select(
  5279. 'Model.multi_field',
  5280. ['first', 'second'],
  5281. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5282. );
  5283. $this->assertNotContains('type="hidden"', $result);
  5284. }
  5285. /**
  5286. * testSelectCheckboxMultipleOverrideName method
  5287. *
  5288. * Test that select() with multiple = checkbox works with overriding name attribute.
  5289. *
  5290. * @return void
  5291. */
  5292. public function testSelectCheckboxMultipleOverrideName()
  5293. {
  5294. $result = $this->Form->select('category', ['1', '2'], [
  5295. 'multiple' => 'checkbox',
  5296. 'name' => 'fish',
  5297. ]);
  5298. $expected = [
  5299. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5300. ['div' => ['class' => 'checkbox']],
  5301. ['label' => ['for' => 'fish-0']],
  5302. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5303. '1',
  5304. '/label',
  5305. '/div',
  5306. ['div' => ['class' => 'checkbox']],
  5307. ['label' => ['for' => 'fish-1']],
  5308. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5309. '2',
  5310. '/label',
  5311. '/div'
  5312. ];
  5313. $this->assertHtml($expected, $result);
  5314. $result = $this->Form->multiCheckbox(
  5315. 'category',
  5316. new Collection(['1', '2']),
  5317. ['name' => 'fish']
  5318. );
  5319. $this->assertHtml($expected, $result);
  5320. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5321. 'name' => 'fish',
  5322. ]);
  5323. $this->assertHtml($expected, $result);
  5324. }
  5325. /**
  5326. * testControlMultiCheckbox method
  5327. *
  5328. * Test that control() works with multicheckbox.
  5329. *
  5330. * @return void
  5331. */
  5332. public function testControlMultiCheckbox()
  5333. {
  5334. $result = $this->Form->control('category', [
  5335. 'type' => 'multicheckbox',
  5336. 'options' => ['1', '2'],
  5337. ]);
  5338. $expected = [
  5339. ['div' => ['class' => 'input multicheckbox']],
  5340. '<label',
  5341. 'Category',
  5342. '/label',
  5343. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5344. ['div' => ['class' => 'checkbox']],
  5345. ['label' => ['for' => 'category-0']],
  5346. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5347. '1',
  5348. '/label',
  5349. '/div',
  5350. ['div' => ['class' => 'checkbox']],
  5351. ['label' => ['for' => 'category-1']],
  5352. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5353. '2',
  5354. '/label',
  5355. '/div',
  5356. '/div',
  5357. ];
  5358. $this->assertHtml($expected, $result);
  5359. }
  5360. /**
  5361. * testCheckbox method
  5362. *
  5363. * Test generation of checkboxes.
  5364. *
  5365. * @return void
  5366. */
  5367. public function testCheckbox()
  5368. {
  5369. $result = $this->Form->checkbox('Model.field');
  5370. $expected = [
  5371. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5372. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5373. ];
  5374. $this->assertHtml($expected, $result);
  5375. $result = $this->Form->checkbox('Model.field', [
  5376. 'id' => 'theID',
  5377. 'value' => 'myvalue',
  5378. 'form' => 'my-form',
  5379. ]);
  5380. $expected = [
  5381. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5382. ['input' => [
  5383. 'type' => 'checkbox', 'name' => 'Model[field]',
  5384. 'value' => 'myvalue', 'id' => 'theID',
  5385. 'form' => 'my-form',
  5386. ]]
  5387. ];
  5388. $this->assertHtml($expected, $result);
  5389. }
  5390. /**
  5391. * testCheckboxDefaultValue method
  5392. *
  5393. * Test default value setting on checkbox() method.
  5394. *
  5395. * @return void
  5396. */
  5397. public function testCheckboxDefaultValue()
  5398. {
  5399. $this->Form->request->data['Model']['field'] = false;
  5400. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5401. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5402. $this->assertHtml($expected, $result);
  5403. unset($this->Form->request->data['Model']['field']);
  5404. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5405. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5406. $this->assertHtml($expected, $result);
  5407. $this->Form->request->data['Model']['field'] = true;
  5408. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5409. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5410. $this->assertHtml($expected, $result);
  5411. unset($this->Form->request->data['Model']['field']);
  5412. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5413. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5414. $this->assertHtml($expected, $result);
  5415. $Articles = TableRegistry::get('Articles');
  5416. $Articles->schema()->addColumn(
  5417. 'published',
  5418. ['type' => 'boolean', 'null' => false, 'default' => true]
  5419. );
  5420. $this->Form->create($Articles->newEntity());
  5421. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5422. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5423. $this->assertHtml($expected, $result);
  5424. }
  5425. /**
  5426. * testCheckboxCheckedAndError method
  5427. *
  5428. * Test checkbox being checked or having errors.
  5429. *
  5430. * @return void
  5431. */
  5432. public function testCheckboxCheckedAndError()
  5433. {
  5434. $this->article['errors'] = [
  5435. 'published' => true
  5436. ];
  5437. $this->Form->request->data['published'] = 'myvalue';
  5438. $this->Form->create($this->article);
  5439. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5440. $expected = [
  5441. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5442. ['input' => [
  5443. 'type' => 'checkbox',
  5444. 'name' => 'published',
  5445. 'value' => 'myvalue',
  5446. 'id' => 'theID',
  5447. 'checked' => 'checked',
  5448. 'class' => 'form-error'
  5449. ]]
  5450. ];
  5451. $this->assertHtml($expected, $result);
  5452. $this->Form->request->data['published'] = '';
  5453. $result = $this->Form->checkbox('published');
  5454. $expected = [
  5455. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5456. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5457. ];
  5458. $this->assertHtml($expected, $result);
  5459. }
  5460. /**
  5461. * testCheckboxCustomNameAttribute method
  5462. *
  5463. * Test checkbox() with a custom name attribute.
  5464. *
  5465. * @return void
  5466. */
  5467. public function testCheckboxCustomNameAttribute()
  5468. {
  5469. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5470. $expected = [
  5471. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5472. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5473. ];
  5474. $this->assertHtml($expected, $result);
  5475. }
  5476. /**
  5477. * testCheckboxHiddenField method
  5478. *
  5479. * Test that the hidden input for checkboxes can be omitted or set to a
  5480. * specific value.
  5481. *
  5482. * @return void
  5483. */
  5484. public function testCheckboxHiddenField()
  5485. {
  5486. $result = $this->Form->checkbox('UserForm.something', [
  5487. 'hiddenField' => false
  5488. ]);
  5489. $expected = [
  5490. 'input' => [
  5491. 'type' => 'checkbox',
  5492. 'name' => 'UserForm[something]',
  5493. 'value' => '1'
  5494. ],
  5495. ];
  5496. $this->assertHtml($expected, $result);
  5497. $result = $this->Form->checkbox('UserForm.something', [
  5498. 'value' => 'Y',
  5499. 'hiddenField' => 'N',
  5500. ]);
  5501. $expected = [
  5502. ['input' => [
  5503. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5504. 'value' => 'N'
  5505. ]],
  5506. ['input' => [
  5507. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5508. 'value' => 'Y'
  5509. ]],
  5510. ];
  5511. $this->assertHtml($expected, $result);
  5512. }
  5513. /**
  5514. * testTime method
  5515. *
  5516. * Test the time type.
  5517. *
  5518. * @return void
  5519. */
  5520. public function testTime()
  5521. {
  5522. $result = $this->Form->time('start_time', [
  5523. 'timeFormat' => 12,
  5524. 'interval' => 5,
  5525. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5526. ]);
  5527. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5528. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5529. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5530. $this->assertNotContains('year', $result);
  5531. $this->assertNotContains('month', $result);
  5532. $this->assertNotContains('day', $result);
  5533. $result = $this->Form->time('start_time', [
  5534. 'timeFormat' => 12,
  5535. 'interval' => 5,
  5536. 'value' => '2014-03-08 16:30:00'
  5537. ]);
  5538. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5539. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5540. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5541. $this->assertNotContains('year', $result);
  5542. $this->assertNotContains('month', $result);
  5543. $this->assertNotContains('day', $result);
  5544. }
  5545. /**
  5546. * testTimeFormat24NoMeridian method
  5547. *
  5548. * Ensure that timeFormat=24 has no merdian.
  5549. *
  5550. * @return void.
  5551. */
  5552. public function testTimeFormat24NoMeridian()
  5553. {
  5554. $result = $this->Form->time('start_time', [
  5555. 'timeFormat' => 24,
  5556. 'interval' => 5,
  5557. 'value' => '2014-03-08 16:30:00'
  5558. ]);
  5559. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5560. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5561. $this->assertNotContains('meridian', $result);
  5562. $this->assertNotContains('pm', $result);
  5563. $this->assertNotContains('year', $result);
  5564. $this->assertNotContains('month', $result);
  5565. $this->assertNotContains('day', $result);
  5566. }
  5567. /**
  5568. * testDate method
  5569. *
  5570. * Test the date type.
  5571. *
  5572. * @return void
  5573. */
  5574. public function testDate()
  5575. {
  5576. $result = $this->Form->date('start_day', [
  5577. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5578. ]);
  5579. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5580. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5581. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5582. $this->assertNotContains('hour', $result);
  5583. $this->assertNotContains('minute', $result);
  5584. $this->assertNotContains('second', $result);
  5585. $this->assertNotContains('meridian', $result);
  5586. }
  5587. /**
  5588. * testDateTime method
  5589. *
  5590. * Test generation of date/time select elements.
  5591. *
  5592. * @return void
  5593. */
  5594. public function testDateTime()
  5595. {
  5596. extract($this->dateRegex);
  5597. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5598. $now = strtotime('now');
  5599. $expected = [
  5600. ['select' => ['name' => 'Contact[date][year]']],
  5601. ['option' => ['value' => '']],
  5602. '/option',
  5603. $yearsRegex,
  5604. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5605. date('Y', $now),
  5606. '/option',
  5607. '*/select',
  5608. ['select' => ['name' => 'Contact[date][month]']],
  5609. ['option' => ['value' => '']],
  5610. '/option',
  5611. $monthsRegex,
  5612. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5613. date('F', $now),
  5614. '/option',
  5615. '*/select',
  5616. ['select' => ['name' => 'Contact[date][day]']],
  5617. ['option' => ['value' => '']],
  5618. '/option',
  5619. $daysRegex,
  5620. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5621. date('j', $now),
  5622. '/option',
  5623. '*/select',
  5624. ['select' => ['name' => 'Contact[date][hour]']],
  5625. ['option' => ['value' => '']],
  5626. '/option',
  5627. $hoursRegex,
  5628. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5629. date('G', $now),
  5630. '/option',
  5631. '*/select',
  5632. ['select' => ['name' => 'Contact[date][minute]']],
  5633. ['option' => ['value' => '']],
  5634. '/option',
  5635. $minutesRegex,
  5636. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5637. date('i', $now),
  5638. '/option',
  5639. '*/select',
  5640. ];
  5641. $this->assertHtml($expected, $result);
  5642. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5643. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5644. $now = strtotime('now');
  5645. $expected = [
  5646. ['select' => ['name' => 'Contact[date][year]']],
  5647. $yearsRegex,
  5648. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5649. date('Y', $now),
  5650. '/option',
  5651. '*/select',
  5652. ['select' => ['name' => 'Contact[date][month]']],
  5653. $monthsRegex,
  5654. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5655. date('F', $now),
  5656. '/option',
  5657. '*/select',
  5658. ['select' => ['name' => 'Contact[date][day]']],
  5659. $daysRegex,
  5660. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5661. date('j', $now),
  5662. '/option',
  5663. '*/select',
  5664. ['select' => ['name' => 'Contact[date][hour]']],
  5665. $hoursRegex,
  5666. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5667. date('G', $now),
  5668. '/option',
  5669. '*/select',
  5670. ['select' => ['name' => 'Contact[date][minute]']],
  5671. $minutesRegex,
  5672. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5673. date('i', $now),
  5674. '/option',
  5675. '*/select',
  5676. ];
  5677. $this->assertHtml($expected, $result);
  5678. }
  5679. /**
  5680. * testDateTimeSecured method
  5681. *
  5682. * Test that datetime fields are added to protected fields list.
  5683. *
  5684. * @return void
  5685. */
  5686. public function testDateTimeSecured()
  5687. {
  5688. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5689. $this->Form->dateTime('Contact.date');
  5690. $expected = [
  5691. 'Contact.date.year',
  5692. 'Contact.date.month',
  5693. 'Contact.date.day',
  5694. 'Contact.date.hour',
  5695. 'Contact.date.minute',
  5696. ];
  5697. $this->assertEquals($expected, $this->Form->fields);
  5698. $this->Form->fields = [];
  5699. $this->Form->date('Contact.published');
  5700. $expected = [
  5701. 'Contact.published.year',
  5702. 'Contact.published.month',
  5703. 'Contact.published.day',
  5704. ];
  5705. $this->assertEquals($expected, $this->Form->fields);
  5706. }
  5707. /**
  5708. * testDateTimeSecuredDisabled method
  5709. *
  5710. * Test that datetime fields are added to protected fields list.
  5711. *
  5712. * @return void
  5713. */
  5714. public function testDateTimeSecuredDisabled()
  5715. {
  5716. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5717. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5718. $expected = [];
  5719. $this->assertEquals($expected, $this->Form->fields);
  5720. $this->Form->fields = [];
  5721. $this->Form->date('Contact.published', ['secure' => false]);
  5722. $expected = [];
  5723. $this->assertEquals($expected, $this->Form->fields);
  5724. }
  5725. /**
  5726. * testDatetimeEmpty method
  5727. *
  5728. * Test empty defaulting to true for datetime.
  5729. *
  5730. * @return void
  5731. */
  5732. public function testDatetimeEmpty()
  5733. {
  5734. extract($this->dateRegex);
  5735. $result = $this->Form->dateTime('Contact.date', [
  5736. 'timeFormat' => 12,
  5737. 'empty' => true,
  5738. 'default' => true
  5739. ]);
  5740. $expected = [
  5741. ['select' => ['name' => 'Contact[date][year]']],
  5742. $yearsRegex,
  5743. ['option' => ['value' => '']],
  5744. '/option',
  5745. '*/select',
  5746. ['select' => ['name' => 'Contact[date][month]']],
  5747. $monthsRegex,
  5748. ['option' => ['value' => '']],
  5749. '/option',
  5750. '*/select',
  5751. ['select' => ['name' => 'Contact[date][day]']],
  5752. $daysRegex,
  5753. ['option' => ['value' => '']],
  5754. '/option',
  5755. '*/select',
  5756. ['select' => ['name' => 'Contact[date][hour]']],
  5757. $hoursRegex,
  5758. ['option' => ['value' => '']],
  5759. '/option',
  5760. '*/select',
  5761. ['select' => ['name' => 'Contact[date][minute]']],
  5762. $minutesRegex,
  5763. ['option' => ['value' => '']],
  5764. '/option',
  5765. '*/select',
  5766. ['select' => ['name' => 'Contact[date][meridian]']],
  5767. $meridianRegex,
  5768. ['option' => ['value' => '']],
  5769. '/option',
  5770. '*/select'
  5771. ];
  5772. $this->assertHtml($expected, $result);
  5773. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5774. }
  5775. /**
  5776. * testDatetimeMinuteInterval method
  5777. *
  5778. * Test datetime with interval option.
  5779. *
  5780. * @return void
  5781. */
  5782. public function testDatetimeMinuteInterval()
  5783. {
  5784. extract($this->dateRegex);
  5785. $result = $this->Form->dateTime('Contact.date', [
  5786. 'interval' => 5,
  5787. 'value' => ''
  5788. ]);
  5789. $expected = [
  5790. ['select' => ['name' => 'Contact[date][year]']],
  5791. $yearsRegex,
  5792. ['option' => ['selected' => 'selected', 'value' => '']],
  5793. '/option',
  5794. '*/select',
  5795. ['select' => ['name' => 'Contact[date][month]']],
  5796. $monthsRegex,
  5797. ['option' => ['selected' => 'selected', 'value' => '']],
  5798. '/option',
  5799. '*/select',
  5800. ['select' => ['name' => 'Contact[date][day]']],
  5801. $daysRegex,
  5802. ['option' => ['selected' => 'selected', 'value' => '']],
  5803. '/option',
  5804. '*/select',
  5805. ['select' => ['name' => 'Contact[date][hour]']],
  5806. $hoursRegex,
  5807. ['option' => ['selected' => 'selected', 'value' => '']],
  5808. '/option',
  5809. '*/select',
  5810. ['select' => ['name' => 'Contact[date][minute]']],
  5811. $minutesRegex,
  5812. ['option' => ['selected' => 'selected', 'value' => '']],
  5813. '/option',
  5814. ['option' => ['value' => '00']],
  5815. '00',
  5816. '/option',
  5817. ['option' => ['value' => '05']],
  5818. '05',
  5819. '/option',
  5820. ['option' => ['value' => '10']],
  5821. '10',
  5822. '/option',
  5823. '*/select',
  5824. ];
  5825. $this->assertHtml($expected, $result);
  5826. }
  5827. /**
  5828. * testDateTimeRounding method
  5829. *
  5830. * Test dateTime with rounding.
  5831. *
  5832. * @return void
  5833. */
  5834. public function testDateTimeRounding()
  5835. {
  5836. $this->Form->request->data['Contact'] = [
  5837. 'date' => [
  5838. 'day' => '13',
  5839. 'month' => '12',
  5840. 'year' => '2010',
  5841. 'hour' => '04',
  5842. 'minute' => '19',
  5843. 'meridian' => 'AM'
  5844. ]
  5845. ];
  5846. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5847. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5848. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5849. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5850. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5851. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5852. }
  5853. /**
  5854. * testDatetimeWithDefault method
  5855. *
  5856. * Test that datetime() and default values work.
  5857. *
  5858. * @return void
  5859. */
  5860. public function testDatetimeWithDefault()
  5861. {
  5862. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5863. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5864. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5865. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5866. $result = $this->Form->dateTime('Contact.updated', [
  5867. 'default' => '2009-06-01 11:15:30'
  5868. ]);
  5869. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5870. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5871. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5872. }
  5873. /**
  5874. * testDateTimeAllZeros method
  5875. *
  5876. * Test datetime() with all zeros.
  5877. *
  5878. * @return void
  5879. */
  5880. public function testDateTimeAllZeros()
  5881. {
  5882. $result = $this->Form->dateTime('Contact.date', [
  5883. 'timeFormat' => false,
  5884. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5885. 'value' => '0000-00-00'
  5886. ]);
  5887. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5888. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5889. }
  5890. /**
  5891. * testDateTimeEmptyAsArray method
  5892. *
  5893. * @return void
  5894. */
  5895. public function testDateTimeEmptyAsArray()
  5896. {
  5897. $result = $this->Form->dateTime('Contact.date', [
  5898. 'empty' => [
  5899. 'day' => 'DAY',
  5900. 'month' => 'MONTH',
  5901. 'year' => 'YEAR',
  5902. 'hour' => 'HOUR',
  5903. 'minute' => 'MINUTE',
  5904. 'meridian' => false
  5905. ],
  5906. 'default' => true
  5907. ]);
  5908. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5909. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5910. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5911. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5912. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5913. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5914. $result = $this->Form->dateTime('Contact.date', [
  5915. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5916. 'default' => true
  5917. ]);
  5918. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5919. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5920. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5921. }
  5922. /**
  5923. * testFormDateTimeMulti method
  5924. *
  5925. * Test multiple datetime element generation.
  5926. *
  5927. * @return void
  5928. */
  5929. public function testFormDateTimeMulti()
  5930. {
  5931. extract($this->dateRegex);
  5932. $result = $this->Form->dateTime('Contact.1.updated');
  5933. $this->assertContains('Contact[1][updated][month]', $result);
  5934. $this->assertContains('Contact[1][updated][day]', $result);
  5935. $this->assertContains('Contact[1][updated][year]', $result);
  5936. $this->assertContains('Contact[1][updated][hour]', $result);
  5937. $this->assertContains('Contact[1][updated][minute]', $result);
  5938. }
  5939. /**
  5940. * testDateTimeLabelIdMatchesFirstControl method
  5941. *
  5942. * When changing the date format, the label should always focus the first select box when
  5943. * clicked.
  5944. *
  5945. * @return void
  5946. */
  5947. public function testDateTimeLabelIdMatchesFirstControl()
  5948. {
  5949. $result = $this->Form->control('Model.date', ['type' => 'date']);
  5950. $this->assertContains('<label>Date</label>', $result);
  5951. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  5952. $this->assertContains('<label>Date</label>', $result);
  5953. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  5954. $this->assertContains('<label>Date</label>', $result);
  5955. }
  5956. /**
  5957. * testDateTimeSecondOptions method
  5958. *
  5959. * Test datetime second=true.
  5960. *
  5961. * @return void
  5962. */
  5963. public function testDateTimeSecondOptions()
  5964. {
  5965. $result = $this->Form->dateTime('updated', ['second' => true]);
  5966. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5967. $result = $this->Form->dateTime('updated', ['second' => []]);
  5968. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5969. $result = $this->Form->dateTime('updated', ['second' => null]);
  5970. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5971. $result = $this->Form->dateTime('updated', ['second' => false]);
  5972. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5973. }
  5974. /**
  5975. * testMonth method
  5976. *
  5977. * Test generation of a month input.
  5978. *
  5979. * @return void
  5980. */
  5981. public function testMonth()
  5982. {
  5983. $result = $this->Form->month('Model.field', ['value' => '']);
  5984. $expected = [
  5985. ['select' => ['name' => 'Model[field][month]']],
  5986. ['option' => ['value' => '', 'selected' => 'selected']],
  5987. '/option',
  5988. ['option' => ['value' => '01']],
  5989. date('F', strtotime('2008-01-01 00:00:00')),
  5990. '/option',
  5991. ['option' => ['value' => '02']],
  5992. date('F', strtotime('2008-02-01 00:00:00')),
  5993. '/option',
  5994. '*/select',
  5995. ];
  5996. $this->assertHtml($expected, $result);
  5997. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  5998. $expected = [
  5999. ['select' => ['name' => 'Model[field][month]']],
  6000. ['option' => ['selected' => 'selected', 'value' => '']],
  6001. '/option',
  6002. ['option' => ['value' => '01']],
  6003. date('F', strtotime('2008-01-01 00:00:00')),
  6004. '/option',
  6005. ['option' => ['value' => '02']],
  6006. date('F', strtotime('2008-02-01 00:00:00')),
  6007. '/option',
  6008. '*/select',
  6009. ];
  6010. $this->assertHtml($expected, $result);
  6011. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  6012. $expected = [
  6013. ['select' => ['name' => 'Model[field][month]']],
  6014. ['option' => ['selected' => 'selected', 'value' => '']],
  6015. '/option',
  6016. ['option' => ['value' => '01']],
  6017. '1',
  6018. '/option',
  6019. ['option' => ['value' => '02']],
  6020. '2',
  6021. '/option',
  6022. '*/select',
  6023. ];
  6024. $this->assertHtml($expected, $result);
  6025. $monthNames = [
  6026. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6027. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  6028. ];
  6029. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  6030. $expected = [
  6031. ['select' => ['name' => 'Model[field][month]']],
  6032. ['option' => ['value' => '']],
  6033. '/option',
  6034. ['option' => ['value' => '01', 'selected' => 'selected']],
  6035. 'Jan',
  6036. '/option',
  6037. ['option' => ['value' => '02']],
  6038. 'Feb',
  6039. '/option',
  6040. '*/select',
  6041. ];
  6042. $this->assertHtml($expected, $result);
  6043. $this->Form->request->data['Project']['release'] = '2050-02-10';
  6044. $result = $this->Form->month('Project.release');
  6045. $expected = [
  6046. ['select' => ['name' => 'Project[release][month]']],
  6047. ['option' => ['value' => '']],
  6048. '/option',
  6049. ['option' => ['value' => '01']],
  6050. 'January',
  6051. '/option',
  6052. ['option' => ['value' => '02', 'selected' => 'selected']],
  6053. 'February',
  6054. '/option',
  6055. '*/select',
  6056. ];
  6057. $this->assertHtml($expected, $result);
  6058. $result = $this->Form->month('Contact.published', [
  6059. 'empty' => 'Published on',
  6060. ]);
  6061. $this->assertContains('Published on', $result);
  6062. }
  6063. /**
  6064. * testDay method
  6065. *
  6066. * Test generation of a day input.
  6067. *
  6068. * @return void
  6069. */
  6070. public function testDay()
  6071. {
  6072. extract($this->dateRegex);
  6073. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  6074. $expected = [
  6075. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  6076. ['option' => ['selected' => 'selected', 'value' => '']],
  6077. '/option',
  6078. ['option' => ['value' => '01']],
  6079. '1',
  6080. '/option',
  6081. ['option' => ['value' => '02']],
  6082. '2',
  6083. '/option',
  6084. $daysRegex,
  6085. '/select',
  6086. ];
  6087. $this->assertHtml($expected, $result);
  6088. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6089. $result = $this->Form->day('Model.field');
  6090. $expected = [
  6091. ['select' => ['name' => 'Model[field][day]']],
  6092. ['option' => ['value' => '']],
  6093. '/option',
  6094. ['option' => ['value' => '01']],
  6095. '1',
  6096. '/option',
  6097. ['option' => ['value' => '02']],
  6098. '2',
  6099. '/option',
  6100. $daysRegex,
  6101. ['option' => ['value' => '10', 'selected' => 'selected']],
  6102. '10',
  6103. '/option',
  6104. $daysRegex,
  6105. '/select',
  6106. ];
  6107. $this->assertHtml($expected, $result);
  6108. $this->Form->request->data['Model']['field'] = '';
  6109. $result = $this->Form->day('Model.field', ['value' => '10']);
  6110. $expected = [
  6111. ['select' => ['name' => 'Model[field][day]']],
  6112. ['option' => ['value' => '']],
  6113. '/option',
  6114. ['option' => ['value' => '01']],
  6115. '1',
  6116. '/option',
  6117. ['option' => ['value' => '02']],
  6118. '2',
  6119. '/option',
  6120. $daysRegex,
  6121. ['option' => ['value' => '10', 'selected' => 'selected']],
  6122. '10',
  6123. '/option',
  6124. $daysRegex,
  6125. '/select',
  6126. ];
  6127. $this->assertHtml($expected, $result);
  6128. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6129. $result = $this->Form->day('Project.release');
  6130. $expected = [
  6131. ['select' => ['name' => 'Project[release][day]']],
  6132. ['option' => ['value' => '']],
  6133. '/option',
  6134. ['option' => ['value' => '01']],
  6135. '1',
  6136. '/option',
  6137. ['option' => ['value' => '02']],
  6138. '2',
  6139. '/option',
  6140. $daysRegex,
  6141. ['option' => ['value' => '10', 'selected' => 'selected']],
  6142. '10',
  6143. '/option',
  6144. $daysRegex,
  6145. '/select',
  6146. ];
  6147. $this->assertHtml($expected, $result);
  6148. $result = $this->Form->day('Contact.published', [
  6149. 'empty' => 'Published on',
  6150. ]);
  6151. $this->assertContains('Published on', $result);
  6152. }
  6153. /**
  6154. * testMinute method
  6155. *
  6156. * Test generation of a minute input.
  6157. *
  6158. * @return void
  6159. */
  6160. public function testMinute()
  6161. {
  6162. extract($this->dateRegex);
  6163. $result = $this->Form->minute('Model.field', ['value' => '']);
  6164. $expected = [
  6165. ['select' => ['name' => 'Model[field][minute]']],
  6166. ['option' => ['selected' => 'selected', 'value' => '']],
  6167. '/option',
  6168. ['option' => ['value' => '00']],
  6169. '00',
  6170. '/option',
  6171. ['option' => ['value' => '01']],
  6172. '01',
  6173. '/option',
  6174. ['option' => ['value' => '02']],
  6175. '02',
  6176. '/option',
  6177. $minutesRegex,
  6178. '/select',
  6179. ];
  6180. $this->assertHtml($expected, $result);
  6181. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6182. $result = $this->Form->minute('Model.field');
  6183. $expected = [
  6184. ['select' => ['name' => 'Model[field][minute]']],
  6185. ['option' => ['value' => '']],
  6186. '/option',
  6187. ['option' => ['value' => '00']],
  6188. '00',
  6189. '/option',
  6190. ['option' => ['value' => '01']],
  6191. '01',
  6192. '/option',
  6193. ['option' => ['value' => '02']],
  6194. '02',
  6195. '/option',
  6196. $minutesRegex,
  6197. ['option' => ['value' => '12', 'selected' => 'selected']],
  6198. '12',
  6199. '/option',
  6200. $minutesRegex,
  6201. '/select',
  6202. ];
  6203. $this->assertHtml($expected, $result);
  6204. $this->Form->request->data['Model']['field'] = '';
  6205. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6206. $expected = [
  6207. ['select' => ['name' => 'Model[field][minute]']],
  6208. ['option' => ['selected' => 'selected', 'value' => '']],
  6209. '/option',
  6210. ['option' => ['value' => '00']],
  6211. '00',
  6212. '/option',
  6213. ['option' => ['value' => '05']],
  6214. '05',
  6215. '/option',
  6216. ['option' => ['value' => '10']],
  6217. '10',
  6218. '/option',
  6219. $minutesRegex,
  6220. '/select',
  6221. ];
  6222. $this->assertHtml($expected, $result);
  6223. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6224. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6225. $expected = [
  6226. ['select' => ['name' => 'Model[field][minute]']],
  6227. ['option' => ['value' => '']],
  6228. '/option',
  6229. ['option' => ['value' => '00']],
  6230. '00',
  6231. '/option',
  6232. ['option' => ['value' => '05']],
  6233. '05',
  6234. '/option',
  6235. ['option' => ['value' => '10', 'selected' => 'selected']],
  6236. '10',
  6237. '/option',
  6238. $minutesRegex,
  6239. '/select',
  6240. ];
  6241. $this->assertHtml($expected, $result);
  6242. }
  6243. /**
  6244. * testMeridian method
  6245. *
  6246. * Test generating an input for the meridian.
  6247. *
  6248. * @return void
  6249. */
  6250. public function testMeridian()
  6251. {
  6252. extract($this->dateRegex);
  6253. $now = new \DateTime();
  6254. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6255. $expected = [
  6256. ['select' => ['name' => 'Model[field][meridian]']],
  6257. ['option' => ['value' => '']],
  6258. '/option',
  6259. $meridianRegex,
  6260. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6261. $now->format('a'),
  6262. '/option',
  6263. '*/select'
  6264. ];
  6265. $this->assertHtml($expected, $result);
  6266. }
  6267. /**
  6268. * testHour method
  6269. *
  6270. * Test generation of an hour input.
  6271. *
  6272. * @return void
  6273. */
  6274. public function testHour()
  6275. {
  6276. extract($this->dateRegex);
  6277. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6278. $expected = [
  6279. ['select' => ['name' => 'Model[field][hour]']],
  6280. ['option' => ['selected' => 'selected', 'value' => '']],
  6281. '/option',
  6282. ['option' => ['value' => '01']],
  6283. '1',
  6284. '/option',
  6285. ['option' => ['value' => '02']],
  6286. '2',
  6287. '/option',
  6288. $hoursRegex,
  6289. '/select',
  6290. ];
  6291. $this->assertHtml($expected, $result);
  6292. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6293. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6294. $expected = [
  6295. ['select' => ['name' => 'Model[field][hour]']],
  6296. ['option' => ['value' => '']],
  6297. '/option',
  6298. ['option' => ['value' => '01']],
  6299. '1',
  6300. '/option',
  6301. ['option' => ['value' => '02']],
  6302. '2',
  6303. '/option',
  6304. $hoursRegex,
  6305. ['option' => ['value' => '12', 'selected' => 'selected']],
  6306. '12',
  6307. '/option',
  6308. '/select',
  6309. ];
  6310. $this->assertHtml($expected, $result);
  6311. $this->Form->request->data['Model']['field'] = '';
  6312. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6313. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6314. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6315. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6316. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6317. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6318. $expected = [
  6319. ['select' => ['name' => 'Model[field][hour]']],
  6320. ['option' => ['value' => '']],
  6321. '/option',
  6322. ['option' => ['value' => '00', 'selected' => 'selected']],
  6323. '0',
  6324. '/option',
  6325. ['option' => ['value' => '01']],
  6326. '1',
  6327. '/option',
  6328. ['option' => ['value' => '02']],
  6329. '2',
  6330. '/option',
  6331. $hoursRegex,
  6332. '/select',
  6333. ];
  6334. $this->assertHtml($expected, $result);
  6335. unset($this->Form->request->data['Model']['field']);
  6336. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6337. $thisHour = date('H');
  6338. $optValue = date('G');
  6339. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6340. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6341. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6342. $expected = [
  6343. ['select' => ['name' => 'Model[field][hour]']],
  6344. ['option' => ['value' => '']],
  6345. '/option',
  6346. ['option' => ['value' => '00']],
  6347. '0',
  6348. '/option',
  6349. ['option' => ['value' => '01', 'selected' => 'selected']],
  6350. '1',
  6351. '/option',
  6352. ['option' => ['value' => '02']],
  6353. '2',
  6354. '/option',
  6355. $hoursRegex,
  6356. '/select',
  6357. ];
  6358. $this->assertHtml($expected, $result);
  6359. }
  6360. /**
  6361. * testYear method
  6362. *
  6363. * Test generation of a year input.
  6364. *
  6365. * @return void
  6366. */
  6367. public function testYear()
  6368. {
  6369. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6370. $expected = [
  6371. ['select' => ['name' => 'Model[field][year]']],
  6372. ['option' => ['selected' => 'selected', 'value' => '']],
  6373. '/option',
  6374. ['option' => ['value' => '2007']],
  6375. '2007',
  6376. '/option',
  6377. ['option' => ['value' => '2006']],
  6378. '2006',
  6379. '/option',
  6380. '/select',
  6381. ];
  6382. $this->assertHtml($expected, $result);
  6383. $result = $this->Form->year('Model.field', [
  6384. 'value' => '',
  6385. 'minYear' => 2006,
  6386. 'maxYear' => 2007,
  6387. 'orderYear' => 'asc'
  6388. ]);
  6389. $expected = [
  6390. ['select' => ['name' => 'Model[field][year]']],
  6391. ['option' => ['selected' => 'selected', 'value' => '']],
  6392. '/option',
  6393. ['option' => ['value' => '2006']],
  6394. '2006',
  6395. '/option',
  6396. ['option' => ['value' => '2007']],
  6397. '2007',
  6398. '/option',
  6399. '/select',
  6400. ];
  6401. $this->assertHtml($expected, $result);
  6402. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6403. $result = $this->Form->year('Contact.published', [
  6404. 'empty' => false,
  6405. 'minYear' => 2006,
  6406. 'maxYear' => 2007,
  6407. ]);
  6408. $expected = [
  6409. ['select' => ['name' => 'Contact[published][year]']],
  6410. ['option' => ['value' => '2007']],
  6411. '2007',
  6412. '/option',
  6413. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6414. '2006',
  6415. '/option',
  6416. '/select',
  6417. ];
  6418. $this->assertHtml($expected, $result);
  6419. $result = $this->Form->year('Contact.published', [
  6420. 'empty' => 'Published on',
  6421. ]);
  6422. $this->assertContains('Published on', $result);
  6423. }
  6424. /**
  6425. * testControlDatetimePreEpoch method
  6426. *
  6427. * Test minYear being prior to the unix epoch.
  6428. *
  6429. * @return void
  6430. */
  6431. public function testControlDatetimePreEpoch()
  6432. {
  6433. $start = date('Y') - 80;
  6434. $end = date('Y') - 18;
  6435. $result = $this->Form->control('birth_year', [
  6436. 'type' => 'date',
  6437. 'label' => 'Birth Year',
  6438. 'minYear' => $start,
  6439. 'maxYear' => $end,
  6440. 'month' => false,
  6441. 'day' => false,
  6442. ]);
  6443. $this->assertContains('value="' . $start . '">' . $start, $result);
  6444. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6445. $this->assertNotContains('value="00">00', $result);
  6446. }
  6447. /**
  6448. * testYearAutoExpandRange method
  6449. *
  6450. * @return void
  6451. */
  6452. public function testYearAutoExpandRange()
  6453. {
  6454. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6455. $result = $this->Form->year('User.birthday');
  6456. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6457. $result = $matches[1];
  6458. $expected = range(date('Y') + 5, 1930);
  6459. $this->assertEquals($expected, $result);
  6460. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6461. $result = $this->Form->year('Project.release');
  6462. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6463. $result = $matches[1];
  6464. $expected = range(2050, date('Y') - 5);
  6465. $this->assertEquals($expected, $result);
  6466. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6467. $result = $this->Form->year('Project.release', [
  6468. 'minYear' => 1890,
  6469. 'maxYear' => 1900
  6470. ]);
  6471. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6472. $result = $matches[1];
  6473. $expected = range(1900, 1881);
  6474. $this->assertEquals($expected, $result);
  6475. }
  6476. /**
  6477. * testControlDate method
  6478. *
  6479. * Test that control() accepts the type of date and passes options in.
  6480. *
  6481. * @return void
  6482. */
  6483. public function testControlDate()
  6484. {
  6485. $this->Form->request->data = [
  6486. 'month_year' => ['month' => date('m')],
  6487. ];
  6488. $this->Form->create($this->article);
  6489. $result = $this->Form->control('month_year', [
  6490. 'label' => false,
  6491. 'type' => 'date',
  6492. 'minYear' => 2006,
  6493. 'maxYear' => 2008
  6494. ]);
  6495. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6496. $this->assertNotContains('value="2008" selected="selected"', $result);
  6497. }
  6498. /**
  6499. * testControlLabelFalse method
  6500. *
  6501. * Test the label option being set to false.
  6502. *
  6503. * @return void
  6504. */
  6505. public function testControlLabelFalse()
  6506. {
  6507. $this->Form->create($this->article);
  6508. $result = $this->Form->control('title', ['label' => false]);
  6509. $expected = [
  6510. 'div' => ['class' => 'input text required'],
  6511. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6512. '/div'
  6513. ];
  6514. $this->assertHtml($expected, $result);
  6515. }
  6516. /**
  6517. * testControlDateMaxYear method
  6518. *
  6519. * Let's say we want to only allow users born from 2006 to 2008 to register
  6520. * This being the first signup page, we still don't have any data.
  6521. *
  6522. * @return void
  6523. */
  6524. public function testControlDateMaxYear()
  6525. {
  6526. $this->Form->request->data = [];
  6527. $this->Form->create($this->article);
  6528. $result = $this->Form->control('birthday', [
  6529. 'label' => false,
  6530. 'type' => 'date',
  6531. 'minYear' => 2006,
  6532. 'maxYear' => 2008,
  6533. 'default' => true
  6534. ]);
  6535. $this->assertContains('value="2008" selected="selected"', $result);
  6536. $this->assertContains('value="2006"', $result);
  6537. $this->assertNotContains('value="2005"', $result);
  6538. $this->assertNotContains('value="2009"', $result);
  6539. }
  6540. /**
  6541. * testTextArea method
  6542. *
  6543. * Test generation of a textarea input.
  6544. *
  6545. * @return void
  6546. */
  6547. public function testTextArea()
  6548. {
  6549. $this->Form->request->data = ['field' => 'some test data'];
  6550. $result = $this->Form->textarea('field');
  6551. $expected = [
  6552. 'textarea' => ['name' => 'field', 'rows' => 5],
  6553. 'some test data',
  6554. '/textarea',
  6555. ];
  6556. $this->assertHtml($expected, $result);
  6557. $result = $this->Form->textarea('user.bio');
  6558. $expected = [
  6559. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6560. '/textarea',
  6561. ];
  6562. $this->assertHtml($expected, $result);
  6563. $this->Form->request->data = ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'];
  6564. $result = $this->Form->textarea('field');
  6565. $expected = [
  6566. 'textarea' => ['name' => 'field', 'rows' => 5],
  6567. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6568. '/textarea',
  6569. ];
  6570. $this->assertHtml($expected, $result);
  6571. $this->Form->request->data = [
  6572. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  6573. ];
  6574. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6575. $expected = [
  6576. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6577. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6578. '/textarea',
  6579. ];
  6580. $this->assertHtml($expected, $result);
  6581. $result = $this->Form->textarea('0.OtherModel.field');
  6582. $expected = [
  6583. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6584. '/textarea'
  6585. ];
  6586. $this->assertHtml($expected, $result);
  6587. }
  6588. /**
  6589. * testTextAreaWithStupidCharacters method
  6590. *
  6591. * Test text area with non-ascii characters.
  6592. *
  6593. * @return void
  6594. */
  6595. public function testTextAreaWithStupidCharacters()
  6596. {
  6597. $result = $this->Form->textarea('Post.content', [
  6598. 'value' => 'GREAT®',
  6599. 'rows' => '15',
  6600. 'cols' => '75'
  6601. ]);
  6602. $expected = [
  6603. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6604. 'GREAT®',
  6605. '/textarea',
  6606. ];
  6607. $this->assertHtml($expected, $result);
  6608. }
  6609. /**
  6610. * testTextAreaMaxLength method
  6611. *
  6612. * Test textareas maxlength read from schema.
  6613. *
  6614. * @return void
  6615. */
  6616. public function testTextAreaMaxLength()
  6617. {
  6618. $this->Form->create([
  6619. 'schema' => [
  6620. 'stuff' => ['type' => 'string', 'length' => 10],
  6621. ]
  6622. ]);
  6623. $result = $this->Form->control('other', ['type' => 'textarea']);
  6624. $expected = [
  6625. 'div' => ['class' => 'input textarea'],
  6626. 'label' => ['for' => 'other'],
  6627. 'Other',
  6628. '/label',
  6629. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6630. '/textarea',
  6631. '/div'
  6632. ];
  6633. $this->assertHtml($expected, $result);
  6634. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6635. $expected = [
  6636. 'div' => ['class' => 'input textarea'],
  6637. 'label' => ['for' => 'stuff'],
  6638. 'Stuff',
  6639. '/label',
  6640. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6641. '/textarea',
  6642. '/div'
  6643. ];
  6644. $this->assertHtml($expected, $result);
  6645. }
  6646. /**
  6647. * testHiddenField method
  6648. *
  6649. * Test generation of a hidden input.
  6650. *
  6651. * @return void
  6652. */
  6653. public function testHiddenField()
  6654. {
  6655. $this->article['errors'] = [
  6656. 'field' => true
  6657. ];
  6658. $this->Form->request->data['field'] = 'test';
  6659. $this->Form->create($this->article);
  6660. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6661. $expected = [
  6662. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6663. $this->assertHtml($expected, $result);
  6664. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6665. $expected = [
  6666. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6667. ];
  6668. $this->assertHtml($expected, $result);
  6669. }
  6670. /**
  6671. * testFileUploadField method
  6672. *
  6673. * Test generation of a file upload input.
  6674. *
  6675. * @return void
  6676. */
  6677. public function testFileUploadField()
  6678. {
  6679. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6680. $result = $this->Form->file('Model.upload');
  6681. $this->assertHtml($expected, $result);
  6682. $this->Form->request->data['Model']['upload'] = [
  6683. 'name' => '', 'type' => '', 'tmp_name' => '',
  6684. 'error' => 4, 'size' => 0
  6685. ];
  6686. $result = $this->Form->file('Model.upload');
  6687. $this->assertHtml($expected, $result);
  6688. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6689. $result = $this->Form->file('Model.upload');
  6690. $this->assertHtml($expected, $result);
  6691. }
  6692. /**
  6693. * testFileUploadOnOtherModel method
  6694. *
  6695. * Test File upload input on a model not used in create().
  6696. *
  6697. * @return void
  6698. */
  6699. public function testFileUploadOnOtherModel()
  6700. {
  6701. $this->Form->create($this->article, ['type' => 'file']);
  6702. $result = $this->Form->file('ValidateProfile.city');
  6703. $expected = [
  6704. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6705. ];
  6706. $this->assertHtml($expected, $result);
  6707. }
  6708. /**
  6709. * testButton method
  6710. *
  6711. * Test generation of a form button.
  6712. *
  6713. * @return void
  6714. */
  6715. public function testButton()
  6716. {
  6717. $result = $this->Form->button('Hi');
  6718. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6719. $this->assertHtml($expected, $result);
  6720. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6721. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6722. $this->assertHtml($expected, $result);
  6723. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6724. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6725. $this->assertHtml($expected, $result);
  6726. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6727. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6728. $this->assertHtml($expected, $result);
  6729. $result = $this->Form->button('No type', ['type' => false]);
  6730. $expected = ['button' => [], 'No type', '/button'];
  6731. $this->assertHtml($expected, $result);
  6732. $result = $this->Form->button('Upload Text', [
  6733. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6734. 'escape' => false
  6735. ]);
  6736. $this->assertNotRegExp('/\&039/', $result);
  6737. }
  6738. /**
  6739. * testButtonUnlockedByDefault method
  6740. *
  6741. * Test that button() makes unlocked fields by default.
  6742. *
  6743. * @return void
  6744. */
  6745. public function testButtonUnlockedByDefault()
  6746. {
  6747. $this->Form->request->params['_csrfToken'] = 'secured';
  6748. $this->Form->button('Save', ['name' => 'save']);
  6749. $this->Form->button('Clear');
  6750. $result = $this->Form->unlockField();
  6751. $this->assertEquals(['save'], $result);
  6752. }
  6753. /**
  6754. * testPostButton method
  6755. *
  6756. * @return void
  6757. */
  6758. public function testPostButton()
  6759. {
  6760. $result = $this->Form->postButton('Hi', '/controller/action');
  6761. $expected = [
  6762. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6763. 'div' => ['style' => 'display:none;'],
  6764. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6765. '/div',
  6766. 'button' => ['type' => 'submit'],
  6767. 'Hi',
  6768. '/button',
  6769. '/form'
  6770. ];
  6771. $this->assertHtml($expected, $result);
  6772. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6773. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  6774. }
  6775. /**
  6776. * testPostButtonMethodType method
  6777. *
  6778. * @return void
  6779. */
  6780. public function testPostButtonMethodType()
  6781. {
  6782. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6783. $expected = [
  6784. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6785. 'div' => ['style' => 'display:none;'],
  6786. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6787. '/div',
  6788. 'button' => ['type' => 'submit'],
  6789. 'Hi',
  6790. '/button',
  6791. '/form'
  6792. ];
  6793. $this->assertHtml($expected, $result);
  6794. }
  6795. /**
  6796. * testPostButtonFormOptions method
  6797. *
  6798. * @return void
  6799. */
  6800. public function testPostButtonFormOptions()
  6801. {
  6802. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6803. $expected = [
  6804. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6805. 'div' => ['style' => 'display:none;'],
  6806. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6807. '/div',
  6808. 'button' => ['type' => 'submit'],
  6809. 'Hi',
  6810. '/button',
  6811. '/form'
  6812. ];
  6813. $this->assertHtml($expected, $result);
  6814. }
  6815. /**
  6816. * testPostButtonNestedData method
  6817. *
  6818. * Test using postButton with N dimensional data.
  6819. *
  6820. * @return void
  6821. */
  6822. public function testPostButtonNestedData()
  6823. {
  6824. $data = [
  6825. 'one' => [
  6826. 'two' => [
  6827. 3, 4, 5
  6828. ]
  6829. ]
  6830. ];
  6831. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6832. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6833. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6834. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6835. }
  6836. /**
  6837. * testSecurePostButton method
  6838. *
  6839. * Test that postButton adds _Token fields.
  6840. *
  6841. * @return void
  6842. */
  6843. public function testSecurePostButton()
  6844. {
  6845. $this->Form->request->params['_csrfToken'] = 'testkey';
  6846. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  6847. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6848. $tokenDebug = urlencode(json_encode([
  6849. '/posts/delete/1',
  6850. [],
  6851. []
  6852. ]));
  6853. $expected = [
  6854. 'form' => [
  6855. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6856. ],
  6857. ['div' => ['style' => 'display:none;']],
  6858. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6859. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6860. '/div',
  6861. 'button' => ['type' => 'submit'],
  6862. 'Delete',
  6863. '/button',
  6864. ['div' => ['style' => 'display:none;']],
  6865. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6866. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6867. ['input' => [
  6868. 'type' => 'hidden',
  6869. 'name' => '_Token[debug]',
  6870. 'value' => $tokenDebug,
  6871. 'autocomplete' => 'off',
  6872. ]],
  6873. '/div',
  6874. '/form',
  6875. ];
  6876. $this->assertHtml($expected, $result);
  6877. }
  6878. /**
  6879. * testPostLink method
  6880. *
  6881. * @return void
  6882. */
  6883. public function testPostLink()
  6884. {
  6885. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6886. $expected = [
  6887. 'form' => [
  6888. 'method' => 'post', 'action' => '/posts/delete/1',
  6889. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6890. ],
  6891. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6892. '/form',
  6893. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6894. 'Delete',
  6895. '/a'
  6896. ];
  6897. $this->assertHtml($expected, $result);
  6898. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6899. $expected = [
  6900. 'form' => [
  6901. 'method' => 'post', 'action' => '/posts/delete/1',
  6902. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6903. ],
  6904. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6905. '/form',
  6906. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6907. 'Delete',
  6908. '/a'
  6909. ];
  6910. $this->assertHtml($expected, $result);
  6911. $result = $this->Form->postLink(
  6912. 'Delete',
  6913. '/posts/delete/1',
  6914. ['target' => '_blank', 'class' => 'btn btn-danger']
  6915. );
  6916. $expected = [
  6917. 'form' => [
  6918. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6919. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6920. ],
  6921. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6922. '/form',
  6923. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6924. 'Delete',
  6925. '/a'
  6926. ];
  6927. $this->assertHtml($expected, $result);
  6928. }
  6929. /**
  6930. * testPostLinkWithConfirm method
  6931. *
  6932. * Test the confirm option for postLink().
  6933. *
  6934. * @return void
  6935. */
  6936. public function testPostLinkWithConfirm()
  6937. {
  6938. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6939. $expected = [
  6940. 'form' => [
  6941. 'method' => 'post', 'action' => '/posts/delete/1',
  6942. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6943. ],
  6944. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6945. '/form',
  6946. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  6947. 'Delete',
  6948. '/a'
  6949. ];
  6950. $this->assertHtml($expected, $result);
  6951. $result = $this->Form->postLink(
  6952. 'Delete',
  6953. '/posts/delete/1',
  6954. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  6955. );
  6956. $expected = [
  6957. 'form' => [
  6958. 'method' => 'post', 'action' => '/posts/delete/1',
  6959. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6960. ],
  6961. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6962. '/form',
  6963. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  6964. 'Delete',
  6965. '/a'
  6966. ];
  6967. $this->assertHtml($expected, $result);
  6968. }
  6969. /**
  6970. * testPostLinkWithQuery method
  6971. *
  6972. * Test postLink() with query string args.
  6973. *
  6974. * @return void
  6975. */
  6976. public function testPostLinkWithQuery()
  6977. {
  6978. $result = $this->Form->postLink(
  6979. 'Delete',
  6980. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6981. );
  6982. $expected = [
  6983. 'form' => [
  6984. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  6985. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6986. ],
  6987. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6988. '/form',
  6989. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6990. 'Delete',
  6991. '/a'
  6992. ];
  6993. $this->assertHtml($expected, $result);
  6994. }
  6995. /**
  6996. * testPostLinkWithData method
  6997. *
  6998. * Test postLink with additional data.
  6999. *
  7000. * @return void
  7001. */
  7002. public function testPostLinkWithData()
  7003. {
  7004. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  7005. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  7006. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  7007. $this->Form->create($entity);
  7008. $this->Form->end();
  7009. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  7010. $this->assertContains(
  7011. '<input type="hidden" name="name" value="show"',
  7012. $result,
  7013. 'should not contain entity data.'
  7014. );
  7015. }
  7016. /**
  7017. * testPostLinkSecurityHash method
  7018. *
  7019. * Test that security hashes for postLink include the url.
  7020. *
  7021. * @return void
  7022. */
  7023. public function testPostLinkSecurityHash()
  7024. {
  7025. $hash = Security::hash(
  7026. '/posts/delete/1' .
  7027. serialize(['id' => '1']) .
  7028. '' .
  7029. Security::salt()
  7030. );
  7031. $hash .= '%3Aid';
  7032. $this->Form->request->params['_Token']['key'] = 'test';
  7033. $result = $this->Form->postLink(
  7034. 'Delete',
  7035. '/posts/delete/1',
  7036. ['data' => ['id' => 1]]
  7037. );
  7038. $tokenDebug = urlencode(json_encode([
  7039. '/posts/delete/1',
  7040. [
  7041. 'id' => 1
  7042. ],
  7043. []
  7044. ]));
  7045. $expected = [
  7046. 'form' => [
  7047. 'method' => 'post', 'action' => '/posts/delete/1',
  7048. 'name', 'style' => 'display:none;'
  7049. ],
  7050. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7051. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7052. 'div' => ['style' => 'display:none;'],
  7053. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7054. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7055. ['input' => [
  7056. 'type' => 'hidden',
  7057. 'name' => '_Token[debug]',
  7058. 'value' => $tokenDebug,
  7059. 'autocomplete' => 'off',
  7060. ]],
  7061. '/div',
  7062. '/form',
  7063. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7064. 'Delete',
  7065. '/a'
  7066. ];
  7067. $this->assertHtml($expected, $result);
  7068. }
  7069. /**
  7070. * testPostLinkSecurityHashBlockMode method
  7071. *
  7072. * Test that postLink doesn't modify the fields in the containing form.
  7073. *
  7074. * postLink() calls inside open forms should not modify the field list
  7075. * for the form.
  7076. *
  7077. * @return void
  7078. */
  7079. public function testPostLinkSecurityHashBlockMode()
  7080. {
  7081. $hash = Security::hash(
  7082. '/posts/delete/1' .
  7083. serialize([]) .
  7084. '' .
  7085. Security::salt()
  7086. );
  7087. $hash .= '%3A';
  7088. $this->Form->request->params['_Token']['key'] = 'test';
  7089. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  7090. $this->Form->control('title');
  7091. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7092. $result = $this->View->fetch('postLink');
  7093. $this->assertEquals(['title'], $this->Form->fields);
  7094. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  7095. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  7096. }
  7097. /**
  7098. * testPostLinkSecurityHashNoDebugMode method
  7099. *
  7100. * Test that security does not include debug token if debug is false.
  7101. *
  7102. * @return void
  7103. */
  7104. public function testPostLinkSecurityHashNoDebugMode()
  7105. {
  7106. Configure::write('debug', false);
  7107. $hash = Security::hash(
  7108. '/posts/delete/1' .
  7109. serialize(['id' => '1']) .
  7110. '' .
  7111. Security::salt()
  7112. );
  7113. $hash .= '%3Aid';
  7114. $this->Form->request->params['_Token']['key'] = 'test';
  7115. $result = $this->Form->postLink(
  7116. 'Delete',
  7117. '/posts/delete/1',
  7118. ['data' => ['id' => 1]]
  7119. );
  7120. $expected = [
  7121. 'form' => [
  7122. 'method' => 'post', 'action' => '/posts/delete/1',
  7123. 'name', 'style' => 'display:none;'
  7124. ],
  7125. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7126. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7127. 'div' => ['style' => 'display:none;'],
  7128. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7129. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7130. '/div',
  7131. '/form',
  7132. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7133. 'Delete',
  7134. '/a'
  7135. ];
  7136. $this->assertHtml($expected, $result);
  7137. }
  7138. /**
  7139. * testPostLinkNestedData method
  7140. *
  7141. * Test using postLink with N dimensional data.
  7142. *
  7143. * @return void
  7144. */
  7145. public function testPostLinkNestedData()
  7146. {
  7147. $data = [
  7148. 'one' => [
  7149. 'two' => [
  7150. 3, 4, 5
  7151. ]
  7152. ]
  7153. ];
  7154. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7155. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7156. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7157. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7158. }
  7159. /**
  7160. * testPostLinkAfterGetForm method
  7161. *
  7162. * Test creating postLinks after a GET form.
  7163. *
  7164. * @return void
  7165. */
  7166. public function testPostLinkAfterGetForm()
  7167. {
  7168. $this->Form->request->params['_csrfToken'] = 'testkey';
  7169. $this->Form->request->params['_Token'] = 'val';
  7170. $this->Form->create($this->article, ['type' => 'get']);
  7171. $this->Form->end();
  7172. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7173. $tokenDebug = urlencode(json_encode([
  7174. '/posts/delete/1',
  7175. [],
  7176. []
  7177. ]));
  7178. $expected = [
  7179. 'form' => [
  7180. 'method' => 'post', 'action' => '/posts/delete/1',
  7181. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7182. ],
  7183. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7184. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7185. 'div' => ['style' => 'display:none;'],
  7186. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7187. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7188. ['input' => [
  7189. 'type' => 'hidden', 'name' => '_Token[debug]',
  7190. 'value' => $tokenDebug,
  7191. 'autocomplete' => 'off'
  7192. ]],
  7193. '/div',
  7194. '/form',
  7195. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7196. 'Delete',
  7197. '/a'
  7198. ];
  7199. $this->assertHtml($expected, $result);
  7200. }
  7201. /**
  7202. * testPostLinkFormBuffer method
  7203. *
  7204. * Test that postLink adds form tags to view block.
  7205. *
  7206. * @return void
  7207. */
  7208. public function testPostLinkFormBuffer()
  7209. {
  7210. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7211. $expected = [
  7212. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7213. 'Delete',
  7214. '/a'
  7215. ];
  7216. $this->assertHtml($expected, $result);
  7217. $result = $this->View->fetch('postLink');
  7218. $expected = [
  7219. 'form' => [
  7220. 'method' => 'post', 'action' => '/posts/delete/1',
  7221. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7222. ],
  7223. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7224. '/form'
  7225. ];
  7226. $this->assertHtml($expected, $result);
  7227. $result = $this->Form->postLink(
  7228. 'Delete',
  7229. '/posts/delete/2',
  7230. ['block' => true, 'method' => 'DELETE']
  7231. );
  7232. $expected = [
  7233. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7234. 'Delete',
  7235. '/a'
  7236. ];
  7237. $this->assertHtml($expected, $result);
  7238. $result = $this->View->fetch('postLink');
  7239. $expected = [
  7240. 'form' => [
  7241. 'method' => 'post', 'action' => '/posts/delete/1',
  7242. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7243. ],
  7244. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7245. '/form',
  7246. [
  7247. 'form' => [
  7248. 'method' => 'post', 'action' => '/posts/delete/2',
  7249. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7250. ],
  7251. ],
  7252. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7253. '/form'
  7254. ];
  7255. $this->assertHtml($expected, $result);
  7256. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7257. $expected = [
  7258. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7259. 'Delete',
  7260. '/a'
  7261. ];
  7262. $this->assertHtml($expected, $result);
  7263. $result = $this->View->fetch('foobar');
  7264. $expected = [
  7265. 'form' => [
  7266. 'method' => 'post', 'action' => '/posts/delete/1',
  7267. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7268. ],
  7269. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7270. '/form'
  7271. ];
  7272. $this->assertHtml($expected, $result);
  7273. }
  7274. /**
  7275. * testSubmitButton method
  7276. *
  7277. * @return void
  7278. */
  7279. public function testSubmitButton()
  7280. {
  7281. $result = $this->Form->submit('');
  7282. $expected = [
  7283. 'div' => ['class' => 'submit'],
  7284. 'input' => ['type' => 'submit', 'value' => ''],
  7285. '/div'
  7286. ];
  7287. $this->assertHtml($expected, $result);
  7288. $result = $this->Form->submit('Test Submit');
  7289. $expected = [
  7290. 'div' => ['class' => 'submit'],
  7291. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7292. '/div'
  7293. ];
  7294. $this->assertHtml($expected, $result);
  7295. $result = $this->Form->submit('Next >');
  7296. $expected = [
  7297. 'div' => ['class' => 'submit'],
  7298. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7299. '/div'
  7300. ];
  7301. $this->assertHtml($expected, $result);
  7302. $result = $this->Form->submit('Next >', ['escape' => false]);
  7303. $expected = [
  7304. 'div' => ['class' => 'submit'],
  7305. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7306. '/div'
  7307. ];
  7308. $this->assertHtml($expected, $result);
  7309. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7310. $expected = [
  7311. 'div' => ['class' => 'submit'],
  7312. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7313. '/div'
  7314. ];
  7315. $this->assertHtml($expected, $result);
  7316. }
  7317. /**
  7318. * testSubmitImage method
  7319. *
  7320. * Test image submit types.
  7321. *
  7322. * @return void
  7323. */
  7324. public function testSubmitImage()
  7325. {
  7326. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7327. $expected = [
  7328. 'div' => ['class' => 'submit'],
  7329. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7330. '/div'
  7331. ];
  7332. $this->assertHtml($expected, $result);
  7333. $result = $this->Form->submit('/relative/cake.power.gif');
  7334. $expected = [
  7335. 'div' => ['class' => 'submit'],
  7336. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7337. '/div'
  7338. ];
  7339. $this->assertHtml($expected, $result);
  7340. $result = $this->Form->submit('cake.power.gif');
  7341. $expected = [
  7342. 'div' => ['class' => 'submit'],
  7343. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7344. '/div'
  7345. ];
  7346. $this->assertHtml($expected, $result);
  7347. $result = $this->Form->submit('Not.an.image');
  7348. $expected = [
  7349. 'div' => ['class' => 'submit'],
  7350. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7351. '/div'
  7352. ];
  7353. $this->assertHtml($expected, $result);
  7354. }
  7355. /**
  7356. * testSubmitUnlockedByDefault method
  7357. *
  7358. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7359. * be submitted at a time.
  7360. *
  7361. * @return void
  7362. */
  7363. public function testSubmitUnlockedByDefault()
  7364. {
  7365. $this->Form->request->params['_Token'] = 'secured';
  7366. $this->Form->submit('Go go');
  7367. $this->Form->submit('Save', ['name' => 'save']);
  7368. $result = $this->Form->unlockField();
  7369. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7370. }
  7371. /**
  7372. * testSubmitImageTimestamp method
  7373. *
  7374. * Test submit image with timestamps.
  7375. *
  7376. * @return void
  7377. */
  7378. public function testSubmitImageTimestamp()
  7379. {
  7380. Configure::write('Asset.timestamp', 'force');
  7381. $result = $this->Form->submit('cake.power.gif');
  7382. $expected = [
  7383. 'div' => ['class' => 'submit'],
  7384. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7385. '/div'
  7386. ];
  7387. $this->assertHtml($expected, $result);
  7388. }
  7389. /**
  7390. * testDateTimeWithGetForms method
  7391. *
  7392. * Test that datetime() works with GET style forms.
  7393. *
  7394. * @return void
  7395. */
  7396. public function testDateTimeWithGetForms()
  7397. {
  7398. extract($this->dateRegex);
  7399. $this->Form->create($this->article, ['type' => 'get']);
  7400. $result = $this->Form->datetime('created');
  7401. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7402. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7403. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7404. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7405. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7406. }
  7407. /**
  7408. * testForMagicControlNonExistingNorValidated method
  7409. *
  7410. * @return void
  7411. */
  7412. public function testForMagicControlNonExistingNorValidated()
  7413. {
  7414. $this->Form->create($this->article);
  7415. $this->Form->templates(['inputContainer' => '{{content}}']);
  7416. $result = $this->Form->control('non_existing_nor_validated');
  7417. $expected = [
  7418. 'label' => ['for' => 'non-existing-nor-validated'],
  7419. 'Non Existing Nor Validated',
  7420. '/label',
  7421. 'input' => [
  7422. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7423. 'id' => 'non-existing-nor-validated'
  7424. ]
  7425. ];
  7426. $this->assertHtml($expected, $result);
  7427. $result = $this->Form->control('non_existing_nor_validated', [
  7428. 'val' => 'my value'
  7429. ]);
  7430. $expected = [
  7431. 'label' => ['for' => 'non-existing-nor-validated'],
  7432. 'Non Existing Nor Validated',
  7433. '/label',
  7434. 'input' => [
  7435. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7436. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7437. ]
  7438. ];
  7439. $this->assertHtml($expected, $result);
  7440. $this->Form->request->data = ['non_existing_nor_validated' => 'CakePHP magic'];
  7441. $result = $this->Form->control('non_existing_nor_validated');
  7442. $expected = [
  7443. 'label' => ['for' => 'non-existing-nor-validated'],
  7444. 'Non Existing Nor Validated',
  7445. '/label',
  7446. 'input' => [
  7447. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7448. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7449. ]
  7450. ];
  7451. $this->assertHtml($expected, $result);
  7452. }
  7453. /**
  7454. * testFormMagicControlLabel method
  7455. *
  7456. * @return void
  7457. */
  7458. public function testFormMagicControlLabel()
  7459. {
  7460. TableRegistry::get('Contacts', [
  7461. 'className' => __NAMESPACE__ . '\ContactsTable'
  7462. ]);
  7463. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7464. $this->Form->templates(['inputContainer' => '{{content}}']);
  7465. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7466. $expected = [
  7467. 'label' => ['for' => 'contacts-name'],
  7468. 'My label',
  7469. '/label',
  7470. 'input' => [
  7471. 'type' => 'text', 'name' => 'Contacts[name]',
  7472. 'id' => 'contacts-name', 'maxlength' => '255'
  7473. ]
  7474. ];
  7475. $this->assertHtml($expected, $result);
  7476. $result = $this->Form->control('name', [
  7477. 'label' => ['class' => 'mandatory']
  7478. ]);
  7479. $expected = [
  7480. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7481. 'Name',
  7482. '/label',
  7483. 'input' => [
  7484. 'type' => 'text', 'name' => 'name',
  7485. 'id' => 'name', 'maxlength' => '255'
  7486. ]
  7487. ];
  7488. $this->assertHtml($expected, $result);
  7489. $result = $this->Form->control('name', [
  7490. 'div' => false,
  7491. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7492. ]);
  7493. $expected = [
  7494. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7495. 'My label',
  7496. '/label',
  7497. 'input' => [
  7498. 'type' => 'text', 'name' => 'name',
  7499. 'id' => 'name', 'maxlength' => '255'
  7500. ]
  7501. ];
  7502. $this->assertHtml($expected, $result);
  7503. $result = $this->Form->control('Contact.name', [
  7504. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7505. ]);
  7506. $expected = [
  7507. 'label' => ['for' => 'my_id'],
  7508. 'Name',
  7509. '/label',
  7510. 'input' => [
  7511. 'type' => 'text', 'name' => 'Contact[name]',
  7512. 'id' => 'my_id', 'maxlength' => '255'
  7513. ]
  7514. ];
  7515. $this->assertHtml($expected, $result);
  7516. $result = $this->Form->control('1.id');
  7517. $expected = ['input' => [
  7518. 'type' => 'hidden', 'name' => '1[id]',
  7519. 'id' => '1-id'
  7520. ]];
  7521. $this->assertHtml($expected, $result);
  7522. $result = $this->Form->control('1.name');
  7523. $expected = [
  7524. 'label' => ['for' => '1-name'],
  7525. 'Name',
  7526. '/label',
  7527. 'input' => [
  7528. 'type' => 'text', 'name' => '1[name]',
  7529. 'id' => '1-name', 'maxlength' => '255'
  7530. ]
  7531. ];
  7532. $this->assertHtml($expected, $result);
  7533. }
  7534. /**
  7535. * testFormEnd method
  7536. *
  7537. * @return void
  7538. */
  7539. public function testFormEnd()
  7540. {
  7541. $this->assertEquals('</form>', $this->Form->end());
  7542. }
  7543. /**
  7544. * testMultiRecordForm method
  7545. *
  7546. * Test the generation of fields for a multi record form.
  7547. *
  7548. * @return void
  7549. */
  7550. public function testMultiRecordForm()
  7551. {
  7552. $this->loadFixtures('Articles', 'Comments');
  7553. $articles = TableRegistry::get('Articles');
  7554. $articles->hasMany('Comments');
  7555. $comment = new Entity(['comment' => 'Value']);
  7556. $article = new Article(['comments' => [$comment]]);
  7557. $this->Form->create([$article]);
  7558. $result = $this->Form->control('0.comments.1.comment');
  7559. //@codingStandardsIgnoreStart
  7560. $expected = [
  7561. 'div' => ['class' => 'input textarea'],
  7562. 'label' => ['for' => '0-comments-1-comment'],
  7563. 'Comment',
  7564. '/label',
  7565. 'textarea' => [
  7566. 'name',
  7567. 'id' => '0-comments-1-comment',
  7568. 'rows' => 5
  7569. ],
  7570. '/textarea',
  7571. '/div'
  7572. ];
  7573. //@codingStandardsIgnoreEnd
  7574. $this->assertHtml($expected, $result);
  7575. $result = $this->Form->control('0.comments.0.comment');
  7576. //@codingStandardsIgnoreStart
  7577. $expected = [
  7578. 'div' => ['class' => 'input textarea'],
  7579. 'label' => ['for' => '0-comments-0-comment'],
  7580. 'Comment',
  7581. '/label',
  7582. 'textarea' => [
  7583. 'name',
  7584. 'id' => '0-comments-0-comment',
  7585. 'rows' => 5
  7586. ],
  7587. 'Value',
  7588. '/textarea',
  7589. '/div'
  7590. ];
  7591. //@codingStandardsIgnoreEnd
  7592. $this->assertHtml($expected, $result);
  7593. $comment->errors('comment', ['Not valid']);
  7594. $result = $this->Form->control('0.comments.0.comment');
  7595. //@codingStandardsIgnoreStart
  7596. $expected = [
  7597. 'div' => ['class' => 'input textarea error'],
  7598. 'label' => ['for' => '0-comments-0-comment'],
  7599. 'Comment',
  7600. '/label',
  7601. 'textarea' => [
  7602. 'name',
  7603. 'class' => 'form-error',
  7604. 'id' => '0-comments-0-comment',
  7605. 'rows' => 5
  7606. ],
  7607. 'Value',
  7608. '/textarea',
  7609. ['div' => ['class' => 'error-message']],
  7610. 'Not valid',
  7611. '/div',
  7612. '/div'
  7613. ];
  7614. //@codingStandardsIgnoreEnd
  7615. $this->assertHtml($expected, $result);
  7616. TableRegistry::get('Comments')
  7617. ->validator('default')
  7618. ->allowEmpty('comment', false);
  7619. $result = $this->Form->control('0.comments.1.comment');
  7620. //@codingStandardsIgnoreStart
  7621. $expected = [
  7622. 'div' => ['class' => 'input textarea required'],
  7623. 'label' => ['for' => '0-comments-1-comment'],
  7624. 'Comment',
  7625. '/label',
  7626. 'textarea' => [
  7627. 'name',
  7628. 'required' => 'required',
  7629. 'id' => '0-comments-1-comment',
  7630. 'rows' => 5
  7631. ],
  7632. '/textarea',
  7633. '/div'
  7634. ];
  7635. //@codingStandardsIgnoreEnd
  7636. $this->assertHtml($expected, $result);
  7637. }
  7638. /**
  7639. * testHtml5Controls method
  7640. *
  7641. * Test that some html5 inputs + FormHelper::__call() work.
  7642. *
  7643. * @return void
  7644. */
  7645. public function testHtml5Controls()
  7646. {
  7647. $result = $this->Form->email('User.email');
  7648. $expected = [
  7649. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7650. ];
  7651. $this->assertHtml($expected, $result);
  7652. $result = $this->Form->search('User.query');
  7653. $expected = [
  7654. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7655. ];
  7656. $this->assertHtml($expected, $result);
  7657. $result = $this->Form->search('User.query', ['value' => 'test']);
  7658. $expected = [
  7659. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7660. ];
  7661. $this->assertHtml($expected, $result);
  7662. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7663. $expected = [
  7664. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7665. ];
  7666. $this->assertHtml($expected, $result);
  7667. }
  7668. /**
  7669. * testHtml5ControlWithControl method
  7670. *
  7671. * Test accessing html5 inputs through control().
  7672. *
  7673. * @return void
  7674. */
  7675. public function testHtml5ControlWithControl()
  7676. {
  7677. $this->Form->create();
  7678. $this->Form->templates(['inputContainer' => '{{content}}']);
  7679. $result = $this->Form->control('website', [
  7680. 'type' => 'url',
  7681. 'val' => 'http://domain.tld',
  7682. 'label' => false
  7683. ]);
  7684. $expected = [
  7685. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7686. ];
  7687. $this->assertHtml($expected, $result);
  7688. }
  7689. /**
  7690. * testHtml5ControlException method
  7691. *
  7692. * Test errors when field name is missing.
  7693. *
  7694. * @expectedException \Cake\Core\Exception\Exception
  7695. * @return void
  7696. */
  7697. public function testHtml5ControlException()
  7698. {
  7699. $this->Form->email();
  7700. }
  7701. /**
  7702. * testRequiredAttribute method
  7703. *
  7704. * Tests that formhelper sets required attributes.
  7705. *
  7706. * @return void
  7707. */
  7708. public function testRequiredAttribute()
  7709. {
  7710. $this->article['required'] = [
  7711. 'title' => true,
  7712. 'body' => false,
  7713. ];
  7714. $this->Form->create($this->article);
  7715. $result = $this->Form->control('title');
  7716. $expected = [
  7717. 'div' => ['class' => 'input text required'],
  7718. 'label' => ['for' => 'title'],
  7719. 'Title',
  7720. '/label',
  7721. 'input' => [
  7722. 'type' => 'text',
  7723. 'name' => 'title',
  7724. 'id' => 'title',
  7725. 'required' => 'required',
  7726. ],
  7727. '/div'
  7728. ];
  7729. $this->assertHtml($expected, $result);
  7730. $result = $this->Form->control('title', ['required' => false]);
  7731. $this->assertNotContains('required', $result);
  7732. $result = $this->Form->control('body');
  7733. $expected = [
  7734. 'div' => ['class' => 'input text'],
  7735. 'label' => ['for' => 'body'],
  7736. 'Body',
  7737. '/label',
  7738. 'input' => [
  7739. 'type' => 'text',
  7740. 'name' => 'body',
  7741. 'id' => 'body',
  7742. ],
  7743. '/div'
  7744. ];
  7745. $this->assertHtml($expected, $result);
  7746. $result = $this->Form->control('body', ['required' => true]);
  7747. $this->assertContains('required', $result);
  7748. }
  7749. /**
  7750. * testControlsNotNested method
  7751. *
  7752. * Tests that it is possible to put inputs outside of the label.
  7753. *
  7754. * @return void
  7755. */
  7756. public function testControlsNotNested()
  7757. {
  7758. $this->Form->templates([
  7759. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7760. 'formGroup' => '{{input}}{{label}}',
  7761. ]);
  7762. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7763. $expected = [
  7764. 'div' => ['class' => 'input checkbox'],
  7765. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7766. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7767. 'label' => ['for' => 'foo'],
  7768. 'Foo',
  7769. '/label',
  7770. '/div'
  7771. ];
  7772. $this->assertHtml($expected, $result);
  7773. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7774. $expected = [
  7775. 'div' => ['class' => 'input checkbox'],
  7776. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7777. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7778. '/div'
  7779. ];
  7780. $this->assertHtml($expected, $result);
  7781. $result = $this->Form->control('confirm', [
  7782. 'type' => 'radio',
  7783. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7784. ]);
  7785. $expected = [
  7786. 'div' => ['class' => 'input radio'],
  7787. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7788. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7789. ['label' => ['for' => 'confirm-y']],
  7790. 'Yes',
  7791. '/label',
  7792. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7793. ['label' => ['for' => 'confirm-n']],
  7794. 'No',
  7795. '/label',
  7796. '<label',
  7797. 'Confirm',
  7798. '/label',
  7799. '/div',
  7800. ];
  7801. $this->assertHtml($expected, $result);
  7802. $result = $this->Form->select('category', ['1', '2'], [
  7803. 'multiple' => 'checkbox',
  7804. 'name' => 'fish',
  7805. ]);
  7806. $expected = [
  7807. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7808. ['div' => ['class' => 'checkbox']],
  7809. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7810. ['label' => ['for' => 'fish-0']],
  7811. '1',
  7812. '/label',
  7813. '/div',
  7814. ['div' => ['class' => 'checkbox']],
  7815. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7816. ['label' => ['for' => 'fish-1']],
  7817. '2',
  7818. '/label',
  7819. '/div'
  7820. ];
  7821. $this->assertHtml($expected, $result);
  7822. }
  7823. /**
  7824. * testControlContainerTemplates method
  7825. *
  7826. * Test that *Container templates are used by input.
  7827. *
  7828. * @return void
  7829. */
  7830. public function testControlContainerTemplates()
  7831. {
  7832. $this->Form->templates([
  7833. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7834. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7835. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7836. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7837. ]);
  7838. $this->article['errors'] = [
  7839. 'Article' => ['published' => 'error message']
  7840. ];
  7841. $this->Form->create($this->article);
  7842. $result = $this->Form->control('accept', [
  7843. 'type' => 'checkbox'
  7844. ]);
  7845. $expected = [
  7846. 'div' => ['class' => 'check'],
  7847. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7848. 'label' => ['for' => 'accept'],
  7849. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7850. 'Accept',
  7851. '/label',
  7852. '/div'
  7853. ];
  7854. $this->assertHtml($expected, $result);
  7855. $result = $this->Form->control('accept', [
  7856. 'type' => 'radio',
  7857. 'options' => ['Y', 'N']
  7858. ]);
  7859. $this->assertContains('<div class="rad">', $result);
  7860. $result = $this->Form->control('Article.published', [
  7861. 'type' => 'radio',
  7862. 'options' => ['Y', 'N']
  7863. ]);
  7864. $this->assertContains('<div class="rad err">', $result);
  7865. $result = $this->Form->control('Article.created', [
  7866. 'type' => 'datetime',
  7867. ]);
  7868. $this->assertContains('<div class="dt">', $result);
  7869. }
  7870. /**
  7871. * testFormGroupTemplates method
  7872. *
  7873. * Test that *Container templates are used by input.
  7874. *
  7875. * @return void
  7876. */
  7877. public function testFormGroupTemplates()
  7878. {
  7879. $this->Form->templates([
  7880. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7881. ]);
  7882. $this->Form->create($this->article);
  7883. $result = $this->Form->control('accept', [
  7884. 'type' => 'radio',
  7885. 'options' => ['Y', 'N']
  7886. ]);
  7887. $this->assertContains('<div class="radio">', $result);
  7888. }
  7889. /**
  7890. * testResetTemplates method
  7891. *
  7892. * Test resetting templates.
  7893. *
  7894. * @return void
  7895. */
  7896. public function testResetTemplates()
  7897. {
  7898. $this->Form->templates(['input' => '<input/>']);
  7899. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  7900. $this->Form->resetTemplates();
  7901. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7902. }
  7903. /**
  7904. * testContext method
  7905. *
  7906. * Test the context method.
  7907. *
  7908. * @return void
  7909. */
  7910. public function testContext()
  7911. {
  7912. $result = $this->Form->context();
  7913. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7914. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7915. $this->assertSame($mock, $this->Form->context($mock));
  7916. $this->assertSame($mock, $this->Form->context());
  7917. }
  7918. /**
  7919. * testAutoDomId method
  7920. *
  7921. * @return void
  7922. */
  7923. public function testAutoDomId()
  7924. {
  7925. $result = $this->Form->text('field', ['id' => true]);
  7926. $expected = [
  7927. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7928. ];
  7929. $this->assertHtml($expected, $result);
  7930. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7931. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7932. $expected = [
  7933. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7934. ['label' => ['for' => 'field-0']],
  7935. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7936. 'option A',
  7937. '/label',
  7938. ['label' => ['for' => 'field-1']],
  7939. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7940. 'option B',
  7941. '/label',
  7942. ];
  7943. $this->assertHtml($expected, $result);
  7944. $result = $this->Form->select(
  7945. 'multi_field',
  7946. ['first', 'second'],
  7947. ['multiple' => 'checkbox', 'id' => true]
  7948. );
  7949. $expected = [
  7950. 'input' => [
  7951. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  7952. ],
  7953. ['div' => ['class' => 'checkbox']],
  7954. ['label' => ['for' => 'multi-field-0']],
  7955. ['input' => [
  7956. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7957. 'value' => '0', 'id' => 'multi-field-0'
  7958. ]],
  7959. 'first',
  7960. '/label',
  7961. '/div',
  7962. ['div' => ['class' => 'checkbox']],
  7963. ['label' => ['for' => 'multi-field-1']],
  7964. ['input' => [
  7965. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7966. 'value' => '1', 'id' => 'multi-field-1'
  7967. ]],
  7968. 'second',
  7969. '/label',
  7970. '/div',
  7971. ];
  7972. $this->assertHtml($expected, $result);
  7973. }
  7974. /**
  7975. * Test the basic setters and getters for value sources
  7976. *
  7977. * @return void
  7978. */
  7979. public function testFormValueSourcesSettersGetters()
  7980. {
  7981. $expected = ['context'];
  7982. $result = $this->Form->getValueSources();
  7983. $this->assertEquals($expected, $result);
  7984. $expected = null;
  7985. $result = $this->Form->getSourceValue('id');
  7986. $this->assertEquals($expected, $result);
  7987. $expected = ['query', 'data', 'context'];
  7988. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7989. $result = $this->Form->getValueSources();
  7990. $this->assertEquals($expected, $result);
  7991. $this->Form->request->data['id'] = '1';
  7992. $this->Form->request->query['id'] = '2';
  7993. $this->Form->setValueSources(['context']);
  7994. $expected = '1';
  7995. $result = $this->Form->getSourceValue('id');
  7996. $this->assertEquals($expected, $result);
  7997. $this->Form->setValueSources('query');
  7998. $expected = ['query'];
  7999. $result = $this->Form->getValueSources();
  8000. $this->assertEquals($expected, $result);
  8001. $expected = '2';
  8002. $result = $this->Form->getSourceValue('id');
  8003. $this->assertEquals($expected, $result);
  8004. $this->Form->setValueSources(['data']);
  8005. $expected = '1';
  8006. $result = $this->Form->getSourceValue('id');
  8007. $this->assertEquals($expected, $result);
  8008. $this->Form->setValueSources(['query', 'data']);
  8009. $expected = '2';
  8010. $result = $this->Form->getSourceValue('id');
  8011. $this->assertEquals($expected, $result);
  8012. }
  8013. /**
  8014. * Tests the different input rendering values based on sources values switching
  8015. *
  8016. * @return void
  8017. */
  8018. public function testFormValueSourcesSingleSwitchRendering()
  8019. {
  8020. $this->loadFixtures('Articles');
  8021. $articles = TableRegistry::get('Articles');
  8022. $article = new Article();
  8023. $articles->patchEntity($article, ['id' => '3']);
  8024. $this->Form->create($article);
  8025. $this->Form->setValueSources(['context']);
  8026. $result = $this->Form->control('id');
  8027. $expected = [
  8028. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8029. ];
  8030. $this->assertHtml($expected, $result);
  8031. $this->Form->request->query['id'] = '5';
  8032. $this->Form->setValueSources(['query']);
  8033. $result = $this->Form->control('id');
  8034. $expected = [
  8035. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8036. ];
  8037. $this->assertHtml($expected, $result);
  8038. $this->Form->request->query['id'] = '5a';
  8039. $this->Form->request->data['id'] = '5b';
  8040. $this->Form->setValueSources(['context']);
  8041. $this->Form->create($article);
  8042. $result = $this->Form->control('id');
  8043. $expected = [
  8044. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8045. ];
  8046. $this->assertHtml($expected, $result);
  8047. $this->Form->setValueSources(['data']);
  8048. $this->Form->create($article);
  8049. $result = $this->Form->control('id');
  8050. $expected = [
  8051. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8052. ];
  8053. $this->assertHtml($expected, $result);
  8054. $this->Form->setValueSources(['query']);
  8055. $result = $this->Form->control('id');
  8056. $expected = [
  8057. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  8058. ];
  8059. $this->assertHtml($expected, $result);
  8060. }
  8061. /**
  8062. * Tests the different input rendering values based on sources values switching while supplying
  8063. * an entity (base context) and multiple sources (such as data, query)
  8064. *
  8065. * @return void
  8066. */
  8067. public function testFormValueSourcesListSwitchRendering()
  8068. {
  8069. $this->loadFixtures('Articles');
  8070. $articles = TableRegistry::get('Articles');
  8071. $article = new Article();
  8072. $articles->patchEntity($article, ['id' => '3']);
  8073. $this->Form->request->query['id'] = '9';
  8074. $this->Form->create($article);
  8075. $this->Form->setValueSources(['context', 'query']);
  8076. $result = $this->Form->control('id');
  8077. $expected = [
  8078. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8079. ];
  8080. $this->assertHtml($expected, $result);
  8081. $this->Form->setValueSources(['query', 'context']);
  8082. $result = $this->Form->control('id');
  8083. $expected = [
  8084. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8085. ];
  8086. $this->assertHtml($expected, $result);
  8087. $this->Form->setValueSources(['data', 'query', 'context']);
  8088. $result = $this->Form->control('id');
  8089. $expected = [
  8090. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8091. ];
  8092. $this->assertHtml($expected, $result);
  8093. $this->Form->request->data['id'] = '8';
  8094. $this->Form->request->query['id'] = '9';
  8095. $this->Form->setValueSources(['data', 'query', 'context']);
  8096. $result = $this->Form->control('id');
  8097. $expected = [
  8098. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  8099. ];
  8100. $this->assertHtml($expected, $result);
  8101. }
  8102. /**
  8103. * Test the different form input renderings based on values sources switchings through form options
  8104. *
  8105. * @return void
  8106. */
  8107. public function testFormValueSourcesSwitchViaOptionsRendering()
  8108. {
  8109. $this->loadFixtures('Articles');
  8110. $articles = TableRegistry::get('Articles');
  8111. $article = new Article();
  8112. $articles->patchEntity($article, ['id' => '3']);
  8113. $this->Form->request->data['id'] = '4';
  8114. $this->Form->request->query['id'] = '5';
  8115. $this->Form->create($article, ['valueSources' => 'query']);
  8116. $result = $this->Form->control('id');
  8117. $expected = [
  8118. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8119. ];
  8120. $this->assertHtml($expected, $result);
  8121. $result = $this->Form->getSourceValue('id');
  8122. $this->assertEquals('5', $result);
  8123. $this->Form->setValueSources(['context']);
  8124. $this->Form->create($article, ['valueSources' => 'query']);
  8125. $result = $this->Form->control('id');
  8126. $expected = [
  8127. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8128. ];
  8129. $this->assertHtml($expected, $result);
  8130. $result = $this->Form->getSourceValue('id');
  8131. $this->assertEquals('5', $result);
  8132. $this->Form->setValueSources(['query']);
  8133. $this->Form->create($article, ['valueSources' => 'data']);
  8134. $result = $this->Form->control('id');
  8135. $expected = [
  8136. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8137. ];
  8138. $this->assertHtml($expected, $result);
  8139. $result = $this->Form->getSourceValue('id');
  8140. $this->assertEquals('4', $result);
  8141. $this->Form->setValueSources(['query']);
  8142. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8143. $result = $this->Form->control('id');
  8144. $expected = [
  8145. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8146. ];
  8147. $this->assertHtml($expected, $result);
  8148. $result = $this->Form->getSourceValue('id');
  8149. $this->assertEquals('4', $result);
  8150. }
  8151. /**
  8152. * Test the different form input renderings based on values sources switchings through form options
  8153. *
  8154. * @return void
  8155. */
  8156. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8157. {
  8158. $this->loadFixtures('Articles');
  8159. $articles = TableRegistry::get('Articles');
  8160. $article = new Article();
  8161. $articles->patchEntity($article, ['id' => '3']);
  8162. $this->Form->request->data['id'] = '10';
  8163. $this->Form->request->query['id'] = '11';
  8164. $this->Form->setValueSources(['context'])
  8165. ->create($article, ['valueSources' => ['query', 'data']]);
  8166. $result = $this->Form->control('id');
  8167. $expected = [
  8168. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8169. ];
  8170. $this->assertHtml($expected, $result);
  8171. $result = $this->Form->getSourceValue('id');
  8172. $this->assertEquals('11', $result);
  8173. unset($this->Form->request->query['id']);
  8174. $this->Form->setValueSources(['context'])
  8175. ->create($article, ['valueSources' => ['query', 'data']]);
  8176. $result = $this->Form->control('id');
  8177. $expected = [
  8178. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8179. ];
  8180. $this->assertHtml($expected, $result);
  8181. $result = $this->Form->getSourceValue('id');
  8182. $this->assertEquals('10', $result);
  8183. }
  8184. /**
  8185. * Test the different form values sources resetting through From::end();
  8186. *
  8187. * @return void
  8188. */
  8189. public function testFormValueSourcesResetViaEnd()
  8190. {
  8191. $expected = ['context'];
  8192. $result = $this->Form->getValueSources();
  8193. $this->assertEquals($expected, $result);
  8194. $expected = ['query', 'context', 'data'];
  8195. $this->Form->setValueSources(['query', 'context', 'data']);
  8196. $result = $this->Form->getValueSources();
  8197. $this->assertEquals($expected, $result);
  8198. $this->Form->create();
  8199. $result = $this->Form->getValueSources();
  8200. $this->assertEquals($expected, $result);
  8201. $this->Form->end();
  8202. $result = $this->Form->getValueSources();
  8203. $this->assertEquals(['context'], $result);
  8204. }
  8205. /**
  8206. * Test sources values defaults handling
  8207. *
  8208. * @return void
  8209. */
  8210. public function testFormValueSourcesDefaults()
  8211. {
  8212. $this->Form->request->query['password'] = 'open Sesame';
  8213. $this->Form->create();
  8214. $result = $this->Form->password('password');
  8215. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8216. $this->assertHtml($expected, $result);
  8217. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8218. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8219. $this->assertHtml($expected, $result);
  8220. $this->Form->setValueSources('query');
  8221. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8222. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8223. $this->assertHtml($expected, $result);
  8224. $this->Form->setValueSources('data');
  8225. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8226. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8227. $this->assertHtml($expected, $result);
  8228. }
  8229. /**
  8230. * Test sources values schema defaults handling
  8231. *
  8232. * @return void
  8233. */
  8234. public function testSourcesValueDoesntExistPassThrough()
  8235. {
  8236. $this->Form->request->query['category'] = 'sesame-cookies';
  8237. $articles = TableRegistry::get('Articles');
  8238. $entity = $articles->newEntity();
  8239. $this->Form->create($entity);
  8240. $this->Form->setValueSources(['query', 'context']);
  8241. $result = $this->Form->getSourceValue('category');
  8242. $this->assertEquals('sesame-cookies', $result);
  8243. $this->Form->setValueSources(['context', 'query']);
  8244. $result = $this->Form->getSourceValue('category');
  8245. $this->assertEquals('sesame-cookies', $result);
  8246. }
  8247. /**
  8248. * testNestedLabelInput method
  8249. *
  8250. * Test the `nestedInput` parameter
  8251. *
  8252. * @return void
  8253. */
  8254. public function testNestedLabelInput()
  8255. {
  8256. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8257. $expected = [
  8258. 'div' => ['class' => 'input text'],
  8259. 'label' => ['for' => 'foo'],
  8260. ['input' => [
  8261. 'type' => 'text',
  8262. 'name' => 'foo',
  8263. 'id' => 'foo'
  8264. ]],
  8265. 'Foo',
  8266. '/label',
  8267. '/div'
  8268. ];
  8269. $this->assertHtml($expected, $result);
  8270. }
  8271. /**
  8272. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8273. *
  8274. * This test makes sure `false` excludes the label from the render
  8275. *
  8276. * @return void
  8277. */
  8278. public function testInputLabelManipulationDisableLabels()
  8279. {
  8280. $result = $this->Form->input('test', [
  8281. 'type' => 'radio',
  8282. 'options' => ['A', 'B'],
  8283. 'labelOptions' => false
  8284. ]);
  8285. $expected = [
  8286. ['div' => ['class' => 'input radio']],
  8287. '<label',
  8288. 'Test',
  8289. '/label',
  8290. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8291. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8292. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8293. '/div',
  8294. ];
  8295. $this->assertHtml($expected, $result);
  8296. $result = $this->Form->input('checkbox1', [
  8297. 'label' => 'My checkboxes',
  8298. 'multiple' => 'checkbox',
  8299. 'type' => 'select',
  8300. 'options' => [
  8301. ['text' => 'First Checkbox', 'value' => 1],
  8302. ['text' => 'Second Checkbox', 'value' => 2]
  8303. ],
  8304. 'labelOptions' => false
  8305. ]);
  8306. $expected = [
  8307. ['div' => ['class' => 'input select']],
  8308. ['label' => ['for' => 'checkbox1']],
  8309. 'My checkboxes',
  8310. '/label',
  8311. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8312. ['div' => ['class' => 'checkbox']],
  8313. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8314. '/div',
  8315. ['div' => ['class' => 'checkbox']],
  8316. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8317. '/div',
  8318. '/div'
  8319. ];
  8320. $this->assertHtml($expected, $result);
  8321. }
  8322. /**
  8323. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8324. *
  8325. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8326. * added to the class if checked.
  8327. *
  8328. * Also checks to make sure any custom attributes are rendered correctly
  8329. *
  8330. * @return void
  8331. */
  8332. public function testInputLabelManipulationRadios()
  8333. {
  8334. $result = $this->Form->input('test', [
  8335. 'type' => 'radio',
  8336. 'options' => ['A', 'B'],
  8337. 'labelOptions' => ['class' => 'custom-class']
  8338. ]);
  8339. $expected = [
  8340. ['div' => ['class' => 'input radio']],
  8341. '<label',
  8342. 'Test',
  8343. '/label',
  8344. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8345. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8346. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8347. 'A',
  8348. '/label',
  8349. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8350. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8351. 'B',
  8352. '/label',
  8353. '/div',
  8354. ];
  8355. $this->assertHtml($expected, $result);
  8356. $result = $this->Form->input('test', [
  8357. 'type' => 'radio',
  8358. 'options' => ['A', 'B'],
  8359. 'value' => 1,
  8360. 'labelOptions' => ['class' => 'custom-class']
  8361. ]);
  8362. $expected = [
  8363. ['div' => ['class' => 'input radio']],
  8364. '<label',
  8365. 'Test',
  8366. '/label',
  8367. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8368. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8369. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8370. 'A',
  8371. '/label',
  8372. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8373. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8374. 'B',
  8375. '/label',
  8376. '/div',
  8377. ];
  8378. $this->assertHtml($expected, $result);
  8379. $result = $this->Form->input('test', [
  8380. 'type' => 'radio',
  8381. 'options' => ['A', 'B'],
  8382. 'value' => 1,
  8383. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']]
  8384. ]);
  8385. $expected = [
  8386. ['div' => ['class' => 'input radio']],
  8387. '<label',
  8388. 'Test',
  8389. '/label',
  8390. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8391. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8392. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8393. 'A',
  8394. '/label',
  8395. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8396. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8397. 'B',
  8398. '/label',
  8399. '/div',
  8400. ];
  8401. $this->assertHtml($expected, $result);
  8402. $result = $this->Form->radio('test', ['A', 'B'], [
  8403. 'label' => [
  8404. 'class' => ['custom-class', 'another-class'],
  8405. 'data-name' => 'bob'
  8406. ],
  8407. 'value' => 1
  8408. ]);
  8409. $expected = [
  8410. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8411. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8412. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8413. 'A',
  8414. '/label',
  8415. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8416. ['input' => [
  8417. 'type' => 'radio',
  8418. 'name' => 'test',
  8419. 'value' => '1',
  8420. 'id' => 'test-1',
  8421. 'checked' => 'checked'
  8422. ]],
  8423. 'B',
  8424. '/label',
  8425. ];
  8426. $this->assertHtml($expected, $result);
  8427. }
  8428. /**
  8429. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8430. *
  8431. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8432. * added to the class if checked.
  8433. *
  8434. * Also checks to make sure any custom attributes are rendered correctly
  8435. *
  8436. * @return void
  8437. */
  8438. public function testInputLabelManipulationCheckboxes()
  8439. {
  8440. $result = $this->Form->input('checkbox1', [
  8441. 'label' => 'My checkboxes',
  8442. 'multiple' => 'checkbox',
  8443. 'type' => 'select',
  8444. 'options' => [
  8445. ['text' => 'First Checkbox', 'value' => 1],
  8446. ['text' => 'Second Checkbox', 'value' => 2]
  8447. ],
  8448. 'labelOptions' => ['class' => 'custom-class'],
  8449. 'value' => ['1']
  8450. ]);
  8451. $expected = [
  8452. ['div' => ['class' => 'input select']],
  8453. ['label' => ['for' => 'checkbox1']],
  8454. 'My checkboxes',
  8455. '/label',
  8456. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8457. ['div' => ['class' => 'checkbox']],
  8458. ['label' => [
  8459. 'class' => 'custom-class selected',
  8460. 'for' => 'checkbox1-1'
  8461. ]],
  8462. ['input' => [
  8463. 'type' => 'checkbox',
  8464. 'name' => 'checkbox1[]',
  8465. 'value' => '1',
  8466. 'id' => 'checkbox1-1',
  8467. 'checked' => 'checked'
  8468. ]],
  8469. 'First Checkbox',
  8470. '/label',
  8471. '/div',
  8472. ['div' => ['class' => 'checkbox']],
  8473. ['label' => [
  8474. 'class' => 'custom-class',
  8475. 'for' => 'checkbox1-2'
  8476. ]],
  8477. ['input' => [
  8478. 'type' => 'checkbox',
  8479. 'name' => 'checkbox1[]',
  8480. 'value' => '2',
  8481. 'id' => 'checkbox1-2'
  8482. ]],
  8483. 'Second Checkbox',
  8484. '/label',
  8485. '/div',
  8486. '/div'
  8487. ];
  8488. $this->assertHtml($expected, $result);
  8489. $result = $this->Form->input('checkbox1', [
  8490. 'label' => 'My checkboxes',
  8491. 'multiple' => 'checkbox',
  8492. 'type' => 'select',
  8493. 'options' => [
  8494. ['text' => 'First Checkbox', 'value' => 1],
  8495. ['text' => 'Second Checkbox', 'value' => 2]
  8496. ],
  8497. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8498. 'value' => ['1']
  8499. ]);
  8500. $expected = [
  8501. ['div' => ['class' => 'input select']],
  8502. ['label' => ['for' => 'checkbox1']],
  8503. 'My checkboxes',
  8504. '/label',
  8505. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8506. ['div' => ['class' => 'checkbox']],
  8507. ['label' => [
  8508. 'class' => 'custom-class another-class selected',
  8509. 'data-name' => 'bob',
  8510. 'for' => 'checkbox1-1'
  8511. ]],
  8512. ['input' => [
  8513. 'type' => 'checkbox',
  8514. 'name' => 'checkbox1[]',
  8515. 'value' => '1',
  8516. 'id' => 'checkbox1-1',
  8517. 'checked' => 'checked'
  8518. ]],
  8519. 'First Checkbox',
  8520. '/label',
  8521. '/div',
  8522. ['div' => ['class' => 'checkbox']],
  8523. ['label' => [
  8524. 'class' => 'custom-class another-class',
  8525. 'data-name' => 'bob',
  8526. 'for' => 'checkbox1-2'
  8527. ]],
  8528. ['input' => [
  8529. 'type' => 'checkbox',
  8530. 'name' => 'checkbox1[]',
  8531. 'value' => '2',
  8532. 'id' => 'checkbox1-2'
  8533. ]],
  8534. 'Second Checkbox',
  8535. '/label',
  8536. '/div',
  8537. '/div'
  8538. ];
  8539. $this->assertHtml($expected, $result);
  8540. }
  8541. }