FormHelperTest.php 305 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266
  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->setSchema($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->setSchema($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::setSalt('foo!');
  169. Router::connect('/:controller', ['action' => 'index']);
  170. Router::connect('/:controller/:action/*');
  171. }
  172. /**
  173. * tearDown method
  174. *
  175. * @return void
  176. */
  177. public function tearDown()
  178. {
  179. parent::tearDown();
  180. unset($this->Form, $this->Controller, $this->View);
  181. 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. * @return void
  289. */
  290. public function testAddWidgetInvalid()
  291. {
  292. $this->expectException(\RuntimeException::class);
  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. * @return void
  353. */
  354. public function testAddContextProviderInvalid()
  355. {
  356. $this->expectException(\RuntimeException::class);
  357. $this->expectExceptionMessage('Context providers must return object implementing Cake\View\Form\ContextInterface. Got "stdClass" instead.');
  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->setTemplates([
  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 = $this->Form->request
  670. ->withRequestTarget('/articles/edit/1')
  671. ->withParam('action', 'edit');
  672. $this->article['defaults']['id'] = 1;
  673. $result = $this->Form->create($this->article);
  674. $expected = [
  675. 'form' => [
  676. 'method' => 'post',
  677. 'action' => '/articles/edit/1',
  678. 'accept-charset' => $encoding
  679. ],
  680. 'div' => ['style' => 'display:none;'],
  681. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  682. '/div'
  683. ];
  684. $this->assertHtml($expected, $result);
  685. }
  686. /**
  687. * test create() with automatic url generation
  688. *
  689. * @return void
  690. */
  691. public function testCreateAutoUrl()
  692. {
  693. $encoding = strtolower(Configure::read('App.encoding'));
  694. $this->Form->request = $this->Form->request
  695. ->withRequestTarget('/articles/delete/10')
  696. ->withParam('action', 'delete');
  697. $result = $this->Form->create($this->article);
  698. $expected = [
  699. 'form' => [
  700. 'method' => 'post', 'action' => '/articles/delete/10',
  701. 'accept-charset' => $encoding
  702. ],
  703. 'div' => ['style' => 'display:none;'],
  704. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  705. '/div'
  706. ];
  707. $this->assertHtml($expected, $result);
  708. $this->article['defaults'] = ['id' => 1];
  709. $this->Form->request->here = '/articles/edit/1';
  710. $this->Form->request->params['action'] = 'delete';
  711. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  712. $expected = [
  713. 'form' => [
  714. 'method' => 'post',
  715. 'action' => '/articles/edit/1',
  716. 'accept-charset' => $encoding
  717. ],
  718. 'div' => ['style' => 'display:none;'],
  719. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  720. '/div'
  721. ];
  722. $this->assertHtml($expected, $result);
  723. $this->Form->request->params['action'] = 'add';
  724. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  725. $expected = [
  726. 'form' => [
  727. 'method' => 'post',
  728. 'action' => '/articles/publish/1',
  729. 'accept-charset' => $encoding
  730. ],
  731. 'div' => ['style' => 'display:none;'],
  732. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  733. '/div'
  734. ];
  735. $this->assertHtml($expected, $result);
  736. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  737. $expected = [
  738. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  739. 'div' => ['style' => 'display:none;'],
  740. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  741. '/div'
  742. ];
  743. $this->assertHtml($expected, $result);
  744. $this->Form->request->params['controller'] = 'Pages';
  745. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  746. $expected = [
  747. 'form' => [
  748. 'method' => 'post', 'action' => '/Pages/signup/1',
  749. 'accept-charset' => $encoding
  750. ],
  751. 'div' => ['style' => 'display:none;'],
  752. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  753. '/div'
  754. ];
  755. $this->assertHtml($expected, $result);
  756. }
  757. /**
  758. * Test create() with no URL (no "action" attribute for <form> tag)
  759. *
  760. * @return void
  761. */
  762. public function testCreateNoUrl()
  763. {
  764. $result = $this->Form->create(false, ['url' => false]);
  765. $expected = [
  766. 'form' => [
  767. 'method' => 'post',
  768. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  769. ],
  770. 'div' => ['style' => 'display:none;'],
  771. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  772. '/div'
  773. ];
  774. $this->assertHtml($expected, $result);
  775. }
  776. /**
  777. * test create() with a custom route
  778. *
  779. * @return void
  780. */
  781. public function testCreateCustomRoute()
  782. {
  783. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  784. $encoding = strtolower(Configure::read('App.encoding'));
  785. $this->Form->request->params['controller'] = 'users';
  786. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  787. $expected = [
  788. 'form' => [
  789. 'method' => 'post', 'action' => '/login',
  790. 'accept-charset' => $encoding
  791. ],
  792. 'div' => ['style' => 'display:none;'],
  793. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  794. '/div'
  795. ];
  796. $this->assertHtml($expected, $result);
  797. Router::connect(
  798. '/new-article',
  799. ['controller' => 'articles', 'action' => 'myaction'],
  800. ['_name' => 'my-route']
  801. );
  802. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  803. $expected = [
  804. 'form' => [
  805. 'method' => 'post', 'action' => '/new-article',
  806. 'accept-charset' => $encoding,
  807. ],
  808. 'div' => ['style' => 'display:none;'],
  809. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  810. '/div'
  811. ];
  812. $this->assertHtml($expected, $result);
  813. }
  814. /**
  815. * test automatic accept-charset overriding
  816. *
  817. * @return void
  818. */
  819. public function testCreateWithAcceptCharset()
  820. {
  821. $result = $this->Form->create(
  822. $this->article,
  823. [
  824. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  825. ]
  826. );
  827. $expected = [
  828. 'form' => [
  829. 'method' => 'post', 'action' => '/articles',
  830. 'accept-charset' => 'iso-8859-1'
  831. ],
  832. 'div' => ['style' => 'display:none;'],
  833. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  834. '/div'
  835. ];
  836. $this->assertHtml($expected, $result);
  837. }
  838. /**
  839. * Test base form URL when url param is passed with multiple parameters (&)
  840. *
  841. */
  842. public function testCreateQueryStringRequest()
  843. {
  844. $encoding = strtolower(Configure::read('App.encoding'));
  845. $result = $this->Form->create($this->article, [
  846. 'type' => 'post',
  847. 'escape' => false,
  848. 'url' => [
  849. 'controller' => 'controller',
  850. 'action' => 'action',
  851. '?' => ['param1' => 'value1', 'param2' => 'value2']
  852. ]
  853. ]);
  854. $expected = [
  855. 'form' => [
  856. 'method' => 'post',
  857. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  858. 'accept-charset' => $encoding
  859. ],
  860. 'div' => ['style' => 'display:none;'],
  861. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  862. '/div'
  863. ];
  864. $this->assertHtml($expected, $result);
  865. $result = $this->Form->create($this->article, [
  866. 'type' => 'post',
  867. 'url' => [
  868. 'controller' => 'controller',
  869. 'action' => 'action',
  870. '?' => ['param1' => 'value1', 'param2' => 'value2']
  871. ]
  872. ]);
  873. $this->assertHtml($expected, $result);
  874. }
  875. /**
  876. * test that create() doesn't cause errors by multiple id's being in the primary key
  877. * as could happen with multiple select or checkboxes.
  878. *
  879. * @return void
  880. */
  881. public function testCreateWithMultipleIdInData()
  882. {
  883. $encoding = strtolower(Configure::read('App.encoding'));
  884. $this->Form->request->data['Article']['id'] = [1, 2];
  885. $result = $this->Form->create($this->article);
  886. $expected = [
  887. 'form' => [
  888. 'method' => 'post',
  889. 'action' => '/articles/add',
  890. 'accept-charset' => $encoding
  891. ],
  892. 'div' => ['style' => 'display:none;'],
  893. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  894. '/div'
  895. ];
  896. $this->assertHtml($expected, $result);
  897. }
  898. /**
  899. * test that create() doesn't add in extra passed params.
  900. *
  901. * @return void
  902. */
  903. public function testCreatePassedArgs()
  904. {
  905. $encoding = strtolower(Configure::read('App.encoding'));
  906. $this->Form->request->data['Article']['id'] = 1;
  907. $result = $this->Form->create($this->article, [
  908. 'type' => 'post',
  909. 'escape' => false,
  910. 'url' => [
  911. 'action' => 'edit',
  912. 'myparam'
  913. ]
  914. ]);
  915. $expected = [
  916. 'form' => [
  917. 'method' => 'post',
  918. 'action' => '/articles/edit/myparam',
  919. 'accept-charset' => $encoding
  920. ],
  921. 'div' => ['style' => 'display:none;'],
  922. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  923. '/div'
  924. ];
  925. $this->assertHtml($expected, $result);
  926. }
  927. /**
  928. * test creating a get form, and get form inputs.
  929. *
  930. * @return void
  931. */
  932. public function testGetFormCreate()
  933. {
  934. $encoding = strtolower(Configure::read('App.encoding'));
  935. $result = $this->Form->create($this->article, ['type' => 'get']);
  936. $expected = ['form' => [
  937. 'method' => 'get', 'action' => '/articles/add',
  938. 'accept-charset' => $encoding
  939. ]];
  940. $this->assertHtml($expected, $result);
  941. $result = $this->Form->text('title');
  942. $expected = ['input' => [
  943. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  944. ]];
  945. $this->assertHtml($expected, $result);
  946. $result = $this->Form->password('password');
  947. $expected = ['input' => [
  948. 'name' => 'password', 'type' => 'password'
  949. ]];
  950. $this->assertHtml($expected, $result);
  951. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  952. $result = $this->Form->text('user_form');
  953. $expected = ['input' => [
  954. 'name' => 'user_form', 'type' => 'text'
  955. ]];
  956. $this->assertHtml($expected, $result);
  957. }
  958. /**
  959. * test get form, and inputs when the model param is false
  960. *
  961. * @return void
  962. */
  963. public function testGetFormWithFalseModel()
  964. {
  965. $encoding = strtolower(Configure::read('App.encoding'));
  966. $this->Form->request->params['controller'] = 'contact_test';
  967. $result = $this->Form->create(false, [
  968. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  969. ]);
  970. $expected = ['form' => [
  971. 'method' => 'get', 'action' => '/contact_test/add',
  972. 'accept-charset' => $encoding
  973. ]];
  974. $this->assertHtml($expected, $result);
  975. $result = $this->Form->text('reason');
  976. $expected = [
  977. 'input' => ['type' => 'text', 'name' => 'reason']
  978. ];
  979. $this->assertHtml($expected, $result);
  980. }
  981. /**
  982. * testFormCreateWithSecurity method
  983. *
  984. * Test form->create() with security key.
  985. *
  986. * @return void
  987. */
  988. public function testCreateWithSecurity()
  989. {
  990. $this->Form->request->params['_csrfToken'] = 'testKey';
  991. $encoding = strtolower(Configure::read('App.encoding'));
  992. $result = $this->Form->create($this->article, [
  993. 'url' => '/articles/publish',
  994. ]);
  995. $expected = [
  996. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  997. 'div' => ['style' => 'display:none;'],
  998. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  999. ['input' => [
  1000. 'type' => 'hidden',
  1001. 'name' => '_csrfToken',
  1002. 'value' => 'testKey',
  1003. 'autocomplete' => 'off',
  1004. ]],
  1005. '/div'
  1006. ];
  1007. $this->assertHtml($expected, $result);
  1008. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1009. $expected['form']['id'] = 'MyForm';
  1010. $this->assertHtml($expected, $result);
  1011. }
  1012. /**
  1013. * testFormCreateGetNoSecurity method
  1014. *
  1015. * Test form->create() with no security key as its a get form
  1016. *
  1017. * @return void
  1018. */
  1019. public function testCreateEndGetNoSecurity()
  1020. {
  1021. $this->Form->request->params['_csrfToken'] = 'testKey';
  1022. $article = new Article();
  1023. $result = $this->Form->create($article, [
  1024. 'type' => 'get',
  1025. 'url' => '/contacts/add'
  1026. ]);
  1027. $this->assertNotContains('testKey', $result);
  1028. $result = $this->Form->end();
  1029. $this->assertNotContains('testKey', $result);
  1030. }
  1031. /**
  1032. * test that create() clears the fields property so it starts fresh
  1033. *
  1034. * @return void
  1035. */
  1036. public function testCreateClearingFields()
  1037. {
  1038. $this->Form->fields = ['model_id'];
  1039. $this->Form->create($this->article);
  1040. $this->assertEquals([], $this->Form->fields);
  1041. }
  1042. /**
  1043. * Tests form hash generation with model-less data
  1044. *
  1045. * @return void
  1046. */
  1047. public function testValidateHashNoModel()
  1048. {
  1049. $this->Form->request->params['_Token'] = 'foo';
  1050. $fields = ['anything'];
  1051. $result = $this->Form->secure($fields);
  1052. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1053. $this->assertContains($hash, $result);
  1054. }
  1055. /**
  1056. * Tests that hidden fields generated for checkboxes don't get locked
  1057. *
  1058. * @return void
  1059. */
  1060. public function testNoCheckboxLocking()
  1061. {
  1062. $this->Form->request->params['_Token'] = 'foo';
  1063. $this->assertSame([], $this->Form->fields);
  1064. $this->Form->checkbox('check', ['value' => '1']);
  1065. $this->assertSame($this->Form->fields, ['check']);
  1066. }
  1067. /**
  1068. * testFormSecurityFields method
  1069. *
  1070. * Test generation of secure form hash generation.
  1071. *
  1072. * @return void
  1073. */
  1074. public function testFormSecurityFields()
  1075. {
  1076. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1077. $this->Form->request->params['_Token'] = 'testKey';
  1078. $result = $this->Form->secure($fields);
  1079. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1080. $hash .= ':' . 'Model.valid';
  1081. $hash = urlencode($hash);
  1082. $tokenDebug = urlencode(json_encode([
  1083. '',
  1084. $fields,
  1085. []
  1086. ]));
  1087. $expected = [
  1088. 'div' => ['style' => 'display:none;'],
  1089. ['input' => [
  1090. 'type' => 'hidden',
  1091. 'name' => '_Token[fields]',
  1092. 'value' => $hash,
  1093. 'autocomplete' => 'off',
  1094. ]],
  1095. ['input' => [
  1096. 'type' => 'hidden',
  1097. 'name' => '_Token[unlocked]',
  1098. 'value' => '',
  1099. 'autocomplete' => 'off',
  1100. ]],
  1101. ['input' => [
  1102. 'type' => 'hidden',
  1103. 'name' => '_Token[debug]',
  1104. 'value' => $tokenDebug,
  1105. 'autocomplete' => 'off',
  1106. ]],
  1107. '/div'
  1108. ];
  1109. $this->assertHtml($expected, $result);
  1110. }
  1111. /**
  1112. * testFormSecurityFields method
  1113. *
  1114. * Test debug token is not generated if debug is false
  1115. *
  1116. * @return void
  1117. */
  1118. public function testFormSecurityFieldsNoDebugMode()
  1119. {
  1120. Configure::write('debug', false);
  1121. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1122. $this->Form->request->params['_Token'] = 'testKey';
  1123. $result = $this->Form->secure($fields);
  1124. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1125. $hash .= ':' . 'Model.valid';
  1126. $hash = urlencode($hash);
  1127. $expected = [
  1128. 'div' => ['style' => 'display:none;'],
  1129. ['input' => [
  1130. 'type' => 'hidden',
  1131. 'name' => '_Token[fields]',
  1132. 'autocomplete' => 'off',
  1133. 'value' => $hash
  1134. ]],
  1135. ['input' => [
  1136. 'type' => 'hidden',
  1137. 'name' => '_Token[unlocked]',
  1138. 'autocomplete' => 'off',
  1139. 'value' => '',
  1140. ]],
  1141. '/div'
  1142. ];
  1143. $this->assertHtml($expected, $result);
  1144. }
  1145. /**
  1146. * Tests correct generation of number fields for smallint
  1147. *
  1148. * @return void
  1149. */
  1150. public function testTextFieldGenerationForSmallint()
  1151. {
  1152. $this->article['schema'] = [
  1153. 'foo' => [
  1154. 'type' => 'smallinteger',
  1155. 'null' => false,
  1156. 'default' => null,
  1157. 'length' => 10
  1158. ]
  1159. ];
  1160. $this->Form->create($this->article);
  1161. $result = $this->Form->control('foo');
  1162. $this->assertContains('class="input number"', $result);
  1163. $this->assertContains('type="number"', $result);
  1164. }
  1165. /**
  1166. * Tests correct generation of number fields for tinyint
  1167. *
  1168. * @return void
  1169. */
  1170. public function testTextFieldGenerationForTinyint()
  1171. {
  1172. $this->article['schema'] = [
  1173. 'foo' => [
  1174. 'type' => 'tinyinteger',
  1175. 'null' => false,
  1176. 'default' => null,
  1177. 'length' => 10
  1178. ]
  1179. ];
  1180. $this->Form->create($this->article);
  1181. $result = $this->Form->control('foo');
  1182. $this->assertContains('class="input number"', $result);
  1183. $this->assertContains('type="number"', $result);
  1184. }
  1185. /**
  1186. * Tests correct generation of number fields for double and float fields
  1187. *
  1188. * @return void
  1189. */
  1190. public function testTextFieldGenerationForFloats()
  1191. {
  1192. $this->article['schema'] = [
  1193. 'foo' => [
  1194. 'type' => 'float',
  1195. 'null' => false,
  1196. 'default' => null,
  1197. 'length' => 10
  1198. ]
  1199. ];
  1200. $this->Form->create($this->article);
  1201. $result = $this->Form->control('foo');
  1202. $expected = [
  1203. 'div' => ['class' => 'input number'],
  1204. 'label' => ['for' => 'foo'],
  1205. 'Foo',
  1206. '/label',
  1207. ['input' => [
  1208. 'type' => 'number',
  1209. 'name' => 'foo',
  1210. 'id' => 'foo',
  1211. 'step' => 'any'
  1212. ]],
  1213. '/div'
  1214. ];
  1215. $this->assertHtml($expected, $result);
  1216. $result = $this->Form->control('foo', ['step' => 0.5]);
  1217. $expected = [
  1218. 'div' => ['class' => 'input number'],
  1219. 'label' => ['for' => 'foo'],
  1220. 'Foo',
  1221. '/label',
  1222. ['input' => [
  1223. 'type' => 'number',
  1224. 'name' => 'foo',
  1225. 'id' => 'foo',
  1226. 'step' => '0.5'
  1227. ]],
  1228. '/div'
  1229. ];
  1230. $this->assertHtml($expected, $result);
  1231. }
  1232. /**
  1233. * Tests correct generation of number fields for integer fields
  1234. *
  1235. * @return void
  1236. */
  1237. public function testTextFieldTypeNumberGenerationForIntegers()
  1238. {
  1239. TableRegistry::get('Contacts', [
  1240. 'className' => __NAMESPACE__ . '\ContactsTable'
  1241. ]);
  1242. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1243. $result = $this->Form->control('age');
  1244. $expected = [
  1245. 'div' => ['class' => 'input number'],
  1246. 'label' => ['for' => 'age'],
  1247. 'Age',
  1248. '/label',
  1249. ['input' => [
  1250. 'type' => 'number', 'name' => 'age',
  1251. 'id' => 'age'
  1252. ]],
  1253. '/div'
  1254. ];
  1255. $this->assertHtml($expected, $result);
  1256. }
  1257. /**
  1258. * Tests correct generation of file upload fields for binary fields
  1259. *
  1260. * @return void
  1261. */
  1262. public function testFileUploadFieldTypeGenerationForBinaries()
  1263. {
  1264. $table = TableRegistry::get('Contacts', [
  1265. 'className' => __NAMESPACE__ . '\ContactsTable'
  1266. ]);
  1267. $table->setSchema(['foo' => [
  1268. 'type' => 'binary',
  1269. 'null' => false,
  1270. 'default' => null,
  1271. 'length' => 1024
  1272. ]]);
  1273. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1274. $result = $this->Form->control('foo');
  1275. $expected = [
  1276. 'div' => ['class' => 'input file'],
  1277. 'label' => ['for' => 'foo'],
  1278. 'Foo',
  1279. '/label',
  1280. ['input' => [
  1281. 'type' => 'file', 'name' => 'foo',
  1282. 'id' => 'foo'
  1283. ]],
  1284. '/div'
  1285. ];
  1286. $this->assertHtml($expected, $result);
  1287. }
  1288. /**
  1289. * testFormSecurityMultipleFields method
  1290. *
  1291. * Test secure() with multiple row form. Ensure hash is correct.
  1292. *
  1293. * @return void
  1294. */
  1295. public function testFormSecurityMultipleFields()
  1296. {
  1297. $this->Form->request->params['_Token'] = 'foo';
  1298. $fields = [
  1299. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1300. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1301. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1302. ];
  1303. $result = $this->Form->secure($fields);
  1304. $hash = '8670192c3f040bf58680479060b4755b7a5c3596' .
  1305. '%3AModel.0.hidden%7CModel.0.valid%7CModel.1.hidden%7CModel.1.valid';
  1306. $tokenDebug = urlencode(json_encode([
  1307. '',
  1308. $fields,
  1309. []
  1310. ]));
  1311. $expected = [
  1312. 'div' => ['style' => 'display:none;'],
  1313. ['input' => [
  1314. 'type' => 'hidden',
  1315. 'name' => '_Token[fields]',
  1316. 'value' => $hash,
  1317. 'autocomplete' => 'off',
  1318. ]],
  1319. ['input' => [
  1320. 'type' => 'hidden',
  1321. 'name' => '_Token[unlocked]',
  1322. 'autocomplete' => 'off',
  1323. 'value' => ''
  1324. ]],
  1325. ['input' => [
  1326. 'type' => 'hidden',
  1327. 'name' => '_Token[debug]',
  1328. 'value' => $tokenDebug,
  1329. 'autocomplete' => 'off',
  1330. ]],
  1331. '/div'
  1332. ];
  1333. $this->assertHtml($expected, $result);
  1334. }
  1335. /**
  1336. * testFormSecurityMultipleSubmitButtons
  1337. *
  1338. * test form submit generation and ensure that _Token is only created on end()
  1339. *
  1340. * @return void
  1341. */
  1342. public function testFormSecurityMultipleSubmitButtons()
  1343. {
  1344. $this->Form->request->params['_Token'] = 'testKey';
  1345. $this->Form->create($this->article);
  1346. $this->Form->text('Address.title');
  1347. $this->Form->text('Address.first_name');
  1348. $result = $this->Form->submit('Save', ['name' => 'save']);
  1349. $expected = [
  1350. 'div' => ['class' => 'submit'],
  1351. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1352. '/div',
  1353. ];
  1354. $this->assertHtml($expected, $result);
  1355. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1356. $expected = [
  1357. 'div' => ['class' => 'submit'],
  1358. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1359. '/div',
  1360. ];
  1361. $this->assertHtml($expected, $result);
  1362. $result = $this->Form->end();
  1363. $tokenDebug = urlencode(json_encode([
  1364. '/articles/add',
  1365. [
  1366. 'Address.title',
  1367. 'Address.first_name',
  1368. ],
  1369. ['save', 'cancel']
  1370. ]));
  1371. $expected = [
  1372. 'div' => ['style' => 'display:none;'],
  1373. ['input' => [
  1374. 'type' => 'hidden',
  1375. 'name' => '_Token[fields]',
  1376. 'autocomplete',
  1377. 'value'
  1378. ]],
  1379. ['input' => [
  1380. 'type' => 'hidden',
  1381. 'name' => '_Token[unlocked]',
  1382. 'value' => 'cancel%7Csave',
  1383. 'autocomplete' => 'off',
  1384. ]],
  1385. ['input' => [
  1386. 'type' => 'hidden',
  1387. 'name' => '_Token[debug]',
  1388. 'value' => $tokenDebug,
  1389. 'autocomplete' => 'off',
  1390. ]],
  1391. '/div'
  1392. ];
  1393. $this->assertHtml($expected, $result);
  1394. }
  1395. /**
  1396. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1397. *
  1398. * @return void
  1399. */
  1400. public function testSecurityButtonNestedNamed()
  1401. {
  1402. $key = 'testKey';
  1403. $this->Form->request->params['_csrfToken'] = $key;
  1404. $this->Form->create('Addresses');
  1405. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1406. $result = $this->Form->unlockField();
  1407. $this->assertEquals(['Address.button'], $result);
  1408. }
  1409. /**
  1410. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1411. *
  1412. * @return void
  1413. */
  1414. public function testSecuritySubmitNestedNamed()
  1415. {
  1416. $this->Form->request->params['_Token'] = 'testKey';
  1417. $this->Form->create($this->article);
  1418. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1419. $result = $this->Form->unlockField();
  1420. $this->assertEquals(['Address.button'], $result);
  1421. }
  1422. /**
  1423. * Test that the correct fields are unlocked for image submits with no names.
  1424. *
  1425. * @return void
  1426. */
  1427. public function testSecuritySubmitImageNoName()
  1428. {
  1429. $this->Form->request->params['_Token'] = 'testKey';
  1430. $this->Form->create(false);
  1431. $result = $this->Form->submit('save.png');
  1432. $expected = [
  1433. 'div' => ['class' => 'submit'],
  1434. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1435. '/div'
  1436. ];
  1437. $this->assertHtml($expected, $result);
  1438. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1439. }
  1440. /**
  1441. * Test that the correct fields are unlocked for image submits with names.
  1442. *
  1443. * @return void
  1444. */
  1445. public function testSecuritySubmitImageName()
  1446. {
  1447. $this->Form->request->params['_Token'] = 'testKey';
  1448. $this->Form->create(null);
  1449. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1450. $expected = [
  1451. 'div' => ['class' => 'submit'],
  1452. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1453. '/div'
  1454. ];
  1455. $this->assertHtml($expected, $result);
  1456. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1457. }
  1458. /**
  1459. * testFormSecurityMultipleControlFields method
  1460. *
  1461. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1462. *
  1463. * @return void
  1464. */
  1465. public function testFormSecurityMultipleControlFields()
  1466. {
  1467. $this->Form->request->params['_Token'] = 'testKey';
  1468. $this->Form->create();
  1469. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1470. $this->Form->control('Addresses.0.title');
  1471. $this->Form->control('Addresses.0.first_name');
  1472. $this->Form->control('Addresses.0.last_name');
  1473. $this->Form->control('Addresses.0.address');
  1474. $this->Form->control('Addresses.0.city');
  1475. $this->Form->control('Addresses.0.phone');
  1476. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1477. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1478. $this->Form->control('Addresses.1.title');
  1479. $this->Form->control('Addresses.1.first_name');
  1480. $this->Form->control('Addresses.1.last_name');
  1481. $this->Form->control('Addresses.1.address');
  1482. $this->Form->control('Addresses.1.city');
  1483. $this->Form->control('Addresses.1.phone');
  1484. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1485. $result = $this->Form->secure($this->Form->fields);
  1486. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1487. $tokenDebug = urlencode(json_encode([
  1488. '/articles/add',
  1489. [
  1490. 'Addresses.0.id' => '123456',
  1491. 'Addresses.0.title',
  1492. 'Addresses.0.first_name',
  1493. 'Addresses.0.last_name',
  1494. 'Addresses.0.address',
  1495. 'Addresses.0.city',
  1496. 'Addresses.0.phone',
  1497. 'Addresses.0.primary',
  1498. 'Addresses.1.id' => '654321',
  1499. 'Addresses.1.title',
  1500. 'Addresses.1.first_name',
  1501. 'Addresses.1.last_name',
  1502. 'Addresses.1.address',
  1503. 'Addresses.1.city',
  1504. 'Addresses.1.phone',
  1505. 'Addresses.1.primary',
  1506. ],
  1507. []
  1508. ]));
  1509. $expected = [
  1510. 'div' => ['style' => 'display:none;'],
  1511. ['input' => [
  1512. 'type' => 'hidden',
  1513. 'name' => '_Token[fields]',
  1514. 'value' => $hash,
  1515. 'autocomplete' => 'off',
  1516. ]],
  1517. ['input' => [
  1518. 'type' => 'hidden',
  1519. 'name' => '_Token[unlocked]',
  1520. 'autocomplete' => 'off',
  1521. 'value' => ''
  1522. ]],
  1523. ['input' => [
  1524. 'type' => 'hidden',
  1525. 'name' => '_Token[debug]',
  1526. 'value' => $tokenDebug,
  1527. 'autocomplete' => 'off',
  1528. ]],
  1529. '/div'
  1530. ];
  1531. $this->assertHtml($expected, $result);
  1532. }
  1533. /**
  1534. * testFormSecurityArrayFields method
  1535. *
  1536. * Test form security with Model.field.0 style inputs.
  1537. *
  1538. * @return void
  1539. */
  1540. public function testFormSecurityArrayFields()
  1541. {
  1542. $this->Form->request->params['_Token'] = 'testKey';
  1543. $this->Form->create();
  1544. $this->Form->text('Address.primary.1');
  1545. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1546. $this->Form->text('Address.secondary.1.0');
  1547. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1548. }
  1549. /**
  1550. * testFormSecurityMultipleControlDisabledFields method
  1551. *
  1552. * Test secure form generation with multiple records and disabled fields.
  1553. *
  1554. * @return void
  1555. */
  1556. public function testFormSecurityMultipleControlDisabledFields()
  1557. {
  1558. $this->Form->request->params['_Token'] = [
  1559. 'unlockedFields' => ['first_name', 'address']
  1560. ];
  1561. $this->Form->create();
  1562. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1563. $this->Form->text('Addresses.0.title');
  1564. $this->Form->text('Addresses.0.first_name');
  1565. $this->Form->text('Addresses.0.last_name');
  1566. $this->Form->text('Addresses.0.address');
  1567. $this->Form->text('Addresses.0.city');
  1568. $this->Form->text('Addresses.0.phone');
  1569. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1570. $this->Form->text('Addresses.1.title');
  1571. $this->Form->text('Addresses.1.first_name');
  1572. $this->Form->text('Addresses.1.last_name');
  1573. $this->Form->text('Addresses.1.address');
  1574. $this->Form->text('Addresses.1.city');
  1575. $this->Form->text('Addresses.1.phone');
  1576. $result = $this->Form->secure($this->Form->fields);
  1577. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1578. $tokenDebug = urlencode(json_encode([
  1579. '/articles/add',
  1580. [
  1581. 'Addresses.0.id' => '123456',
  1582. 'Addresses.0.title',
  1583. 'Addresses.0.last_name',
  1584. 'Addresses.0.city',
  1585. 'Addresses.0.phone',
  1586. 'Addresses.1.id' => '654321',
  1587. 'Addresses.1.title',
  1588. 'Addresses.1.last_name',
  1589. 'Addresses.1.city',
  1590. 'Addresses.1.phone'
  1591. ],
  1592. [
  1593. 'first_name',
  1594. 'address'
  1595. ]
  1596. ]));
  1597. $expected = [
  1598. 'div' => ['style' => 'display:none;'],
  1599. ['input' => [
  1600. 'type' => 'hidden',
  1601. 'name' => '_Token[fields]',
  1602. 'autocomplete' => 'off',
  1603. 'value' => $hash
  1604. ]],
  1605. ['input' => [
  1606. 'type' => 'hidden',
  1607. 'name' => '_Token[unlocked]',
  1608. 'autocomplete' => 'off',
  1609. 'value' => 'address%7Cfirst_name',
  1610. ]],
  1611. ['input' => [
  1612. 'type' => 'hidden',
  1613. 'name' => '_Token[debug]',
  1614. 'autocomplete' => 'off',
  1615. 'value' => $tokenDebug,
  1616. ]],
  1617. '/div'
  1618. ];
  1619. $this->assertHtml($expected, $result);
  1620. }
  1621. /**
  1622. * testFormSecurityControlDisabledFields method
  1623. *
  1624. * Test single record form with disabled fields.
  1625. *
  1626. * @return void
  1627. */
  1628. public function testFormSecurityControlUnlockedFields()
  1629. {
  1630. $this->Form->request->params['_Token'] = [
  1631. 'unlockedFields' => ['first_name', 'address']
  1632. ];
  1633. $this->Form->create();
  1634. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1635. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1636. $this->Form->text('Addresses.title');
  1637. $this->Form->text('Addresses.first_name');
  1638. $this->Form->text('Addresses.last_name');
  1639. $this->Form->text('Addresses.address');
  1640. $this->Form->text('Addresses.city');
  1641. $this->Form->text('Addresses.phone');
  1642. $result = $this->Form->fields;
  1643. $expected = [
  1644. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1645. 'Addresses.city', 'Addresses.phone'
  1646. ];
  1647. $this->assertEquals($expected, $result);
  1648. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1649. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1650. $tokenDebug = urlencode(json_encode([
  1651. '/articles/add',
  1652. [
  1653. 'Addresses.id' => '123456',
  1654. 'Addresses.title',
  1655. 'Addresses.last_name',
  1656. 'Addresses.city',
  1657. 'Addresses.phone'
  1658. ],
  1659. [
  1660. 'first_name',
  1661. 'address'
  1662. ]
  1663. ]));
  1664. $expected = [
  1665. 'div' => ['style' => 'display:none;'],
  1666. ['input' => [
  1667. 'type' => 'hidden',
  1668. 'name' => '_Token[fields]',
  1669. 'value' => $hash,
  1670. 'autocomplete' => 'off',
  1671. 'data-foo' => 'bar',
  1672. ]],
  1673. ['input' => [
  1674. 'type' => 'hidden',
  1675. 'name' => '_Token[unlocked]',
  1676. 'value' => 'address%7Cfirst_name',
  1677. 'autocomplete' => 'off',
  1678. 'data-foo' => 'bar',
  1679. ]],
  1680. ['input' => [
  1681. 'type' => 'hidden', 'name' => '_Token[debug]',
  1682. 'value' => $tokenDebug,
  1683. 'autocomplete' => 'off',
  1684. 'data-foo' => 'bar'
  1685. ]],
  1686. '/div'
  1687. ];
  1688. $this->assertHtml($expected, $result);
  1689. }
  1690. /**
  1691. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1692. *
  1693. * Test single record form with debugSecurity param.
  1694. *
  1695. * @return void
  1696. */
  1697. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1698. {
  1699. $this->Form->request->params['_Token'] = [
  1700. 'unlockedFields' => ['first_name', 'address']
  1701. ];
  1702. $this->Form->create();
  1703. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1704. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1705. $this->Form->text('Addresses.title');
  1706. $this->Form->text('Addresses.first_name');
  1707. $this->Form->text('Addresses.last_name');
  1708. $this->Form->text('Addresses.address');
  1709. $this->Form->text('Addresses.city');
  1710. $this->Form->text('Addresses.phone');
  1711. $result = $this->Form->fields;
  1712. $expected = [
  1713. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1714. 'Addresses.city', 'Addresses.phone'
  1715. ];
  1716. $this->assertEquals($expected, $result);
  1717. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1718. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1719. $tokenDebug = urlencode(json_encode([
  1720. '/articles/add',
  1721. [
  1722. 'Addresses.id' => '123456',
  1723. 'Addresses.title',
  1724. 'Addresses.last_name',
  1725. 'Addresses.city',
  1726. 'Addresses.phone'
  1727. ],
  1728. [
  1729. 'first_name',
  1730. 'address'
  1731. ]
  1732. ]));
  1733. $expected = [
  1734. 'div' => ['style' => 'display:none;'],
  1735. ['input' => [
  1736. 'type' => 'hidden',
  1737. 'name' => '_Token[fields]',
  1738. 'value' => $hash,
  1739. 'autocomplete' => 'off',
  1740. 'data-foo' => 'bar',
  1741. ]],
  1742. ['input' => [
  1743. 'type' => 'hidden',
  1744. 'name' => '_Token[unlocked]',
  1745. 'value' => 'address%7Cfirst_name',
  1746. 'autocomplete' => 'off',
  1747. 'data-foo' => 'bar',
  1748. ]],
  1749. ['input' => [
  1750. 'type' => 'hidden', 'name' => '_Token[debug]',
  1751. 'value' => $tokenDebug,
  1752. 'autocomplete' => 'off',
  1753. 'data-foo' => 'bar'
  1754. ]],
  1755. '/div'
  1756. ];
  1757. $this->assertHtml($expected, $result);
  1758. }
  1759. /**
  1760. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1761. *
  1762. * Debug is false, debugSecurity is true -> no debug
  1763. *
  1764. * @return void
  1765. */
  1766. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1767. {
  1768. $this->Form->request->params['_Token'] = [
  1769. 'unlockedFields' => ['first_name', 'address']
  1770. ];
  1771. $this->Form->create();
  1772. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1773. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1774. $this->Form->text('Addresses.title');
  1775. $this->Form->text('Addresses.first_name');
  1776. $this->Form->text('Addresses.last_name');
  1777. $this->Form->text('Addresses.address');
  1778. $this->Form->text('Addresses.city');
  1779. $this->Form->text('Addresses.phone');
  1780. $result = $this->Form->fields;
  1781. $expected = [
  1782. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1783. 'Addresses.city', 'Addresses.phone'
  1784. ];
  1785. $this->assertEquals($expected, $result);
  1786. Configure::write('debug', false);
  1787. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1788. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1789. $expected = [
  1790. 'div' => ['style' => 'display:none;'],
  1791. ['input' => [
  1792. 'type' => 'hidden',
  1793. 'name' => '_Token[fields]',
  1794. 'value' => $hash,
  1795. 'autocomplete' => 'off',
  1796. 'data-foo' => 'bar',
  1797. ]],
  1798. ['input' => [
  1799. 'type' => 'hidden',
  1800. 'name' => '_Token[unlocked]',
  1801. 'value' => 'address%7Cfirst_name',
  1802. 'autocomplete' => 'off',
  1803. 'data-foo' => 'bar',
  1804. ]],
  1805. '/div'
  1806. ];
  1807. $this->assertHtml($expected, $result);
  1808. }
  1809. /**
  1810. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1811. *
  1812. * Test single record form with debugSecurity param.
  1813. *
  1814. * @return void
  1815. */
  1816. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1817. {
  1818. $this->Form->request->params['_Token'] = [
  1819. 'unlockedFields' => ['first_name', 'address']
  1820. ];
  1821. $this->Form->create();
  1822. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1823. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1824. $this->Form->text('Addresses.title');
  1825. $this->Form->text('Addresses.first_name');
  1826. $this->Form->text('Addresses.last_name');
  1827. $this->Form->text('Addresses.address');
  1828. $this->Form->text('Addresses.city');
  1829. $this->Form->text('Addresses.phone');
  1830. $result = $this->Form->fields;
  1831. $expected = [
  1832. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1833. 'Addresses.city', 'Addresses.phone'
  1834. ];
  1835. $this->assertEquals($expected, $result);
  1836. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1837. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1838. $expected = [
  1839. 'div' => ['style' => 'display:none;'],
  1840. ['input' => [
  1841. 'type' => 'hidden',
  1842. 'name' => '_Token[fields]',
  1843. 'value' => $hash,
  1844. 'autocomplete' => 'off',
  1845. 'data-foo' => 'bar',
  1846. ]],
  1847. ['input' => [
  1848. 'type' => 'hidden',
  1849. 'name' => '_Token[unlocked]',
  1850. 'value' => 'address%7Cfirst_name',
  1851. 'autocomplete' => 'off',
  1852. 'data-foo' => 'bar',
  1853. ]],
  1854. '/div'
  1855. ];
  1856. $this->assertHtml($expected, $result);
  1857. }
  1858. /**
  1859. * testFormSecureWithCustomNameAttribute method
  1860. *
  1861. * Test securing inputs with custom name attributes.
  1862. *
  1863. * @return void
  1864. */
  1865. public function testFormSecureWithCustomNameAttribute()
  1866. {
  1867. $this->Form->request->params['_Token'] = 'testKey';
  1868. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1869. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1870. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1871. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1872. }
  1873. /**
  1874. * testFormSecuredControl method
  1875. *
  1876. * Test generation of entire secure form, assertions made on control() output.
  1877. *
  1878. * @return void
  1879. */
  1880. public function testFormSecuredControl()
  1881. {
  1882. $this->Form->request->params['_csrfToken'] = 'testKey';
  1883. $this->Form->request->params['_Token'] = 'stuff';
  1884. $this->article['schema'] = [
  1885. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1886. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1887. ];
  1888. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1889. $encoding = strtolower(Configure::read('App.encoding'));
  1890. $expected = [
  1891. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1892. 'div' => ['style' => 'display:none;'],
  1893. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1894. ['input' => [
  1895. 'type' => 'hidden',
  1896. 'name' => '_csrfToken',
  1897. 'value' => 'testKey',
  1898. 'autocomplete' => 'off'
  1899. ]],
  1900. '/div'
  1901. ];
  1902. $this->assertHtml($expected, $result);
  1903. $result = $this->Form->control('ratio');
  1904. $expected = [
  1905. 'div' => ['class'],
  1906. 'label' => ['for'],
  1907. 'Ratio',
  1908. '/label',
  1909. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1910. '/div',
  1911. ];
  1912. $this->assertHtml($expected, $result);
  1913. $result = $this->Form->control('population');
  1914. $expected = [
  1915. 'div' => ['class'],
  1916. 'label' => ['for'],
  1917. 'Population',
  1918. '/label',
  1919. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1920. '/div',
  1921. ];
  1922. $this->assertHtml($expected, $result);
  1923. $result = $this->Form->control('published', ['type' => 'text']);
  1924. $expected = [
  1925. 'div' => ['class' => 'input text'],
  1926. 'label' => ['for' => 'published'],
  1927. 'Published',
  1928. '/label',
  1929. ['input' => [
  1930. 'type' => 'text',
  1931. 'name' => 'published',
  1932. 'id' => 'published'
  1933. ]],
  1934. '/div'
  1935. ];
  1936. $this->assertHtml($expected, $result);
  1937. $result = $this->Form->control('other', ['type' => 'text']);
  1938. $expected = [
  1939. 'div' => ['class' => 'input text'],
  1940. 'label' => ['for' => 'other'],
  1941. 'Other',
  1942. '/label',
  1943. ['input' => [
  1944. 'type' => 'text',
  1945. 'name' => 'other',
  1946. 'id',
  1947. ]],
  1948. '/div'
  1949. ];
  1950. $this->assertHtml($expected, $result);
  1951. $result = $this->Form->hidden('stuff');
  1952. $expected = [
  1953. 'input' => [
  1954. 'type' => 'hidden',
  1955. 'name' => 'stuff'
  1956. ]
  1957. ];
  1958. $this->assertHtml($expected, $result);
  1959. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1960. $expected = ['input' => [
  1961. 'type' => 'hidden',
  1962. 'name' => 'hidden',
  1963. 'value' => '0'
  1964. ]];
  1965. $this->assertHtml($expected, $result);
  1966. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1967. $expected = [
  1968. 'div' => ['class' => 'input checkbox'],
  1969. ['input' => [
  1970. 'type' => 'hidden',
  1971. 'name' => 'something',
  1972. 'value' => '0'
  1973. ]],
  1974. 'label' => ['for' => 'something'],
  1975. ['input' => [
  1976. 'type' => 'checkbox',
  1977. 'name' => 'something',
  1978. 'value' => '1',
  1979. 'id' => 'something'
  1980. ]],
  1981. 'Something',
  1982. '/label',
  1983. '/div'
  1984. ];
  1985. $this->assertHtml($expected, $result);
  1986. $result = $this->Form->fields;
  1987. $expectedFields = [
  1988. 'ratio',
  1989. 'population',
  1990. 'published',
  1991. 'other',
  1992. 'stuff' => '',
  1993. 'hidden' => '0',
  1994. 'something'
  1995. ];
  1996. $this->assertEquals($expectedFields, $result);
  1997. $result = $this->Form->secure($this->Form->fields);
  1998. $tokenDebug = urlencode(json_encode([
  1999. '/articles/add',
  2000. $expectedFields,
  2001. []
  2002. ]));
  2003. $expected = [
  2004. 'div' => ['style' => 'display:none;'],
  2005. ['input' => [
  2006. 'type' => 'hidden',
  2007. 'name' => '_Token[fields]',
  2008. 'value',
  2009. 'autocomplete'
  2010. ]],
  2011. ['input' => [
  2012. 'type' => 'hidden',
  2013. 'name' => '_Token[unlocked]',
  2014. 'value' => '',
  2015. 'autocomplete' => 'off'
  2016. ]],
  2017. ['input' => [
  2018. 'type' => 'hidden', 'name' => '_Token[debug]',
  2019. 'value' => $tokenDebug,
  2020. 'autocomplete' => 'off'
  2021. ]],
  2022. '/div'
  2023. ];
  2024. $this->assertHtml($expected, $result);
  2025. }
  2026. /**
  2027. * testSecuredControlCustomName method
  2028. *
  2029. * Test secured inputs with custom names.
  2030. *
  2031. * @return void
  2032. */
  2033. public function testSecuredControlCustomName()
  2034. {
  2035. $this->Form->request->params['_Token'] = 'testKey';
  2036. $this->assertEquals([], $this->Form->fields);
  2037. $this->Form->text('text_input', [
  2038. 'name' => 'Option[General.default_role]',
  2039. ]);
  2040. $expected = ['Option.General.default_role'];
  2041. $this->assertEquals($expected, $this->Form->fields);
  2042. $this->Form->select('select_box', [1, 2], [
  2043. 'name' => 'Option[General.select_role]',
  2044. ]);
  2045. $expected[] = 'Option.General.select_role';
  2046. $this->assertEquals($expected, $this->Form->fields);
  2047. $this->Form->text('other.things[]');
  2048. $expected[] = 'other.things';
  2049. $this->assertEquals($expected, $this->Form->fields);
  2050. }
  2051. /**
  2052. * testSecuredControlDuplicate method
  2053. *
  2054. * Test that a hidden field followed by a visible field
  2055. * undoes the hidden field locking.
  2056. *
  2057. * @return void
  2058. */
  2059. public function testSecuredControlDuplicate()
  2060. {
  2061. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2062. $this->assertEquals([], $this->Form->fields);
  2063. $this->Form->control('text_val', [
  2064. 'type' => 'hidden',
  2065. 'value' => 'some text',
  2066. ]);
  2067. $expected = ['text_val' => 'some text'];
  2068. $this->assertEquals($expected, $this->Form->fields);
  2069. $this->Form->control('text_val', [
  2070. 'type' => 'text',
  2071. ]);
  2072. $expected = ['text_val'];
  2073. $this->assertEquals($expected, $this->Form->fields);
  2074. }
  2075. /**
  2076. * testFormSecuredFileControl method
  2077. *
  2078. * Tests that the correct keys are added to the field hash index.
  2079. *
  2080. * @return void
  2081. */
  2082. public function testFormSecuredFileControl()
  2083. {
  2084. $this->assertEquals([], $this->Form->fields);
  2085. $this->Form->file('Attachment.file');
  2086. $expected = [
  2087. 'Attachment.file.name', 'Attachment.file.type',
  2088. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2089. 'Attachment.file.size'
  2090. ];
  2091. $this->assertEquals($expected, $this->Form->fields);
  2092. }
  2093. /**
  2094. * testFormSecuredMultipleSelect method
  2095. *
  2096. * Test that multiple selects keys are added to field hash.
  2097. *
  2098. * @return void
  2099. */
  2100. public function testFormSecuredMultipleSelect()
  2101. {
  2102. $this->Form->request->params['_csrfToken'] = 'testKey';
  2103. $this->assertEquals([], $this->Form->fields);
  2104. $options = ['1' => 'one', '2' => 'two'];
  2105. $this->Form->select('Model.select', $options);
  2106. $expected = ['Model.select'];
  2107. $this->assertEquals($expected, $this->Form->fields);
  2108. $this->Form->fields = [];
  2109. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2110. $this->assertEquals($expected, $this->Form->fields);
  2111. }
  2112. /**
  2113. * testFormSecuredRadio method
  2114. *
  2115. * @return void
  2116. */
  2117. public function testFormSecuredRadio()
  2118. {
  2119. $this->Form->request->params['_Token'] = 'testKey';
  2120. $this->assertEquals([], $this->Form->fields);
  2121. $options = ['1' => 'option1', '2' => 'option2'];
  2122. $this->Form->radio('Test.test', $options);
  2123. $expected = ['Test.test'];
  2124. $this->assertEquals($expected, $this->Form->fields);
  2125. $this->Form->radio('Test.all', $options, [
  2126. 'disabled' => ['option1', 'option2']
  2127. ]);
  2128. $expected = ['Test.test', 'Test.all' => ''];
  2129. $this->assertEquals($expected, $this->Form->fields);
  2130. $this->Form->radio('Test.some', $options, [
  2131. 'disabled' => ['option1']
  2132. ]);
  2133. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2134. $this->assertEquals($expected, $this->Form->fields);
  2135. }
  2136. /**
  2137. * testFormSecuredAndDisabledNotAssoc method
  2138. *
  2139. * Test that when disabled is in a list based attribute array it works.
  2140. *
  2141. * @return void
  2142. */
  2143. public function testFormSecuredAndDisabledNotAssoc()
  2144. {
  2145. $this->Form->request->params['_Token'] = 'testKey';
  2146. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2147. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2148. $this->Form->text('Model.text', ['disabled']);
  2149. $this->Form->textarea('Model.textarea', ['disabled']);
  2150. $this->Form->password('Model.password', ['disabled']);
  2151. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2152. $expected = [
  2153. 'Model.radio' => ''
  2154. ];
  2155. $this->assertEquals($expected, $this->Form->fields);
  2156. }
  2157. /**
  2158. * testFormSecuredAndDisabled method
  2159. *
  2160. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2161. * hashing.
  2162. *
  2163. * @return void
  2164. */
  2165. public function testFormSecuredAndDisabled()
  2166. {
  2167. $this->Form->request->params['_Token'] = 'testKey';
  2168. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2169. $this->Form->text('Model.text', ['disabled' => true]);
  2170. $this->Form->password('Model.text', ['disabled' => true]);
  2171. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2172. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2173. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2174. $this->Form->year('Model.year', ['disabled' => true]);
  2175. $this->Form->month('Model.month', ['disabled' => true]);
  2176. $this->Form->day('Model.day', ['disabled' => true]);
  2177. $this->Form->hour('Model.hour', ['disabled' => true]);
  2178. $this->Form->minute('Model.minute', ['disabled' => true]);
  2179. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2180. $expected = [
  2181. 'Model.radio' => ''
  2182. ];
  2183. $this->assertEquals($expected, $this->Form->fields);
  2184. }
  2185. /**
  2186. * testDisableSecurityUsingForm method
  2187. *
  2188. * @return void
  2189. */
  2190. public function testDisableSecurityUsingForm()
  2191. {
  2192. $this->Form->request->params['_Token'] = [
  2193. 'disabledFields' => []
  2194. ];
  2195. $this->Form->create();
  2196. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2197. $this->Form->text('Addresses.title');
  2198. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2199. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2200. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2201. $result = $this->Form->fields;
  2202. $expected = [
  2203. 'Addresses.id' => '123456', 'Addresses.title',
  2204. ];
  2205. $this->assertEquals($expected, $result);
  2206. }
  2207. /**
  2208. * testUnlockFieldAddsToList method
  2209. *
  2210. * Test disableField.
  2211. *
  2212. * @return void
  2213. */
  2214. public function testUnlockFieldAddsToList()
  2215. {
  2216. $this->Form->request->params['_Token'] = [
  2217. 'unlockedFields' => []
  2218. ];
  2219. $this->Form->unlockField('Contact.name');
  2220. $this->Form->text('Contact.name');
  2221. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2222. $this->assertEquals([], $this->Form->fields);
  2223. }
  2224. /**
  2225. * testUnlockFieldRemovingFromFields method
  2226. *
  2227. * Test unlockField removing from fields array.
  2228. *
  2229. * @return void
  2230. */
  2231. public function testUnlockFieldRemovingFromFields()
  2232. {
  2233. $this->Form->request->params['_Token'] = [
  2234. 'unlockedFields' => []
  2235. ];
  2236. $this->Form->create($this->article);
  2237. $this->Form->hidden('Article.id', ['value' => 1]);
  2238. $this->Form->text('Article.title');
  2239. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2240. $this->assertContains('Article.title', $this->Form->fields, 'Field should be secured.');
  2241. $this->Form->unlockField('Article.title');
  2242. $this->Form->unlockField('Article.id');
  2243. $this->assertEquals([], $this->Form->fields);
  2244. }
  2245. /**
  2246. * testResetUnlockFields method
  2247. *
  2248. * Test reset unlockFields, when create new form.
  2249. *
  2250. * @return void
  2251. */
  2252. public function testResetUnlockFields()
  2253. {
  2254. $this->Form->request->params['_Token'] = [
  2255. 'key' => 'testKey',
  2256. 'unlockedFields' => []
  2257. ];
  2258. $this->Form->unlockField('Contact.id');
  2259. $this->Form->create('Contact');
  2260. $this->Form->hidden('Contact.id', ['value' => 1]);
  2261. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2262. $this->Form->end();
  2263. $this->Form->create('Contact');
  2264. $this->Form->hidden('Contact.id', ['value' => 1]);
  2265. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2266. }
  2267. /**
  2268. * testSecuredFormUrlIgnoresHost method
  2269. *
  2270. * Test that only the path + query elements of a form's URL show up in their hash.
  2271. *
  2272. * @return void
  2273. */
  2274. public function testSecuredFormUrlIgnoresHost()
  2275. {
  2276. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2277. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2278. $this->Form->create($this->article, [
  2279. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2280. ]);
  2281. $result = $this->Form->secure();
  2282. $this->assertContains($expected, $result);
  2283. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2284. $result = $this->Form->secure();
  2285. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2286. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2287. $result = $this->Form->secure();
  2288. $this->assertContains($expected, $result, 'URL path + query should work.');
  2289. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2290. $result = $this->Form->secure();
  2291. $this->assertNotContains($expected, $result, 'URL is different');
  2292. }
  2293. /**
  2294. * testSecuredFormUrlHasHtmlAndIdentifier method
  2295. *
  2296. * Test that URL, HTML and identifier show up in their hashes.
  2297. *
  2298. * @return void
  2299. */
  2300. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2301. {
  2302. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2303. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2304. $this->Form->create($this->article, [
  2305. 'url' => [
  2306. 'controller' => 'articles',
  2307. 'action' => 'view',
  2308. '?' => [
  2309. 'page' => 1,
  2310. 'limit' => 10,
  2311. 'html' => '<>"',
  2312. ],
  2313. '#' => 'result',
  2314. ],
  2315. ]);
  2316. $result = $this->Form->secure();
  2317. $this->assertContains($expected, $result);
  2318. $this->Form->create($this->article, [
  2319. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2320. ]);
  2321. $result = $this->Form->secure();
  2322. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2323. $this->Form->create($this->article, [
  2324. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2325. ]);
  2326. $result = $this->Form->secure();
  2327. $this->assertContains($expected, $result, 'URL path + query should work.');
  2328. }
  2329. /**
  2330. * testErrorMessageDisplay method
  2331. *
  2332. * Test error message display.
  2333. *
  2334. * @return void
  2335. */
  2336. public function testErrorMessageDisplay()
  2337. {
  2338. $this->article['errors'] = [
  2339. 'Article' => [
  2340. 'title' => 'error message',
  2341. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2342. ]
  2343. ];
  2344. $this->Form->create($this->article);
  2345. $result = $this->Form->control('Article.title');
  2346. $expected = [
  2347. 'div' => ['class' => 'input text error'],
  2348. 'label' => ['for' => 'article-title'],
  2349. 'Title',
  2350. '/label',
  2351. 'input' => [
  2352. 'type' => 'text', 'name' => 'Article[title]',
  2353. 'id' => 'article-title', 'class' => 'form-error'
  2354. ],
  2355. ['div' => ['class' => 'error-message']],
  2356. 'error message',
  2357. '/div',
  2358. '/div'
  2359. ];
  2360. $this->assertHtml($expected, $result);
  2361. $result = $this->Form->control('Article.title', [
  2362. 'templates' => [
  2363. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2364. ]
  2365. ]);
  2366. $expected = [
  2367. 'div' => ['class' => 'input text error'],
  2368. 'label' => ['for' => 'article-title'],
  2369. 'Title',
  2370. '/label',
  2371. 'input' => [
  2372. 'type' => 'text', 'name' => 'Article[title]',
  2373. 'id' => 'article-title', 'class' => 'form-error'
  2374. ],
  2375. '/div'
  2376. ];
  2377. $this->assertHtml($expected, $result);
  2378. $result = $this->Form->control('Article.content');
  2379. $expected = [
  2380. 'div' => ['class' => 'input text error'],
  2381. 'label' => ['for' => 'article-content'],
  2382. 'Content',
  2383. '/label',
  2384. 'input' => [
  2385. 'type' => 'text', 'name' => 'Article[content]',
  2386. 'id' => 'article-content', 'class' => 'form-error'
  2387. ],
  2388. ['div' => ['class' => 'error-message']],
  2389. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2390. '/div',
  2391. '/div'
  2392. ];
  2393. $this->assertHtml($expected, $result);
  2394. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2395. $expected = [
  2396. 'div' => ['class' => 'input text error'],
  2397. 'label' => ['for' => 'article-content'],
  2398. 'Content',
  2399. '/label',
  2400. 'input' => [
  2401. 'type' => 'text', 'name' => 'Article[content]',
  2402. 'id' => 'article-content', 'class' => 'form-error'
  2403. ],
  2404. ['div' => ['class' => 'error-message']],
  2405. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2406. '/div',
  2407. '/div'
  2408. ];
  2409. $this->assertHtml($expected, $result);
  2410. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2411. $expected = [
  2412. 'div' => ['class' => 'input text error'],
  2413. 'label' => ['for' => 'article-content'],
  2414. 'Content',
  2415. '/label',
  2416. 'input' => [
  2417. 'type' => 'text', 'name' => 'Article[content]',
  2418. 'id' => 'article-content', 'class' => 'form-error'
  2419. ],
  2420. ['div' => ['class' => 'error-message']],
  2421. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2422. '/div',
  2423. '/div'
  2424. ];
  2425. $this->assertHtml($expected, $result);
  2426. }
  2427. /**
  2428. * testEmptyErrorValidation method
  2429. *
  2430. * Test validation errors, when validation message is an empty string.
  2431. *
  2432. * @return void
  2433. */
  2434. public function testEmptyErrorValidation()
  2435. {
  2436. $this->article['errors'] = [
  2437. 'Article' => ['title' => '']
  2438. ];
  2439. $this->Form->create($this->article);
  2440. $result = $this->Form->control('Article.title');
  2441. $expected = [
  2442. 'div' => ['class' => 'input text error'],
  2443. 'label' => ['for' => 'article-title'],
  2444. 'Title',
  2445. '/label',
  2446. 'input' => [
  2447. 'type' => 'text', 'name' => 'Article[title]',
  2448. 'id' => 'article-title', 'class' => 'form-error'
  2449. ],
  2450. ['div' => ['class' => 'error-message']],
  2451. [],
  2452. '/div',
  2453. '/div'
  2454. ];
  2455. $this->assertHtml($expected, $result);
  2456. }
  2457. /**
  2458. * testEmptyControlErrorValidation method
  2459. *
  2460. * Test validation errors, when calling control() overriding validation message by an empty string.
  2461. *
  2462. * @return void
  2463. */
  2464. public function testEmptyControlErrorValidation()
  2465. {
  2466. $this->article['errors'] = [
  2467. 'Article' => ['title' => 'error message']
  2468. ];
  2469. $this->Form->create($this->article);
  2470. $result = $this->Form->control('Article.title', ['error' => '']);
  2471. $expected = [
  2472. 'div' => ['class' => 'input text error'],
  2473. 'label' => ['for' => 'article-title'],
  2474. 'Title',
  2475. '/label',
  2476. 'input' => [
  2477. 'type' => 'text', 'name' => 'Article[title]',
  2478. 'id' => 'article-title', 'class' => 'form-error'
  2479. ],
  2480. ['div' => ['class' => 'error-message']],
  2481. [],
  2482. '/div',
  2483. '/div'
  2484. ];
  2485. $this->assertHtml($expected, $result);
  2486. }
  2487. /**
  2488. * testControlErrorMessage method
  2489. *
  2490. * Test validation errors, when calling control() overriding validation messages.
  2491. *
  2492. * @return void
  2493. */
  2494. public function testControlErrorMessage()
  2495. {
  2496. $this->article['errors'] = [
  2497. 'title' => ['error message']
  2498. ];
  2499. $this->Form->create($this->article);
  2500. $result = $this->Form->control('title', [
  2501. 'error' => 'Custom error!'
  2502. ]);
  2503. $expected = [
  2504. 'div' => ['class' => 'input text required error'],
  2505. 'label' => ['for' => 'title'],
  2506. 'Title',
  2507. '/label',
  2508. 'input' => [
  2509. 'type' => 'text', 'name' => 'title',
  2510. 'id' => 'title', 'class' => 'form-error',
  2511. 'required' => 'required',
  2512. ],
  2513. ['div' => ['class' => 'error-message']],
  2514. 'Custom error!',
  2515. '/div',
  2516. '/div'
  2517. ];
  2518. $this->assertHtml($expected, $result);
  2519. $result = $this->Form->control('title', [
  2520. 'error' => ['error message' => 'Custom error!']
  2521. ]);
  2522. $expected = [
  2523. 'div' => ['class' => 'input text required error'],
  2524. 'label' => ['for' => 'title'],
  2525. 'Title',
  2526. '/label',
  2527. 'input' => [
  2528. 'type' => 'text',
  2529. 'name' => 'title',
  2530. 'id' => 'title',
  2531. 'class' => 'form-error',
  2532. 'required' => 'required'
  2533. ],
  2534. ['div' => ['class' => 'error-message']],
  2535. 'Custom error!',
  2536. '/div',
  2537. '/div'
  2538. ];
  2539. $this->assertHtml($expected, $result);
  2540. }
  2541. /**
  2542. * testFormValidationAssociated method
  2543. *
  2544. * Tests displaying errors for nested entities.
  2545. *
  2546. * @return void
  2547. */
  2548. public function testFormValidationAssociated()
  2549. {
  2550. $nested = new Entity(['foo' => 'bar']);
  2551. $nested->setError('foo', ['not a valid bar']);
  2552. $entity = new Entity(['nested' => $nested]);
  2553. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2554. $result = $this->Form->error('nested.foo');
  2555. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2556. }
  2557. /**
  2558. * testFormValidationAssociatedSecondLevel method
  2559. *
  2560. * Test form error display with associated model.
  2561. *
  2562. * @return void
  2563. */
  2564. public function testFormValidationAssociatedSecondLevel()
  2565. {
  2566. $inner = new Entity(['bar' => 'baz']);
  2567. $nested = new Entity(['foo' => $inner]);
  2568. $entity = new Entity(['nested' => $nested]);
  2569. $inner->setError('bar', ['not a valid one']);
  2570. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2571. $result = $this->Form->error('nested.foo.bar');
  2572. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2573. }
  2574. /**
  2575. * testFormValidationMultiRecord method
  2576. *
  2577. * Test form error display with multiple records.
  2578. *
  2579. * @return void
  2580. */
  2581. public function testFormValidationMultiRecord()
  2582. {
  2583. $one = new Entity();
  2584. $two = new Entity();
  2585. TableRegistry::get('Contacts', [
  2586. 'className' => __NAMESPACE__ . '\ContactsTable'
  2587. ]);
  2588. $one->set('email', '');
  2589. $one->setError('email', ['invalid email']);
  2590. $two->set('name', '');
  2591. $two->setError('name', ['This is wrong']);
  2592. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2593. $result = $this->Form->control('0.email');
  2594. $expected = [
  2595. 'div' => ['class' => 'input email error'],
  2596. 'label' => ['for' => '0-email'],
  2597. 'Email',
  2598. '/label',
  2599. 'input' => [
  2600. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2601. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2602. ],
  2603. ['div' => ['class' => 'error-message']],
  2604. 'invalid email',
  2605. '/div',
  2606. '/div'
  2607. ];
  2608. $this->assertHtml($expected, $result);
  2609. $result = $this->Form->control('1.name');
  2610. $expected = [
  2611. 'div' => ['class' => 'input text error'],
  2612. 'label' => ['for' => '1-name'],
  2613. 'Name',
  2614. '/label',
  2615. 'input' => [
  2616. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2617. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2618. ],
  2619. ['div' => ['class' => 'error-message']],
  2620. 'This is wrong',
  2621. '/div',
  2622. '/div'
  2623. ];
  2624. $this->assertHtml($expected, $result);
  2625. }
  2626. /**
  2627. * testControl method
  2628. *
  2629. * Test various incarnations of control().
  2630. *
  2631. * @return void
  2632. */
  2633. public function testControl()
  2634. {
  2635. TableRegistry::get('ValidateUsers', [
  2636. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2637. ]);
  2638. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2639. $result = $this->Form->control('ValidateUsers.balance');
  2640. $expected = [
  2641. 'div' => ['class'],
  2642. 'label' => ['for'],
  2643. 'Balance',
  2644. '/label',
  2645. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2646. '/div',
  2647. ];
  2648. $this->assertHtml($expected, $result);
  2649. $result = $this->Form->control('ValidateUser.cost_decimal');
  2650. $expected = [
  2651. 'div' => ['class'],
  2652. 'label' => ['for'],
  2653. 'Cost Decimal',
  2654. '/label',
  2655. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2656. '/div',
  2657. ];
  2658. $this->assertHtml($expected, $result);
  2659. $result = $this->Form->control('ValidateUser.null_decimal');
  2660. $expected = [
  2661. 'div' => ['class'],
  2662. 'label' => ['for'],
  2663. 'Null Decimal',
  2664. '/label',
  2665. 'input' => ['name', 'type' => 'number', 'id'],
  2666. '/div',
  2667. ];
  2668. $this->assertHtml($expected, $result);
  2669. }
  2670. /**
  2671. * testControlCustomization method
  2672. *
  2673. * Tests the input method and passing custom options.
  2674. *
  2675. * @return void
  2676. */
  2677. public function testControlCustomization()
  2678. {
  2679. TableRegistry::get('Contacts', [
  2680. 'className' => __NAMESPACE__ . '\ContactsTable'
  2681. ]);
  2682. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2683. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2684. $expected = [
  2685. 'div' => ['class' => 'input email'],
  2686. 'label' => ['for' => 'custom'],
  2687. 'Email',
  2688. '/label',
  2689. ['input' => [
  2690. 'type' => 'email', 'name' => 'Contact[email]',
  2691. 'id' => 'custom', 'maxlength' => 255
  2692. ]],
  2693. '/div'
  2694. ];
  2695. $this->assertHtml($expected, $result);
  2696. $result = $this->Form->control('Contact.email', [
  2697. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2698. ]);
  2699. $expected = [
  2700. '<div',
  2701. 'label' => ['for' => 'contact-email'],
  2702. 'Email',
  2703. '/label',
  2704. ['input' => [
  2705. 'type' => 'email', 'name' => 'Contact[email]',
  2706. 'id' => 'contact-email', 'maxlength' => 255
  2707. ]],
  2708. '/div'
  2709. ];
  2710. $this->assertHtml($expected, $result);
  2711. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2712. $expected = [
  2713. 'div' => ['class' => 'input text'],
  2714. 'label' => ['for' => 'contact-email'],
  2715. 'Email',
  2716. '/label',
  2717. ['input' => [
  2718. 'type' => 'text', 'name' => 'Contact[email]',
  2719. 'id' => 'contact-email', 'maxlength' => '255'
  2720. ]],
  2721. '/div'
  2722. ];
  2723. $this->assertHtml($expected, $result);
  2724. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2725. $expected = [
  2726. 'div' => ['class' => 'input text'],
  2727. 'label' => ['for' => 'contact-5-email'],
  2728. 'Email',
  2729. '/label',
  2730. ['input' => [
  2731. 'type' => 'text', 'name' => 'Contact[5][email]',
  2732. 'id' => 'contact-5-email', 'maxlength' => '255'
  2733. ]],
  2734. '/div'
  2735. ];
  2736. $this->assertHtml($expected, $result);
  2737. $result = $this->Form->control('Contact.password');
  2738. $expected = [
  2739. 'div' => ['class' => 'input password'],
  2740. 'label' => ['for' => 'contact-password'],
  2741. 'Password',
  2742. '/label',
  2743. ['input' => [
  2744. 'type' => 'password', 'name' => 'Contact[password]',
  2745. 'id' => 'contact-password'
  2746. ]],
  2747. '/div'
  2748. ];
  2749. $this->assertHtml($expected, $result);
  2750. $result = $this->Form->control('Contact.email', [
  2751. 'type' => 'file', 'class' => 'textbox'
  2752. ]);
  2753. $expected = [
  2754. 'div' => ['class' => 'input file'],
  2755. 'label' => ['for' => 'contact-email'],
  2756. 'Email',
  2757. '/label',
  2758. ['input' => [
  2759. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2760. 'id' => 'contact-email'
  2761. ]],
  2762. '/div'
  2763. ];
  2764. $this->assertHtml($expected, $result);
  2765. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2766. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2767. $result = $this->Form->control('phone');
  2768. $expected = [
  2769. 'div' => ['class' => 'input tel'],
  2770. 'label' => ['for' => 'phone'],
  2771. 'Phone',
  2772. '/label',
  2773. ['input' => [
  2774. 'type' => 'tel', 'name' => 'phone',
  2775. 'value' => 'Hello &amp; World &gt; weird chars',
  2776. 'id' => 'phone', 'maxlength' => 255
  2777. ]],
  2778. '/div'
  2779. ];
  2780. $this->assertHtml($expected, $result);
  2781. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2782. $this->Form->create();
  2783. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2784. $expected = [
  2785. 'div' => ['class' => 'input text'],
  2786. 'label' => ['for' => 'myId'],
  2787. 'Field',
  2788. '/label',
  2789. 'input' => [
  2790. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2791. 'value' => 'My value', 'id' => 'myId'
  2792. ],
  2793. '/div'
  2794. ];
  2795. $this->assertHtml($expected, $result);
  2796. $this->Form->request->data = [];
  2797. $entity->setError('field', 'Badness!');
  2798. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2799. $result = $this->Form->control('field');
  2800. $expected = [
  2801. 'div' => ['class' => 'input text error'],
  2802. 'label' => ['for' => 'field'],
  2803. 'Field',
  2804. '/label',
  2805. 'input' => [
  2806. 'type' => 'text', 'name' => 'field',
  2807. 'id' => 'field', 'class' => 'form-error'
  2808. ],
  2809. ['div' => ['class' => 'error-message']],
  2810. 'Badness!',
  2811. '/div',
  2812. '/div'
  2813. ];
  2814. $this->assertHtml($expected, $result);
  2815. $result = $this->Form->control('field', [
  2816. 'templates' => [
  2817. 'inputContainerError' => '{{content}}{{error}}',
  2818. 'error' => '<span class="error-message">{{content}}</span>'
  2819. ]
  2820. ]);
  2821. $expected = [
  2822. 'label' => ['for' => 'field'],
  2823. 'Field',
  2824. '/label',
  2825. 'input' => [
  2826. 'type' => 'text', 'name' => 'field',
  2827. 'id' => 'field', 'class' => 'form-error'
  2828. ],
  2829. ['span' => ['class' => 'error-message']],
  2830. 'Badness!',
  2831. '/span'
  2832. ];
  2833. $this->assertHtml($expected, $result);
  2834. $entity->setError('field', ['minLength'], true);
  2835. $result = $this->Form->control('field', [
  2836. 'error' => [
  2837. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2838. 'maxLength' => 'login too large'
  2839. ]
  2840. ]);
  2841. $expected = [
  2842. 'div' => ['class' => 'input text error'],
  2843. 'label' => ['for' => 'field'],
  2844. 'Field',
  2845. '/label',
  2846. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2847. ['div' => ['class' => 'error-message']],
  2848. 'Le login doit contenir au moins 2 caractères',
  2849. '/div',
  2850. '/div'
  2851. ];
  2852. $this->assertHtml($expected, $result);
  2853. $entity->setError('field', ['maxLength'], true);
  2854. $result = $this->Form->control('field', [
  2855. 'error' => [
  2856. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2857. 'maxLength' => 'login too large',
  2858. ]
  2859. ]);
  2860. $expected = [
  2861. 'div' => ['class' => 'input text error'],
  2862. 'label' => ['for' => 'field'],
  2863. 'Field',
  2864. '/label',
  2865. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2866. ['div' => ['class' => 'error-message']],
  2867. 'login too large',
  2868. '/div',
  2869. '/div'
  2870. ];
  2871. $this->assertHtml($expected, $result);
  2872. }
  2873. /**
  2874. * testControlWithTemplateFile method
  2875. *
  2876. * Test that control() accepts a template file.
  2877. *
  2878. * @return void
  2879. */
  2880. public function testControlWithTemplateFile()
  2881. {
  2882. $result = $this->Form->control('field', [
  2883. 'templates' => 'htmlhelper_tags'
  2884. ]);
  2885. $expected = [
  2886. 'label' => ['for' => 'field'],
  2887. 'Field',
  2888. '/label',
  2889. 'input' => [
  2890. 'type' => 'text', 'name' => 'field',
  2891. 'id' => 'field'
  2892. ],
  2893. ];
  2894. $this->assertHtml($expected, $result);
  2895. }
  2896. /**
  2897. * testNestedControlsEndWithBrackets method
  2898. *
  2899. * Test that nested inputs end with brackets.
  2900. *
  2901. * @return void
  2902. */
  2903. public function testNestedControlsEndWithBrackets()
  2904. {
  2905. $result = $this->Form->text('nested.text[]');
  2906. $expected = [
  2907. 'input' => [
  2908. 'type' => 'text', 'name' => 'nested[text][]'
  2909. ],
  2910. ];
  2911. $this->assertHtml($expected, $result);
  2912. $result = $this->Form->file('nested.file[]');
  2913. $expected = [
  2914. 'input' => [
  2915. 'type' => 'file', 'name' => 'nested[file][]'
  2916. ],
  2917. ];
  2918. $this->assertHtml($expected, $result);
  2919. }
  2920. /**
  2921. * testCreateIdPrefix method
  2922. *
  2923. * Test id prefix.
  2924. *
  2925. * @return void
  2926. */
  2927. public function testCreateIdPrefix()
  2928. {
  2929. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2930. $result = $this->Form->control('field');
  2931. $expected = [
  2932. 'div' => ['class' => 'input text'],
  2933. 'label' => ['for' => 'prefix-field'],
  2934. 'Field',
  2935. '/label',
  2936. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2937. '/div'
  2938. ];
  2939. $this->assertHtml($expected, $result);
  2940. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2941. $expected = [
  2942. 'div' => ['class' => 'input text'],
  2943. 'label' => ['for' => 'custom-id'],
  2944. 'Field',
  2945. '/label',
  2946. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2947. '/div'
  2948. ];
  2949. $this->assertHtml($expected, $result);
  2950. $result = $this->Form->radio('Model.field', ['option A']);
  2951. $expected = [
  2952. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2953. 'label' => ['for' => 'prefix-model-field-0'],
  2954. ['input' => [
  2955. 'type' => 'radio',
  2956. 'name' => 'Model[field]',
  2957. 'value' => '0',
  2958. 'id' => 'prefix-model-field-0'
  2959. ]],
  2960. 'option A',
  2961. '/label'
  2962. ];
  2963. $this->assertHtml($expected, $result);
  2964. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2965. $expected = [
  2966. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2967. 'label' => ['for' => 'prefix-model-field-0'],
  2968. ['input' => [
  2969. 'type' => 'radio',
  2970. 'name' => 'Model[field]',
  2971. 'value' => '0',
  2972. 'id' => 'prefix-model-field-0'
  2973. ]],
  2974. 'option A',
  2975. '/label'
  2976. ];
  2977. $this->assertHtml($expected, $result);
  2978. $result = $this->Form->select(
  2979. 'Model.multi_field',
  2980. ['first'],
  2981. ['multiple' => 'checkbox']
  2982. );
  2983. $expected = [
  2984. 'input' => [
  2985. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2986. ],
  2987. ['div' => ['class' => 'checkbox']],
  2988. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2989. ['input' => [
  2990. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2991. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2992. ]],
  2993. 'first',
  2994. '/label',
  2995. '/div',
  2996. ];
  2997. $this->assertHtml($expected, $result);
  2998. $this->Form->end();
  2999. $result = $this->Form->control('field');
  3000. $expected = [
  3001. 'div' => ['class' => 'input text'],
  3002. 'label' => ['for' => 'field'],
  3003. 'Field',
  3004. '/label',
  3005. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  3006. '/div'
  3007. ];
  3008. $this->assertHtml($expected, $result);
  3009. }
  3010. /**
  3011. * testControlZero method
  3012. *
  3013. * Test that inputs with 0 can be created.
  3014. *
  3015. * @return void
  3016. */
  3017. public function testControlZero()
  3018. {
  3019. TableRegistry::get('Contacts', [
  3020. 'className' => __NAMESPACE__ . '\ContactsTable'
  3021. ]);
  3022. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  3023. $result = $this->Form->control('0');
  3024. $expected = [
  3025. 'div' => ['class' => 'input text'],
  3026. 'label' => ['for' => '0'], '/label',
  3027. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  3028. '/div'
  3029. ];
  3030. $this->assertHtml($expected, $result);
  3031. }
  3032. /**
  3033. * testControlCheckbox method
  3034. *
  3035. * Test control() with checkbox creation.
  3036. *
  3037. * @return void
  3038. */
  3039. public function testControlCheckbox()
  3040. {
  3041. $articles = TableRegistry::get('Articles');
  3042. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3043. $article = $articles->newEntity();
  3044. $this->Form->create($article);
  3045. $result = $this->Form->control('Articles.active');
  3046. $expected = [
  3047. 'div' => ['class' => 'input checkbox'],
  3048. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3049. 'label' => ['for' => 'articles-active'],
  3050. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3051. 'Active',
  3052. '/label',
  3053. '/div'
  3054. ];
  3055. $this->assertHtml($expected, $result);
  3056. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3057. $expected = [
  3058. 'div' => ['class' => 'input checkbox'],
  3059. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3060. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3061. '/div'
  3062. ];
  3063. $this->assertHtml($expected, $result);
  3064. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3065. $expected = [
  3066. 'div' => ['class' => 'input checkbox'],
  3067. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3068. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3069. '/div'
  3070. ];
  3071. $this->assertHtml($expected, $result);
  3072. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3073. $expected = [
  3074. 'div' => ['class' => 'input checkbox'],
  3075. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3076. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3077. '/div'
  3078. ];
  3079. $this->assertHtml($expected, $result);
  3080. $result = $this->Form->control('Articles.disabled', [
  3081. 'label' => 'Disabled',
  3082. 'type' => 'checkbox',
  3083. 'data-foo' => 'disabled'
  3084. ]);
  3085. $expected = [
  3086. 'div' => ['class' => 'input checkbox'],
  3087. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3088. 'label' => ['for' => 'articles-disabled'],
  3089. ['input' => [
  3090. 'type' => 'checkbox',
  3091. 'name' => 'Articles[disabled]',
  3092. 'value' => '1',
  3093. 'id' => 'articles-disabled',
  3094. 'data-foo' => 'disabled'
  3095. ]],
  3096. 'Disabled',
  3097. '/label',
  3098. '/div'
  3099. ];
  3100. $this->assertHtml($expected, $result);
  3101. $result = $this->Form->control('Articles.confirm', [
  3102. 'label' => 'Confirm <b>me</b>!',
  3103. 'type' => 'checkbox',
  3104. 'escape' => false
  3105. ]);
  3106. $expected = [
  3107. 'div' => ['class' => 'input checkbox'],
  3108. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3109. 'label' => ['for' => 'articles-confirm'],
  3110. ['input' => [
  3111. 'type' => 'checkbox',
  3112. 'name' => 'Articles[confirm]',
  3113. 'value' => '1',
  3114. 'id' => 'articles-confirm',
  3115. ]],
  3116. 'Confirm <b>me</b>!',
  3117. '/label',
  3118. '/div'
  3119. ];
  3120. $this->assertHtml($expected, $result);
  3121. }
  3122. /**
  3123. * testControlHidden method
  3124. *
  3125. * Test that control() does not create wrapping div and label tag for hidden fields.
  3126. *
  3127. * @return void
  3128. */
  3129. public function testControlHidden()
  3130. {
  3131. TableRegistry::get('ValidateUsers', [
  3132. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3133. ]);
  3134. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3135. $result = $this->Form->control('ValidateUser.id');
  3136. $expected = [
  3137. 'input' => ['name', 'type' => 'hidden', 'id']
  3138. ];
  3139. $this->assertHtml($expected, $result);
  3140. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3141. $expected = [
  3142. 'input' => ['name', 'type' => 'hidden', 'id']
  3143. ];
  3144. $this->assertHtml($expected, $result);
  3145. }
  3146. /**
  3147. * testControlDatetime method
  3148. *
  3149. * Test form->control() with datetime.
  3150. *
  3151. * @return void
  3152. */
  3153. public function testControlDatetime()
  3154. {
  3155. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3156. ->setMethods(['datetime'])
  3157. ->setConstructorArgs([new View()])
  3158. ->getMock();
  3159. $this->Form->expects($this->once())->method('datetime')
  3160. ->with('prueba', [
  3161. 'type' => 'datetime',
  3162. 'timeFormat' => 24,
  3163. 'minYear' => 2008,
  3164. 'maxYear' => 2011,
  3165. 'interval' => 15,
  3166. 'options' => null,
  3167. 'empty' => false,
  3168. 'id' => 'prueba',
  3169. 'required' => false,
  3170. 'templateVars' => []
  3171. ])
  3172. ->will($this->returnValue('This is it!'));
  3173. $result = $this->Form->control('prueba', [
  3174. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3175. 'maxYear' => 2011, 'interval' => 15
  3176. ]);
  3177. $expected = [
  3178. 'div' => ['class' => 'input datetime'],
  3179. '<label',
  3180. 'Prueba',
  3181. '/label',
  3182. 'This is it!',
  3183. '/div'
  3184. ];
  3185. $this->assertHtml($expected, $result);
  3186. }
  3187. /**
  3188. * testControlDatetimeIdPrefix method
  3189. *
  3190. * Test form->control() with datetime with id prefix.
  3191. *
  3192. * @return void
  3193. */
  3194. public function testControlDatetimeIdPrefix()
  3195. {
  3196. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3197. ->setMethods(['datetime'])
  3198. ->setConstructorArgs([new View()])
  3199. ->getMock();
  3200. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3201. $this->Form->expects($this->once())->method('datetime')
  3202. ->with('prueba', [
  3203. 'type' => 'datetime',
  3204. 'timeFormat' => 24,
  3205. 'minYear' => 2008,
  3206. 'maxYear' => 2011,
  3207. 'interval' => 15,
  3208. 'options' => null,
  3209. 'empty' => false,
  3210. 'id' => 'prefix-prueba',
  3211. 'required' => false,
  3212. 'templateVars' => []
  3213. ])
  3214. ->will($this->returnValue('This is it!'));
  3215. $result = $this->Form->control('prueba', [
  3216. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3217. 'maxYear' => 2011, 'interval' => 15
  3218. ]);
  3219. $expected = [
  3220. 'div' => ['class' => 'input datetime'],
  3221. '<label',
  3222. 'Prueba',
  3223. '/label',
  3224. 'This is it!',
  3225. '/div'
  3226. ];
  3227. $this->assertHtml($expected, $result);
  3228. }
  3229. /**
  3230. * testControlCheckboxWithDisabledElements method
  3231. *
  3232. * Test generating checkboxes with disabled elements.
  3233. *
  3234. * @return void
  3235. */
  3236. public function testControlCheckboxWithDisabledElements()
  3237. {
  3238. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3239. $result = $this->Form->control('Contact.multiple', [
  3240. 'multiple' => 'checkbox',
  3241. 'disabled' => 'disabled',
  3242. 'options' => $options
  3243. ]);
  3244. $expected = [
  3245. ['div' => ['class' => 'input select']],
  3246. ['label' => ['for' => 'contact-multiple']],
  3247. 'Multiple',
  3248. '/label',
  3249. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3250. ['div' => ['class' => 'checkbox']],
  3251. ['label' => ['for' => 'contact-multiple-1']],
  3252. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3253. 'One',
  3254. '/label',
  3255. '/div',
  3256. ['div' => ['class' => 'checkbox']],
  3257. ['label' => ['for' => 'contact-multiple-2']],
  3258. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3259. 'Two',
  3260. '/label',
  3261. '/div',
  3262. ['div' => ['class' => 'checkbox']],
  3263. ['label' => ['for' => 'contact-multiple-3']],
  3264. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3265. 'Three',
  3266. '/label',
  3267. '/div',
  3268. '/div'
  3269. ];
  3270. $this->assertHtml($expected, $result);
  3271. // make sure 50 does only disable 50, and not 50f5c0cf
  3272. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3273. $disabled = [50];
  3274. $expected = [
  3275. ['div' => ['class' => 'input select']],
  3276. ['label' => ['for' => 'contact-multiple']],
  3277. 'Multiple',
  3278. '/label',
  3279. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3280. ['div' => ['class' => 'checkbox']],
  3281. ['label' => ['for' => 'contact-multiple-50']],
  3282. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3283. 'Fifty',
  3284. '/label',
  3285. '/div',
  3286. ['div' => ['class' => 'checkbox']],
  3287. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3288. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3289. 'Stringy',
  3290. '/label',
  3291. '/div',
  3292. '/div'
  3293. ];
  3294. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3295. $this->assertHtml($expected, $result);
  3296. }
  3297. /**
  3298. * testControlWithLeadingInteger method
  3299. *
  3300. * Test input name with leading integer, ensure attributes are generated correctly.
  3301. *
  3302. * @return void
  3303. */
  3304. public function testControlWithLeadingInteger()
  3305. {
  3306. $result = $this->Form->text('0.Node.title');
  3307. $expected = [
  3308. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3309. ];
  3310. $this->assertHtml($expected, $result);
  3311. }
  3312. /**
  3313. * testControlSelectType method
  3314. *
  3315. * Test form->control() with select type inputs.
  3316. *
  3317. * @return void
  3318. */
  3319. public function testControlSelectType()
  3320. {
  3321. $result = $this->Form->control(
  3322. 'email',
  3323. [
  3324. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3325. );
  3326. $expected = [
  3327. 'div' => ['class' => 'input select'],
  3328. 'label' => ['for' => 'email'],
  3329. 'Email',
  3330. '/label',
  3331. ['select' => ['name' => 'email', 'id' => 'email']],
  3332. ['option' => ['value' => '']],
  3333. '/option',
  3334. ['option' => ['value' => 'è']],
  3335. 'Firést',
  3336. '/option',
  3337. ['option' => ['value' => 'é']],
  3338. 'Secoènd',
  3339. '/option',
  3340. '/select',
  3341. '/div'
  3342. ];
  3343. $this->assertHtml($expected, $result);
  3344. $result = $this->Form->control(
  3345. 'email',
  3346. [
  3347. 'options' => ['First', 'Second'], 'empty' => true]
  3348. );
  3349. $expected = [
  3350. 'div' => ['class' => 'input select'],
  3351. 'label' => ['for' => 'email'],
  3352. 'Email',
  3353. '/label',
  3354. ['select' => ['name' => 'email', 'id' => 'email']],
  3355. ['option' => ['value' => '']],
  3356. '/option',
  3357. ['option' => ['value' => '0']],
  3358. 'First',
  3359. '/option',
  3360. ['option' => ['value' => '1']],
  3361. 'Second',
  3362. '/option',
  3363. '/select',
  3364. '/div'
  3365. ];
  3366. $this->assertHtml($expected, $result);
  3367. $result = $this->Form->control('email', [
  3368. 'type' => 'select',
  3369. 'options' => new \ArrayObject(['First', 'Second']),
  3370. 'empty' => true
  3371. ]);
  3372. $this->assertHtml($expected, $result);
  3373. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3374. $this->Form->request->data = ['Model' => ['user_id' => 'value']];
  3375. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3376. $expected = [
  3377. 'div' => ['class' => 'input select'],
  3378. 'label' => ['for' => 'model-user-id'],
  3379. 'User',
  3380. '/label',
  3381. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3382. ['option' => ['value' => '']],
  3383. '/option',
  3384. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3385. 'good',
  3386. '/option',
  3387. ['option' => ['value' => 'other']],
  3388. 'bad',
  3389. '/option',
  3390. '/select',
  3391. '/div'
  3392. ];
  3393. $this->assertHtml($expected, $result);
  3394. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3395. $this->Form->request->data = ['Thing' => ['user_id' => null]];
  3396. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3397. $expected = [
  3398. 'div' => ['class' => 'input select'],
  3399. 'label' => ['for' => 'thing-user-id'],
  3400. 'User',
  3401. '/label',
  3402. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3403. ['option' => ['value' => '']],
  3404. 'Some Empty',
  3405. '/option',
  3406. ['option' => ['value' => 'value']],
  3407. 'good',
  3408. '/option',
  3409. ['option' => ['value' => 'other']],
  3410. 'bad',
  3411. '/option',
  3412. '/select',
  3413. '/div'
  3414. ];
  3415. $this->assertHtml($expected, $result);
  3416. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3417. $this->Form->request->data = ['Thing' => ['user_id' => 'value']];
  3418. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3419. $expected = [
  3420. 'div' => ['class' => 'input select'],
  3421. 'label' => ['for' => 'thing-user-id'],
  3422. 'User',
  3423. '/label',
  3424. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3425. ['option' => ['value' => '']],
  3426. 'Some Empty',
  3427. '/option',
  3428. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3429. 'good',
  3430. '/option',
  3431. ['option' => ['value' => 'other']],
  3432. 'bad',
  3433. '/option',
  3434. '/select',
  3435. '/div'
  3436. ];
  3437. $this->assertHtml($expected, $result);
  3438. $this->Form->data = [];
  3439. $result = $this->Form->control('Publisher.id', [
  3440. 'label' => 'Publisher',
  3441. 'type' => 'select',
  3442. 'multiple' => 'checkbox',
  3443. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3444. ]);
  3445. $expected = [
  3446. ['div' => ['class' => 'input select']],
  3447. ['label' => ['for' => 'publisher-id']],
  3448. 'Publisher',
  3449. '/label',
  3450. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3451. ['div' => ['class' => 'checkbox']],
  3452. ['label' => ['for' => 'publisher-id-value-1']],
  3453. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3454. 'Label 1',
  3455. '/label',
  3456. '/div',
  3457. ['div' => ['class' => 'checkbox']],
  3458. ['label' => ['for' => 'publisher-id-value-2']],
  3459. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3460. 'Label 2',
  3461. '/label',
  3462. '/div',
  3463. '/div'
  3464. ];
  3465. $this->assertHtml($expected, $result);
  3466. }
  3467. /**
  3468. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3469. *
  3470. * Test that control() and a non standard primary key makes a hidden input by default.
  3471. *
  3472. * @return void
  3473. */
  3474. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3475. {
  3476. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3477. $this->Form->create($this->article);
  3478. $result = $this->Form->control('title');
  3479. $expected = [
  3480. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3481. ];
  3482. $this->assertHtml($expected, $result);
  3483. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3484. $this->Form->create($this->article);
  3485. $result = $this->Form->control('title');
  3486. $expected = [
  3487. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3488. ];
  3489. $this->assertHtml($expected, $result);
  3490. $result = $this->Form->control('body');
  3491. $expected = [
  3492. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3493. ];
  3494. $this->assertHtml($expected, $result);
  3495. }
  3496. /**
  3497. * testControlOverridingMagicSelectType method
  3498. *
  3499. * Test that overriding the magic select type widget is possible.
  3500. *
  3501. * @return void
  3502. */
  3503. public function testControlOverridingMagicSelectType()
  3504. {
  3505. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3506. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3507. $expected = [
  3508. 'div' => ['class' => 'input text'],
  3509. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3510. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3511. '/div'
  3512. ];
  3513. $this->assertHtml($expected, $result);
  3514. //Check that magic types still work for plural/singular vars
  3515. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3516. $result = $this->Form->control('Model.type');
  3517. $expected = [
  3518. 'div' => ['class' => 'input select'],
  3519. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3520. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3521. ['option' => ['value' => 'value']], 'good', '/option',
  3522. ['option' => ['value' => 'other']], 'bad', '/option',
  3523. '/select',
  3524. '/div'
  3525. ];
  3526. $this->assertHtml($expected, $result);
  3527. }
  3528. /**
  3529. * testControlMagicTypeDoesNotOverride method
  3530. *
  3531. * Test that inferred types do not override developer input.
  3532. *
  3533. * @return void
  3534. */
  3535. public function testControlMagicTypeDoesNotOverride()
  3536. {
  3537. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3538. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3539. $expected = [
  3540. 'div' => ['class' => 'input checkbox'],
  3541. ['input' => [
  3542. 'type' => 'hidden',
  3543. 'name' => 'Model[user]',
  3544. 'value' => 0,
  3545. ]],
  3546. 'label' => ['for' => 'model-user'],
  3547. ['input' => [
  3548. 'name' => 'Model[user]',
  3549. 'type' => 'checkbox',
  3550. 'id' => 'model-user',
  3551. 'value' => 1
  3552. ]],
  3553. 'User',
  3554. '/label',
  3555. '/div'
  3556. ];
  3557. $this->assertHtml($expected, $result);
  3558. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3559. $options = [
  3560. 1 => 'blue',
  3561. 2 => 'red'
  3562. ];
  3563. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3564. $expected = [
  3565. 'div' => ['class' => 'input select'],
  3566. 'label' => ['for' => 'tags-ids'],
  3567. 'Tags',
  3568. '/label',
  3569. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3570. ['div' => ['class' => 'checkbox']],
  3571. ['label' => ['for' => 'tags-ids-1']],
  3572. ['input' => [
  3573. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3574. 'value' => '1', 'name' => 'tags[_ids][]'
  3575. ]],
  3576. 'blue',
  3577. '/label',
  3578. '/div',
  3579. ['div' => ['class' => 'checkbox']],
  3580. ['label' => ['for' => 'tags-ids-2']],
  3581. ['input' => [
  3582. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3583. 'value' => '2', 'name' => 'tags[_ids][]'
  3584. ]],
  3585. 'red',
  3586. '/label',
  3587. '/div',
  3588. '/div'
  3589. ];
  3590. $this->assertHtml($expected, $result);
  3591. }
  3592. /**
  3593. * testControlMagicSelectForTypeNumber method
  3594. *
  3595. * Test that magic control() selects are created for type=number.
  3596. *
  3597. * @return void
  3598. */
  3599. public function testControlMagicSelectForTypeNumber()
  3600. {
  3601. TableRegistry::get('ValidateUsers', [
  3602. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3603. ]);
  3604. $entity = new Entity(['balance' => 1]);
  3605. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3606. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3607. $result = $this->Form->control('balance');
  3608. $expected = [
  3609. 'div' => ['class' => 'input select'],
  3610. 'label' => ['for' => 'balance'],
  3611. 'Balance',
  3612. '/label',
  3613. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3614. ['option' => ['value' => '0']],
  3615. 'nothing',
  3616. '/option',
  3617. ['option' => ['value' => '1', 'selected' => 'selected']],
  3618. 'some',
  3619. '/option',
  3620. ['option' => ['value' => '100']],
  3621. 'a lot',
  3622. '/option',
  3623. '/select',
  3624. '/div'
  3625. ];
  3626. $this->assertHtml($expected, $result);
  3627. }
  3628. /**
  3629. * testInvalidControlTypeOption method
  3630. *
  3631. * Test invalid 'input' type option to control() function.
  3632. *
  3633. * @return void
  3634. */
  3635. public function testInvalidControlTypeOption()
  3636. {
  3637. $this->expectException(\RuntimeException::class);
  3638. $this->expectExceptionMessage('Invalid type \'input\' used for field \'text\'');
  3639. $this->Form->control('text', ['type' => 'input']);
  3640. }
  3641. /**
  3642. * testControlMagicSelectChangeToRadio method
  3643. *
  3644. * Test that magic control() selects can easily be converted into radio types without error.
  3645. *
  3646. * @return void
  3647. */
  3648. public function testControlMagicSelectChangeToRadio()
  3649. {
  3650. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3651. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3652. $this->assertContains('input type="radio"', $result);
  3653. }
  3654. /**
  3655. * testFormControlSubmit method
  3656. *
  3657. * Test correct results for form::control() and type submit.
  3658. *
  3659. * @return void
  3660. */
  3661. public function testFormControlSubmit()
  3662. {
  3663. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3664. $expected = [
  3665. 'div' => ['class' => 'submit'],
  3666. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3667. '/div'
  3668. ];
  3669. $this->assertHtml($expected, $result);
  3670. }
  3671. /**
  3672. * testFormControls method
  3673. *
  3674. * Test correct results from Form::controls().
  3675. *
  3676. * @return void
  3677. */
  3678. public function testFormControlsLegendFieldset()
  3679. {
  3680. $this->Form->create($this->article);
  3681. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3682. $expected = [
  3683. '<fieldset',
  3684. '<legend',
  3685. 'The Legend',
  3686. '/legend',
  3687. '*/fieldset',
  3688. ];
  3689. $this->assertHtml($expected, $result);
  3690. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3691. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3692. $this->assertContains('<fieldset>', $result);
  3693. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3694. $this->assertNotContains('<legend>', $result);
  3695. $this->assertNotContains('<fieldset>', $result);
  3696. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3697. $this->assertNotContains('<legend>', $result);
  3698. $this->assertNotContains('<fieldset>', $result);
  3699. $this->Form->create($this->article);
  3700. $this->Form->request->params['prefix'] = 'admin';
  3701. $this->Form->request->params['action'] = 'admin_edit';
  3702. $this->Form->request->params['controller'] = 'articles';
  3703. $result = $this->Form->allControls();
  3704. $expected = [
  3705. '<fieldset',
  3706. '<legend',
  3707. 'New Article',
  3708. '/legend',
  3709. '*/fieldset',
  3710. ];
  3711. $this->assertHtml($expected, $result);
  3712. $this->Form->create($this->article);
  3713. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3714. $expected = [
  3715. '<fieldset',
  3716. '<legend',
  3717. 'The Legend',
  3718. '/legend',
  3719. '*/fieldset',
  3720. ];
  3721. $this->assertHtml($expected, $result);
  3722. $this->Form->create($this->article);
  3723. $result = $this->Form->allControls([], [
  3724. 'fieldset' => [
  3725. 'class' => 'some-class some-other-class',
  3726. 'disabled' => true,
  3727. 'data-param' => 'a-param'
  3728. ],
  3729. 'legend' => 'The Legend'
  3730. ]);
  3731. $expected = [
  3732. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3733. '<legend',
  3734. 'The Legend',
  3735. '/legend',
  3736. '*/fieldset',
  3737. ];
  3738. $this->assertHtml($expected, $result);
  3739. }
  3740. /**
  3741. * testFormControls method
  3742. *
  3743. * Test the controls() method.
  3744. *
  3745. * @return void
  3746. */
  3747. public function testFormControls()
  3748. {
  3749. $this->Form->create($this->article);
  3750. $result = $this->Form->allControls();
  3751. $expected = [
  3752. '<fieldset',
  3753. '<legend', 'New Article', '/legend',
  3754. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3755. ['div' => ['class' => 'input select required']],
  3756. '*/div',
  3757. ['div' => ['class' => 'input text required']],
  3758. '*/div',
  3759. ['div' => ['class' => 'input text']],
  3760. '*/div',
  3761. ['div' => ['class' => 'input text']],
  3762. '*/div',
  3763. '/fieldset',
  3764. ];
  3765. $this->assertHtml($expected, $result);
  3766. $result = $this->Form->allControls([
  3767. 'published' => ['type' => 'boolean']
  3768. ]);
  3769. $expected = [
  3770. '<fieldset',
  3771. '<legend', 'New Article', '/legend',
  3772. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3773. ['div' => ['class' => 'input select required']],
  3774. '*/div',
  3775. ['div' => ['class' => 'input text required']],
  3776. '*/div',
  3777. ['div' => ['class' => 'input text']],
  3778. '*/div',
  3779. ['div' => ['class' => 'input boolean']],
  3780. '*/div',
  3781. '/fieldset',
  3782. ];
  3783. $this->assertHtml($expected, $result);
  3784. $this->Form->create($this->article);
  3785. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3786. $expected = [
  3787. 'fieldset' => [],
  3788. 'legend' => [],
  3789. 'Hello',
  3790. '/legend',
  3791. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3792. ['div' => ['class' => 'input select required']],
  3793. '*/div',
  3794. ['div' => ['class' => 'input text required']],
  3795. '*/div',
  3796. ['div' => ['class' => 'input text']],
  3797. '*/div',
  3798. ['div' => ['class' => 'input text']],
  3799. '*/div',
  3800. '/fieldset'
  3801. ];
  3802. $this->assertHtml($expected, $result);
  3803. $this->Form->create(false);
  3804. $expected = [
  3805. 'fieldset' => [],
  3806. ['div' => ['class' => 'input text']],
  3807. 'label' => ['for' => 'foo'],
  3808. 'Foo',
  3809. '/label',
  3810. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3811. '*/div',
  3812. '/fieldset'
  3813. ];
  3814. $result = $this->Form->allControls(
  3815. ['foo' => ['type' => 'text']],
  3816. ['legend' => false]
  3817. );
  3818. $this->assertHtml($expected, $result);
  3819. }
  3820. /**
  3821. * testFormControlsBlacklist method
  3822. *
  3823. * @return void
  3824. */
  3825. public function testFormControlsBlacklist()
  3826. {
  3827. $this->Form->create($this->article);
  3828. $result = $this->Form->allControls([
  3829. 'id' => false
  3830. ]);
  3831. $expected = [
  3832. '<fieldset',
  3833. '<legend', 'New Article', '/legend',
  3834. ['div' => ['class' => 'input select required']],
  3835. '*/div',
  3836. ['div' => ['class' => 'input text required']],
  3837. '*/div',
  3838. ['div' => ['class' => 'input text']],
  3839. '*/div',
  3840. ['div' => ['class' => 'input text']],
  3841. '*/div',
  3842. '/fieldset',
  3843. ];
  3844. $this->assertHtml($expected, $result);
  3845. $this->Form->create($this->article);
  3846. $result = $this->Form->allControls([
  3847. 'id' => []
  3848. ]);
  3849. $expected = [
  3850. '<fieldset',
  3851. '<legend', 'New Article', '/legend',
  3852. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3853. ['div' => ['class' => 'input select required']],
  3854. '*/div',
  3855. ['div' => ['class' => 'input text required']],
  3856. '*/div',
  3857. ['div' => ['class' => 'input text']],
  3858. '*/div',
  3859. ['div' => ['class' => 'input text']],
  3860. '*/div',
  3861. '/fieldset',
  3862. ];
  3863. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3864. }
  3865. /**
  3866. * testSelectAsCheckbox method
  3867. *
  3868. * Test multi-select widget with checkbox formatting.
  3869. *
  3870. * @return void
  3871. */
  3872. public function testSelectAsCheckbox()
  3873. {
  3874. $result = $this->Form->select(
  3875. 'Model.multi_field',
  3876. ['first', 'second', 'third'],
  3877. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3878. );
  3879. $expected = [
  3880. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3881. ['div' => ['class' => 'checkbox']],
  3882. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3883. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3884. 'first',
  3885. '/label',
  3886. '/div',
  3887. ['div' => ['class' => 'checkbox']],
  3888. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3889. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3890. 'second',
  3891. '/label',
  3892. '/div',
  3893. ['div' => ['class' => 'checkbox']],
  3894. ['label' => ['for' => 'model-multi-field-2']],
  3895. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3896. 'third',
  3897. '/label',
  3898. '/div',
  3899. ];
  3900. $this->assertHtml($expected, $result);
  3901. $result = $this->Form->select(
  3902. 'Model.multi_field',
  3903. ['1/2' => 'half'],
  3904. ['multiple' => 'checkbox']
  3905. );
  3906. $expected = [
  3907. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3908. ['div' => ['class' => 'checkbox']],
  3909. ['label' => ['for' => 'model-multi-field-1-2']],
  3910. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3911. 'half',
  3912. '/label',
  3913. '/div',
  3914. ];
  3915. $this->assertHtml($expected, $result);
  3916. }
  3917. /**
  3918. * testLabel method
  3919. *
  3920. * Test label generation.
  3921. *
  3922. * @return void
  3923. */
  3924. public function testLabel()
  3925. {
  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.name');
  3930. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3931. $this->assertHtml($expected, $result);
  3932. $result = $this->Form->label('Person.first_name');
  3933. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3934. $this->assertHtml($expected, $result);
  3935. $result = $this->Form->label('Person.first_name', 'Your first name');
  3936. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3937. $this->assertHtml($expected, $result);
  3938. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3939. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3940. $this->assertHtml($expected, $result);
  3941. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3942. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3943. $this->assertHtml($expected, $result);
  3944. $result = $this->Form->label('Person.first_name', '');
  3945. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3946. $this->assertHtml($expected, $result);
  3947. $result = $this->Form->label('Person.2.name', '');
  3948. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3949. $this->assertHtml($expected, $result);
  3950. }
  3951. /**
  3952. * testLabelContainControl method
  3953. *
  3954. * Test that label() can accept an input with the correct template vars.
  3955. *
  3956. * @return void
  3957. */
  3958. public function testLabelContainControl()
  3959. {
  3960. $this->Form->setTemplates([
  3961. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3962. ]);
  3963. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3964. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3965. ]);
  3966. $expected = [
  3967. 'label' => ['for' => 'person-accept-terms'],
  3968. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3969. 'Accept',
  3970. '/label',
  3971. ];
  3972. $this->assertHtml($expected, $result);
  3973. }
  3974. /**
  3975. * testTextbox method
  3976. *
  3977. * Test textbox element generation.
  3978. *
  3979. * @return void
  3980. */
  3981. public function testTextbox()
  3982. {
  3983. $result = $this->Form->text('Model.field');
  3984. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3985. $this->assertHtml($expected, $result);
  3986. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3987. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3988. $this->assertHtml($expected, $result);
  3989. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3990. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3991. $this->assertHtml($expected, $result);
  3992. }
  3993. /**
  3994. * testTextBoxDataAndError method
  3995. *
  3996. * Test that text() hooks up with request data and error fields.
  3997. *
  3998. * @return void
  3999. */
  4000. public function testTextBoxDataAndError()
  4001. {
  4002. $this->article['errors'] = [
  4003. 'Contact' => ['text' => 'wrong']
  4004. ];
  4005. $this->Form->create($this->article);
  4006. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  4007. $result = $this->Form->text('Model.text');
  4008. $expected = [
  4009. 'input' => [
  4010. 'type' => 'text',
  4011. 'name' => 'Model[text]',
  4012. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  4013. ]
  4014. ];
  4015. $this->assertHtml($expected, $result);
  4016. $this->Form->request->data['Contact']['text'] = 'test';
  4017. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  4018. $expected = [
  4019. 'input' => [
  4020. 'type' => 'text',
  4021. 'name' => 'Contact[text]',
  4022. 'value' => 'test',
  4023. 'id' => 'theID',
  4024. 'class' => 'form-error'
  4025. ]
  4026. ];
  4027. $this->assertHtml($expected, $result);
  4028. }
  4029. /**
  4030. * testDefaultValue method
  4031. *
  4032. * Test default value setting.
  4033. *
  4034. * @return void
  4035. */
  4036. public function testTextDefaultValue()
  4037. {
  4038. $this->Form->request->data['Model']['field'] = 'test';
  4039. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4040. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  4041. $this->assertHtml($expected, $result);
  4042. unset($this->Form->request->data['Model']['field']);
  4043. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4044. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4045. $this->assertHtml($expected, $result);
  4046. $Articles = TableRegistry::get('Articles');
  4047. $title = $Articles->getSchema()->getColumn('title');
  4048. $Articles->getSchema()->addColumn(
  4049. 'title',
  4050. ['default' => 'default title'] + $title
  4051. );
  4052. $entity = $Articles->newEntity();
  4053. $this->Form->create($entity);
  4054. // Get default value from schema
  4055. $result = $this->Form->text('title');
  4056. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4057. $this->assertHtml($expected, $result);
  4058. // Don't get value from schema
  4059. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4060. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4061. $this->assertHtml($expected, $result);
  4062. // Custom default value overrides default value from schema
  4063. $result = $this->Form->text('title', ['default' => 'override default']);
  4064. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4065. $this->assertHtml($expected, $result);
  4066. // Default value from schema is used only for new entities.
  4067. $entity->isNew(false);
  4068. $result = $this->Form->text('title');
  4069. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4070. $this->assertHtml($expected, $result);
  4071. }
  4072. /**
  4073. * testError method
  4074. *
  4075. * Test field error generation.
  4076. *
  4077. * @return void
  4078. */
  4079. public function testError()
  4080. {
  4081. $this->article['errors'] = [
  4082. 'Article' => ['field' => 'email']
  4083. ];
  4084. $this->Form->create($this->article);
  4085. $result = $this->Form->error('Article.field');
  4086. $expected = [
  4087. ['div' => ['class' => 'error-message']],
  4088. 'email',
  4089. '/div',
  4090. ];
  4091. $this->assertHtml($expected, $result);
  4092. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4093. $expected = [
  4094. ['div' => ['class' => 'error-message']],
  4095. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4096. '/div',
  4097. ];
  4098. $this->assertHtml($expected, $result);
  4099. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4100. $expected = [
  4101. ['div' => ['class' => 'error-message']],
  4102. '<strong', 'Badness!', '/strong',
  4103. '/div',
  4104. ];
  4105. $this->assertHtml($expected, $result);
  4106. }
  4107. /**
  4108. * testErrorRuleName method
  4109. *
  4110. * Test error translation can use rule names for translating.
  4111. *
  4112. * @return void
  4113. */
  4114. public function testErrorRuleName()
  4115. {
  4116. $this->article['errors'] = [
  4117. 'Article' => [
  4118. 'field' => ['email' => 'Your email was not good']
  4119. ]
  4120. ];
  4121. $this->Form->create($this->article);
  4122. $result = $this->Form->error('Article.field');
  4123. $expected = [
  4124. ['div' => ['class' => 'error-message']],
  4125. 'Your email was not good',
  4126. '/div',
  4127. ];
  4128. $this->assertHtml($expected, $result);
  4129. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4130. $expected = [
  4131. ['div' => ['class' => 'error-message']],
  4132. 'Email in use',
  4133. '/div',
  4134. ];
  4135. $this->assertHtml($expected, $result);
  4136. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4137. $expected = [
  4138. ['div' => ['class' => 'error-message']],
  4139. 'Email in use',
  4140. '/div',
  4141. ];
  4142. $this->assertHtml($expected, $result);
  4143. $result = $this->Form->error('Article.field', [
  4144. 'email' => 'Key is preferred',
  4145. 'Your email was not good' => 'Email in use'
  4146. ]);
  4147. $expected = [
  4148. ['div' => ['class' => 'error-message']],
  4149. 'Key is preferred',
  4150. '/div',
  4151. ];
  4152. $this->assertHtml($expected, $result);
  4153. }
  4154. /**
  4155. * testErrorMessages method
  4156. *
  4157. * Test error with nested lists.
  4158. *
  4159. * @return void
  4160. */
  4161. public function testErrorMessages()
  4162. {
  4163. $this->article['errors'] = [
  4164. 'Article' => ['field' => 'email']
  4165. ];
  4166. $this->Form->create($this->article);
  4167. $result = $this->Form->error('Article.field', [
  4168. 'email' => 'No good!'
  4169. ]);
  4170. $expected = [
  4171. 'div' => ['class' => 'error-message'],
  4172. 'No good!',
  4173. '/div'
  4174. ];
  4175. $this->assertHtml($expected, $result);
  4176. }
  4177. /**
  4178. * testErrorMultipleMessages method
  4179. *
  4180. * Test error() with multiple messages.
  4181. *
  4182. * @return void
  4183. */
  4184. public function testErrorMultipleMessages()
  4185. {
  4186. $this->article['errors'] = [
  4187. 'field' => ['notBlank', 'email', 'Something else']
  4188. ];
  4189. $this->Form->create($this->article);
  4190. $result = $this->Form->error('field', [
  4191. 'notBlank' => 'Cannot be empty',
  4192. 'email' => 'No good!'
  4193. ]);
  4194. $expected = [
  4195. 'div' => ['class' => 'error-message'],
  4196. 'ul' => [],
  4197. '<li', 'Cannot be empty', '/li',
  4198. '<li', 'No good!', '/li',
  4199. '<li', 'Something else', '/li',
  4200. '/ul',
  4201. '/div'
  4202. ];
  4203. $this->assertHtml($expected, $result);
  4204. }
  4205. /**
  4206. * testPassword method
  4207. *
  4208. * Test password element generation.
  4209. *
  4210. * @return void
  4211. */
  4212. public function testPassword()
  4213. {
  4214. $this->article['errors'] = [
  4215. 'Contact' => [
  4216. 'passwd' => 1
  4217. ]
  4218. ];
  4219. $this->Form->create($this->article);
  4220. $result = $this->Form->password('Contact.field');
  4221. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4222. $this->assertHtml($expected, $result);
  4223. $this->Form->request->data['Contact']['passwd'] = 'test';
  4224. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4225. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4226. $this->assertHtml($expected, $result);
  4227. }
  4228. /**
  4229. * testRadio method
  4230. *
  4231. * Test radio element set generation.
  4232. *
  4233. * @return void
  4234. */
  4235. public function testRadio()
  4236. {
  4237. $result = $this->Form->radio('Model.field', ['option A']);
  4238. $expected = [
  4239. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4240. 'label' => ['for' => 'model-field-0'],
  4241. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4242. 'option A',
  4243. '/label'
  4244. ];
  4245. $this->assertHtml($expected, $result);
  4246. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4247. $this->assertHtml($expected, $result);
  4248. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4249. $expected = [
  4250. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4251. ['label' => ['for' => 'model-field-0']],
  4252. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4253. 'option A',
  4254. '/label',
  4255. ['label' => ['for' => 'model-field-1']],
  4256. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4257. 'option B',
  4258. '/label',
  4259. ];
  4260. $this->assertHtml($expected, $result);
  4261. $result = $this->Form->radio(
  4262. 'Employee.gender',
  4263. ['male' => 'Male', 'female' => 'Female'],
  4264. ['form' => 'my-form']
  4265. );
  4266. $expected = [
  4267. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4268. ['label' => ['for' => 'employee-gender-male']],
  4269. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4270. 'Male',
  4271. '/label',
  4272. ['label' => ['for' => 'employee-gender-female']],
  4273. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4274. 'Female',
  4275. '/label',
  4276. ];
  4277. $this->assertHtml($expected, $result);
  4278. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4279. $expected = [
  4280. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4281. ['label' => ['for' => 'model-custom-0']],
  4282. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4283. 'option A',
  4284. '/label',
  4285. ['label' => ['for' => 'model-custom-1']],
  4286. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4287. 'option B',
  4288. '/label',
  4289. ];
  4290. $this->assertHtml($expected, $result);
  4291. $result = $this->Form->radio(
  4292. 'Employee.gender',
  4293. [
  4294. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4295. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4296. ]
  4297. );
  4298. $expected = [
  4299. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4300. ['label' => ['for' => 'employee-gender-male']],
  4301. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4302. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4303. 'Male',
  4304. '/label',
  4305. ['label' => ['for' => 'employee-gender-female']],
  4306. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4307. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4308. 'Female',
  4309. '/label',
  4310. ];
  4311. $this->assertHtml($expected, $result);
  4312. }
  4313. /**
  4314. * Test radio with complex options and empty disabled data.
  4315. *
  4316. * @return void
  4317. */
  4318. public function testRadioComplexDisabled()
  4319. {
  4320. $options = [
  4321. ['value' => 'r', 'text' => 'red'],
  4322. ['value' => 'b', 'text' => 'blue'],
  4323. ];
  4324. $attrs = ['disabled' => []];
  4325. $result = $this->Form->radio('Model.field', $options, $attrs);
  4326. $expected = [
  4327. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4328. ['label' => ['for' => 'model-field-r']],
  4329. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4330. 'red',
  4331. '/label',
  4332. ['label' => ['for' => 'model-field-b']],
  4333. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4334. 'blue',
  4335. '/label',
  4336. ];
  4337. $this->assertHtml($expected, $result);
  4338. $attrs = ['disabled' => ['r']];
  4339. $result = $this->Form->radio('Model.field', $options, $attrs);
  4340. $this->assertContains('disabled="disabled"', $result);
  4341. }
  4342. /**
  4343. * testRadioDefaultValue method
  4344. *
  4345. * Test default value setting on radio() method.
  4346. *
  4347. * @return void
  4348. */
  4349. public function testRadioDefaultValue()
  4350. {
  4351. $Articles = TableRegistry::get('Articles');
  4352. $title = $Articles->getSchema()->getColumn('title');
  4353. $Articles->getSchema()->addColumn(
  4354. 'title',
  4355. ['default' => '1'] + $title
  4356. );
  4357. $this->Form->create($Articles->newEntity());
  4358. $result = $this->Form->radio('title', ['option A', 'option B']);
  4359. $expected = [
  4360. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4361. ['label' => ['for' => 'title-0']],
  4362. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4363. 'option A',
  4364. '/label',
  4365. ['label' => ['for' => 'title-1']],
  4366. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4367. 'option B',
  4368. '/label',
  4369. ];
  4370. $this->assertHtml($expected, $result);
  4371. }
  4372. /**
  4373. * Test setting a hiddenField value on radio buttons.
  4374. *
  4375. * @return void
  4376. */
  4377. public function testRadioHiddenFieldValue()
  4378. {
  4379. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4380. $expected = [
  4381. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4382. 'label' => ['for' => 'title-0'],
  4383. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4384. 'option A',
  4385. '/label',
  4386. ];
  4387. $this->assertHtml($expected, $result);
  4388. }
  4389. /**
  4390. * testControlRadio method
  4391. *
  4392. * Test that input works with radio types.
  4393. *
  4394. * @return void
  4395. */
  4396. public function testControlRadio()
  4397. {
  4398. $result = $this->Form->control('test', [
  4399. 'type' => 'radio',
  4400. 'options' => ['A', 'B'],
  4401. ]);
  4402. $expected = [
  4403. ['div' => ['class' => 'input radio']],
  4404. '<label',
  4405. 'Test',
  4406. '/label',
  4407. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4408. ['label' => ['for' => 'test-0']],
  4409. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4410. 'A',
  4411. '/label',
  4412. ['label' => ['for' => 'test-1']],
  4413. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4414. 'B',
  4415. '/label',
  4416. '/div',
  4417. ];
  4418. $this->assertHtml($expected, $result);
  4419. $result = $this->Form->control('test', [
  4420. 'type' => 'radio',
  4421. 'options' => ['A', 'B'],
  4422. 'value' => '0'
  4423. ]);
  4424. $expected = [
  4425. ['div' => ['class' => 'input radio']],
  4426. '<label',
  4427. 'Test',
  4428. '/label',
  4429. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4430. ['label' => ['for' => 'test-0']],
  4431. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4432. 'A',
  4433. '/label',
  4434. ['label' => ['for' => 'test-1']],
  4435. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4436. 'B',
  4437. '/label',
  4438. '/div',
  4439. ];
  4440. $this->assertHtml($expected, $result);
  4441. $result = $this->Form->control('test', [
  4442. 'type' => 'radio',
  4443. 'options' => ['A', 'B'],
  4444. 'label' => false
  4445. ]);
  4446. $expected = [
  4447. ['div' => ['class' => 'input radio']],
  4448. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4449. ['label' => ['for' => 'test-0']],
  4450. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4451. 'A',
  4452. '/label',
  4453. ['label' => ['for' => 'test-1']],
  4454. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4455. 'B',
  4456. '/label',
  4457. '/div',
  4458. ];
  4459. $this->assertHtml($expected, $result);
  4460. }
  4461. /**
  4462. * testRadioNoLabel method
  4463. *
  4464. * Test that radio() works with label = false.
  4465. *
  4466. * @return void
  4467. */
  4468. public function testRadioNoLabel()
  4469. {
  4470. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4471. $expected = [
  4472. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4473. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4474. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4475. ];
  4476. $this->assertHtml($expected, $result);
  4477. }
  4478. /**
  4479. * testRadioControlInsideLabel method
  4480. *
  4481. * Test generating radio input inside label ala twitter bootstrap.
  4482. *
  4483. * @return void
  4484. */
  4485. public function testRadioControlInsideLabel()
  4486. {
  4487. $this->Form->setTemplates([
  4488. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4489. 'radioWrapper' => '{{label}}'
  4490. ]);
  4491. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4492. //@codingStandardsIgnoreStart
  4493. $expected = [
  4494. ['input' => [
  4495. 'type' => 'hidden',
  4496. 'name' => 'Model[field]',
  4497. 'value' => ''
  4498. ]],
  4499. ['label' => ['for' => 'model-field-0']],
  4500. ['input' => [
  4501. 'type' => 'radio',
  4502. 'name' => 'Model[field]',
  4503. 'value' => '0',
  4504. 'id' => 'model-field-0'
  4505. ]],
  4506. 'option A',
  4507. '/label',
  4508. ['label' => ['for' => 'model-field-1']],
  4509. ['input' => [
  4510. 'type' => 'radio',
  4511. 'name' => 'Model[field]',
  4512. 'value' => '1',
  4513. 'id' => 'model-field-1'
  4514. ]],
  4515. 'option B',
  4516. '/label',
  4517. ];
  4518. //@codingStandardsIgnoreEnd
  4519. $this->assertHtml($expected, $result);
  4520. }
  4521. /**
  4522. * testRadioHiddenControlDisabling method
  4523. *
  4524. * Test disabling the hidden input for radio buttons.
  4525. *
  4526. * @return void
  4527. */
  4528. public function testRadioHiddenControlDisabling()
  4529. {
  4530. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4531. $expected = [
  4532. 'label' => ['for' => 'model-1-field-0'],
  4533. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4534. 'option A',
  4535. '/label'
  4536. ];
  4537. $this->assertHtml($expected, $result);
  4538. }
  4539. /**
  4540. * testRadioOutOfRange method
  4541. *
  4542. * Test radio element set generation.
  4543. *
  4544. * @return void
  4545. */
  4546. public function testRadioOutOfRange()
  4547. {
  4548. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4549. $expected = [
  4550. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4551. 'label' => ['for' => 'model-field-v'],
  4552. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4553. 'value',
  4554. '/label'
  4555. ];
  4556. $this->assertHtml($expected, $result);
  4557. }
  4558. /**
  4559. * testSelect method
  4560. *
  4561. * Test select element generation.
  4562. *
  4563. * @return void
  4564. */
  4565. public function testSelect()
  4566. {
  4567. $result = $this->Form->select('Model.field', []);
  4568. $expected = [
  4569. 'select' => ['name' => 'Model[field]'],
  4570. '/select'
  4571. ];
  4572. $this->assertHtml($expected, $result);
  4573. $this->Form->request->data = ['Model' => ['field' => 'value']];
  4574. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4575. $expected = [
  4576. 'select' => ['name' => 'Model[field]'],
  4577. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4578. 'good',
  4579. '/option',
  4580. ['option' => ['value' => 'other']],
  4581. 'bad',
  4582. '/option',
  4583. '/select'
  4584. ];
  4585. $this->assertHtml($expected, $result);
  4586. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4587. $this->assertHtml($expected, $result);
  4588. $this->Form->request->data = [];
  4589. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4590. $expected = [
  4591. 'select' => ['name' => 'Model[field]'],
  4592. ['option' => ['value' => 'value']],
  4593. 'good',
  4594. '/option',
  4595. ['option' => ['value' => 'other']],
  4596. 'bad',
  4597. '/option',
  4598. '/select'
  4599. ];
  4600. $this->assertHtml($expected, $result);
  4601. $options = [
  4602. ['value' => 'first', 'text' => 'First'],
  4603. ['value' => 'first', 'text' => 'Another First'],
  4604. ];
  4605. $result = $this->Form->select(
  4606. 'Model.field',
  4607. $options,
  4608. ['escape' => false, 'empty' => false]
  4609. );
  4610. $expected = [
  4611. 'select' => ['name' => 'Model[field]'],
  4612. ['option' => ['value' => 'first']],
  4613. 'First',
  4614. '/option',
  4615. ['option' => ['value' => 'first']],
  4616. 'Another First',
  4617. '/option',
  4618. '/select'
  4619. ];
  4620. $this->assertHtml($expected, $result);
  4621. $this->Form->request->data = ['Model' => ['contact_id' => 228]];
  4622. $result = $this->Form->select(
  4623. 'Model.contact_id',
  4624. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4625. ['escape' => false, 'empty' => 'pick something']
  4626. );
  4627. $expected = [
  4628. 'select' => ['name' => 'Model[contact_id]'],
  4629. ['option' => ['value' => '']], 'pick something', '/option',
  4630. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4631. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4632. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4633. '/select'
  4634. ];
  4635. $this->assertHtml($expected, $result);
  4636. $this->Form->request->data['Model']['field'] = 0;
  4637. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4638. $expected = [
  4639. 'select' => ['name' => 'Model[field]'],
  4640. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4641. ['option' => ['value' => '1']], 'Yes', '/option',
  4642. '/select'
  4643. ];
  4644. $this->assertHtml($expected, $result);
  4645. }
  4646. /**
  4647. * testSelectEscapeHtml method
  4648. *
  4649. * Test that select() escapes HTML.
  4650. *
  4651. * @return void
  4652. */
  4653. public function testSelectEscapeHtml()
  4654. {
  4655. $result = $this->Form->select(
  4656. 'Model.field',
  4657. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4658. ['empty' => false]
  4659. );
  4660. $expected = [
  4661. 'select' => ['name' => 'Model[field]'],
  4662. ['option' => ['value' => 'first']],
  4663. 'first &quot;html&quot; &lt;chars&gt;',
  4664. '/option',
  4665. ['option' => ['value' => 'second']],
  4666. 'value',
  4667. '/option',
  4668. '/select'
  4669. ];
  4670. $this->assertHtml($expected, $result);
  4671. $result = $this->Form->select(
  4672. 'Model.field',
  4673. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4674. ['escape' => false, 'empty' => false]
  4675. );
  4676. $expected = [
  4677. 'select' => ['name' => 'Model[field]'],
  4678. ['option' => ['value' => 'first']],
  4679. 'first "html" <chars>',
  4680. '/option',
  4681. ['option' => ['value' => 'second']],
  4682. 'value',
  4683. '/option',
  4684. '/select'
  4685. ];
  4686. $this->assertHtml($expected, $result);
  4687. }
  4688. /**
  4689. * testSelectRequired method
  4690. *
  4691. * Test select() with required and disabled attributes.
  4692. *
  4693. * @return void
  4694. */
  4695. public function testSelectRequired()
  4696. {
  4697. $this->article['required'] = [
  4698. 'user_id' => true
  4699. ];
  4700. $this->Form->create($this->article);
  4701. $result = $this->Form->select('user_id', ['option A']);
  4702. $expected = [
  4703. 'select' => [
  4704. 'name' => 'user_id',
  4705. 'required' => 'required'
  4706. ],
  4707. ['option' => ['value' => '0']], 'option A', '/option',
  4708. '/select'
  4709. ];
  4710. $this->assertHtml($expected, $result);
  4711. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4712. $expected = [
  4713. 'select' => [
  4714. 'name' => 'user_id',
  4715. 'disabled' => 'disabled'
  4716. ],
  4717. ['option' => ['value' => '0']], 'option A', '/option',
  4718. '/select'
  4719. ];
  4720. $this->assertHtml($expected, $result);
  4721. }
  4722. /**
  4723. * testNestedSelect method
  4724. *
  4725. * Test select element generation with optgroups.
  4726. *
  4727. * @return void
  4728. */
  4729. public function testNestedSelect()
  4730. {
  4731. $result = $this->Form->select(
  4732. 'Model.field',
  4733. [1 => 'One', 2 => 'Two', 'Three' => [
  4734. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4735. ]],
  4736. ['empty' => false]
  4737. );
  4738. $expected = [
  4739. 'select' => ['name' => 'Model[field]'],
  4740. ['option' => ['value' => 1]],
  4741. 'One',
  4742. '/option',
  4743. ['option' => ['value' => 2]],
  4744. 'Two',
  4745. '/option',
  4746. ['optgroup' => ['label' => 'Three']],
  4747. ['option' => ['value' => 3]],
  4748. 'Three',
  4749. '/option',
  4750. ['option' => ['value' => 4]],
  4751. 'Four',
  4752. '/option',
  4753. ['option' => ['value' => 5]],
  4754. 'Five',
  4755. '/option',
  4756. '/optgroup',
  4757. '/select'
  4758. ];
  4759. $this->assertHtml($expected, $result);
  4760. }
  4761. /**
  4762. * testSelectMultiple method
  4763. *
  4764. * Test generation of multiple select elements.
  4765. *
  4766. * @return void
  4767. */
  4768. public function testSelectMultiple()
  4769. {
  4770. $options = ['first', 'second', 'third'];
  4771. $result = $this->Form->select(
  4772. 'Model.multi_field',
  4773. $options,
  4774. ['form' => 'my-form', 'multiple' => true]
  4775. );
  4776. $expected = [
  4777. 'input' => [
  4778. 'type' => 'hidden',
  4779. 'name' => 'Model[multi_field]',
  4780. 'value' => '',
  4781. 'form' => 'my-form',
  4782. ],
  4783. 'select' => [
  4784. 'name' => 'Model[multi_field][]',
  4785. 'multiple' => 'multiple',
  4786. 'form' => 'my-form',
  4787. ],
  4788. ['option' => ['value' => '0']],
  4789. 'first',
  4790. '/option',
  4791. ['option' => ['value' => '1']],
  4792. 'second',
  4793. '/option',
  4794. ['option' => ['value' => '2']],
  4795. 'third',
  4796. '/option',
  4797. '/select'
  4798. ];
  4799. $this->assertHtml($expected, $result);
  4800. $result = $this->Form->select(
  4801. 'Model.multi_field',
  4802. $options,
  4803. ['multiple' => 'multiple', 'form' => 'my-form']
  4804. );
  4805. $this->assertHtml($expected, $result);
  4806. }
  4807. /**
  4808. * testCheckboxZeroValue method
  4809. *
  4810. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4811. *
  4812. * @return void
  4813. */
  4814. public function testCheckboxZeroValue()
  4815. {
  4816. $result = $this->Form->control('User.get_spam', [
  4817. 'type' => 'checkbox',
  4818. 'value' => '0',
  4819. 'hiddenField' => '1',
  4820. ]);
  4821. $expected = [
  4822. 'div' => ['class' => 'input checkbox'],
  4823. 'label' => ['for' => 'user-get-spam'],
  4824. ['input' => [
  4825. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4826. 'value' => '1'
  4827. ]],
  4828. ['input' => [
  4829. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4830. 'value' => '0', 'id' => 'user-get-spam'
  4831. ]],
  4832. 'Get Spam',
  4833. '/label',
  4834. '/div'
  4835. ];
  4836. $this->assertHtml($expected, $result);
  4837. }
  4838. /**
  4839. * testHabtmSelectBox method
  4840. *
  4841. * Test generation of habtm select boxes.
  4842. *
  4843. * @return void
  4844. */
  4845. public function testHabtmSelectBox()
  4846. {
  4847. $this->loadFixtures('Articles');
  4848. $options = [
  4849. 1 => 'blue',
  4850. 2 => 'red',
  4851. 3 => 'green'
  4852. ];
  4853. $tags = [
  4854. new Entity(['id' => 1, 'name' => 'blue']),
  4855. new Entity(['id' => 3, 'name' => 'green'])
  4856. ];
  4857. $article = new Article(['tags' => $tags]);
  4858. $this->Form->create($article);
  4859. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4860. $expected = [
  4861. 'div' => ['class' => 'input select'],
  4862. 'label' => ['for' => 'tags-ids'],
  4863. 'Tags',
  4864. '/label',
  4865. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4866. 'select' => [
  4867. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4868. 'multiple' => 'multiple'
  4869. ],
  4870. ['option' => ['value' => '1', 'selected' => 'selected']],
  4871. 'blue',
  4872. '/option',
  4873. ['option' => ['value' => '2']],
  4874. 'red',
  4875. '/option',
  4876. ['option' => ['value' => '3', 'selected' => 'selected']],
  4877. 'green',
  4878. '/option',
  4879. '/select',
  4880. '/div'
  4881. ];
  4882. $this->assertHtml($expected, $result);
  4883. // make sure only 50 is selected, and not 50f5c0cf
  4884. $options = [
  4885. '1' => 'blue',
  4886. '50f5c0cf' => 'red',
  4887. '50' => 'green'
  4888. ];
  4889. $tags = [
  4890. new Entity(['id' => 1, 'name' => 'blue']),
  4891. new Entity(['id' => 50, 'name' => 'green'])
  4892. ];
  4893. $article = new Article(['tags' => $tags]);
  4894. $this->Form->create($article);
  4895. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4896. $expected = [
  4897. 'div' => ['class' => 'input select'],
  4898. 'label' => ['for' => 'tags-ids'],
  4899. 'Tags',
  4900. '/label',
  4901. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4902. 'select' => [
  4903. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4904. 'multiple' => 'multiple'
  4905. ],
  4906. ['option' => ['value' => '1', 'selected' => 'selected']],
  4907. 'blue',
  4908. '/option',
  4909. ['option' => ['value' => '50f5c0cf']],
  4910. 'red',
  4911. '/option',
  4912. ['option' => ['value' => '50', 'selected' => 'selected']],
  4913. 'green',
  4914. '/option',
  4915. '/select',
  4916. '/div'
  4917. ];
  4918. $this->assertHtml($expected, $result);
  4919. $spacecraft = [
  4920. 1 => 'Orion',
  4921. 2 => 'Helios'
  4922. ];
  4923. $this->View->viewVars['spacecraft'] = $spacecraft;
  4924. $this->Form->create();
  4925. $result = $this->Form->control('spacecraft._ids');
  4926. $expected = [
  4927. 'div' => ['class' => 'input select'],
  4928. 'label' => ['for' => 'spacecraft-ids'],
  4929. 'Spacecraft',
  4930. '/label',
  4931. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4932. 'select' => [
  4933. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4934. 'multiple' => 'multiple'
  4935. ],
  4936. ['option' => ['value' => '1']],
  4937. 'Orion',
  4938. '/option',
  4939. ['option' => ['value' => '2']],
  4940. 'Helios',
  4941. '/option',
  4942. '/select',
  4943. '/div'
  4944. ];
  4945. $this->assertHtml($expected, $result);
  4946. }
  4947. /**
  4948. * testErrorsForBelongsToManySelect method
  4949. *
  4950. * Tests that errors for belongsToMany select fields are being
  4951. * picked up properly.
  4952. *
  4953. * @return void
  4954. */
  4955. public function testErrorsForBelongsToManySelect()
  4956. {
  4957. $spacecraft = [
  4958. 1 => 'Orion',
  4959. 2 => 'Helios'
  4960. ];
  4961. $this->View->viewVars['spacecraft'] = $spacecraft;
  4962. $article = new Article();
  4963. $article->setError('spacecraft', ['Invalid']);
  4964. $this->Form->create($article);
  4965. $result = $this->Form->control('spacecraft._ids');
  4966. $expected = [
  4967. ['div' => ['class' => 'input select error']],
  4968. 'label' => ['for' => 'spacecraft-ids'],
  4969. 'Spacecraft',
  4970. '/label',
  4971. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4972. 'select' => [
  4973. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4974. 'multiple' => 'multiple'
  4975. ],
  4976. ['option' => ['value' => '1']],
  4977. 'Orion',
  4978. '/option',
  4979. ['option' => ['value' => '2']],
  4980. 'Helios',
  4981. '/option',
  4982. '/select',
  4983. ['div' => ['class' => 'error-message']],
  4984. 'Invalid',
  4985. '/div',
  4986. '/div'
  4987. ];
  4988. $this->assertHtml($expected, $result);
  4989. }
  4990. /**
  4991. * testSelectMultipleCheckboxes method
  4992. *
  4993. * Test generation of multi select elements in checkbox format.
  4994. *
  4995. * @return void
  4996. */
  4997. public function testSelectMultipleCheckboxes()
  4998. {
  4999. $result = $this->Form->select(
  5000. 'Model.multi_field',
  5001. ['first', 'second', 'third'],
  5002. ['multiple' => 'checkbox']
  5003. );
  5004. $expected = [
  5005. 'input' => [
  5006. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5007. ],
  5008. ['div' => ['class' => 'checkbox']],
  5009. ['label' => ['for' => 'model-multi-field-0']],
  5010. ['input' => [
  5011. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5012. 'value' => '0', 'id' => 'model-multi-field-0'
  5013. ]],
  5014. 'first',
  5015. '/label',
  5016. '/div',
  5017. ['div' => ['class' => 'checkbox']],
  5018. ['label' => ['for' => 'model-multi-field-1']],
  5019. ['input' => [
  5020. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5021. 'value' => '1', 'id' => 'model-multi-field-1'
  5022. ]],
  5023. 'second',
  5024. '/label',
  5025. '/div',
  5026. ['div' => ['class' => 'checkbox']],
  5027. ['label' => ['for' => 'model-multi-field-2']],
  5028. ['input' => [
  5029. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5030. 'value' => '2', 'id' => 'model-multi-field-2'
  5031. ]],
  5032. 'third',
  5033. '/label',
  5034. '/div'
  5035. ];
  5036. $this->assertHtml($expected, $result);
  5037. $result = $this->Form->select(
  5038. 'Model.multi_field',
  5039. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5040. ['multiple' => 'checkbox']
  5041. );
  5042. $expected = [
  5043. 'input' => [
  5044. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5045. ],
  5046. ['div' => ['class' => 'checkbox']],
  5047. ['label' => ['for' => 'model-multi-field-a+']],
  5048. ['input' => [
  5049. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5050. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  5051. ]],
  5052. 'first',
  5053. '/label',
  5054. '/div',
  5055. ['div' => ['class' => 'checkbox']],
  5056. ['label' => ['for' => 'model-multi-field-a++']],
  5057. ['input' => [
  5058. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5059. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  5060. ]],
  5061. 'second',
  5062. '/label',
  5063. '/div',
  5064. ['div' => ['class' => 'checkbox']],
  5065. ['label' => ['for' => 'model-multi-field-a+++']],
  5066. ['input' => [
  5067. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5068. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  5069. ]],
  5070. 'third',
  5071. '/label',
  5072. '/div'
  5073. ];
  5074. $this->assertHtml($expected, $result);
  5075. $result = $this->Form->select(
  5076. 'Model.multi_field',
  5077. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5078. ['multiple' => 'checkbox']
  5079. );
  5080. $expected = [
  5081. 'input' => [
  5082. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5083. ],
  5084. ['div' => ['class' => 'checkbox']],
  5085. ['label' => ['for' => 'model-multi-field-a-b']],
  5086. ['input' => [
  5087. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5088. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  5089. ]],
  5090. 'first',
  5091. '/label',
  5092. '/div',
  5093. ['div' => ['class' => 'checkbox']],
  5094. ['label' => ['for' => 'model-multi-field-a-b1']],
  5095. ['input' => [
  5096. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5097. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  5098. ]],
  5099. 'second',
  5100. '/label',
  5101. '/div',
  5102. ['div' => ['class' => 'checkbox']],
  5103. ['label' => ['for' => 'model-multi-field-a-b2']],
  5104. ['input' => [
  5105. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5106. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  5107. ]],
  5108. 'third',
  5109. '/label',
  5110. '/div'
  5111. ];
  5112. $this->assertHtml($expected, $result);
  5113. }
  5114. /**
  5115. * testSelectMultipleCheckboxRequestData method
  5116. *
  5117. * Ensure that multiCheckbox reads from the request data.
  5118. *
  5119. * @return void
  5120. */
  5121. public function testSelectMultipleCheckboxRequestData()
  5122. {
  5123. $this->Form->request->data = ['Model' => ['tags' => [1]]];
  5124. $result = $this->Form->select(
  5125. 'Model.tags',
  5126. ['1' => 'first', 'Array' => 'Array'],
  5127. ['multiple' => 'checkbox']
  5128. );
  5129. $expected = [
  5130. 'input' => [
  5131. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  5132. ],
  5133. ['div' => ['class' => 'checkbox']],
  5134. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5135. ['input' => [
  5136. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5137. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  5138. ]],
  5139. 'first',
  5140. '/label',
  5141. '/div',
  5142. ['div' => ['class' => 'checkbox']],
  5143. ['label' => ['for' => 'model-tags-array']],
  5144. ['input' => [
  5145. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5146. 'value' => 'Array', 'id' => 'model-tags-array'
  5147. ]],
  5148. 'Array',
  5149. '/label',
  5150. '/div'
  5151. ];
  5152. $this->assertHtml($expected, $result);
  5153. }
  5154. /**
  5155. * testSelectMultipleCheckboxSecurity method
  5156. *
  5157. * Checks the security hash array generated for multiple-input checkbox elements.
  5158. *
  5159. * @return void
  5160. */
  5161. public function testSelectMultipleCheckboxSecurity()
  5162. {
  5163. $this->Form->request->params['_Token'] = 'testKey';
  5164. $this->assertEquals([], $this->Form->fields);
  5165. $this->Form->select(
  5166. 'Model.multi_field',
  5167. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5168. ['multiple' => 'checkbox']
  5169. );
  5170. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5171. $result = $this->Form->secure($this->Form->fields);
  5172. $key = '8af36fb34e6f2ef8ba0eb473bb4365ec232f3fe5%3A';
  5173. $this->assertContains('"' . $key . '"', $result);
  5174. }
  5175. /**
  5176. * testSelectMultipleSecureWithNoOptions method
  5177. *
  5178. * Multiple select elements should always be secured as they always participate
  5179. * in the POST data.
  5180. *
  5181. * @return void
  5182. */
  5183. public function testSelectMultipleSecureWithNoOptions()
  5184. {
  5185. $this->assertEquals([], $this->Form->fields);
  5186. $this->Form->select(
  5187. 'Model.select',
  5188. [],
  5189. ['multiple' => true]
  5190. );
  5191. $this->assertEquals(['Model.select'], $this->Form->fields);
  5192. }
  5193. /**
  5194. * testSelectNoSecureWithNoOptions method
  5195. *
  5196. * When a select box has no options it should not be added to the fields list
  5197. * as it always fail post validation.
  5198. *
  5199. * @return void
  5200. */
  5201. public function testSelectNoSecureWithNoOptions()
  5202. {
  5203. $this->Form->request->params['_Token'] = 'testkey';
  5204. $this->assertEquals([], $this->Form->fields);
  5205. $this->Form->select(
  5206. 'Model.select',
  5207. []
  5208. );
  5209. $this->assertEquals([], $this->Form->fields);
  5210. $this->Form->select(
  5211. 'Model.user_id',
  5212. [],
  5213. ['empty' => true]
  5214. );
  5215. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5216. }
  5217. /**
  5218. * testControlMultipleCheckboxes method
  5219. *
  5220. * Test control() resulting in multi select elements being generated.
  5221. *
  5222. * @return void
  5223. */
  5224. public function testControlMultipleCheckboxes()
  5225. {
  5226. $result = $this->Form->control('Model.multi_field', [
  5227. 'options' => ['first', 'second', 'third'],
  5228. 'multiple' => 'checkbox'
  5229. ]);
  5230. $expected = [
  5231. ['div' => ['class' => 'input select']],
  5232. ['label' => ['for' => 'model-multi-field']],
  5233. 'Multi Field',
  5234. '/label',
  5235. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5236. ['div' => ['class' => 'checkbox']],
  5237. ['label' => ['for' => 'model-multi-field-0']],
  5238. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5239. 'first',
  5240. '/label',
  5241. '/div',
  5242. ['div' => ['class' => 'checkbox']],
  5243. ['label' => ['for' => 'model-multi-field-1']],
  5244. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5245. 'second',
  5246. '/label',
  5247. '/div',
  5248. ['div' => ['class' => 'checkbox']],
  5249. ['label' => ['for' => 'model-multi-field-2']],
  5250. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5251. 'third',
  5252. '/label',
  5253. '/div',
  5254. '/div'
  5255. ];
  5256. $this->assertHtml($expected, $result);
  5257. $result = $this->Form->control('Model.multi_field', [
  5258. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5259. 'multiple' => 'checkbox'
  5260. ]);
  5261. $expected = [
  5262. ['div' => ['class' => 'input select']],
  5263. ['label' => ['for' => 'model-multi-field']],
  5264. 'Multi Field',
  5265. '/label',
  5266. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5267. ['div' => ['class' => 'checkbox']],
  5268. ['label' => ['for' => 'model-multi-field-a']],
  5269. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5270. 'first',
  5271. '/label',
  5272. '/div',
  5273. ['div' => ['class' => 'checkbox']],
  5274. ['label' => ['for' => 'model-multi-field-b']],
  5275. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5276. 'second',
  5277. '/label',
  5278. '/div',
  5279. ['div' => ['class' => 'checkbox']],
  5280. ['label' => ['for' => 'model-multi-field-c']],
  5281. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5282. 'third',
  5283. '/label',
  5284. '/div',
  5285. '/div'
  5286. ];
  5287. $this->assertHtml($expected, $result);
  5288. }
  5289. /**
  5290. * testSelectHiddenFieldOmission method
  5291. *
  5292. * Test that select() with 'hiddenField' => false omits the hidden field.
  5293. *
  5294. * @return void
  5295. */
  5296. public function testSelectHiddenFieldOmission()
  5297. {
  5298. $result = $this->Form->select(
  5299. 'Model.multi_field',
  5300. ['first', 'second'],
  5301. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5302. );
  5303. $this->assertNotContains('type="hidden"', $result);
  5304. }
  5305. /**
  5306. * testSelectCheckboxMultipleOverrideName method
  5307. *
  5308. * Test that select() with multiple = checkbox works with overriding name attribute.
  5309. *
  5310. * @return void
  5311. */
  5312. public function testSelectCheckboxMultipleOverrideName()
  5313. {
  5314. $result = $this->Form->select('category', ['1', '2'], [
  5315. 'multiple' => 'checkbox',
  5316. 'name' => 'fish',
  5317. ]);
  5318. $expected = [
  5319. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5320. ['div' => ['class' => 'checkbox']],
  5321. ['label' => ['for' => 'fish-0']],
  5322. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5323. '1',
  5324. '/label',
  5325. '/div',
  5326. ['div' => ['class' => 'checkbox']],
  5327. ['label' => ['for' => 'fish-1']],
  5328. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5329. '2',
  5330. '/label',
  5331. '/div'
  5332. ];
  5333. $this->assertHtml($expected, $result);
  5334. $result = $this->Form->multiCheckbox(
  5335. 'category',
  5336. new Collection(['1', '2']),
  5337. ['name' => 'fish']
  5338. );
  5339. $this->assertHtml($expected, $result);
  5340. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5341. 'name' => 'fish',
  5342. ]);
  5343. $this->assertHtml($expected, $result);
  5344. }
  5345. /**
  5346. * testControlMultiCheckbox method
  5347. *
  5348. * Test that control() works with multicheckbox.
  5349. *
  5350. * @return void
  5351. */
  5352. public function testControlMultiCheckbox()
  5353. {
  5354. $result = $this->Form->control('category', [
  5355. 'type' => 'multicheckbox',
  5356. 'options' => ['1', '2'],
  5357. ]);
  5358. $expected = [
  5359. ['div' => ['class' => 'input multicheckbox']],
  5360. '<label',
  5361. 'Category',
  5362. '/label',
  5363. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5364. ['div' => ['class' => 'checkbox']],
  5365. ['label' => ['for' => 'category-0']],
  5366. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5367. '1',
  5368. '/label',
  5369. '/div',
  5370. ['div' => ['class' => 'checkbox']],
  5371. ['label' => ['for' => 'category-1']],
  5372. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5373. '2',
  5374. '/label',
  5375. '/div',
  5376. '/div',
  5377. ];
  5378. $this->assertHtml($expected, $result);
  5379. }
  5380. /**
  5381. * testCheckbox method
  5382. *
  5383. * Test generation of checkboxes.
  5384. *
  5385. * @return void
  5386. */
  5387. public function testCheckbox()
  5388. {
  5389. $result = $this->Form->checkbox('Model.field');
  5390. $expected = [
  5391. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5392. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5393. ];
  5394. $this->assertHtml($expected, $result);
  5395. $result = $this->Form->checkbox('Model.field', [
  5396. 'id' => 'theID',
  5397. 'value' => 'myvalue',
  5398. 'form' => 'my-form',
  5399. ]);
  5400. $expected = [
  5401. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5402. ['input' => [
  5403. 'type' => 'checkbox', 'name' => 'Model[field]',
  5404. 'value' => 'myvalue', 'id' => 'theID',
  5405. 'form' => 'my-form',
  5406. ]]
  5407. ];
  5408. $this->assertHtml($expected, $result);
  5409. }
  5410. /**
  5411. * testCheckboxDefaultValue method
  5412. *
  5413. * Test default value setting on checkbox() method.
  5414. *
  5415. * @return void
  5416. */
  5417. public function testCheckboxDefaultValue()
  5418. {
  5419. $this->Form->request->data['Model']['field'] = false;
  5420. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5421. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5422. $this->assertHtml($expected, $result);
  5423. unset($this->Form->request->data['Model']['field']);
  5424. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5425. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5426. $this->assertHtml($expected, $result);
  5427. $this->Form->request->data['Model']['field'] = true;
  5428. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5429. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5430. $this->assertHtml($expected, $result);
  5431. unset($this->Form->request->data['Model']['field']);
  5432. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5433. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5434. $this->assertHtml($expected, $result);
  5435. $Articles = TableRegistry::get('Articles');
  5436. $Articles->getSchema()->addColumn(
  5437. 'published',
  5438. ['type' => 'boolean', 'null' => false, 'default' => true]
  5439. );
  5440. $this->Form->create($Articles->newEntity());
  5441. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5442. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5443. $this->assertHtml($expected, $result);
  5444. }
  5445. /**
  5446. * testCheckboxCheckedAndError method
  5447. *
  5448. * Test checkbox being checked or having errors.
  5449. *
  5450. * @return void
  5451. */
  5452. public function testCheckboxCheckedAndError()
  5453. {
  5454. $this->article['errors'] = [
  5455. 'published' => true
  5456. ];
  5457. $this->Form->request->data['published'] = 'myvalue';
  5458. $this->Form->create($this->article);
  5459. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5460. $expected = [
  5461. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5462. ['input' => [
  5463. 'type' => 'checkbox',
  5464. 'name' => 'published',
  5465. 'value' => 'myvalue',
  5466. 'id' => 'theID',
  5467. 'checked' => 'checked',
  5468. 'class' => 'form-error'
  5469. ]]
  5470. ];
  5471. $this->assertHtml($expected, $result);
  5472. $this->Form->request->data['published'] = '';
  5473. $result = $this->Form->checkbox('published');
  5474. $expected = [
  5475. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5476. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5477. ];
  5478. $this->assertHtml($expected, $result);
  5479. }
  5480. /**
  5481. * testCheckboxCustomNameAttribute method
  5482. *
  5483. * Test checkbox() with a custom name attribute.
  5484. *
  5485. * @return void
  5486. */
  5487. public function testCheckboxCustomNameAttribute()
  5488. {
  5489. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5490. $expected = [
  5491. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5492. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5493. ];
  5494. $this->assertHtml($expected, $result);
  5495. }
  5496. /**
  5497. * testCheckboxHiddenField method
  5498. *
  5499. * Test that the hidden input for checkboxes can be omitted or set to a
  5500. * specific value.
  5501. *
  5502. * @return void
  5503. */
  5504. public function testCheckboxHiddenField()
  5505. {
  5506. $result = $this->Form->checkbox('UserForm.something', [
  5507. 'hiddenField' => false
  5508. ]);
  5509. $expected = [
  5510. 'input' => [
  5511. 'type' => 'checkbox',
  5512. 'name' => 'UserForm[something]',
  5513. 'value' => '1'
  5514. ],
  5515. ];
  5516. $this->assertHtml($expected, $result);
  5517. $result = $this->Form->checkbox('UserForm.something', [
  5518. 'value' => 'Y',
  5519. 'hiddenField' => 'N',
  5520. ]);
  5521. $expected = [
  5522. ['input' => [
  5523. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5524. 'value' => 'N'
  5525. ]],
  5526. ['input' => [
  5527. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5528. 'value' => 'Y'
  5529. ]],
  5530. ];
  5531. $this->assertHtml($expected, $result);
  5532. }
  5533. /**
  5534. * testTime method
  5535. *
  5536. * Test the time type.
  5537. *
  5538. * @return void
  5539. */
  5540. public function testTime()
  5541. {
  5542. $result = $this->Form->time('start_time', [
  5543. 'timeFormat' => 12,
  5544. 'interval' => 5,
  5545. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5546. ]);
  5547. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5548. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5549. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5550. $this->assertNotContains('year', $result);
  5551. $this->assertNotContains('month', $result);
  5552. $this->assertNotContains('day', $result);
  5553. $result = $this->Form->time('start_time', [
  5554. 'timeFormat' => 12,
  5555. 'interval' => 5,
  5556. 'value' => '2014-03-08 16:30:00'
  5557. ]);
  5558. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5559. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5560. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5561. $this->assertNotContains('year', $result);
  5562. $this->assertNotContains('month', $result);
  5563. $this->assertNotContains('day', $result);
  5564. }
  5565. /**
  5566. * testTimeFormat24NoMeridian method
  5567. *
  5568. * Ensure that timeFormat=24 has no merdian.
  5569. *
  5570. * @return void.
  5571. */
  5572. public function testTimeFormat24NoMeridian()
  5573. {
  5574. $result = $this->Form->time('start_time', [
  5575. 'timeFormat' => 24,
  5576. 'interval' => 5,
  5577. 'value' => '2014-03-08 16:30:00'
  5578. ]);
  5579. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5580. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5581. $this->assertNotContains('meridian', $result);
  5582. $this->assertNotContains('pm', $result);
  5583. $this->assertNotContains('year', $result);
  5584. $this->assertNotContains('month', $result);
  5585. $this->assertNotContains('day', $result);
  5586. }
  5587. /**
  5588. * testDate method
  5589. *
  5590. * Test the date type.
  5591. *
  5592. * @return void
  5593. */
  5594. public function testDate()
  5595. {
  5596. $result = $this->Form->date('start_day', [
  5597. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5598. ]);
  5599. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5600. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5601. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5602. $this->assertNotContains('hour', $result);
  5603. $this->assertNotContains('minute', $result);
  5604. $this->assertNotContains('second', $result);
  5605. $this->assertNotContains('meridian', $result);
  5606. }
  5607. /**
  5608. * testDateTime method
  5609. *
  5610. * Test generation of date/time select elements.
  5611. *
  5612. * @return void
  5613. */
  5614. public function testDateTime()
  5615. {
  5616. extract($this->dateRegex);
  5617. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5618. $now = strtotime('now');
  5619. $expected = [
  5620. ['select' => ['name' => 'Contact[date][year]']],
  5621. ['option' => ['value' => '']],
  5622. '/option',
  5623. $yearsRegex,
  5624. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5625. date('Y', $now),
  5626. '/option',
  5627. '*/select',
  5628. ['select' => ['name' => 'Contact[date][month]']],
  5629. ['option' => ['value' => '']],
  5630. '/option',
  5631. $monthsRegex,
  5632. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5633. date('F', $now),
  5634. '/option',
  5635. '*/select',
  5636. ['select' => ['name' => 'Contact[date][day]']],
  5637. ['option' => ['value' => '']],
  5638. '/option',
  5639. $daysRegex,
  5640. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5641. date('j', $now),
  5642. '/option',
  5643. '*/select',
  5644. ['select' => ['name' => 'Contact[date][hour]']],
  5645. ['option' => ['value' => '']],
  5646. '/option',
  5647. $hoursRegex,
  5648. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5649. date('G', $now),
  5650. '/option',
  5651. '*/select',
  5652. ['select' => ['name' => 'Contact[date][minute]']],
  5653. ['option' => ['value' => '']],
  5654. '/option',
  5655. $minutesRegex,
  5656. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5657. date('i', $now),
  5658. '/option',
  5659. '*/select',
  5660. ];
  5661. $this->assertHtml($expected, $result);
  5662. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5663. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5664. $now = strtotime('now');
  5665. $expected = [
  5666. ['select' => ['name' => 'Contact[date][year]']],
  5667. $yearsRegex,
  5668. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5669. date('Y', $now),
  5670. '/option',
  5671. '*/select',
  5672. ['select' => ['name' => 'Contact[date][month]']],
  5673. $monthsRegex,
  5674. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5675. date('F', $now),
  5676. '/option',
  5677. '*/select',
  5678. ['select' => ['name' => 'Contact[date][day]']],
  5679. $daysRegex,
  5680. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5681. date('j', $now),
  5682. '/option',
  5683. '*/select',
  5684. ['select' => ['name' => 'Contact[date][hour]']],
  5685. $hoursRegex,
  5686. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5687. date('G', $now),
  5688. '/option',
  5689. '*/select',
  5690. ['select' => ['name' => 'Contact[date][minute]']],
  5691. $minutesRegex,
  5692. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5693. date('i', $now),
  5694. '/option',
  5695. '*/select',
  5696. ];
  5697. $this->assertHtml($expected, $result);
  5698. }
  5699. /**
  5700. * testDateTimeSecured method
  5701. *
  5702. * Test that datetime fields are added to protected fields list.
  5703. *
  5704. * @return void
  5705. */
  5706. public function testDateTimeSecured()
  5707. {
  5708. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5709. $this->Form->dateTime('Contact.date');
  5710. $expected = [
  5711. 'Contact.date.year',
  5712. 'Contact.date.month',
  5713. 'Contact.date.day',
  5714. 'Contact.date.hour',
  5715. 'Contact.date.minute',
  5716. ];
  5717. $this->assertEquals($expected, $this->Form->fields);
  5718. $this->Form->fields = [];
  5719. $this->Form->date('Contact.published');
  5720. $expected = [
  5721. 'Contact.published.year',
  5722. 'Contact.published.month',
  5723. 'Contact.published.day',
  5724. ];
  5725. $this->assertEquals($expected, $this->Form->fields);
  5726. }
  5727. /**
  5728. * testDateTimeSecuredDisabled method
  5729. *
  5730. * Test that datetime fields are added to protected fields list.
  5731. *
  5732. * @return void
  5733. */
  5734. public function testDateTimeSecuredDisabled()
  5735. {
  5736. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5737. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5738. $expected = [];
  5739. $this->assertEquals($expected, $this->Form->fields);
  5740. $this->Form->fields = [];
  5741. $this->Form->date('Contact.published', ['secure' => false]);
  5742. $expected = [];
  5743. $this->assertEquals($expected, $this->Form->fields);
  5744. }
  5745. /**
  5746. * testDatetimeEmpty method
  5747. *
  5748. * Test empty defaulting to true for datetime.
  5749. *
  5750. * @return void
  5751. */
  5752. public function testDatetimeEmpty()
  5753. {
  5754. extract($this->dateRegex);
  5755. $result = $this->Form->dateTime('Contact.date', [
  5756. 'timeFormat' => 12,
  5757. 'empty' => true,
  5758. 'default' => true
  5759. ]);
  5760. $expected = [
  5761. ['select' => ['name' => 'Contact[date][year]']],
  5762. $yearsRegex,
  5763. ['option' => ['value' => '']],
  5764. '/option',
  5765. '*/select',
  5766. ['select' => ['name' => 'Contact[date][month]']],
  5767. $monthsRegex,
  5768. ['option' => ['value' => '']],
  5769. '/option',
  5770. '*/select',
  5771. ['select' => ['name' => 'Contact[date][day]']],
  5772. $daysRegex,
  5773. ['option' => ['value' => '']],
  5774. '/option',
  5775. '*/select',
  5776. ['select' => ['name' => 'Contact[date][hour]']],
  5777. $hoursRegex,
  5778. ['option' => ['value' => '']],
  5779. '/option',
  5780. '*/select',
  5781. ['select' => ['name' => 'Contact[date][minute]']],
  5782. $minutesRegex,
  5783. ['option' => ['value' => '']],
  5784. '/option',
  5785. '*/select',
  5786. ['select' => ['name' => 'Contact[date][meridian]']],
  5787. $meridianRegex,
  5788. ['option' => ['value' => '']],
  5789. '/option',
  5790. '*/select'
  5791. ];
  5792. $this->assertHtml($expected, $result);
  5793. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5794. }
  5795. /**
  5796. * testDatetimeMinuteInterval method
  5797. *
  5798. * Test datetime with interval option.
  5799. *
  5800. * @return void
  5801. */
  5802. public function testDatetimeMinuteInterval()
  5803. {
  5804. extract($this->dateRegex);
  5805. $result = $this->Form->dateTime('Contact.date', [
  5806. 'interval' => 5,
  5807. 'value' => ''
  5808. ]);
  5809. $expected = [
  5810. ['select' => ['name' => 'Contact[date][year]']],
  5811. $yearsRegex,
  5812. ['option' => ['selected' => 'selected', 'value' => '']],
  5813. '/option',
  5814. '*/select',
  5815. ['select' => ['name' => 'Contact[date][month]']],
  5816. $monthsRegex,
  5817. ['option' => ['selected' => 'selected', 'value' => '']],
  5818. '/option',
  5819. '*/select',
  5820. ['select' => ['name' => 'Contact[date][day]']],
  5821. $daysRegex,
  5822. ['option' => ['selected' => 'selected', 'value' => '']],
  5823. '/option',
  5824. '*/select',
  5825. ['select' => ['name' => 'Contact[date][hour]']],
  5826. $hoursRegex,
  5827. ['option' => ['selected' => 'selected', 'value' => '']],
  5828. '/option',
  5829. '*/select',
  5830. ['select' => ['name' => 'Contact[date][minute]']],
  5831. $minutesRegex,
  5832. ['option' => ['selected' => 'selected', 'value' => '']],
  5833. '/option',
  5834. ['option' => ['value' => '00']],
  5835. '00',
  5836. '/option',
  5837. ['option' => ['value' => '05']],
  5838. '05',
  5839. '/option',
  5840. ['option' => ['value' => '10']],
  5841. '10',
  5842. '/option',
  5843. '*/select',
  5844. ];
  5845. $this->assertHtml($expected, $result);
  5846. }
  5847. /**
  5848. * testDateTimeRounding method
  5849. *
  5850. * Test dateTime with rounding.
  5851. *
  5852. * @return void
  5853. */
  5854. public function testDateTimeRounding()
  5855. {
  5856. $this->Form->request->data['Contact'] = [
  5857. 'date' => [
  5858. 'day' => '13',
  5859. 'month' => '12',
  5860. 'year' => '2010',
  5861. 'hour' => '04',
  5862. 'minute' => '19',
  5863. 'meridian' => 'AM'
  5864. ]
  5865. ];
  5866. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5867. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5868. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5869. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5870. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5871. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5872. }
  5873. /**
  5874. * testDatetimeWithDefault method
  5875. *
  5876. * Test that datetime() and default values work.
  5877. *
  5878. * @return void
  5879. */
  5880. public function testDatetimeWithDefault()
  5881. {
  5882. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5883. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5884. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5885. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5886. $result = $this->Form->dateTime('Contact.updated', [
  5887. 'default' => '2009-06-01 11:15:30'
  5888. ]);
  5889. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5890. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5891. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5892. }
  5893. /**
  5894. * testDateTimeAllZeros method
  5895. *
  5896. * Test datetime() with all zeros.
  5897. *
  5898. * @return void
  5899. */
  5900. public function testDateTimeAllZeros()
  5901. {
  5902. $result = $this->Form->dateTime('Contact.date', [
  5903. 'timeFormat' => false,
  5904. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5905. 'value' => '0000-00-00'
  5906. ]);
  5907. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5908. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5909. }
  5910. /**
  5911. * testDateTimeEmptyAsArray method
  5912. *
  5913. * @return void
  5914. */
  5915. public function testDateTimeEmptyAsArray()
  5916. {
  5917. $result = $this->Form->dateTime('Contact.date', [
  5918. 'empty' => [
  5919. 'day' => 'DAY',
  5920. 'month' => 'MONTH',
  5921. 'year' => 'YEAR',
  5922. 'hour' => 'HOUR',
  5923. 'minute' => 'MINUTE',
  5924. 'meridian' => false
  5925. ],
  5926. 'default' => true
  5927. ]);
  5928. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5929. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5930. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5931. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5932. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5933. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5934. $result = $this->Form->dateTime('Contact.date', [
  5935. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5936. 'default' => true
  5937. ]);
  5938. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5939. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5940. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5941. }
  5942. /**
  5943. * testFormDateTimeMulti method
  5944. *
  5945. * Test multiple datetime element generation.
  5946. *
  5947. * @return void
  5948. */
  5949. public function testFormDateTimeMulti()
  5950. {
  5951. extract($this->dateRegex);
  5952. $result = $this->Form->dateTime('Contact.1.updated');
  5953. $this->assertContains('Contact[1][updated][month]', $result);
  5954. $this->assertContains('Contact[1][updated][day]', $result);
  5955. $this->assertContains('Contact[1][updated][year]', $result);
  5956. $this->assertContains('Contact[1][updated][hour]', $result);
  5957. $this->assertContains('Contact[1][updated][minute]', $result);
  5958. }
  5959. /**
  5960. * testDateTimeLabelIdMatchesFirstControl method
  5961. *
  5962. * When changing the date format, the label should always focus the first select box when
  5963. * clicked.
  5964. *
  5965. * @return void
  5966. */
  5967. public function testDateTimeLabelIdMatchesFirstControl()
  5968. {
  5969. $result = $this->Form->control('Model.date', ['type' => 'date']);
  5970. $this->assertContains('<label>Date</label>', $result);
  5971. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  5972. $this->assertContains('<label>Date</label>', $result);
  5973. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  5974. $this->assertContains('<label>Date</label>', $result);
  5975. }
  5976. /**
  5977. * testDateTimeSecondOptions method
  5978. *
  5979. * Test datetime second=true.
  5980. *
  5981. * @return void
  5982. */
  5983. public function testDateTimeSecondOptions()
  5984. {
  5985. $result = $this->Form->dateTime('updated', ['second' => true]);
  5986. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5987. $result = $this->Form->dateTime('updated', ['second' => []]);
  5988. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5989. $result = $this->Form->dateTime('updated', ['second' => null]);
  5990. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5991. $result = $this->Form->dateTime('updated', ['second' => false]);
  5992. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5993. }
  5994. /**
  5995. * testMonth method
  5996. *
  5997. * Test generation of a month input.
  5998. *
  5999. * @return void
  6000. */
  6001. public function testMonth()
  6002. {
  6003. $result = $this->Form->month('Model.field', ['value' => '']);
  6004. $expected = [
  6005. ['select' => ['name' => 'Model[field][month]']],
  6006. ['option' => ['value' => '', 'selected' => 'selected']],
  6007. '/option',
  6008. ['option' => ['value' => '01']],
  6009. date('F', strtotime('2008-01-01 00:00:00')),
  6010. '/option',
  6011. ['option' => ['value' => '02']],
  6012. date('F', strtotime('2008-02-01 00:00:00')),
  6013. '/option',
  6014. '*/select',
  6015. ];
  6016. $this->assertHtml($expected, $result);
  6017. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  6018. $expected = [
  6019. ['select' => ['name' => 'Model[field][month]']],
  6020. ['option' => ['selected' => 'selected', 'value' => '']],
  6021. '/option',
  6022. ['option' => ['value' => '01']],
  6023. date('F', strtotime('2008-01-01 00:00:00')),
  6024. '/option',
  6025. ['option' => ['value' => '02']],
  6026. date('F', strtotime('2008-02-01 00:00:00')),
  6027. '/option',
  6028. '*/select',
  6029. ];
  6030. $this->assertHtml($expected, $result);
  6031. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  6032. $expected = [
  6033. ['select' => ['name' => 'Model[field][month]']],
  6034. ['option' => ['selected' => 'selected', 'value' => '']],
  6035. '/option',
  6036. ['option' => ['value' => '01']],
  6037. '1',
  6038. '/option',
  6039. ['option' => ['value' => '02']],
  6040. '2',
  6041. '/option',
  6042. '*/select',
  6043. ];
  6044. $this->assertHtml($expected, $result);
  6045. $monthNames = [
  6046. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6047. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  6048. ];
  6049. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  6050. $expected = [
  6051. ['select' => ['name' => 'Model[field][month]']],
  6052. ['option' => ['value' => '']],
  6053. '/option',
  6054. ['option' => ['value' => '01', 'selected' => 'selected']],
  6055. 'Jan',
  6056. '/option',
  6057. ['option' => ['value' => '02']],
  6058. 'Feb',
  6059. '/option',
  6060. '*/select',
  6061. ];
  6062. $this->assertHtml($expected, $result);
  6063. $this->Form->request->data['Project']['release'] = '2050-02-10';
  6064. $result = $this->Form->month('Project.release');
  6065. $expected = [
  6066. ['select' => ['name' => 'Project[release][month]']],
  6067. ['option' => ['value' => '']],
  6068. '/option',
  6069. ['option' => ['value' => '01']],
  6070. 'January',
  6071. '/option',
  6072. ['option' => ['value' => '02', 'selected' => 'selected']],
  6073. 'February',
  6074. '/option',
  6075. '*/select',
  6076. ];
  6077. $this->assertHtml($expected, $result);
  6078. $result = $this->Form->month('Contact.published', [
  6079. 'empty' => 'Published on',
  6080. ]);
  6081. $this->assertContains('Published on', $result);
  6082. }
  6083. /**
  6084. * testDay method
  6085. *
  6086. * Test generation of a day input.
  6087. *
  6088. * @return void
  6089. */
  6090. public function testDay()
  6091. {
  6092. extract($this->dateRegex);
  6093. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  6094. $expected = [
  6095. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  6096. ['option' => ['selected' => 'selected', 'value' => '']],
  6097. '/option',
  6098. ['option' => ['value' => '01']],
  6099. '1',
  6100. '/option',
  6101. ['option' => ['value' => '02']],
  6102. '2',
  6103. '/option',
  6104. $daysRegex,
  6105. '/select',
  6106. ];
  6107. $this->assertHtml($expected, $result);
  6108. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6109. $result = $this->Form->day('Model.field');
  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['Model']['field'] = '';
  6129. $result = $this->Form->day('Model.field', ['value' => '10']);
  6130. $expected = [
  6131. ['select' => ['name' => 'Model[field][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. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6149. $result = $this->Form->day('Project.release');
  6150. $expected = [
  6151. ['select' => ['name' => 'Project[release][day]']],
  6152. ['option' => ['value' => '']],
  6153. '/option',
  6154. ['option' => ['value' => '01']],
  6155. '1',
  6156. '/option',
  6157. ['option' => ['value' => '02']],
  6158. '2',
  6159. '/option',
  6160. $daysRegex,
  6161. ['option' => ['value' => '10', 'selected' => 'selected']],
  6162. '10',
  6163. '/option',
  6164. $daysRegex,
  6165. '/select',
  6166. ];
  6167. $this->assertHtml($expected, $result);
  6168. $result = $this->Form->day('Contact.published', [
  6169. 'empty' => 'Published on',
  6170. ]);
  6171. $this->assertContains('Published on', $result);
  6172. }
  6173. /**
  6174. * testMinute method
  6175. *
  6176. * Test generation of a minute input.
  6177. *
  6178. * @return void
  6179. */
  6180. public function testMinute()
  6181. {
  6182. extract($this->dateRegex);
  6183. $result = $this->Form->minute('Model.field', ['value' => '']);
  6184. $expected = [
  6185. ['select' => ['name' => 'Model[field][minute]']],
  6186. ['option' => ['selected' => 'selected', 'value' => '']],
  6187. '/option',
  6188. ['option' => ['value' => '00']],
  6189. '00',
  6190. '/option',
  6191. ['option' => ['value' => '01']],
  6192. '01',
  6193. '/option',
  6194. ['option' => ['value' => '02']],
  6195. '02',
  6196. '/option',
  6197. $minutesRegex,
  6198. '/select',
  6199. ];
  6200. $this->assertHtml($expected, $result);
  6201. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6202. $result = $this->Form->minute('Model.field');
  6203. $expected = [
  6204. ['select' => ['name' => 'Model[field][minute]']],
  6205. ['option' => ['value' => '']],
  6206. '/option',
  6207. ['option' => ['value' => '00']],
  6208. '00',
  6209. '/option',
  6210. ['option' => ['value' => '01']],
  6211. '01',
  6212. '/option',
  6213. ['option' => ['value' => '02']],
  6214. '02',
  6215. '/option',
  6216. $minutesRegex,
  6217. ['option' => ['value' => '12', 'selected' => 'selected']],
  6218. '12',
  6219. '/option',
  6220. $minutesRegex,
  6221. '/select',
  6222. ];
  6223. $this->assertHtml($expected, $result);
  6224. $this->Form->request->data['Model']['field'] = '';
  6225. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6226. $expected = [
  6227. ['select' => ['name' => 'Model[field][minute]']],
  6228. ['option' => ['selected' => 'selected', 'value' => '']],
  6229. '/option',
  6230. ['option' => ['value' => '00']],
  6231. '00',
  6232. '/option',
  6233. ['option' => ['value' => '05']],
  6234. '05',
  6235. '/option',
  6236. ['option' => ['value' => '10']],
  6237. '10',
  6238. '/option',
  6239. $minutesRegex,
  6240. '/select',
  6241. ];
  6242. $this->assertHtml($expected, $result);
  6243. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6244. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6245. $expected = [
  6246. ['select' => ['name' => 'Model[field][minute]']],
  6247. ['option' => ['value' => '']],
  6248. '/option',
  6249. ['option' => ['value' => '00']],
  6250. '00',
  6251. '/option',
  6252. ['option' => ['value' => '05']],
  6253. '05',
  6254. '/option',
  6255. ['option' => ['value' => '10', 'selected' => 'selected']],
  6256. '10',
  6257. '/option',
  6258. $minutesRegex,
  6259. '/select',
  6260. ];
  6261. $this->assertHtml($expected, $result);
  6262. }
  6263. /**
  6264. * testMeridian method
  6265. *
  6266. * Test generating an input for the meridian.
  6267. *
  6268. * @return void
  6269. */
  6270. public function testMeridian()
  6271. {
  6272. extract($this->dateRegex);
  6273. $now = new \DateTime();
  6274. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6275. $expected = [
  6276. ['select' => ['name' => 'Model[field][meridian]']],
  6277. ['option' => ['value' => '']],
  6278. '/option',
  6279. $meridianRegex,
  6280. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6281. $now->format('a'),
  6282. '/option',
  6283. '*/select'
  6284. ];
  6285. $this->assertHtml($expected, $result);
  6286. }
  6287. /**
  6288. * testHour method
  6289. *
  6290. * Test generation of an hour input.
  6291. *
  6292. * @return void
  6293. */
  6294. public function testHour()
  6295. {
  6296. extract($this->dateRegex);
  6297. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6298. $expected = [
  6299. ['select' => ['name' => 'Model[field][hour]']],
  6300. ['option' => ['selected' => 'selected', 'value' => '']],
  6301. '/option',
  6302. ['option' => ['value' => '01']],
  6303. '1',
  6304. '/option',
  6305. ['option' => ['value' => '02']],
  6306. '2',
  6307. '/option',
  6308. $hoursRegex,
  6309. '/select',
  6310. ];
  6311. $this->assertHtml($expected, $result);
  6312. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6313. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6314. $expected = [
  6315. ['select' => ['name' => 'Model[field][hour]']],
  6316. ['option' => ['value' => '']],
  6317. '/option',
  6318. ['option' => ['value' => '01']],
  6319. '1',
  6320. '/option',
  6321. ['option' => ['value' => '02']],
  6322. '2',
  6323. '/option',
  6324. $hoursRegex,
  6325. ['option' => ['value' => '12', 'selected' => 'selected']],
  6326. '12',
  6327. '/option',
  6328. '/select',
  6329. ];
  6330. $this->assertHtml($expected, $result);
  6331. $this->Form->request->data['Model']['field'] = '';
  6332. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6333. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6334. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6335. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6336. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6337. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6338. $expected = [
  6339. ['select' => ['name' => 'Model[field][hour]']],
  6340. ['option' => ['value' => '']],
  6341. '/option',
  6342. ['option' => ['value' => '00', 'selected' => 'selected']],
  6343. '0',
  6344. '/option',
  6345. ['option' => ['value' => '01']],
  6346. '1',
  6347. '/option',
  6348. ['option' => ['value' => '02']],
  6349. '2',
  6350. '/option',
  6351. $hoursRegex,
  6352. '/select',
  6353. ];
  6354. $this->assertHtml($expected, $result);
  6355. unset($this->Form->request->data['Model']['field']);
  6356. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6357. $thisHour = date('H');
  6358. $optValue = date('G');
  6359. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6360. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6361. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6362. $expected = [
  6363. ['select' => ['name' => 'Model[field][hour]']],
  6364. ['option' => ['value' => '']],
  6365. '/option',
  6366. ['option' => ['value' => '00']],
  6367. '0',
  6368. '/option',
  6369. ['option' => ['value' => '01', 'selected' => 'selected']],
  6370. '1',
  6371. '/option',
  6372. ['option' => ['value' => '02']],
  6373. '2',
  6374. '/option',
  6375. $hoursRegex,
  6376. '/select',
  6377. ];
  6378. $this->assertHtml($expected, $result);
  6379. }
  6380. /**
  6381. * testYear method
  6382. *
  6383. * Test generation of a year input.
  6384. *
  6385. * @return void
  6386. */
  6387. public function testYear()
  6388. {
  6389. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6390. $expected = [
  6391. ['select' => ['name' => 'Model[field][year]']],
  6392. ['option' => ['selected' => 'selected', 'value' => '']],
  6393. '/option',
  6394. ['option' => ['value' => '2007']],
  6395. '2007',
  6396. '/option',
  6397. ['option' => ['value' => '2006']],
  6398. '2006',
  6399. '/option',
  6400. '/select',
  6401. ];
  6402. $this->assertHtml($expected, $result);
  6403. $result = $this->Form->year('Model.field', [
  6404. 'value' => '',
  6405. 'minYear' => 2006,
  6406. 'maxYear' => 2007,
  6407. 'orderYear' => 'asc'
  6408. ]);
  6409. $expected = [
  6410. ['select' => ['name' => 'Model[field][year]']],
  6411. ['option' => ['selected' => 'selected', 'value' => '']],
  6412. '/option',
  6413. ['option' => ['value' => '2006']],
  6414. '2006',
  6415. '/option',
  6416. ['option' => ['value' => '2007']],
  6417. '2007',
  6418. '/option',
  6419. '/select',
  6420. ];
  6421. $this->assertHtml($expected, $result);
  6422. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6423. $result = $this->Form->year('Contact.published', [
  6424. 'empty' => false,
  6425. 'minYear' => 2006,
  6426. 'maxYear' => 2007,
  6427. ]);
  6428. $expected = [
  6429. ['select' => ['name' => 'Contact[published][year]']],
  6430. ['option' => ['value' => '2007']],
  6431. '2007',
  6432. '/option',
  6433. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6434. '2006',
  6435. '/option',
  6436. '/select',
  6437. ];
  6438. $this->assertHtml($expected, $result);
  6439. $result = $this->Form->year('Contact.published', [
  6440. 'empty' => 'Published on',
  6441. ]);
  6442. $this->assertContains('Published on', $result);
  6443. }
  6444. /**
  6445. * testControlDatetimePreEpoch method
  6446. *
  6447. * Test minYear being prior to the unix epoch.
  6448. *
  6449. * @return void
  6450. */
  6451. public function testControlDatetimePreEpoch()
  6452. {
  6453. $start = date('Y') - 80;
  6454. $end = date('Y') - 18;
  6455. $result = $this->Form->control('birth_year', [
  6456. 'type' => 'date',
  6457. 'label' => 'Birth Year',
  6458. 'minYear' => $start,
  6459. 'maxYear' => $end,
  6460. 'month' => false,
  6461. 'day' => false,
  6462. ]);
  6463. $this->assertContains('value="' . $start . '">' . $start, $result);
  6464. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6465. $this->assertNotContains('value="00">00', $result);
  6466. }
  6467. /**
  6468. * testYearAutoExpandRange method
  6469. *
  6470. * @return void
  6471. */
  6472. public function testYearAutoExpandRange()
  6473. {
  6474. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6475. $result = $this->Form->year('User.birthday');
  6476. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6477. $result = $matches[1];
  6478. $expected = range(date('Y') + 5, 1930);
  6479. $this->assertEquals($expected, $result);
  6480. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6481. $result = $this->Form->year('Project.release');
  6482. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6483. $result = $matches[1];
  6484. $expected = range(2050, date('Y') - 5);
  6485. $this->assertEquals($expected, $result);
  6486. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6487. $result = $this->Form->year('Project.release', [
  6488. 'minYear' => 1890,
  6489. 'maxYear' => 1900
  6490. ]);
  6491. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6492. $result = $matches[1];
  6493. $expected = range(1900, 1881);
  6494. $this->assertEquals($expected, $result);
  6495. }
  6496. /**
  6497. * testControlDate method
  6498. *
  6499. * Test that control() accepts the type of date and passes options in.
  6500. *
  6501. * @return void
  6502. */
  6503. public function testControlDate()
  6504. {
  6505. $this->Form->request->data = [
  6506. 'month_year' => ['month' => date('m')],
  6507. ];
  6508. $this->Form->create($this->article);
  6509. $result = $this->Form->control('month_year', [
  6510. 'label' => false,
  6511. 'type' => 'date',
  6512. 'minYear' => 2006,
  6513. 'maxYear' => 2008
  6514. ]);
  6515. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6516. $this->assertNotContains('value="2008" selected="selected"', $result);
  6517. }
  6518. /**
  6519. * testControlLabelFalse method
  6520. *
  6521. * Test the label option being set to false.
  6522. *
  6523. * @return void
  6524. */
  6525. public function testControlLabelFalse()
  6526. {
  6527. $this->Form->create($this->article);
  6528. $result = $this->Form->control('title', ['label' => false]);
  6529. $expected = [
  6530. 'div' => ['class' => 'input text required'],
  6531. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6532. '/div'
  6533. ];
  6534. $this->assertHtml($expected, $result);
  6535. }
  6536. /**
  6537. * testControlDateMaxYear method
  6538. *
  6539. * Let's say we want to only allow users born from 2006 to 2008 to register
  6540. * This being the first signup page, we still don't have any data.
  6541. *
  6542. * @return void
  6543. */
  6544. public function testControlDateMaxYear()
  6545. {
  6546. $this->Form->request->data = [];
  6547. $this->Form->create($this->article);
  6548. $result = $this->Form->control('birthday', [
  6549. 'label' => false,
  6550. 'type' => 'date',
  6551. 'minYear' => 2006,
  6552. 'maxYear' => 2008,
  6553. 'default' => true
  6554. ]);
  6555. $this->assertContains('value="2008" selected="selected"', $result);
  6556. $this->assertContains('value="2006"', $result);
  6557. $this->assertNotContains('value="2005"', $result);
  6558. $this->assertNotContains('value="2009"', $result);
  6559. }
  6560. /**
  6561. * testTextArea method
  6562. *
  6563. * Test generation of a textarea input.
  6564. *
  6565. * @return void
  6566. */
  6567. public function testTextArea()
  6568. {
  6569. $this->Form->request->data = ['field' => 'some test data'];
  6570. $result = $this->Form->textarea('field');
  6571. $expected = [
  6572. 'textarea' => ['name' => 'field', 'rows' => 5],
  6573. 'some test data',
  6574. '/textarea',
  6575. ];
  6576. $this->assertHtml($expected, $result);
  6577. $result = $this->Form->textarea('user.bio');
  6578. $expected = [
  6579. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6580. '/textarea',
  6581. ];
  6582. $this->assertHtml($expected, $result);
  6583. $this->Form->request->data = ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'];
  6584. $result = $this->Form->textarea('field');
  6585. $expected = [
  6586. 'textarea' => ['name' => 'field', 'rows' => 5],
  6587. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6588. '/textarea',
  6589. ];
  6590. $this->assertHtml($expected, $result);
  6591. $this->Form->request->data = [
  6592. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  6593. ];
  6594. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6595. $expected = [
  6596. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6597. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6598. '/textarea',
  6599. ];
  6600. $this->assertHtml($expected, $result);
  6601. $result = $this->Form->textarea('0.OtherModel.field');
  6602. $expected = [
  6603. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6604. '/textarea'
  6605. ];
  6606. $this->assertHtml($expected, $result);
  6607. }
  6608. /**
  6609. * testTextAreaWithStupidCharacters method
  6610. *
  6611. * Test text area with non-ascii characters.
  6612. *
  6613. * @return void
  6614. */
  6615. public function testTextAreaWithStupidCharacters()
  6616. {
  6617. $result = $this->Form->textarea('Post.content', [
  6618. 'value' => 'GREAT®',
  6619. 'rows' => '15',
  6620. 'cols' => '75'
  6621. ]);
  6622. $expected = [
  6623. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6624. 'GREAT®',
  6625. '/textarea',
  6626. ];
  6627. $this->assertHtml($expected, $result);
  6628. }
  6629. /**
  6630. * testTextAreaMaxLength method
  6631. *
  6632. * Test textareas maxlength read from schema.
  6633. *
  6634. * @return void
  6635. */
  6636. public function testTextAreaMaxLength()
  6637. {
  6638. $this->Form->create([
  6639. 'schema' => [
  6640. 'stuff' => ['type' => 'string', 'length' => 10],
  6641. ]
  6642. ]);
  6643. $result = $this->Form->control('other', ['type' => 'textarea']);
  6644. $expected = [
  6645. 'div' => ['class' => 'input textarea'],
  6646. 'label' => ['for' => 'other'],
  6647. 'Other',
  6648. '/label',
  6649. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6650. '/textarea',
  6651. '/div'
  6652. ];
  6653. $this->assertHtml($expected, $result);
  6654. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6655. $expected = [
  6656. 'div' => ['class' => 'input textarea'],
  6657. 'label' => ['for' => 'stuff'],
  6658. 'Stuff',
  6659. '/label',
  6660. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6661. '/textarea',
  6662. '/div'
  6663. ];
  6664. $this->assertHtml($expected, $result);
  6665. }
  6666. /**
  6667. * testHiddenField method
  6668. *
  6669. * Test generation of a hidden input.
  6670. *
  6671. * @return void
  6672. */
  6673. public function testHiddenField()
  6674. {
  6675. $this->article['errors'] = [
  6676. 'field' => true
  6677. ];
  6678. $this->Form->request->data['field'] = 'test';
  6679. $this->Form->create($this->article);
  6680. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6681. $expected = [
  6682. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6683. $this->assertHtml($expected, $result);
  6684. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6685. $expected = [
  6686. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6687. ];
  6688. $this->assertHtml($expected, $result);
  6689. }
  6690. /**
  6691. * testFileUploadField method
  6692. *
  6693. * Test generation of a file upload input.
  6694. *
  6695. * @return void
  6696. */
  6697. public function testFileUploadField()
  6698. {
  6699. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6700. $result = $this->Form->file('Model.upload');
  6701. $this->assertHtml($expected, $result);
  6702. $this->Form->request->data['Model']['upload'] = [
  6703. 'name' => '', 'type' => '', 'tmp_name' => '',
  6704. 'error' => 4, 'size' => 0
  6705. ];
  6706. $result = $this->Form->file('Model.upload');
  6707. $this->assertHtml($expected, $result);
  6708. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6709. $result = $this->Form->file('Model.upload');
  6710. $this->assertHtml($expected, $result);
  6711. }
  6712. /**
  6713. * testFileUploadOnOtherModel method
  6714. *
  6715. * Test File upload input on a model not used in create().
  6716. *
  6717. * @return void
  6718. */
  6719. public function testFileUploadOnOtherModel()
  6720. {
  6721. $this->Form->create($this->article, ['type' => 'file']);
  6722. $result = $this->Form->file('ValidateProfile.city');
  6723. $expected = [
  6724. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6725. ];
  6726. $this->assertHtml($expected, $result);
  6727. }
  6728. /**
  6729. * testButton method
  6730. *
  6731. * Test generation of a form button.
  6732. *
  6733. * @return void
  6734. */
  6735. public function testButton()
  6736. {
  6737. $result = $this->Form->button('Hi');
  6738. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6739. $this->assertHtml($expected, $result);
  6740. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6741. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6742. $this->assertHtml($expected, $result);
  6743. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6744. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6745. $this->assertHtml($expected, $result);
  6746. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6747. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6748. $this->assertHtml($expected, $result);
  6749. $result = $this->Form->button('No type', ['type' => false]);
  6750. $expected = ['button' => [], 'No type', '/button'];
  6751. $this->assertHtml($expected, $result);
  6752. $result = $this->Form->button('Upload Text', [
  6753. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6754. 'escape' => false
  6755. ]);
  6756. $this->assertNotRegExp('/\&039/', $result);
  6757. }
  6758. /**
  6759. * testButtonUnlockedByDefault method
  6760. *
  6761. * Test that button() makes unlocked fields by default.
  6762. *
  6763. * @return void
  6764. */
  6765. public function testButtonUnlockedByDefault()
  6766. {
  6767. $this->Form->request->params['_csrfToken'] = 'secured';
  6768. $this->Form->button('Save', ['name' => 'save']);
  6769. $this->Form->button('Clear');
  6770. $result = $this->Form->unlockField();
  6771. $this->assertEquals(['save'], $result);
  6772. }
  6773. /**
  6774. * Test generation of a form button with confirm message.
  6775. *
  6776. * @return void
  6777. */
  6778. public function testButtonWithConfirm()
  6779. {
  6780. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6781. $expected = ['button' => ['type' => 'submit', 'onclick' => 'if (confirm(&quot;Confirm me!&quot;)) { return true; } return false;'], 'Hi', '/button'];
  6782. $this->assertHtml($expected, $result);
  6783. }
  6784. /**
  6785. * testPostButton method
  6786. *
  6787. * @return void
  6788. */
  6789. public function testPostButton()
  6790. {
  6791. $result = $this->Form->postButton('Hi', '/controller/action');
  6792. $expected = [
  6793. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6794. 'div' => ['style' => 'display:none;'],
  6795. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6796. '/div',
  6797. 'button' => ['type' => 'submit'],
  6798. 'Hi',
  6799. '/button',
  6800. '/form'
  6801. ];
  6802. $this->assertHtml($expected, $result);
  6803. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6804. $this->assertNotFalse(strpos($result, '<input type="hidden" name="extra" value="value"'));
  6805. }
  6806. /**
  6807. * testPostButtonMethodType method
  6808. *
  6809. * @return void
  6810. */
  6811. public function testPostButtonMethodType()
  6812. {
  6813. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6814. $expected = [
  6815. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6816. 'div' => ['style' => 'display:none;'],
  6817. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6818. '/div',
  6819. 'button' => ['type' => 'submit'],
  6820. 'Hi',
  6821. '/button',
  6822. '/form'
  6823. ];
  6824. $this->assertHtml($expected, $result);
  6825. }
  6826. /**
  6827. * testPostButtonFormOptions method
  6828. *
  6829. * @return void
  6830. */
  6831. public function testPostButtonFormOptions()
  6832. {
  6833. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6834. $expected = [
  6835. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6836. 'div' => ['style' => 'display:none;'],
  6837. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6838. '/div',
  6839. 'button' => ['type' => 'submit'],
  6840. 'Hi',
  6841. '/button',
  6842. '/form'
  6843. ];
  6844. $this->assertHtml($expected, $result);
  6845. }
  6846. /**
  6847. * testPostButtonNestedData method
  6848. *
  6849. * Test using postButton with N dimensional data.
  6850. *
  6851. * @return void
  6852. */
  6853. public function testPostButtonNestedData()
  6854. {
  6855. $data = [
  6856. 'one' => [
  6857. 'two' => [
  6858. 3, 4, 5
  6859. ]
  6860. ]
  6861. ];
  6862. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6863. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6864. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6865. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6866. }
  6867. /**
  6868. * testSecurePostButton method
  6869. *
  6870. * Test that postButton adds _Token fields.
  6871. *
  6872. * @return void
  6873. */
  6874. public function testSecurePostButton()
  6875. {
  6876. $this->Form->request->params['_csrfToken'] = 'testkey';
  6877. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  6878. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6879. $tokenDebug = urlencode(json_encode([
  6880. '/posts/delete/1',
  6881. [],
  6882. []
  6883. ]));
  6884. $expected = [
  6885. 'form' => [
  6886. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6887. ],
  6888. ['div' => ['style' => 'display:none;']],
  6889. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6890. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6891. '/div',
  6892. 'button' => ['type' => 'submit'],
  6893. 'Delete',
  6894. '/button',
  6895. ['div' => ['style' => 'display:none;']],
  6896. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6897. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6898. ['input' => [
  6899. 'type' => 'hidden',
  6900. 'name' => '_Token[debug]',
  6901. 'value' => $tokenDebug,
  6902. 'autocomplete' => 'off',
  6903. ]],
  6904. '/div',
  6905. '/form',
  6906. ];
  6907. $this->assertHtml($expected, $result);
  6908. }
  6909. /**
  6910. * testPostLink method
  6911. *
  6912. * @return void
  6913. */
  6914. public function testPostLink()
  6915. {
  6916. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6917. $expected = [
  6918. 'form' => [
  6919. 'method' => 'post', 'action' => '/posts/delete/1',
  6920. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6921. ],
  6922. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6923. '/form',
  6924. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6925. 'Delete',
  6926. '/a'
  6927. ];
  6928. $this->assertHtml($expected, $result);
  6929. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6930. $expected = [
  6931. 'form' => [
  6932. 'method' => 'post', 'action' => '/posts/delete/1',
  6933. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6934. ],
  6935. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6936. '/form',
  6937. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6938. 'Delete',
  6939. '/a'
  6940. ];
  6941. $this->assertHtml($expected, $result);
  6942. $result = $this->Form->postLink(
  6943. 'Delete',
  6944. '/posts/delete/1',
  6945. ['target' => '_blank', 'class' => 'btn btn-danger']
  6946. );
  6947. $expected = [
  6948. 'form' => [
  6949. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6950. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6951. ],
  6952. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6953. '/form',
  6954. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6955. 'Delete',
  6956. '/a'
  6957. ];
  6958. $this->assertHtml($expected, $result);
  6959. }
  6960. /**
  6961. * testPostLinkWithConfirm method
  6962. *
  6963. * Test the confirm option for postLink().
  6964. *
  6965. * @return void
  6966. */
  6967. public function testPostLinkWithConfirm()
  6968. {
  6969. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6970. $expected = [
  6971. 'form' => [
  6972. 'method' => 'post', 'action' => '/posts/delete/1',
  6973. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6974. ],
  6975. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6976. '/form',
  6977. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  6978. 'Delete',
  6979. '/a'
  6980. ];
  6981. $this->assertHtml($expected, $result);
  6982. $result = $this->Form->postLink(
  6983. 'Delete',
  6984. '/posts/delete/1',
  6985. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  6986. );
  6987. $expected = [
  6988. 'form' => [
  6989. 'method' => 'post', 'action' => '/posts/delete/1',
  6990. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6991. ],
  6992. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6993. '/form',
  6994. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  6995. 'Delete',
  6996. '/a'
  6997. ];
  6998. $this->assertHtml($expected, $result);
  6999. }
  7000. /**
  7001. * testPostLinkWithQuery method
  7002. *
  7003. * Test postLink() with query string args.
  7004. *
  7005. * @return void
  7006. */
  7007. public function testPostLinkWithQuery()
  7008. {
  7009. $result = $this->Form->postLink(
  7010. 'Delete',
  7011. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  7012. );
  7013. $expected = [
  7014. 'form' => [
  7015. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  7016. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7017. ],
  7018. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7019. '/form',
  7020. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7021. 'Delete',
  7022. '/a'
  7023. ];
  7024. $this->assertHtml($expected, $result);
  7025. }
  7026. /**
  7027. * testPostLinkWithData method
  7028. *
  7029. * Test postLink with additional data.
  7030. *
  7031. * @return void
  7032. */
  7033. public function testPostLinkWithData()
  7034. {
  7035. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  7036. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  7037. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  7038. $this->Form->create($entity);
  7039. $this->Form->end();
  7040. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  7041. $this->assertContains(
  7042. '<input type="hidden" name="name" value="show"',
  7043. $result,
  7044. 'should not contain entity data.'
  7045. );
  7046. }
  7047. /**
  7048. * testPostLinkSecurityHash method
  7049. *
  7050. * Test that security hashes for postLink include the url.
  7051. *
  7052. * @return void
  7053. */
  7054. public function testPostLinkSecurityHash()
  7055. {
  7056. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7057. $hash .= '%3Aid';
  7058. $this->Form->request->params['_Token']['key'] = 'test';
  7059. $result = $this->Form->postLink(
  7060. 'Delete',
  7061. '/posts/delete/1',
  7062. ['data' => ['id' => 1]]
  7063. );
  7064. $tokenDebug = urlencode(json_encode([
  7065. '/posts/delete/1',
  7066. [
  7067. 'id' => 1
  7068. ],
  7069. []
  7070. ]));
  7071. $expected = [
  7072. 'form' => [
  7073. 'method' => 'post', 'action' => '/posts/delete/1',
  7074. 'name', 'style' => 'display:none;'
  7075. ],
  7076. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7077. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7078. 'div' => ['style' => 'display:none;'],
  7079. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7080. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7081. ['input' => [
  7082. 'type' => 'hidden',
  7083. 'name' => '_Token[debug]',
  7084. 'value' => $tokenDebug,
  7085. 'autocomplete' => 'off',
  7086. ]],
  7087. '/div',
  7088. '/form',
  7089. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7090. 'Delete',
  7091. '/a'
  7092. ];
  7093. $this->assertHtml($expected, $result);
  7094. }
  7095. /**
  7096. * testPostLinkSecurityHashBlockMode method
  7097. *
  7098. * Test that postLink doesn't modify the fields in the containing form.
  7099. *
  7100. * postLink() calls inside open forms should not modify the field list
  7101. * for the form.
  7102. *
  7103. * @return void
  7104. */
  7105. public function testPostLinkSecurityHashBlockMode()
  7106. {
  7107. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  7108. $hash .= '%3A';
  7109. $this->Form->request->params['_Token']['key'] = 'test';
  7110. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  7111. $this->Form->control('title');
  7112. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7113. $result = $this->View->fetch('postLink');
  7114. $this->assertEquals(['title'], $this->Form->fields);
  7115. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  7116. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  7117. }
  7118. /**
  7119. * testPostLinkSecurityHashNoDebugMode method
  7120. *
  7121. * Test that security does not include debug token if debug is false.
  7122. *
  7123. * @return void
  7124. */
  7125. public function testPostLinkSecurityHashNoDebugMode()
  7126. {
  7127. Configure::write('debug', false);
  7128. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7129. $hash .= '%3Aid';
  7130. $this->Form->request->params['_Token']['key'] = 'test';
  7131. $result = $this->Form->postLink(
  7132. 'Delete',
  7133. '/posts/delete/1',
  7134. ['data' => ['id' => 1]]
  7135. );
  7136. $expected = [
  7137. 'form' => [
  7138. 'method' => 'post', 'action' => '/posts/delete/1',
  7139. 'name', 'style' => 'display:none;'
  7140. ],
  7141. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7142. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7143. 'div' => ['style' => 'display:none;'],
  7144. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7145. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7146. '/div',
  7147. '/form',
  7148. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7149. 'Delete',
  7150. '/a'
  7151. ];
  7152. $this->assertHtml($expected, $result);
  7153. }
  7154. /**
  7155. * testPostLinkNestedData method
  7156. *
  7157. * Test using postLink with N dimensional data.
  7158. *
  7159. * @return void
  7160. */
  7161. public function testPostLinkNestedData()
  7162. {
  7163. $data = [
  7164. 'one' => [
  7165. 'two' => [
  7166. 3, 4, 5
  7167. ]
  7168. ]
  7169. ];
  7170. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7171. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7172. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7173. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7174. }
  7175. /**
  7176. * testPostLinkAfterGetForm method
  7177. *
  7178. * Test creating postLinks after a GET form.
  7179. *
  7180. * @return void
  7181. */
  7182. public function testPostLinkAfterGetForm()
  7183. {
  7184. $this->Form->request->params['_csrfToken'] = 'testkey';
  7185. $this->Form->request->params['_Token'] = 'val';
  7186. $this->Form->create($this->article, ['type' => 'get']);
  7187. $this->Form->end();
  7188. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7189. $tokenDebug = urlencode(json_encode([
  7190. '/posts/delete/1',
  7191. [],
  7192. []
  7193. ]));
  7194. $expected = [
  7195. 'form' => [
  7196. 'method' => 'post', 'action' => '/posts/delete/1',
  7197. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7198. ],
  7199. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7200. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7201. 'div' => ['style' => 'display:none;'],
  7202. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7203. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7204. ['input' => [
  7205. 'type' => 'hidden', 'name' => '_Token[debug]',
  7206. 'value' => $tokenDebug,
  7207. 'autocomplete' => 'off'
  7208. ]],
  7209. '/div',
  7210. '/form',
  7211. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7212. 'Delete',
  7213. '/a'
  7214. ];
  7215. $this->assertHtml($expected, $result);
  7216. }
  7217. /**
  7218. * testPostLinkFormBuffer method
  7219. *
  7220. * Test that postLink adds form tags to view block.
  7221. *
  7222. * @return void
  7223. */
  7224. public function testPostLinkFormBuffer()
  7225. {
  7226. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7227. $expected = [
  7228. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7229. 'Delete',
  7230. '/a'
  7231. ];
  7232. $this->assertHtml($expected, $result);
  7233. $result = $this->View->fetch('postLink');
  7234. $expected = [
  7235. 'form' => [
  7236. 'method' => 'post', 'action' => '/posts/delete/1',
  7237. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7238. ],
  7239. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7240. '/form'
  7241. ];
  7242. $this->assertHtml($expected, $result);
  7243. $result = $this->Form->postLink(
  7244. 'Delete',
  7245. '/posts/delete/2',
  7246. ['block' => true, 'method' => 'DELETE']
  7247. );
  7248. $expected = [
  7249. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7250. 'Delete',
  7251. '/a'
  7252. ];
  7253. $this->assertHtml($expected, $result);
  7254. $result = $this->View->fetch('postLink');
  7255. $expected = [
  7256. 'form' => [
  7257. 'method' => 'post', 'action' => '/posts/delete/1',
  7258. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7259. ],
  7260. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7261. '/form',
  7262. [
  7263. 'form' => [
  7264. 'method' => 'post', 'action' => '/posts/delete/2',
  7265. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7266. ],
  7267. ],
  7268. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7269. '/form'
  7270. ];
  7271. $this->assertHtml($expected, $result);
  7272. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7273. $expected = [
  7274. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7275. 'Delete',
  7276. '/a'
  7277. ];
  7278. $this->assertHtml($expected, $result);
  7279. $result = $this->View->fetch('foobar');
  7280. $expected = [
  7281. 'form' => [
  7282. 'method' => 'post', 'action' => '/posts/delete/1',
  7283. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7284. ],
  7285. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7286. '/form'
  7287. ];
  7288. $this->assertHtml($expected, $result);
  7289. }
  7290. /**
  7291. * testSubmitButton method
  7292. *
  7293. * @return void
  7294. */
  7295. public function testSubmitButton()
  7296. {
  7297. $result = $this->Form->submit('');
  7298. $expected = [
  7299. 'div' => ['class' => 'submit'],
  7300. 'input' => ['type' => 'submit', 'value' => ''],
  7301. '/div'
  7302. ];
  7303. $this->assertHtml($expected, $result);
  7304. $result = $this->Form->submit('Test Submit');
  7305. $expected = [
  7306. 'div' => ['class' => 'submit'],
  7307. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7308. '/div'
  7309. ];
  7310. $this->assertHtml($expected, $result);
  7311. $result = $this->Form->submit('Next >');
  7312. $expected = [
  7313. 'div' => ['class' => 'submit'],
  7314. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7315. '/div'
  7316. ];
  7317. $this->assertHtml($expected, $result);
  7318. $result = $this->Form->submit('Next >', ['escape' => false]);
  7319. $expected = [
  7320. 'div' => ['class' => 'submit'],
  7321. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7322. '/div'
  7323. ];
  7324. $this->assertHtml($expected, $result);
  7325. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7326. $expected = [
  7327. 'div' => ['class' => 'submit'],
  7328. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7329. '/div'
  7330. ];
  7331. $this->assertHtml($expected, $result);
  7332. }
  7333. /**
  7334. * testSubmitImage method
  7335. *
  7336. * Test image submit types.
  7337. *
  7338. * @return void
  7339. */
  7340. public function testSubmitImage()
  7341. {
  7342. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7343. $expected = [
  7344. 'div' => ['class' => 'submit'],
  7345. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7346. '/div'
  7347. ];
  7348. $this->assertHtml($expected, $result);
  7349. $result = $this->Form->submit('/relative/cake.power.gif');
  7350. $expected = [
  7351. 'div' => ['class' => 'submit'],
  7352. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7353. '/div'
  7354. ];
  7355. $this->assertHtml($expected, $result);
  7356. $result = $this->Form->submit('cake.power.gif');
  7357. $expected = [
  7358. 'div' => ['class' => 'submit'],
  7359. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7360. '/div'
  7361. ];
  7362. $this->assertHtml($expected, $result);
  7363. $result = $this->Form->submit('Not.an.image');
  7364. $expected = [
  7365. 'div' => ['class' => 'submit'],
  7366. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7367. '/div'
  7368. ];
  7369. $this->assertHtml($expected, $result);
  7370. }
  7371. /**
  7372. * testSubmitUnlockedByDefault method
  7373. *
  7374. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7375. * be submitted at a time.
  7376. *
  7377. * @return void
  7378. */
  7379. public function testSubmitUnlockedByDefault()
  7380. {
  7381. $this->Form->request->params['_Token'] = 'secured';
  7382. $this->Form->submit('Go go');
  7383. $this->Form->submit('Save', ['name' => 'save']);
  7384. $result = $this->Form->unlockField();
  7385. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7386. }
  7387. /**
  7388. * testSubmitImageTimestamp method
  7389. *
  7390. * Test submit image with timestamps.
  7391. *
  7392. * @return void
  7393. */
  7394. public function testSubmitImageTimestamp()
  7395. {
  7396. Configure::write('Asset.timestamp', 'force');
  7397. $result = $this->Form->submit('cake.power.gif');
  7398. $expected = [
  7399. 'div' => ['class' => 'submit'],
  7400. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7401. '/div'
  7402. ];
  7403. $this->assertHtml($expected, $result);
  7404. }
  7405. /**
  7406. * testDateTimeWithGetForms method
  7407. *
  7408. * Test that datetime() works with GET style forms.
  7409. *
  7410. * @return void
  7411. */
  7412. public function testDateTimeWithGetForms()
  7413. {
  7414. extract($this->dateRegex);
  7415. $this->Form->create($this->article, ['type' => 'get']);
  7416. $result = $this->Form->datetime('created');
  7417. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7418. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7419. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7420. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7421. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7422. }
  7423. /**
  7424. * testForMagicControlNonExistingNorValidated method
  7425. *
  7426. * @return void
  7427. */
  7428. public function testForMagicControlNonExistingNorValidated()
  7429. {
  7430. $this->Form->create($this->article);
  7431. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7432. $result = $this->Form->control('non_existing_nor_validated');
  7433. $expected = [
  7434. 'label' => ['for' => 'non-existing-nor-validated'],
  7435. 'Non Existing Nor Validated',
  7436. '/label',
  7437. 'input' => [
  7438. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7439. 'id' => 'non-existing-nor-validated'
  7440. ]
  7441. ];
  7442. $this->assertHtml($expected, $result);
  7443. $result = $this->Form->control('non_existing_nor_validated', [
  7444. 'val' => 'my value'
  7445. ]);
  7446. $expected = [
  7447. 'label' => ['for' => 'non-existing-nor-validated'],
  7448. 'Non Existing Nor Validated',
  7449. '/label',
  7450. 'input' => [
  7451. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7452. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7453. ]
  7454. ];
  7455. $this->assertHtml($expected, $result);
  7456. $this->Form->request->data = ['non_existing_nor_validated' => 'CakePHP magic'];
  7457. $result = $this->Form->control('non_existing_nor_validated');
  7458. $expected = [
  7459. 'label' => ['for' => 'non-existing-nor-validated'],
  7460. 'Non Existing Nor Validated',
  7461. '/label',
  7462. 'input' => [
  7463. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7464. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7465. ]
  7466. ];
  7467. $this->assertHtml($expected, $result);
  7468. }
  7469. /**
  7470. * testFormMagicControlLabel method
  7471. *
  7472. * @return void
  7473. */
  7474. public function testFormMagicControlLabel()
  7475. {
  7476. TableRegistry::get('Contacts', [
  7477. 'className' => __NAMESPACE__ . '\ContactsTable'
  7478. ]);
  7479. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7480. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7481. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7482. $expected = [
  7483. 'label' => ['for' => 'contacts-name'],
  7484. 'My label',
  7485. '/label',
  7486. 'input' => [
  7487. 'type' => 'text', 'name' => 'Contacts[name]',
  7488. 'id' => 'contacts-name', 'maxlength' => '255'
  7489. ]
  7490. ];
  7491. $this->assertHtml($expected, $result);
  7492. $result = $this->Form->control('name', [
  7493. 'label' => ['class' => 'mandatory']
  7494. ]);
  7495. $expected = [
  7496. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7497. 'Name',
  7498. '/label',
  7499. 'input' => [
  7500. 'type' => 'text', 'name' => 'name',
  7501. 'id' => 'name', 'maxlength' => '255'
  7502. ]
  7503. ];
  7504. $this->assertHtml($expected, $result);
  7505. $result = $this->Form->control('name', [
  7506. 'div' => false,
  7507. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7508. ]);
  7509. $expected = [
  7510. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7511. 'My label',
  7512. '/label',
  7513. 'input' => [
  7514. 'type' => 'text', 'name' => 'name',
  7515. 'id' => 'name', 'maxlength' => '255'
  7516. ]
  7517. ];
  7518. $this->assertHtml($expected, $result);
  7519. $result = $this->Form->control('Contact.name', [
  7520. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7521. ]);
  7522. $expected = [
  7523. 'label' => ['for' => 'my_id'],
  7524. 'Name',
  7525. '/label',
  7526. 'input' => [
  7527. 'type' => 'text', 'name' => 'Contact[name]',
  7528. 'id' => 'my_id', 'maxlength' => '255'
  7529. ]
  7530. ];
  7531. $this->assertHtml($expected, $result);
  7532. $result = $this->Form->control('1.id');
  7533. $expected = ['input' => [
  7534. 'type' => 'hidden', 'name' => '1[id]',
  7535. 'id' => '1-id'
  7536. ]];
  7537. $this->assertHtml($expected, $result);
  7538. $result = $this->Form->control('1.name');
  7539. $expected = [
  7540. 'label' => ['for' => '1-name'],
  7541. 'Name',
  7542. '/label',
  7543. 'input' => [
  7544. 'type' => 'text', 'name' => '1[name]',
  7545. 'id' => '1-name', 'maxlength' => '255'
  7546. ]
  7547. ];
  7548. $this->assertHtml($expected, $result);
  7549. }
  7550. /**
  7551. * testFormEnd method
  7552. *
  7553. * @return void
  7554. */
  7555. public function testFormEnd()
  7556. {
  7557. $this->assertEquals('</form>', $this->Form->end());
  7558. }
  7559. /**
  7560. * testMultiRecordForm method
  7561. *
  7562. * Test the generation of fields for a multi record form.
  7563. *
  7564. * @return void
  7565. */
  7566. public function testMultiRecordForm()
  7567. {
  7568. $this->loadFixtures('Articles', 'Comments');
  7569. $articles = TableRegistry::get('Articles');
  7570. $articles->hasMany('Comments');
  7571. $comment = new Entity(['comment' => 'Value']);
  7572. $article = new Article(['comments' => [$comment]]);
  7573. $this->Form->create([$article]);
  7574. $result = $this->Form->control('0.comments.1.comment');
  7575. //@codingStandardsIgnoreStart
  7576. $expected = [
  7577. 'div' => ['class' => 'input textarea'],
  7578. 'label' => ['for' => '0-comments-1-comment'],
  7579. 'Comment',
  7580. '/label',
  7581. 'textarea' => [
  7582. 'name',
  7583. 'id' => '0-comments-1-comment',
  7584. 'rows' => 5
  7585. ],
  7586. '/textarea',
  7587. '/div'
  7588. ];
  7589. //@codingStandardsIgnoreEnd
  7590. $this->assertHtml($expected, $result);
  7591. $result = $this->Form->control('0.comments.0.comment');
  7592. //@codingStandardsIgnoreStart
  7593. $expected = [
  7594. 'div' => ['class' => 'input textarea'],
  7595. 'label' => ['for' => '0-comments-0-comment'],
  7596. 'Comment',
  7597. '/label',
  7598. 'textarea' => [
  7599. 'name',
  7600. 'id' => '0-comments-0-comment',
  7601. 'rows' => 5
  7602. ],
  7603. 'Value',
  7604. '/textarea',
  7605. '/div'
  7606. ];
  7607. //@codingStandardsIgnoreEnd
  7608. $this->assertHtml($expected, $result);
  7609. $comment->setError('comment', ['Not valid']);
  7610. $result = $this->Form->control('0.comments.0.comment');
  7611. //@codingStandardsIgnoreStart
  7612. $expected = [
  7613. 'div' => ['class' => 'input textarea error'],
  7614. 'label' => ['for' => '0-comments-0-comment'],
  7615. 'Comment',
  7616. '/label',
  7617. 'textarea' => [
  7618. 'name',
  7619. 'class' => 'form-error',
  7620. 'id' => '0-comments-0-comment',
  7621. 'rows' => 5
  7622. ],
  7623. 'Value',
  7624. '/textarea',
  7625. ['div' => ['class' => 'error-message']],
  7626. 'Not valid',
  7627. '/div',
  7628. '/div'
  7629. ];
  7630. //@codingStandardsIgnoreEnd
  7631. $this->assertHtml($expected, $result);
  7632. TableRegistry::get('Comments')
  7633. ->getValidator('default')
  7634. ->allowEmpty('comment', false);
  7635. $result = $this->Form->control('0.comments.1.comment');
  7636. //@codingStandardsIgnoreStart
  7637. $expected = [
  7638. 'div' => ['class' => 'input textarea required'],
  7639. 'label' => ['for' => '0-comments-1-comment'],
  7640. 'Comment',
  7641. '/label',
  7642. 'textarea' => [
  7643. 'name',
  7644. 'required' => 'required',
  7645. 'id' => '0-comments-1-comment',
  7646. 'rows' => 5
  7647. ],
  7648. '/textarea',
  7649. '/div'
  7650. ];
  7651. //@codingStandardsIgnoreEnd
  7652. $this->assertHtml($expected, $result);
  7653. }
  7654. /**
  7655. * testHtml5Controls method
  7656. *
  7657. * Test that some html5 inputs + FormHelper::__call() work.
  7658. *
  7659. * @return void
  7660. */
  7661. public function testHtml5Controls()
  7662. {
  7663. $result = $this->Form->email('User.email');
  7664. $expected = [
  7665. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7666. ];
  7667. $this->assertHtml($expected, $result);
  7668. $result = $this->Form->search('User.query');
  7669. $expected = [
  7670. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7671. ];
  7672. $this->assertHtml($expected, $result);
  7673. $result = $this->Form->search('User.query', ['value' => 'test']);
  7674. $expected = [
  7675. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7676. ];
  7677. $this->assertHtml($expected, $result);
  7678. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7679. $expected = [
  7680. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7681. ];
  7682. $this->assertHtml($expected, $result);
  7683. }
  7684. /**
  7685. * testHtml5ControlWithControl method
  7686. *
  7687. * Test accessing html5 inputs through control().
  7688. *
  7689. * @return void
  7690. */
  7691. public function testHtml5ControlWithControl()
  7692. {
  7693. $this->Form->create();
  7694. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7695. $result = $this->Form->control('website', [
  7696. 'type' => 'url',
  7697. 'val' => 'http://domain.tld',
  7698. 'label' => false
  7699. ]);
  7700. $expected = [
  7701. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7702. ];
  7703. $this->assertHtml($expected, $result);
  7704. }
  7705. /**
  7706. * testHtml5ControlException method
  7707. *
  7708. * Test errors when field name is missing.
  7709. *
  7710. * @return void
  7711. */
  7712. public function testHtml5ControlException()
  7713. {
  7714. $this->expectException(\Cake\Core\Exception\Exception::class);
  7715. $this->Form->email();
  7716. }
  7717. /**
  7718. * testRequiredAttribute method
  7719. *
  7720. * Tests that formhelper sets required attributes.
  7721. *
  7722. * @return void
  7723. */
  7724. public function testRequiredAttribute()
  7725. {
  7726. $this->article['required'] = [
  7727. 'title' => true,
  7728. 'body' => false,
  7729. ];
  7730. $this->Form->create($this->article);
  7731. $result = $this->Form->control('title');
  7732. $expected = [
  7733. 'div' => ['class' => 'input text required'],
  7734. 'label' => ['for' => 'title'],
  7735. 'Title',
  7736. '/label',
  7737. 'input' => [
  7738. 'type' => 'text',
  7739. 'name' => 'title',
  7740. 'id' => 'title',
  7741. 'required' => 'required',
  7742. ],
  7743. '/div'
  7744. ];
  7745. $this->assertHtml($expected, $result);
  7746. $result = $this->Form->control('title', ['required' => false]);
  7747. $this->assertNotContains('required', $result);
  7748. $result = $this->Form->control('body');
  7749. $expected = [
  7750. 'div' => ['class' => 'input text'],
  7751. 'label' => ['for' => 'body'],
  7752. 'Body',
  7753. '/label',
  7754. 'input' => [
  7755. 'type' => 'text',
  7756. 'name' => 'body',
  7757. 'id' => 'body',
  7758. ],
  7759. '/div'
  7760. ];
  7761. $this->assertHtml($expected, $result);
  7762. $result = $this->Form->control('body', ['required' => true]);
  7763. $this->assertContains('required', $result);
  7764. }
  7765. /**
  7766. * testControlsNotNested method
  7767. *
  7768. * Tests that it is possible to put inputs outside of the label.
  7769. *
  7770. * @return void
  7771. */
  7772. public function testControlsNotNested()
  7773. {
  7774. $this->Form->setTemplates([
  7775. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7776. 'formGroup' => '{{input}}{{label}}',
  7777. ]);
  7778. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7779. $expected = [
  7780. 'div' => ['class' => 'input checkbox'],
  7781. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7782. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7783. 'label' => ['for' => 'foo'],
  7784. 'Foo',
  7785. '/label',
  7786. '/div'
  7787. ];
  7788. $this->assertHtml($expected, $result);
  7789. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7790. $expected = [
  7791. 'div' => ['class' => 'input checkbox'],
  7792. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7793. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7794. '/div'
  7795. ];
  7796. $this->assertHtml($expected, $result);
  7797. $result = $this->Form->control('confirm', [
  7798. 'type' => 'radio',
  7799. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7800. ]);
  7801. $expected = [
  7802. 'div' => ['class' => 'input radio'],
  7803. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7804. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7805. ['label' => ['for' => 'confirm-y']],
  7806. 'Yes',
  7807. '/label',
  7808. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7809. ['label' => ['for' => 'confirm-n']],
  7810. 'No',
  7811. '/label',
  7812. '<label',
  7813. 'Confirm',
  7814. '/label',
  7815. '/div',
  7816. ];
  7817. $this->assertHtml($expected, $result);
  7818. $result = $this->Form->select('category', ['1', '2'], [
  7819. 'multiple' => 'checkbox',
  7820. 'name' => 'fish',
  7821. ]);
  7822. $expected = [
  7823. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7824. ['div' => ['class' => 'checkbox']],
  7825. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7826. ['label' => ['for' => 'fish-0']],
  7827. '1',
  7828. '/label',
  7829. '/div',
  7830. ['div' => ['class' => 'checkbox']],
  7831. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7832. ['label' => ['for' => 'fish-1']],
  7833. '2',
  7834. '/label',
  7835. '/div'
  7836. ];
  7837. $this->assertHtml($expected, $result);
  7838. }
  7839. /**
  7840. * testControlContainerTemplates method
  7841. *
  7842. * Test that *Container templates are used by input.
  7843. *
  7844. * @return void
  7845. */
  7846. public function testControlContainerTemplates()
  7847. {
  7848. $this->Form->setTemplates([
  7849. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7850. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7851. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7852. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7853. ]);
  7854. $this->article['errors'] = [
  7855. 'Article' => ['published' => 'error message']
  7856. ];
  7857. $this->Form->create($this->article);
  7858. $result = $this->Form->control('accept', [
  7859. 'type' => 'checkbox'
  7860. ]);
  7861. $expected = [
  7862. 'div' => ['class' => 'check'],
  7863. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7864. 'label' => ['for' => 'accept'],
  7865. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7866. 'Accept',
  7867. '/label',
  7868. '/div'
  7869. ];
  7870. $this->assertHtml($expected, $result);
  7871. $result = $this->Form->control('accept', [
  7872. 'type' => 'radio',
  7873. 'options' => ['Y', 'N']
  7874. ]);
  7875. $this->assertContains('<div class="rad">', $result);
  7876. $result = $this->Form->control('Article.published', [
  7877. 'type' => 'radio',
  7878. 'options' => ['Y', 'N']
  7879. ]);
  7880. $this->assertContains('<div class="rad err">', $result);
  7881. $result = $this->Form->control('Article.created', [
  7882. 'type' => 'datetime',
  7883. ]);
  7884. $this->assertContains('<div class="dt">', $result);
  7885. }
  7886. /**
  7887. * testFormGroupTemplates method
  7888. *
  7889. * Test that *Container templates are used by input.
  7890. *
  7891. * @return void
  7892. */
  7893. public function testFormGroupTemplates()
  7894. {
  7895. $this->Form->setTemplates([
  7896. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7897. ]);
  7898. $this->Form->create($this->article);
  7899. $result = $this->Form->control('accept', [
  7900. 'type' => 'radio',
  7901. 'options' => ['Y', 'N']
  7902. ]);
  7903. $this->assertContains('<div class="radio">', $result);
  7904. }
  7905. /**
  7906. * testResetTemplates method
  7907. *
  7908. * Test resetting templates.
  7909. *
  7910. * @return void
  7911. */
  7912. public function testResetTemplates()
  7913. {
  7914. $this->Form->setTemplates(['input' => '<input/>']);
  7915. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  7916. $this->Form->resetTemplates();
  7917. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7918. }
  7919. /**
  7920. * testContext method
  7921. *
  7922. * Test the context method.
  7923. *
  7924. * @return void
  7925. */
  7926. public function testContext()
  7927. {
  7928. $result = $this->Form->context();
  7929. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7930. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7931. $this->assertSame($mock, $this->Form->context($mock));
  7932. $this->assertSame($mock, $this->Form->context());
  7933. }
  7934. /**
  7935. * testAutoDomId method
  7936. *
  7937. * @return void
  7938. */
  7939. public function testAutoDomId()
  7940. {
  7941. $result = $this->Form->text('field', ['id' => true]);
  7942. $expected = [
  7943. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7944. ];
  7945. $this->assertHtml($expected, $result);
  7946. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7947. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7948. $expected = [
  7949. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7950. ['label' => ['for' => 'field-0']],
  7951. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7952. 'option A',
  7953. '/label',
  7954. ['label' => ['for' => 'field-1']],
  7955. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7956. 'option B',
  7957. '/label',
  7958. ];
  7959. $this->assertHtml($expected, $result);
  7960. $result = $this->Form->select(
  7961. 'multi_field',
  7962. ['first', 'second'],
  7963. ['multiple' => 'checkbox', 'id' => true]
  7964. );
  7965. $expected = [
  7966. 'input' => [
  7967. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  7968. ],
  7969. ['div' => ['class' => 'checkbox']],
  7970. ['label' => ['for' => 'multi-field-0']],
  7971. ['input' => [
  7972. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7973. 'value' => '0', 'id' => 'multi-field-0'
  7974. ]],
  7975. 'first',
  7976. '/label',
  7977. '/div',
  7978. ['div' => ['class' => 'checkbox']],
  7979. ['label' => ['for' => 'multi-field-1']],
  7980. ['input' => [
  7981. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7982. 'value' => '1', 'id' => 'multi-field-1'
  7983. ]],
  7984. 'second',
  7985. '/label',
  7986. '/div',
  7987. ];
  7988. $this->assertHtml($expected, $result);
  7989. }
  7990. /**
  7991. * Test the basic setters and getters for value sources
  7992. *
  7993. * @return void
  7994. */
  7995. public function testFormValueSourcesSettersGetters()
  7996. {
  7997. $expected = ['context'];
  7998. $result = $this->Form->getValueSources();
  7999. $this->assertEquals($expected, $result);
  8000. $expected = null;
  8001. $result = $this->Form->getSourceValue('id');
  8002. $this->assertEquals($expected, $result);
  8003. $expected = ['query', 'data', 'context'];
  8004. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  8005. $result = $this->Form->getValueSources();
  8006. $this->assertEquals($expected, $result);
  8007. $this->Form->request->data['id'] = '1';
  8008. $this->Form->request->query['id'] = '2';
  8009. $this->Form->setValueSources(['context']);
  8010. $expected = '1';
  8011. $result = $this->Form->getSourceValue('id');
  8012. $this->assertEquals($expected, $result);
  8013. $this->Form->setValueSources('query');
  8014. $expected = ['query'];
  8015. $result = $this->Form->getValueSources();
  8016. $this->assertEquals($expected, $result);
  8017. $expected = '2';
  8018. $result = $this->Form->getSourceValue('id');
  8019. $this->assertEquals($expected, $result);
  8020. $this->Form->setValueSources(['data']);
  8021. $expected = '1';
  8022. $result = $this->Form->getSourceValue('id');
  8023. $this->assertEquals($expected, $result);
  8024. $this->Form->setValueSources(['query', 'data']);
  8025. $expected = '2';
  8026. $result = $this->Form->getSourceValue('id');
  8027. $this->assertEquals($expected, $result);
  8028. }
  8029. /**
  8030. * Tests the different input rendering values based on sources values switching
  8031. *
  8032. * @return void
  8033. */
  8034. public function testFormValueSourcesSingleSwitchRendering()
  8035. {
  8036. $this->loadFixtures('Articles');
  8037. $articles = TableRegistry::get('Articles');
  8038. $article = new Article();
  8039. $articles->patchEntity($article, ['id' => '3']);
  8040. $this->Form->create($article);
  8041. $this->Form->setValueSources(['context']);
  8042. $result = $this->Form->control('id');
  8043. $expected = [
  8044. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8045. ];
  8046. $this->assertHtml($expected, $result);
  8047. $this->Form->request->query['id'] = '5';
  8048. $this->Form->setValueSources(['query']);
  8049. $result = $this->Form->control('id');
  8050. $expected = [
  8051. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8052. ];
  8053. $this->assertHtml($expected, $result);
  8054. $this->Form->request->query['id'] = '5a';
  8055. $this->Form->request->data['id'] = '5b';
  8056. $this->Form->setValueSources(['context']);
  8057. $this->Form->create($article);
  8058. $result = $this->Form->control('id');
  8059. $expected = [
  8060. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8061. ];
  8062. $this->assertHtml($expected, $result);
  8063. $this->Form->setValueSources(['data']);
  8064. $this->Form->create($article);
  8065. $result = $this->Form->control('id');
  8066. $expected = [
  8067. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8068. ];
  8069. $this->assertHtml($expected, $result);
  8070. $this->Form->setValueSources(['query']);
  8071. $result = $this->Form->control('id');
  8072. $expected = [
  8073. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  8074. ];
  8075. $this->assertHtml($expected, $result);
  8076. }
  8077. /**
  8078. * Tests the different input rendering values based on sources values switching while supplying
  8079. * an entity (base context) and multiple sources (such as data, query)
  8080. *
  8081. * @return void
  8082. */
  8083. public function testFormValueSourcesListSwitchRendering()
  8084. {
  8085. $this->loadFixtures('Articles');
  8086. $articles = TableRegistry::get('Articles');
  8087. $article = new Article();
  8088. $articles->patchEntity($article, ['id' => '3']);
  8089. $this->Form->request = $this->Form->request->withQueryParams(['id' => '9']);
  8090. $this->Form->create($article);
  8091. $this->Form->setValueSources(['context', 'query']);
  8092. $result = $this->Form->control('id');
  8093. $expected = [
  8094. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8095. ];
  8096. $this->assertHtml($expected, $result);
  8097. $this->Form->setValueSources(['query', 'context']);
  8098. $result = $this->Form->control('id');
  8099. $expected = [
  8100. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8101. ];
  8102. $this->assertHtml($expected, $result);
  8103. $this->Form->setValueSources(['data', 'query', 'context']);
  8104. $result = $this->Form->control('id');
  8105. $expected = [
  8106. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8107. ];
  8108. $this->assertHtml($expected, $result);
  8109. $this->Form->request = $this->Form->request
  8110. ->withData('id', '8')
  8111. ->withQueryParams(['id' => '9']);
  8112. $this->Form->setValueSources(['data', 'query', 'context']);
  8113. $result = $this->Form->control('id');
  8114. $expected = [
  8115. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  8116. ];
  8117. $this->assertHtml($expected, $result);
  8118. }
  8119. /**
  8120. * Test the different form input renderings based on values sources switchings through form options
  8121. *
  8122. * @return void
  8123. */
  8124. public function testFormValueSourcesSwitchViaOptionsRendering()
  8125. {
  8126. $this->loadFixtures('Articles');
  8127. $articles = TableRegistry::get('Articles');
  8128. $article = new Article();
  8129. $articles->patchEntity($article, ['id' => '3']);
  8130. $this->Form->request->data['id'] = '4';
  8131. $this->Form->request->query['id'] = '5';
  8132. $this->Form->create($article, ['valueSources' => 'query']);
  8133. $result = $this->Form->control('id');
  8134. $expected = [
  8135. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8136. ];
  8137. $this->assertHtml($expected, $result);
  8138. $result = $this->Form->getSourceValue('id');
  8139. $this->assertEquals('5', $result);
  8140. $this->Form->setValueSources(['context']);
  8141. $this->Form->create($article, ['valueSources' => 'query']);
  8142. $result = $this->Form->control('id');
  8143. $expected = [
  8144. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8145. ];
  8146. $this->assertHtml($expected, $result);
  8147. $result = $this->Form->getSourceValue('id');
  8148. $this->assertEquals('5', $result);
  8149. $this->Form->setValueSources(['query']);
  8150. $this->Form->create($article, ['valueSources' => 'data']);
  8151. $result = $this->Form->control('id');
  8152. $expected = [
  8153. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8154. ];
  8155. $this->assertHtml($expected, $result);
  8156. $result = $this->Form->getSourceValue('id');
  8157. $this->assertEquals('4', $result);
  8158. $this->Form->setValueSources(['query']);
  8159. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8160. $result = $this->Form->control('id');
  8161. $expected = [
  8162. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8163. ];
  8164. $this->assertHtml($expected, $result);
  8165. $result = $this->Form->getSourceValue('id');
  8166. $this->assertEquals('4', $result);
  8167. }
  8168. /**
  8169. * Test the different form input renderings based on values sources switchings through form options
  8170. *
  8171. * @return void
  8172. */
  8173. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8174. {
  8175. $this->loadFixtures('Articles');
  8176. $articles = TableRegistry::get('Articles');
  8177. $article = new Article();
  8178. $articles->patchEntity($article, ['id' => '3']);
  8179. $this->Form->request->data['id'] = '10';
  8180. $this->Form->request->query['id'] = '11';
  8181. $this->Form->setValueSources(['context'])
  8182. ->create($article, ['valueSources' => ['query', 'data']]);
  8183. $result = $this->Form->control('id');
  8184. $expected = [
  8185. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8186. ];
  8187. $this->assertHtml($expected, $result);
  8188. $result = $this->Form->getSourceValue('id');
  8189. $this->assertEquals('11', $result);
  8190. unset($this->Form->request->query['id']);
  8191. $this->Form->setValueSources(['context'])
  8192. ->create($article, ['valueSources' => ['query', 'data']]);
  8193. $result = $this->Form->control('id');
  8194. $expected = [
  8195. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8196. ];
  8197. $this->assertHtml($expected, $result);
  8198. $result = $this->Form->getSourceValue('id');
  8199. $this->assertEquals('10', $result);
  8200. }
  8201. /**
  8202. * Test the different form values sources resetting through From::end();
  8203. *
  8204. * @return void
  8205. */
  8206. public function testFormValueSourcesResetViaEnd()
  8207. {
  8208. $expected = ['context'];
  8209. $result = $this->Form->getValueSources();
  8210. $this->assertEquals($expected, $result);
  8211. $expected = ['query', 'context', 'data'];
  8212. $this->Form->setValueSources(['query', 'context', 'data']);
  8213. $result = $this->Form->getValueSources();
  8214. $this->assertEquals($expected, $result);
  8215. $this->Form->create();
  8216. $result = $this->Form->getValueSources();
  8217. $this->assertEquals($expected, $result);
  8218. $this->Form->end();
  8219. $result = $this->Form->getValueSources();
  8220. $this->assertEquals(['context'], $result);
  8221. }
  8222. /**
  8223. * Test sources values defaults handling
  8224. *
  8225. * @return void
  8226. */
  8227. public function testFormValueSourcesDefaults()
  8228. {
  8229. $this->Form->request->query['password'] = 'open Sesame';
  8230. $this->Form->create();
  8231. $result = $this->Form->password('password');
  8232. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8233. $this->assertHtml($expected, $result);
  8234. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8235. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8236. $this->assertHtml($expected, $result);
  8237. $this->Form->setValueSources('query');
  8238. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8239. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8240. $this->assertHtml($expected, $result);
  8241. $this->Form->setValueSources('data');
  8242. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8243. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8244. $this->assertHtml($expected, $result);
  8245. }
  8246. /**
  8247. * Test sources values schema defaults handling
  8248. *
  8249. * @return void
  8250. */
  8251. public function testSourcesValueDoesntExistPassThrough()
  8252. {
  8253. $this->Form->request = $this->Form->request->withQueryParams(['category' => 'sesame-cookies']);
  8254. $articles = TableRegistry::get('Articles');
  8255. $entity = $articles->newEntity();
  8256. $this->Form->create($entity);
  8257. $this->Form->setValueSources(['query', 'context']);
  8258. $result = $this->Form->getSourceValue('category');
  8259. $this->assertEquals('sesame-cookies', $result);
  8260. $this->Form->setValueSources(['context', 'query']);
  8261. $result = $this->Form->getSourceValue('category');
  8262. $this->assertEquals('sesame-cookies', $result);
  8263. }
  8264. /**
  8265. * testNestedLabelInput method
  8266. *
  8267. * Test the `nestedInput` parameter
  8268. *
  8269. * @group deprecated
  8270. * @return void
  8271. */
  8272. public function testNestedLabelInput()
  8273. {
  8274. $this->deprecated(function () {
  8275. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8276. $expected = [
  8277. 'div' => ['class' => 'input text'],
  8278. 'label' => ['for' => 'foo'],
  8279. ['input' => [
  8280. 'type' => 'text',
  8281. 'name' => 'foo',
  8282. 'id' => 'foo'
  8283. ]],
  8284. 'Foo',
  8285. '/label',
  8286. '/div'
  8287. ];
  8288. $this->assertHtml($expected, $result);
  8289. });
  8290. }
  8291. /**
  8292. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8293. *
  8294. * This test makes sure `false` excludes the label from the render
  8295. *
  8296. * @return void
  8297. */
  8298. public function testControlLabelManipulationDisableLabels()
  8299. {
  8300. $result = $this->Form->control('test', [
  8301. 'type' => 'radio',
  8302. 'options' => ['A', 'B'],
  8303. 'labelOptions' => false
  8304. ]);
  8305. $expected = [
  8306. ['div' => ['class' => 'input radio']],
  8307. '<label',
  8308. 'Test',
  8309. '/label',
  8310. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8311. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8312. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8313. '/div',
  8314. ];
  8315. $this->assertHtml($expected, $result);
  8316. $result = $this->Form->control('checkbox1', [
  8317. 'label' => 'My checkboxes',
  8318. 'multiple' => 'checkbox',
  8319. 'type' => 'select',
  8320. 'options' => [
  8321. ['text' => 'First Checkbox', 'value' => 1],
  8322. ['text' => 'Second Checkbox', 'value' => 2]
  8323. ],
  8324. 'labelOptions' => false
  8325. ]);
  8326. $expected = [
  8327. ['div' => ['class' => 'input select']],
  8328. ['label' => ['for' => 'checkbox1']],
  8329. 'My checkboxes',
  8330. '/label',
  8331. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8332. ['div' => ['class' => 'checkbox']],
  8333. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8334. '/div',
  8335. ['div' => ['class' => 'checkbox']],
  8336. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8337. '/div',
  8338. '/div'
  8339. ];
  8340. $this->assertHtml($expected, $result);
  8341. }
  8342. /**
  8343. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8344. *
  8345. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8346. * added to the class if checked.
  8347. *
  8348. * Also checks to make sure any custom attributes are rendered correctly
  8349. *
  8350. * @return void
  8351. */
  8352. public function testControlLabelManipulationRadios()
  8353. {
  8354. $result = $this->Form->control('test', [
  8355. 'type' => 'radio',
  8356. 'options' => ['A', 'B'],
  8357. 'labelOptions' => ['class' => 'custom-class']
  8358. ]);
  8359. $expected = [
  8360. ['div' => ['class' => 'input radio']],
  8361. '<label',
  8362. 'Test',
  8363. '/label',
  8364. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8365. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8366. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8367. 'A',
  8368. '/label',
  8369. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8370. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8371. 'B',
  8372. '/label',
  8373. '/div',
  8374. ];
  8375. $this->assertHtml($expected, $result);
  8376. $result = $this->Form->control('test', [
  8377. 'type' => 'radio',
  8378. 'options' => ['A', 'B'],
  8379. 'value' => 1,
  8380. 'labelOptions' => ['class' => 'custom-class']
  8381. ]);
  8382. $expected = [
  8383. ['div' => ['class' => 'input radio']],
  8384. '<label',
  8385. 'Test',
  8386. '/label',
  8387. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8388. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8389. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8390. 'A',
  8391. '/label',
  8392. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8393. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8394. 'B',
  8395. '/label',
  8396. '/div',
  8397. ];
  8398. $this->assertHtml($expected, $result);
  8399. $result = $this->Form->control('test', [
  8400. 'type' => 'radio',
  8401. 'options' => ['A', 'B'],
  8402. 'value' => 1,
  8403. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']]
  8404. ]);
  8405. $expected = [
  8406. ['div' => ['class' => 'input radio']],
  8407. '<label',
  8408. 'Test',
  8409. '/label',
  8410. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8411. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8412. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8413. 'A',
  8414. '/label',
  8415. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8416. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8417. 'B',
  8418. '/label',
  8419. '/div',
  8420. ];
  8421. $this->assertHtml($expected, $result);
  8422. $result = $this->Form->radio('test', ['A', 'B'], [
  8423. 'label' => [
  8424. 'class' => ['custom-class', 'another-class'],
  8425. 'data-name' => 'bob'
  8426. ],
  8427. 'value' => 1
  8428. ]);
  8429. $expected = [
  8430. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8431. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8432. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8433. 'A',
  8434. '/label',
  8435. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8436. ['input' => [
  8437. 'type' => 'radio',
  8438. 'name' => 'test',
  8439. 'value' => '1',
  8440. 'id' => 'test-1',
  8441. 'checked' => 'checked'
  8442. ]],
  8443. 'B',
  8444. '/label',
  8445. ];
  8446. $this->assertHtml($expected, $result);
  8447. }
  8448. /**
  8449. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8450. *
  8451. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8452. * added to the class if checked.
  8453. *
  8454. * Also checks to make sure any custom attributes are rendered correctly
  8455. *
  8456. * @return void
  8457. */
  8458. public function testControlLabelManipulationCheckboxes()
  8459. {
  8460. $result = $this->Form->control('checkbox1', [
  8461. 'label' => 'My checkboxes',
  8462. 'multiple' => 'checkbox',
  8463. 'type' => 'select',
  8464. 'options' => [
  8465. ['text' => 'First Checkbox', 'value' => 1],
  8466. ['text' => 'Second Checkbox', 'value' => 2]
  8467. ],
  8468. 'labelOptions' => ['class' => 'custom-class'],
  8469. 'value' => ['1']
  8470. ]);
  8471. $expected = [
  8472. ['div' => ['class' => 'input select']],
  8473. ['label' => ['for' => 'checkbox1']],
  8474. 'My checkboxes',
  8475. '/label',
  8476. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8477. ['div' => ['class' => 'checkbox']],
  8478. ['label' => [
  8479. 'class' => 'custom-class selected',
  8480. 'for' => 'checkbox1-1'
  8481. ]],
  8482. ['input' => [
  8483. 'type' => 'checkbox',
  8484. 'name' => 'checkbox1[]',
  8485. 'value' => '1',
  8486. 'id' => 'checkbox1-1',
  8487. 'checked' => 'checked'
  8488. ]],
  8489. 'First Checkbox',
  8490. '/label',
  8491. '/div',
  8492. ['div' => ['class' => 'checkbox']],
  8493. ['label' => [
  8494. 'class' => 'custom-class',
  8495. 'for' => 'checkbox1-2'
  8496. ]],
  8497. ['input' => [
  8498. 'type' => 'checkbox',
  8499. 'name' => 'checkbox1[]',
  8500. 'value' => '2',
  8501. 'id' => 'checkbox1-2'
  8502. ]],
  8503. 'Second Checkbox',
  8504. '/label',
  8505. '/div',
  8506. '/div'
  8507. ];
  8508. $this->assertHtml($expected, $result);
  8509. $result = $this->Form->control('checkbox1', [
  8510. 'label' => 'My checkboxes',
  8511. 'multiple' => 'checkbox',
  8512. 'type' => 'select',
  8513. 'options' => [
  8514. ['text' => 'First Checkbox', 'value' => 1],
  8515. ['text' => 'Second Checkbox', 'value' => 2]
  8516. ],
  8517. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8518. 'value' => ['1']
  8519. ]);
  8520. $expected = [
  8521. ['div' => ['class' => 'input select']],
  8522. ['label' => ['for' => 'checkbox1']],
  8523. 'My checkboxes',
  8524. '/label',
  8525. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8526. ['div' => ['class' => 'checkbox']],
  8527. ['label' => [
  8528. 'class' => 'custom-class another-class selected',
  8529. 'data-name' => 'bob',
  8530. 'for' => 'checkbox1-1'
  8531. ]],
  8532. ['input' => [
  8533. 'type' => 'checkbox',
  8534. 'name' => 'checkbox1[]',
  8535. 'value' => '1',
  8536. 'id' => 'checkbox1-1',
  8537. 'checked' => 'checked'
  8538. ]],
  8539. 'First Checkbox',
  8540. '/label',
  8541. '/div',
  8542. ['div' => ['class' => 'checkbox']],
  8543. ['label' => [
  8544. 'class' => 'custom-class another-class',
  8545. 'data-name' => 'bob',
  8546. 'for' => 'checkbox1-2'
  8547. ]],
  8548. ['input' => [
  8549. 'type' => 'checkbox',
  8550. 'name' => 'checkbox1[]',
  8551. 'value' => '2',
  8552. 'id' => 'checkbox1-2'
  8553. ]],
  8554. 'Second Checkbox',
  8555. '/label',
  8556. '/div',
  8557. '/div'
  8558. ];
  8559. $this->assertHtml($expected, $result);
  8560. }
  8561. }