FormHelperTest.php 304 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Collection\Collection;
  17. use Cake\Core\Configure;
  18. use Cake\Form\Form;
  19. use Cake\Http\ServerRequest;
  20. use Cake\ORM\Entity;
  21. use Cake\ORM\Table;
  22. use Cake\ORM\TableRegistry;
  23. use Cake\Routing\Router;
  24. use Cake\TestSuite\TestCase;
  25. use Cake\Utility\Security;
  26. use Cake\View\Helper\FormHelper;
  27. use Cake\View\View;
  28. /**
  29. * Test stub.
  30. */
  31. class Article extends Entity
  32. {
  33. }
  34. /**
  35. * Contact class
  36. */
  37. class ContactsTable extends Table
  38. {
  39. /**
  40. * Default schema
  41. *
  42. * @var array
  43. */
  44. protected $_schema = [
  45. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  46. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  47. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  48. 'phone' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  49. 'password' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  50. 'published' => ['type' => 'date', 'null' => true, 'default' => null, 'length' => null],
  51. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  52. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  53. 'age' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => null],
  54. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  55. ];
  56. /**
  57. * Initializes the schema
  58. *
  59. * @return void
  60. */
  61. public function initialize(array $config)
  62. {
  63. $this->schema($this->_schema);
  64. }
  65. }
  66. /**
  67. * ValidateUser class
  68. */
  69. class ValidateUsersTable extends Table
  70. {
  71. /**
  72. * schema method
  73. *
  74. * @var array
  75. */
  76. protected $_schema = [
  77. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  78. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  79. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  80. 'balance' => ['type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2],
  81. 'cost_decimal' => ['type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3],
  82. 'null_decimal' => ['type' => 'decimal', 'null' => false, 'length' => null, 'precision' => null],
  83. 'ratio' => ['type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6],
  84. 'population' => ['type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0],
  85. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  86. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  87. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  88. ];
  89. /**
  90. * Initializes the schema
  91. *
  92. * @return void
  93. */
  94. public function initialize(array $config)
  95. {
  96. $this->schema($this->_schema);
  97. }
  98. }
  99. /**
  100. * FormHelperTest class
  101. *
  102. * @property \Cake\View\Helper\FormHelper $Form
  103. * @property \Cake\View\View $View
  104. */
  105. class FormHelperTest extends TestCase
  106. {
  107. /**
  108. * Fixtures to be used
  109. *
  110. * @var array
  111. */
  112. public $fixtures = ['core.articles', 'core.comments'];
  113. /**
  114. * Do not load the fixtures by default
  115. *
  116. * @var bool
  117. */
  118. public $autoFixtures = false;
  119. /**
  120. * @var array
  121. */
  122. protected $article = [];
  123. /**
  124. * setUp method
  125. *
  126. * @return void
  127. */
  128. public function setUp()
  129. {
  130. parent::setUp();
  131. Configure::write('Config.language', 'eng');
  132. Configure::write('App.base', '');
  133. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  134. $this->View = new View();
  135. $this->Form = new FormHelper($this->View);
  136. $request = new ServerRequest([
  137. 'webroot' => '',
  138. 'base' => '',
  139. 'url' => '/articles/add',
  140. 'params' => [
  141. 'controller' => 'articles',
  142. 'action' => 'add',
  143. ]
  144. ]);
  145. $this->Form->Url->request = $this->Form->request = $request;
  146. $this->dateRegex = [
  147. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  148. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  149. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  150. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  151. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  152. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  153. ];
  154. $this->article = [
  155. 'schema' => [
  156. 'id' => ['type' => 'integer'],
  157. 'author_id' => ['type' => 'integer', 'null' => true],
  158. 'title' => ['type' => 'string', 'null' => true],
  159. 'body' => 'text',
  160. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  161. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  162. ],
  163. 'required' => [
  164. 'author_id' => true,
  165. 'title' => true,
  166. ]
  167. ];
  168. Security::salt('foo!');
  169. Router::connect('/:controller', ['action' => 'index']);
  170. Router::connect('/:controller/:action/*');
  171. }
  172. /**
  173. * tearDown method
  174. *
  175. * @return void
  176. */
  177. public function tearDown()
  178. {
  179. parent::tearDown();
  180. unset($this->Form, $this->Controller, $this->View);
  181. TableRegistry::clear();
  182. }
  183. /**
  184. * Test construct() with the templates option.
  185. *
  186. * @return void
  187. */
  188. public function testConstructTemplatesFile()
  189. {
  190. $helper = new FormHelper($this->View, [
  191. 'templates' => 'htmlhelper_tags'
  192. ]);
  193. $result = $helper->control('name');
  194. $this->assertContains('<input', $result);
  195. }
  196. /**
  197. * Test that when specifying custom widgets the config array for that widget
  198. * is overwritten instead of merged.
  199. *
  200. * @return void
  201. */
  202. public function testConstructWithWidgets()
  203. {
  204. $config = [
  205. 'widgets' => [
  206. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
  207. ]
  208. ];
  209. $helper = new FormHelper($this->View, $config);
  210. $registry = $helper->widgetRegistry();
  211. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('datetime'));
  212. }
  213. /**
  214. * Test that when specifying custom widgets config file and it should be
  215. * added to widgets array. WidgetRegistry will load widgets in constructor.
  216. *
  217. * @return void
  218. */
  219. public function testConstructWithWidgetsConfig()
  220. {
  221. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  222. $registry = $helper->widgetRegistry();
  223. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('text'));
  224. }
  225. /**
  226. * Test registering a new widget class and rendering it.
  227. *
  228. * @return void
  229. */
  230. public function testAddWidgetAndRenderWidget()
  231. {
  232. $data = [
  233. 'val' => 1
  234. ];
  235. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  236. $this->Form->addWidget('test', $mock);
  237. $mock->expects($this->once())
  238. ->method('render')
  239. ->with($data)
  240. ->will($this->returnValue('HTML'));
  241. $result = $this->Form->widget('test', $data);
  242. $this->assertEquals('HTML', $result);
  243. }
  244. /**
  245. * Test that secureFields() of widget is called after calling render(),
  246. * not before.
  247. *
  248. * @return void
  249. */
  250. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  251. {
  252. $data = [
  253. 'val' => 1,
  254. 'name' => 'test'
  255. ];
  256. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  257. $this->Form->addWidget('test', $mock);
  258. $mock->expects($this->at(0))
  259. ->method('render')
  260. ->with($data)
  261. ->will($this->returnValue('HTML'));
  262. $mock->expects($this->at(1))
  263. ->method('secureFields')
  264. ->with($data)
  265. ->will($this->returnValue(['test']));
  266. $result = $this->Form->widget('test', $data + ['secure' => true]);
  267. $this->assertEquals('HTML', $result);
  268. }
  269. /**
  270. * Test that empty string is not added to secure fields list when
  271. * rendering input widget without name.
  272. *
  273. * @return void
  274. */
  275. public function testRenderingWidgetWithEmptyName()
  276. {
  277. $this->assertEquals([], $this->Form->fields);
  278. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  279. $this->assertEquals('<select name=""></select>', $result);
  280. $this->assertEquals([], $this->Form->fields);
  281. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  282. $this->assertEquals('<select name="0"></select>', $result);
  283. $this->assertEquals(['0'], $this->Form->fields);
  284. }
  285. /**
  286. * Test registering an invalid widget class.
  287. *
  288. * @expectedException \RuntimeException
  289. * @return void
  290. */
  291. public function testAddWidgetInvalid()
  292. {
  293. $mock = new \StdClass();
  294. $this->Form->addWidget('test', $mock);
  295. $this->Form->widget('test');
  296. }
  297. /**
  298. * Test adding a new context class.
  299. *
  300. * @return void
  301. */
  302. public function testAddContextProvider()
  303. {
  304. $context = 'My data';
  305. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  306. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  307. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  308. $this->assertEquals($context, $data['entity']);
  309. return $stub;
  310. });
  311. $this->Form->create($context);
  312. $result = $this->Form->context();
  313. $this->assertSame($stub, $result);
  314. }
  315. /**
  316. * Test replacing a context class.
  317. *
  318. * @return void
  319. */
  320. public function testAddContextProviderReplace()
  321. {
  322. $entity = new Article();
  323. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  324. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  325. return $stub;
  326. });
  327. $this->Form->create($entity);
  328. $result = $this->Form->context();
  329. $this->assertSame($stub, $result);
  330. }
  331. /**
  332. * Test overriding a context class.
  333. *
  334. * @return void
  335. */
  336. public function testAddContextProviderAdd()
  337. {
  338. $entity = new Article();
  339. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  340. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  341. if ($data['entity'] instanceof Entity) {
  342. return $stub;
  343. }
  344. });
  345. $this->Form->create($entity);
  346. $result = $this->Form->context();
  347. $this->assertSame($stub, $result);
  348. }
  349. /**
  350. * Test adding an invalid context class.
  351. *
  352. * @expectedException \RuntimeException
  353. * @expectedExceptionMessage Context providers must return object implementing Cake\View\Form\ContextInterface. Got "stdClass" instead.
  354. * @return void
  355. */
  356. public function testAddContextProviderInvalid()
  357. {
  358. $context = 'My data';
  359. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  360. return new \StdClass();
  361. });
  362. $this->Form->create($context);
  363. }
  364. /**
  365. * Provides context options for create().
  366. *
  367. * @return array
  368. */
  369. public function contextSelectionProvider()
  370. {
  371. $entity = new Article();
  372. $collection = new Collection([$entity]);
  373. $emptyCollection = new Collection([]);
  374. $arrayObject = new \ArrayObject([]);
  375. $data = [
  376. 'schema' => [
  377. 'title' => ['type' => 'string']
  378. ]
  379. ];
  380. $form = new Form();
  381. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  382. return [
  383. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  384. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  385. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  386. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  387. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  388. 'form' => [$form, 'Cake\View\Form\FormContext'],
  389. 'none' => [null, 'Cake\View\Form\NullContext'],
  390. 'false' => [false, 'Cake\View\Form\NullContext'],
  391. 'custom' => [$custom, get_class($custom)]
  392. ];
  393. }
  394. /**
  395. * Test default context selection in create()
  396. *
  397. * @dataProvider contextSelectionProvider
  398. * @return void
  399. */
  400. public function testCreateContextSelectionBuiltIn($data, $class)
  401. {
  402. $this->loadFixtures('Articles');
  403. $this->Form->create($data);
  404. $this->assertInstanceOf($class, $this->Form->context());
  405. }
  406. /**
  407. * Data provider for type option.
  408. *
  409. * @return array
  410. */
  411. public static function requestTypeProvider()
  412. {
  413. return [
  414. // type, method, override
  415. ['post', 'post', 'POST'],
  416. ['put', 'post', 'PUT'],
  417. ['patch', 'post', 'PATCH'],
  418. ['delete', 'post', 'DELETE'],
  419. ];
  420. }
  421. /**
  422. * Test creating file forms.
  423. *
  424. * @return void
  425. */
  426. public function testCreateFile()
  427. {
  428. $encoding = strtolower(Configure::read('App.encoding'));
  429. $result = $this->Form->create(false, ['type' => 'file']);
  430. $expected = [
  431. 'form' => [
  432. 'method' => 'post', 'action' => '/articles/add',
  433. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  434. ],
  435. 'div' => ['style' => 'display:none;'],
  436. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  437. '/div'
  438. ];
  439. $this->assertHtml($expected, $result);
  440. }
  441. /**
  442. * Test creating GET forms.
  443. *
  444. * @return void
  445. */
  446. public function testCreateGet()
  447. {
  448. $encoding = strtolower(Configure::read('App.encoding'));
  449. $result = $this->Form->create(false, ['type' => 'get']);
  450. $expected = ['form' => [
  451. 'method' => 'get', 'action' => '/articles/add',
  452. 'accept-charset' => $encoding
  453. ]];
  454. $this->assertHtml($expected, $result);
  455. }
  456. /**
  457. * Test explicit method/enctype options.
  458. *
  459. * Explicit method overwrites inferred method from 'type'
  460. *
  461. * @return void
  462. */
  463. public function testCreateExplicitMethodEnctype()
  464. {
  465. $encoding = strtolower(Configure::read('App.encoding'));
  466. $result = $this->Form->create(false, [
  467. 'type' => 'get',
  468. 'method' => 'put',
  469. 'enctype' => 'multipart/form-data'
  470. ]);
  471. $expected = ['form' => [
  472. 'method' => 'put',
  473. 'action' => '/articles/add',
  474. 'enctype' => 'multipart/form-data',
  475. 'accept-charset' => $encoding
  476. ]];
  477. $this->assertHtml($expected, $result);
  478. }
  479. /**
  480. * Test create() with the templates option.
  481. *
  482. * @return void
  483. */
  484. public function testCreateTemplatesArray()
  485. {
  486. $result = $this->Form->create($this->article, [
  487. 'templates' => [
  488. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  489. ]
  490. ]);
  491. $expected = [
  492. 'form' => [
  493. 'class' => 'form-horizontal',
  494. 'method' => 'post',
  495. 'action' => '/articles/add',
  496. 'accept-charset' => 'utf-8'
  497. ]
  498. ];
  499. $this->assertHtml($expected, $result);
  500. }
  501. /**
  502. * Test create() with the templates option.
  503. *
  504. * @return void
  505. */
  506. public function testCreateTemplatesFile()
  507. {
  508. $result = $this->Form->create($this->article, [
  509. 'templates' => 'htmlhelper_tags',
  510. ]);
  511. $expected = [
  512. 'start form',
  513. 'div' => ['class' => 'hidden'],
  514. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  515. '/div'
  516. ];
  517. $this->assertHtml($expected, $result);
  518. }
  519. /**
  520. * Test that create() and end() restore templates.
  521. *
  522. * @return void
  523. */
  524. public function testCreateEndRestoreTemplates()
  525. {
  526. $this->Form->create($this->article, [
  527. 'templates' => ['input' => 'custom input element']
  528. ]);
  529. $this->Form->end();
  530. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  531. }
  532. /**
  533. * Test using template vars in various templates used by control() method.
  534. *
  535. * @return void
  536. */
  537. public function testControlTemplateVars()
  538. {
  539. $result = $this->Form->control('text', [
  540. 'templates' => [
  541. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  542. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  543. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  544. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  545. ],
  546. 'templateVars' => [
  547. 'forinput' => 'in-input',
  548. 'forlabel' => 'in-label',
  549. 'forgroup' => 'in-group',
  550. 'forcontainer' => 'in-container'
  551. ]
  552. ]);
  553. $expected = [
  554. 'div' => ['class'],
  555. 'label' => ['for'],
  556. 'Text in-label',
  557. '/label',
  558. 'in-group',
  559. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  560. 'in-container',
  561. '/div',
  562. ];
  563. $this->assertHtml($expected, $result);
  564. }
  565. /**
  566. * Test ensuring template variables work in template files loaded
  567. * during control().
  568. *
  569. * @return void
  570. */
  571. public function testControlTemplatesFromFile()
  572. {
  573. $result = $this->Form->control('title', [
  574. 'templates' => 'test_templates',
  575. 'templateVars' => [
  576. 'forcontainer' => 'container-data'
  577. ]
  578. ]);
  579. $expected = [
  580. 'div' => ['class'],
  581. 'label' => ['for'],
  582. 'Title',
  583. '/label',
  584. 'input' => ['name', 'type' => 'text', 'id'],
  585. 'container-data',
  586. '/div',
  587. ];
  588. $this->assertHtml($expected, $result);
  589. }
  590. /**
  591. * Test using template vars in inputSubmit and submitContainer template.
  592. *
  593. * @return void
  594. */
  595. public function testSubmitTemplateVars()
  596. {
  597. $this->Form->templates([
  598. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  599. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  600. ]);
  601. $result = $this->Form->submit('Submit', [
  602. 'templateVars' => [
  603. 'forinput' => 'in-input',
  604. 'forcontainer' => 'in-container'
  605. ]
  606. ]);
  607. $expected = [
  608. 'div' => ['class'],
  609. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  610. 'in-container',
  611. '/div',
  612. ];
  613. $this->assertHtml($expected, $result);
  614. }
  615. /**
  616. * test the create() method
  617. *
  618. * @dataProvider requestTypeProvider
  619. * @return void
  620. */
  621. public function testCreateTypeOptions($type, $method, $override)
  622. {
  623. $encoding = strtolower(Configure::read('App.encoding'));
  624. $result = $this->Form->create(false, ['type' => $type]);
  625. $expected = [
  626. 'form' => [
  627. 'method' => $method, 'action' => '/articles/add',
  628. 'accept-charset' => $encoding
  629. ],
  630. 'div' => ['style' => 'display:none;'],
  631. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  632. '/div'
  633. ];
  634. $this->assertHtml($expected, $result);
  635. }
  636. /**
  637. * Test using template vars in Create (formStart template)
  638. *
  639. * @return void
  640. */
  641. public function testCreateTemplateVars()
  642. {
  643. $result = $this->Form->create($this->article, [
  644. 'templates' => [
  645. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  646. ],
  647. 'templateVars' => ['header' => 'headertext']
  648. ]);
  649. $expected = [
  650. 'h4' => ['class'],
  651. 'headertext',
  652. '/h4',
  653. 'form' => [
  654. 'method' => 'post',
  655. 'action' => '/articles/add',
  656. 'accept-charset' => 'utf-8'
  657. ]
  658. ];
  659. $this->assertHtml($expected, $result);
  660. }
  661. /**
  662. * Test opening a form for an update operation.
  663. *
  664. * @return void
  665. */
  666. public function testCreateUpdateForm()
  667. {
  668. $encoding = strtolower(Configure::read('App.encoding'));
  669. $this->Form->request->here = '/articles/edit/1';
  670. $this->Form->request->params['action'] = 'edit';
  671. $this->article['defaults']['id'] = 1;
  672. $result = $this->Form->create($this->article);
  673. $expected = [
  674. 'form' => [
  675. 'method' => 'post',
  676. 'action' => '/articles/edit/1',
  677. 'accept-charset' => $encoding
  678. ],
  679. 'div' => ['style' => 'display:none;'],
  680. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  681. '/div'
  682. ];
  683. $this->assertHtml($expected, $result);
  684. }
  685. /**
  686. * test create() with automatic url generation
  687. *
  688. * @return void
  689. */
  690. public function testCreateAutoUrl()
  691. {
  692. $encoding = strtolower(Configure::read('App.encoding'));
  693. $this->Form->request->params['action'] = 'delete';
  694. $this->Form->request->here = '/articles/delete/10';
  695. $this->Form->request->base = '';
  696. $result = $this->Form->create($this->article);
  697. $expected = [
  698. 'form' => [
  699. 'method' => 'post', 'action' => '/articles/delete/10',
  700. 'accept-charset' => $encoding
  701. ],
  702. 'div' => ['style' => 'display:none;'],
  703. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  704. '/div'
  705. ];
  706. $this->assertHtml($expected, $result);
  707. $this->article['defaults'] = ['id' => 1];
  708. $this->Form->request->here = '/articles/edit/1';
  709. $this->Form->request->params['action'] = 'delete';
  710. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  711. $expected = [
  712. 'form' => [
  713. 'method' => 'post',
  714. 'action' => '/articles/edit/1',
  715. 'accept-charset' => $encoding
  716. ],
  717. 'div' => ['style' => 'display:none;'],
  718. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  719. '/div'
  720. ];
  721. $this->assertHtml($expected, $result);
  722. $this->Form->request->params['action'] = 'add';
  723. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  724. $expected = [
  725. 'form' => [
  726. 'method' => 'post',
  727. 'action' => '/articles/publish/1',
  728. 'accept-charset' => $encoding
  729. ],
  730. 'div' => ['style' => 'display:none;'],
  731. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  732. '/div'
  733. ];
  734. $this->assertHtml($expected, $result);
  735. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  736. $expected = [
  737. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  738. 'div' => ['style' => 'display:none;'],
  739. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  740. '/div'
  741. ];
  742. $this->assertHtml($expected, $result);
  743. $this->Form->request->params['controller'] = 'Pages';
  744. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  745. $expected = [
  746. 'form' => [
  747. 'method' => 'post', 'action' => '/Pages/signup/1',
  748. 'accept-charset' => $encoding
  749. ],
  750. 'div' => ['style' => 'display:none;'],
  751. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  752. '/div'
  753. ];
  754. $this->assertHtml($expected, $result);
  755. }
  756. /**
  757. * Test create() with no URL (no "action" attribute for <form> tag)
  758. *
  759. * @return void
  760. */
  761. public function testCreateNoUrl()
  762. {
  763. $result = $this->Form->create(false, ['url' => false]);
  764. $expected = [
  765. 'form' => [
  766. 'method' => 'post',
  767. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  768. ],
  769. 'div' => ['style' => 'display:none;'],
  770. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  771. '/div'
  772. ];
  773. $this->assertHtml($expected, $result);
  774. }
  775. /**
  776. * test create() with a custom route
  777. *
  778. * @return void
  779. */
  780. public function testCreateCustomRoute()
  781. {
  782. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  783. $encoding = strtolower(Configure::read('App.encoding'));
  784. $this->Form->request->params['controller'] = 'users';
  785. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  786. $expected = [
  787. 'form' => [
  788. 'method' => 'post', 'action' => '/login',
  789. 'accept-charset' => $encoding
  790. ],
  791. 'div' => ['style' => 'display:none;'],
  792. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  793. '/div'
  794. ];
  795. $this->assertHtml($expected, $result);
  796. Router::connect(
  797. '/new-article',
  798. ['controller' => 'articles', 'action' => 'myaction'],
  799. ['_name' => 'my-route']
  800. );
  801. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  802. $expected = [
  803. 'form' => [
  804. 'method' => 'post', 'action' => '/new-article',
  805. 'accept-charset' => $encoding,
  806. ],
  807. 'div' => ['style' => 'display:none;'],
  808. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  809. '/div'
  810. ];
  811. $this->assertHtml($expected, $result);
  812. }
  813. /**
  814. * test automatic accept-charset overriding
  815. *
  816. * @return void
  817. */
  818. public function testCreateWithAcceptCharset()
  819. {
  820. $result = $this->Form->create(
  821. $this->article,
  822. [
  823. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  824. ]
  825. );
  826. $expected = [
  827. 'form' => [
  828. 'method' => 'post', 'action' => '/articles',
  829. 'accept-charset' => 'iso-8859-1'
  830. ],
  831. 'div' => ['style' => 'display:none;'],
  832. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  833. '/div'
  834. ];
  835. $this->assertHtml($expected, $result);
  836. }
  837. /**
  838. * Test base form URL when url param is passed with multiple parameters (&)
  839. *
  840. */
  841. public function testCreateQueryStringRequest()
  842. {
  843. $encoding = strtolower(Configure::read('App.encoding'));
  844. $result = $this->Form->create($this->article, [
  845. 'type' => 'post',
  846. 'escape' => false,
  847. 'url' => [
  848. 'controller' => 'controller',
  849. 'action' => 'action',
  850. '?' => ['param1' => 'value1', 'param2' => 'value2']
  851. ]
  852. ]);
  853. $expected = [
  854. 'form' => [
  855. 'method' => 'post',
  856. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  857. 'accept-charset' => $encoding
  858. ],
  859. 'div' => ['style' => 'display:none;'],
  860. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  861. '/div'
  862. ];
  863. $this->assertHtml($expected, $result);
  864. $result = $this->Form->create($this->article, [
  865. 'type' => 'post',
  866. 'url' => [
  867. 'controller' => 'controller',
  868. 'action' => 'action',
  869. '?' => ['param1' => 'value1', 'param2' => 'value2']
  870. ]
  871. ]);
  872. $this->assertHtml($expected, $result);
  873. }
  874. /**
  875. * test that create() doesn't cause errors by multiple id's being in the primary key
  876. * as could happen with multiple select or checkboxes.
  877. *
  878. * @return void
  879. */
  880. public function testCreateWithMultipleIdInData()
  881. {
  882. $encoding = strtolower(Configure::read('App.encoding'));
  883. $this->Form->request->data['Article']['id'] = [1, 2];
  884. $result = $this->Form->create($this->article);
  885. $expected = [
  886. 'form' => [
  887. 'method' => 'post',
  888. 'action' => '/articles/add',
  889. 'accept-charset' => $encoding
  890. ],
  891. 'div' => ['style' => 'display:none;'],
  892. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  893. '/div'
  894. ];
  895. $this->assertHtml($expected, $result);
  896. }
  897. /**
  898. * test that create() doesn't add in extra passed params.
  899. *
  900. * @return void
  901. */
  902. public function testCreatePassedArgs()
  903. {
  904. $encoding = strtolower(Configure::read('App.encoding'));
  905. $this->Form->request->data['Article']['id'] = 1;
  906. $result = $this->Form->create($this->article, [
  907. 'type' => 'post',
  908. 'escape' => false,
  909. 'url' => [
  910. 'action' => 'edit',
  911. 'myparam'
  912. ]
  913. ]);
  914. $expected = [
  915. 'form' => [
  916. 'method' => 'post',
  917. 'action' => '/articles/edit/myparam',
  918. 'accept-charset' => $encoding
  919. ],
  920. 'div' => ['style' => 'display:none;'],
  921. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  922. '/div'
  923. ];
  924. $this->assertHtml($expected, $result);
  925. }
  926. /**
  927. * test creating a get form, and get form inputs.
  928. *
  929. * @return void
  930. */
  931. public function testGetFormCreate()
  932. {
  933. $encoding = strtolower(Configure::read('App.encoding'));
  934. $result = $this->Form->create($this->article, ['type' => 'get']);
  935. $expected = ['form' => [
  936. 'method' => 'get', 'action' => '/articles/add',
  937. 'accept-charset' => $encoding
  938. ]];
  939. $this->assertHtml($expected, $result);
  940. $result = $this->Form->text('title');
  941. $expected = ['input' => [
  942. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  943. ]];
  944. $this->assertHtml($expected, $result);
  945. $result = $this->Form->password('password');
  946. $expected = ['input' => [
  947. 'name' => 'password', 'type' => 'password'
  948. ]];
  949. $this->assertHtml($expected, $result);
  950. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  951. $result = $this->Form->text('user_form');
  952. $expected = ['input' => [
  953. 'name' => 'user_form', 'type' => 'text'
  954. ]];
  955. $this->assertHtml($expected, $result);
  956. }
  957. /**
  958. * test get form, and inputs when the model param is false
  959. *
  960. * @return void
  961. */
  962. public function testGetFormWithFalseModel()
  963. {
  964. $encoding = strtolower(Configure::read('App.encoding'));
  965. $this->Form->request->params['controller'] = 'contact_test';
  966. $result = $this->Form->create(false, [
  967. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  968. ]);
  969. $expected = ['form' => [
  970. 'method' => 'get', 'action' => '/contact_test/add',
  971. 'accept-charset' => $encoding
  972. ]];
  973. $this->assertHtml($expected, $result);
  974. $result = $this->Form->text('reason');
  975. $expected = [
  976. 'input' => ['type' => 'text', 'name' => 'reason']
  977. ];
  978. $this->assertHtml($expected, $result);
  979. }
  980. /**
  981. * testFormCreateWithSecurity method
  982. *
  983. * Test form->create() with security key.
  984. *
  985. * @return void
  986. */
  987. public function testCreateWithSecurity()
  988. {
  989. $this->Form->request->params['_csrfToken'] = 'testKey';
  990. $encoding = strtolower(Configure::read('App.encoding'));
  991. $result = $this->Form->create($this->article, [
  992. 'url' => '/articles/publish',
  993. ]);
  994. $expected = [
  995. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  996. 'div' => ['style' => 'display:none;'],
  997. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  998. ['input' => [
  999. 'type' => 'hidden',
  1000. 'name' => '_csrfToken',
  1001. 'value' => 'testKey',
  1002. 'autocomplete' => 'off',
  1003. ]],
  1004. '/div'
  1005. ];
  1006. $this->assertHtml($expected, $result);
  1007. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1008. $expected['form']['id'] = 'MyForm';
  1009. $this->assertHtml($expected, $result);
  1010. }
  1011. /**
  1012. * testFormCreateGetNoSecurity method
  1013. *
  1014. * Test form->create() with no security key as its a get form
  1015. *
  1016. * @return void
  1017. */
  1018. public function testCreateEndGetNoSecurity()
  1019. {
  1020. $this->Form->request->params['_csrfToken'] = 'testKey';
  1021. $article = new Article();
  1022. $result = $this->Form->create($article, [
  1023. 'type' => 'get',
  1024. 'url' => '/contacts/add'
  1025. ]);
  1026. $this->assertNotContains('testKey', $result);
  1027. $result = $this->Form->end();
  1028. $this->assertNotContains('testKey', $result);
  1029. }
  1030. /**
  1031. * test that create() clears the fields property so it starts fresh
  1032. *
  1033. * @return void
  1034. */
  1035. public function testCreateClearingFields()
  1036. {
  1037. $this->Form->fields = ['model_id'];
  1038. $this->Form->create($this->article);
  1039. $this->assertEquals([], $this->Form->fields);
  1040. }
  1041. /**
  1042. * Tests form hash generation with model-less data
  1043. *
  1044. * @return void
  1045. */
  1046. public function testValidateHashNoModel()
  1047. {
  1048. $this->Form->request->params['_Token'] = 'foo';
  1049. $result = $this->Form->secure(['anything']);
  1050. $this->assertRegExp('/b9731869b9915e3dee6250db1a1fad464371fb94/', $result);
  1051. }
  1052. /**
  1053. * Tests that hidden fields generated for checkboxes don't get locked
  1054. *
  1055. * @return void
  1056. */
  1057. public function testNoCheckboxLocking()
  1058. {
  1059. $this->Form->request->params['_Token'] = 'foo';
  1060. $this->assertSame([], $this->Form->fields);
  1061. $this->Form->checkbox('check', ['value' => '1']);
  1062. $this->assertSame($this->Form->fields, ['check']);
  1063. }
  1064. /**
  1065. * testFormSecurityFields method
  1066. *
  1067. * Test generation of secure form hash generation.
  1068. *
  1069. * @return void
  1070. */
  1071. public function testFormSecurityFields()
  1072. {
  1073. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1074. $this->Form->request->params['_Token'] = 'testKey';
  1075. $result = $this->Form->secure($fields);
  1076. $hash = hash_hmac('sha1', serialize($fields), Security::salt());
  1077. $hash .= ':' . 'Model.valid';
  1078. $hash = urlencode($hash);
  1079. $tokenDebug = urlencode(json_encode([
  1080. '',
  1081. $fields,
  1082. []
  1083. ]));
  1084. $expected = [
  1085. 'div' => ['style' => 'display:none;'],
  1086. ['input' => [
  1087. 'type' => 'hidden',
  1088. 'name' => '_Token[fields]',
  1089. 'value' => $hash,
  1090. 'autocomplete' => 'off',
  1091. ]],
  1092. ['input' => [
  1093. 'type' => 'hidden',
  1094. 'name' => '_Token[unlocked]',
  1095. 'value' => '',
  1096. 'autocomplete' => 'off',
  1097. ]],
  1098. ['input' => [
  1099. 'type' => 'hidden',
  1100. 'name' => '_Token[debug]',
  1101. 'value' => $tokenDebug,
  1102. 'autocomplete' => 'off',
  1103. ]],
  1104. '/div'
  1105. ];
  1106. $this->assertHtml($expected, $result);
  1107. }
  1108. /**
  1109. * testFormSecurityFields method
  1110. *
  1111. * Test debug token is not generated if debug is false
  1112. *
  1113. * @return void
  1114. */
  1115. public function testFormSecurityFieldsNoDebugMode()
  1116. {
  1117. Configure::write('debug', false);
  1118. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1119. $this->Form->request->params['_Token'] = 'testKey';
  1120. $result = $this->Form->secure($fields);
  1121. $hash = hash_hmac('sha1', serialize($fields), Security::salt());
  1122. $hash .= ':' . 'Model.valid';
  1123. $hash = urlencode($hash);
  1124. $expected = [
  1125. 'div' => ['style' => 'display:none;'],
  1126. ['input' => [
  1127. 'type' => 'hidden',
  1128. 'name' => '_Token[fields]',
  1129. 'autocomplete' => 'off',
  1130. 'value' => $hash
  1131. ]],
  1132. ['input' => [
  1133. 'type' => 'hidden',
  1134. 'name' => '_Token[unlocked]',
  1135. 'autocomplete' => 'off',
  1136. 'value' => '',
  1137. ]],
  1138. '/div'
  1139. ];
  1140. $this->assertHtml($expected, $result);
  1141. }
  1142. /**
  1143. * Tests correct generation of number fields for smallint
  1144. *
  1145. * @return void
  1146. */
  1147. public function testTextFieldGenerationForSmallint()
  1148. {
  1149. $this->article['schema'] = [
  1150. 'foo' => [
  1151. 'type' => 'smallinteger',
  1152. 'null' => false,
  1153. 'default' => null,
  1154. 'length' => 10
  1155. ]
  1156. ];
  1157. $this->Form->create($this->article);
  1158. $result = $this->Form->control('foo');
  1159. $this->assertContains('class="input number"', $result);
  1160. $this->assertContains('type="number"', $result);
  1161. }
  1162. /**
  1163. * Tests correct generation of number fields for tinyint
  1164. *
  1165. * @return void
  1166. */
  1167. public function testTextFieldGenerationForTinyint()
  1168. {
  1169. $this->article['schema'] = [
  1170. 'foo' => [
  1171. 'type' => 'tinyinteger',
  1172. 'null' => false,
  1173. 'default' => null,
  1174. 'length' => 10
  1175. ]
  1176. ];
  1177. $this->Form->create($this->article);
  1178. $result = $this->Form->control('foo');
  1179. $this->assertContains('class="input number"', $result);
  1180. $this->assertContains('type="number"', $result);
  1181. }
  1182. /**
  1183. * Tests correct generation of number fields for double and float fields
  1184. *
  1185. * @return void
  1186. */
  1187. public function testTextFieldGenerationForFloats()
  1188. {
  1189. $this->article['schema'] = [
  1190. 'foo' => [
  1191. 'type' => 'float',
  1192. 'null' => false,
  1193. 'default' => null,
  1194. 'length' => 10
  1195. ]
  1196. ];
  1197. $this->Form->create($this->article);
  1198. $result = $this->Form->control('foo');
  1199. $expected = [
  1200. 'div' => ['class' => 'input number'],
  1201. 'label' => ['for' => 'foo'],
  1202. 'Foo',
  1203. '/label',
  1204. ['input' => [
  1205. 'type' => 'number',
  1206. 'name' => 'foo',
  1207. 'id' => 'foo',
  1208. 'step' => 'any'
  1209. ]],
  1210. '/div'
  1211. ];
  1212. $this->assertHtml($expected, $result);
  1213. $result = $this->Form->control('foo', ['step' => 0.5]);
  1214. $expected = [
  1215. 'div' => ['class' => 'input number'],
  1216. 'label' => ['for' => 'foo'],
  1217. 'Foo',
  1218. '/label',
  1219. ['input' => [
  1220. 'type' => 'number',
  1221. 'name' => 'foo',
  1222. 'id' => 'foo',
  1223. 'step' => '0.5'
  1224. ]],
  1225. '/div'
  1226. ];
  1227. $this->assertHtml($expected, $result);
  1228. }
  1229. /**
  1230. * Tests correct generation of number fields for integer fields
  1231. *
  1232. * @return void
  1233. */
  1234. public function testTextFieldTypeNumberGenerationForIntegers()
  1235. {
  1236. TableRegistry::get('Contacts', [
  1237. 'className' => __NAMESPACE__ . '\ContactsTable'
  1238. ]);
  1239. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1240. $result = $this->Form->control('age');
  1241. $expected = [
  1242. 'div' => ['class' => 'input number'],
  1243. 'label' => ['for' => 'age'],
  1244. 'Age',
  1245. '/label',
  1246. ['input' => [
  1247. 'type' => 'number', 'name' => 'age',
  1248. 'id' => 'age'
  1249. ]],
  1250. '/div'
  1251. ];
  1252. $this->assertHtml($expected, $result);
  1253. }
  1254. /**
  1255. * Tests correct generation of file upload fields for binary fields
  1256. *
  1257. * @return void
  1258. */
  1259. public function testFileUploadFieldTypeGenerationForBinaries()
  1260. {
  1261. $table = TableRegistry::get('Contacts', [
  1262. 'className' => __NAMESPACE__ . '\ContactsTable'
  1263. ]);
  1264. $table->schema(['foo' => [
  1265. 'type' => 'binary',
  1266. 'null' => false,
  1267. 'default' => null,
  1268. 'length' => 1024
  1269. ]]);
  1270. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1271. $result = $this->Form->control('foo');
  1272. $expected = [
  1273. 'div' => ['class' => 'input file'],
  1274. 'label' => ['for' => 'foo'],
  1275. 'Foo',
  1276. '/label',
  1277. ['input' => [
  1278. 'type' => 'file', 'name' => 'foo',
  1279. 'id' => 'foo'
  1280. ]],
  1281. '/div'
  1282. ];
  1283. $this->assertHtml($expected, $result);
  1284. }
  1285. /**
  1286. * testFormSecurityMultipleFields method
  1287. *
  1288. * Test secure() with multiple row form. Ensure hash is correct.
  1289. *
  1290. * @return void
  1291. */
  1292. public function testFormSecurityMultipleFields()
  1293. {
  1294. $this->Form->request->params['_Token'] = 'foo';
  1295. $fields = [
  1296. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1297. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1298. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1299. ];
  1300. $result = $this->Form->secure($fields);
  1301. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  1302. $hash = '16e544e04f6d3007231e3e23f8f73427a53272d4%3AModel.0.hidden%7CModel.0.valid';
  1303. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  1304. $tokenDebug = urlencode(json_encode([
  1305. '',
  1306. $fields,
  1307. []
  1308. ]));
  1309. $expected = [
  1310. 'div' => ['style' => 'display:none;'],
  1311. ['input' => [
  1312. 'type' => 'hidden',
  1313. 'name' => '_Token[fields]',
  1314. 'value' => $hash,
  1315. 'autocomplete' => 'off',
  1316. ]],
  1317. ['input' => [
  1318. 'type' => 'hidden',
  1319. 'name' => '_Token[unlocked]',
  1320. 'autocomplete' => 'off',
  1321. 'value' => ''
  1322. ]],
  1323. ['input' => [
  1324. 'type' => 'hidden',
  1325. 'name' => '_Token[debug]',
  1326. 'value' => $tokenDebug,
  1327. 'autocomplete' => 'off',
  1328. ]],
  1329. '/div'
  1330. ];
  1331. $this->assertHtml($expected, $result);
  1332. }
  1333. /**
  1334. * testFormSecurityMultipleSubmitButtons
  1335. *
  1336. * test form submit generation and ensure that _Token is only created on end()
  1337. *
  1338. * @return void
  1339. */
  1340. public function testFormSecurityMultipleSubmitButtons()
  1341. {
  1342. $this->Form->request->params['_Token'] = 'testKey';
  1343. $this->Form->create($this->article);
  1344. $this->Form->text('Address.title');
  1345. $this->Form->text('Address.first_name');
  1346. $result = $this->Form->submit('Save', ['name' => 'save']);
  1347. $expected = [
  1348. 'div' => ['class' => 'submit'],
  1349. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1350. '/div',
  1351. ];
  1352. $this->assertHtml($expected, $result);
  1353. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1354. $expected = [
  1355. 'div' => ['class' => 'submit'],
  1356. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1357. '/div',
  1358. ];
  1359. $this->assertHtml($expected, $result);
  1360. $result = $this->Form->end();
  1361. $tokenDebug = urlencode(json_encode([
  1362. '/articles/add',
  1363. [
  1364. 'Address.title',
  1365. 'Address.first_name',
  1366. ],
  1367. ['save', 'cancel']
  1368. ]));
  1369. $expected = [
  1370. 'div' => ['style' => 'display:none;'],
  1371. ['input' => [
  1372. 'type' => 'hidden',
  1373. 'name' => '_Token[fields]',
  1374. 'autocomplete',
  1375. 'value'
  1376. ]],
  1377. ['input' => [
  1378. 'type' => 'hidden',
  1379. 'name' => '_Token[unlocked]',
  1380. 'value' => 'cancel%7Csave',
  1381. 'autocomplete' => 'off',
  1382. ]],
  1383. ['input' => [
  1384. 'type' => 'hidden',
  1385. 'name' => '_Token[debug]',
  1386. 'value' => $tokenDebug,
  1387. 'autocomplete' => 'off',
  1388. ]],
  1389. '/div'
  1390. ];
  1391. $this->assertHtml($expected, $result);
  1392. }
  1393. /**
  1394. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1395. *
  1396. * @return void
  1397. */
  1398. public function testSecurityButtonNestedNamed()
  1399. {
  1400. $key = 'testKey';
  1401. $this->Form->request->params['_csrfToken'] = $key;
  1402. $this->Form->create('Addresses');
  1403. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1404. $result = $this->Form->unlockField();
  1405. $this->assertEquals(['Address.button'], $result);
  1406. }
  1407. /**
  1408. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1409. *
  1410. * @return void
  1411. */
  1412. public function testSecuritySubmitNestedNamed()
  1413. {
  1414. $this->Form->request->params['_Token'] = 'testKey';
  1415. $this->Form->create($this->article);
  1416. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1417. $result = $this->Form->unlockField();
  1418. $this->assertEquals(['Address.button'], $result);
  1419. }
  1420. /**
  1421. * Test that the correct fields are unlocked for image submits with no names.
  1422. *
  1423. * @return void
  1424. */
  1425. public function testSecuritySubmitImageNoName()
  1426. {
  1427. $this->Form->request->params['_Token'] = 'testKey';
  1428. $this->Form->create(false);
  1429. $result = $this->Form->submit('save.png');
  1430. $expected = [
  1431. 'div' => ['class' => 'submit'],
  1432. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1433. '/div'
  1434. ];
  1435. $this->assertHtml($expected, $result);
  1436. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1437. }
  1438. /**
  1439. * Test that the correct fields are unlocked for image submits with names.
  1440. *
  1441. * @return void
  1442. */
  1443. public function testSecuritySubmitImageName()
  1444. {
  1445. $this->Form->request->params['_Token'] = 'testKey';
  1446. $this->Form->create(null);
  1447. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1448. $expected = [
  1449. 'div' => ['class' => 'submit'],
  1450. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1451. '/div'
  1452. ];
  1453. $this->assertHtml($expected, $result);
  1454. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1455. }
  1456. /**
  1457. * testFormSecurityMultipleControlFields method
  1458. *
  1459. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1460. *
  1461. * @return void
  1462. */
  1463. public function testFormSecurityMultipleControlFields()
  1464. {
  1465. $this->Form->request->params['_Token'] = 'testKey';
  1466. $this->Form->create();
  1467. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1468. $this->Form->control('Addresses.0.title');
  1469. $this->Form->control('Addresses.0.first_name');
  1470. $this->Form->control('Addresses.0.last_name');
  1471. $this->Form->control('Addresses.0.address');
  1472. $this->Form->control('Addresses.0.city');
  1473. $this->Form->control('Addresses.0.phone');
  1474. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1475. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1476. $this->Form->control('Addresses.1.title');
  1477. $this->Form->control('Addresses.1.first_name');
  1478. $this->Form->control('Addresses.1.last_name');
  1479. $this->Form->control('Addresses.1.address');
  1480. $this->Form->control('Addresses.1.city');
  1481. $this->Form->control('Addresses.1.phone');
  1482. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1483. $result = $this->Form->secure($this->Form->fields);
  1484. $hash = '587942c6810603a6d5a07a394316dda455580227%3AAddresses.0.id%7CAddresses.1.id';
  1485. $tokenDebug = urlencode(json_encode([
  1486. '/articles/add',
  1487. [
  1488. 'Addresses.0.id' => '123456',
  1489. 'Addresses.0.title',
  1490. 'Addresses.0.first_name',
  1491. 'Addresses.0.last_name',
  1492. 'Addresses.0.address',
  1493. 'Addresses.0.city',
  1494. 'Addresses.0.phone',
  1495. 'Addresses.0.primary',
  1496. 'Addresses.1.id' => '654321',
  1497. 'Addresses.1.title',
  1498. 'Addresses.1.first_name',
  1499. 'Addresses.1.last_name',
  1500. 'Addresses.1.address',
  1501. 'Addresses.1.city',
  1502. 'Addresses.1.phone',
  1503. 'Addresses.1.primary',
  1504. ],
  1505. []
  1506. ]));
  1507. $expected = [
  1508. 'div' => ['style' => 'display:none;'],
  1509. ['input' => [
  1510. 'type' => 'hidden',
  1511. 'name' => '_Token[fields]',
  1512. 'value' => $hash,
  1513. 'autocomplete' => 'off',
  1514. ]],
  1515. ['input' => [
  1516. 'type' => 'hidden',
  1517. 'name' => '_Token[unlocked]',
  1518. 'autocomplete' => 'off',
  1519. 'value' => ''
  1520. ]],
  1521. ['input' => [
  1522. 'type' => 'hidden',
  1523. 'name' => '_Token[debug]',
  1524. 'value' => $tokenDebug,
  1525. 'autocomplete' => 'off',
  1526. ]],
  1527. '/div'
  1528. ];
  1529. $this->assertHtml($expected, $result);
  1530. }
  1531. /**
  1532. * testFormSecurityArrayFields method
  1533. *
  1534. * Test form security with Model.field.0 style inputs.
  1535. *
  1536. * @return void
  1537. */
  1538. public function testFormSecurityArrayFields()
  1539. {
  1540. $this->Form->request->params['_Token'] = 'testKey';
  1541. $this->Form->create();
  1542. $this->Form->text('Address.primary.1');
  1543. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1544. $this->Form->text('Address.secondary.1.0');
  1545. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1546. }
  1547. /**
  1548. * testFormSecurityMultipleControlDisabledFields method
  1549. *
  1550. * Test secure form generation with multiple records and disabled fields.
  1551. *
  1552. * @return void
  1553. */
  1554. public function testFormSecurityMultipleControlDisabledFields()
  1555. {
  1556. $this->Form->request->params['_Token'] = [
  1557. 'unlockedFields' => ['first_name', 'address']
  1558. ];
  1559. $this->Form->create();
  1560. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1561. $this->Form->text('Addresses.0.title');
  1562. $this->Form->text('Addresses.0.first_name');
  1563. $this->Form->text('Addresses.0.last_name');
  1564. $this->Form->text('Addresses.0.address');
  1565. $this->Form->text('Addresses.0.city');
  1566. $this->Form->text('Addresses.0.phone');
  1567. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1568. $this->Form->text('Addresses.1.title');
  1569. $this->Form->text('Addresses.1.first_name');
  1570. $this->Form->text('Addresses.1.last_name');
  1571. $this->Form->text('Addresses.1.address');
  1572. $this->Form->text('Addresses.1.city');
  1573. $this->Form->text('Addresses.1.phone');
  1574. $result = $this->Form->secure($this->Form->fields);
  1575. $hash = '8db4b5f1a912dfafd9c264964df7aa598ea322c0%3AAddresses.0.id%7CAddresses.1.id';
  1576. $tokenDebug = urlencode(json_encode([
  1577. '/articles/add',
  1578. [
  1579. 'Addresses.0.id' => '123456',
  1580. 'Addresses.0.title',
  1581. 'Addresses.0.last_name',
  1582. 'Addresses.0.city',
  1583. 'Addresses.0.phone',
  1584. 'Addresses.1.id' => '654321',
  1585. 'Addresses.1.title',
  1586. 'Addresses.1.last_name',
  1587. 'Addresses.1.city',
  1588. 'Addresses.1.phone'
  1589. ],
  1590. [
  1591. 'first_name',
  1592. 'address'
  1593. ]
  1594. ]));
  1595. $expected = [
  1596. 'div' => ['style' => 'display:none;'],
  1597. ['input' => [
  1598. 'type' => 'hidden',
  1599. 'name' => '_Token[fields]',
  1600. 'autocomplete' => 'off',
  1601. 'value' => $hash
  1602. ]],
  1603. ['input' => [
  1604. 'type' => 'hidden',
  1605. 'name' => '_Token[unlocked]',
  1606. 'autocomplete' => 'off',
  1607. 'value' => 'address%7Cfirst_name',
  1608. ]],
  1609. ['input' => [
  1610. 'type' => 'hidden',
  1611. 'name' => '_Token[debug]',
  1612. 'autocomplete' => 'off',
  1613. 'value' => $tokenDebug,
  1614. ]],
  1615. '/div'
  1616. ];
  1617. $this->assertHtml($expected, $result);
  1618. }
  1619. /**
  1620. * testFormSecurityControlDisabledFields method
  1621. *
  1622. * Test single record form with disabled fields.
  1623. *
  1624. * @return void
  1625. */
  1626. public function testFormSecurityControlUnlockedFields()
  1627. {
  1628. $this->Form->request->params['_Token'] = [
  1629. 'unlockedFields' => ['first_name', 'address']
  1630. ];
  1631. $this->Form->create();
  1632. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1633. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1634. $this->Form->text('Addresses.title');
  1635. $this->Form->text('Addresses.first_name');
  1636. $this->Form->text('Addresses.last_name');
  1637. $this->Form->text('Addresses.address');
  1638. $this->Form->text('Addresses.city');
  1639. $this->Form->text('Addresses.phone');
  1640. $result = $this->Form->fields;
  1641. $expected = [
  1642. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1643. 'Addresses.city', 'Addresses.phone'
  1644. ];
  1645. $this->assertEquals($expected, $result);
  1646. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1647. $hash = 'cdc8fa2dd2aa2804c12cd17279c39747f1c57354%3AAddresses.id';
  1648. $tokenDebug = urlencode(json_encode([
  1649. '/articles/add',
  1650. [
  1651. 'Addresses.id' => '123456',
  1652. 'Addresses.title',
  1653. 'Addresses.last_name',
  1654. 'Addresses.city',
  1655. 'Addresses.phone'
  1656. ],
  1657. [
  1658. 'first_name',
  1659. 'address'
  1660. ]
  1661. ]));
  1662. $expected = [
  1663. 'div' => ['style' => 'display:none;'],
  1664. ['input' => [
  1665. 'type' => 'hidden',
  1666. 'name' => '_Token[fields]',
  1667. 'value' => $hash,
  1668. 'autocomplete' => 'off',
  1669. 'data-foo' => 'bar',
  1670. ]],
  1671. ['input' => [
  1672. 'type' => 'hidden',
  1673. 'name' => '_Token[unlocked]',
  1674. 'value' => 'address%7Cfirst_name',
  1675. 'autocomplete' => 'off',
  1676. 'data-foo' => 'bar',
  1677. ]],
  1678. ['input' => [
  1679. 'type' => 'hidden', 'name' => '_Token[debug]',
  1680. 'value' => $tokenDebug,
  1681. 'autocomplete' => 'off',
  1682. 'data-foo' => 'bar'
  1683. ]],
  1684. '/div'
  1685. ];
  1686. $this->assertHtml($expected, $result);
  1687. }
  1688. /**
  1689. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1690. *
  1691. * Test single record form with debugSecurity param.
  1692. *
  1693. * @return void
  1694. */
  1695. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1696. {
  1697. $this->Form->request->params['_Token'] = [
  1698. 'unlockedFields' => ['first_name', 'address']
  1699. ];
  1700. $this->Form->create();
  1701. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1702. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1703. $this->Form->text('Addresses.title');
  1704. $this->Form->text('Addresses.first_name');
  1705. $this->Form->text('Addresses.last_name');
  1706. $this->Form->text('Addresses.address');
  1707. $this->Form->text('Addresses.city');
  1708. $this->Form->text('Addresses.phone');
  1709. $result = $this->Form->fields;
  1710. $expected = [
  1711. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1712. 'Addresses.city', 'Addresses.phone'
  1713. ];
  1714. $this->assertEquals($expected, $result);
  1715. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1716. $hash = 'cdc8fa2dd2aa2804c12cd17279c39747f1c57354%3AAddresses.id';
  1717. $tokenDebug = urlencode(json_encode([
  1718. '/articles/add',
  1719. [
  1720. 'Addresses.id' => '123456',
  1721. 'Addresses.title',
  1722. 'Addresses.last_name',
  1723. 'Addresses.city',
  1724. 'Addresses.phone'
  1725. ],
  1726. [
  1727. 'first_name',
  1728. 'address'
  1729. ]
  1730. ]));
  1731. $expected = [
  1732. 'div' => ['style' => 'display:none;'],
  1733. ['input' => [
  1734. 'type' => 'hidden',
  1735. 'name' => '_Token[fields]',
  1736. 'value' => $hash,
  1737. 'autocomplete' => 'off',
  1738. 'data-foo' => 'bar',
  1739. ]],
  1740. ['input' => [
  1741. 'type' => 'hidden',
  1742. 'name' => '_Token[unlocked]',
  1743. 'value' => 'address%7Cfirst_name',
  1744. 'autocomplete' => 'off',
  1745. 'data-foo' => 'bar',
  1746. ]],
  1747. ['input' => [
  1748. 'type' => 'hidden', 'name' => '_Token[debug]',
  1749. 'value' => $tokenDebug,
  1750. 'autocomplete' => 'off',
  1751. 'data-foo' => 'bar'
  1752. ]],
  1753. '/div'
  1754. ];
  1755. $this->assertHtml($expected, $result);
  1756. }
  1757. /**
  1758. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1759. *
  1760. * Debug is false, debugSecurity is true -> no debug
  1761. *
  1762. * @return void
  1763. */
  1764. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1765. {
  1766. $this->Form->request->params['_Token'] = [
  1767. 'unlockedFields' => ['first_name', 'address']
  1768. ];
  1769. $this->Form->create();
  1770. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1771. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1772. $this->Form->text('Addresses.title');
  1773. $this->Form->text('Addresses.first_name');
  1774. $this->Form->text('Addresses.last_name');
  1775. $this->Form->text('Addresses.address');
  1776. $this->Form->text('Addresses.city');
  1777. $this->Form->text('Addresses.phone');
  1778. $result = $this->Form->fields;
  1779. $expected = [
  1780. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1781. 'Addresses.city', 'Addresses.phone'
  1782. ];
  1783. $this->assertEquals($expected, $result);
  1784. Configure::write('debug', false);
  1785. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1786. $hash = 'cdc8fa2dd2aa2804c12cd17279c39747f1c57354%3AAddresses.id';
  1787. $expected = [
  1788. 'div' => ['style' => 'display:none;'],
  1789. ['input' => [
  1790. 'type' => 'hidden',
  1791. 'name' => '_Token[fields]',
  1792. 'value' => $hash,
  1793. 'autocomplete' => 'off',
  1794. 'data-foo' => 'bar',
  1795. ]],
  1796. ['input' => [
  1797. 'type' => 'hidden',
  1798. 'name' => '_Token[unlocked]',
  1799. 'value' => 'address%7Cfirst_name',
  1800. 'autocomplete' => 'off',
  1801. 'data-foo' => 'bar',
  1802. ]],
  1803. '/div'
  1804. ];
  1805. $this->assertHtml($expected, $result);
  1806. }
  1807. /**
  1808. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1809. *
  1810. * Test single record form with debugSecurity param.
  1811. *
  1812. * @return void
  1813. */
  1814. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1815. {
  1816. $this->Form->request->params['_Token'] = [
  1817. 'unlockedFields' => ['first_name', 'address']
  1818. ];
  1819. $this->Form->create();
  1820. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1821. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1822. $this->Form->text('Addresses.title');
  1823. $this->Form->text('Addresses.first_name');
  1824. $this->Form->text('Addresses.last_name');
  1825. $this->Form->text('Addresses.address');
  1826. $this->Form->text('Addresses.city');
  1827. $this->Form->text('Addresses.phone');
  1828. $result = $this->Form->fields;
  1829. $expected = [
  1830. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1831. 'Addresses.city', 'Addresses.phone'
  1832. ];
  1833. $this->assertEquals($expected, $result);
  1834. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1835. $hash = 'cdc8fa2dd2aa2804c12cd17279c39747f1c57354%3AAddresses.id';
  1836. $expected = [
  1837. 'div' => ['style' => 'display:none;'],
  1838. ['input' => [
  1839. 'type' => 'hidden',
  1840. 'name' => '_Token[fields]',
  1841. 'value' => $hash,
  1842. 'autocomplete' => 'off',
  1843. 'data-foo' => 'bar',
  1844. ]],
  1845. ['input' => [
  1846. 'type' => 'hidden',
  1847. 'name' => '_Token[unlocked]',
  1848. 'value' => 'address%7Cfirst_name',
  1849. 'autocomplete' => 'off',
  1850. 'data-foo' => 'bar',
  1851. ]],
  1852. '/div'
  1853. ];
  1854. $this->assertHtml($expected, $result);
  1855. }
  1856. /**
  1857. * testFormSecureWithCustomNameAttribute method
  1858. *
  1859. * Test securing inputs with custom name attributes.
  1860. *
  1861. * @return void
  1862. */
  1863. public function testFormSecureWithCustomNameAttribute()
  1864. {
  1865. $this->Form->request->params['_Token'] = 'testKey';
  1866. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1867. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1868. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1869. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1870. }
  1871. /**
  1872. * testFormSecuredControl method
  1873. *
  1874. * Test generation of entire secure form, assertions made on control() output.
  1875. *
  1876. * @return void
  1877. */
  1878. public function testFormSecuredControl()
  1879. {
  1880. $this->Form->request->params['_csrfToken'] = 'testKey';
  1881. $this->Form->request->params['_Token'] = 'stuff';
  1882. $this->article['schema'] = [
  1883. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1884. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1885. ];
  1886. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1887. $encoding = strtolower(Configure::read('App.encoding'));
  1888. $expected = [
  1889. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1890. 'div' => ['style' => 'display:none;'],
  1891. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1892. ['input' => [
  1893. 'type' => 'hidden',
  1894. 'name' => '_csrfToken',
  1895. 'value' => 'testKey',
  1896. 'autocomplete' => 'off'
  1897. ]],
  1898. '/div'
  1899. ];
  1900. $this->assertHtml($expected, $result);
  1901. $result = $this->Form->control('ratio');
  1902. $expected = [
  1903. 'div' => ['class'],
  1904. 'label' => ['for'],
  1905. 'Ratio',
  1906. '/label',
  1907. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1908. '/div',
  1909. ];
  1910. $this->assertHtml($expected, $result);
  1911. $result = $this->Form->control('population');
  1912. $expected = [
  1913. 'div' => ['class'],
  1914. 'label' => ['for'],
  1915. 'Population',
  1916. '/label',
  1917. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1918. '/div',
  1919. ];
  1920. $this->assertHtml($expected, $result);
  1921. $result = $this->Form->control('published', ['type' => 'text']);
  1922. $expected = [
  1923. 'div' => ['class' => 'input text'],
  1924. 'label' => ['for' => 'published'],
  1925. 'Published',
  1926. '/label',
  1927. ['input' => [
  1928. 'type' => 'text',
  1929. 'name' => 'published',
  1930. 'id' => 'published'
  1931. ]],
  1932. '/div'
  1933. ];
  1934. $this->assertHtml($expected, $result);
  1935. $result = $this->Form->control('other', ['type' => 'text']);
  1936. $expected = [
  1937. 'div' => ['class' => 'input text'],
  1938. 'label' => ['for' => 'other'],
  1939. 'Other',
  1940. '/label',
  1941. ['input' => [
  1942. 'type' => 'text',
  1943. 'name' => 'other',
  1944. 'id',
  1945. ]],
  1946. '/div'
  1947. ];
  1948. $this->assertHtml($expected, $result);
  1949. $result = $this->Form->hidden('stuff');
  1950. $expected = [
  1951. 'input' => [
  1952. 'type' => 'hidden',
  1953. 'name' => 'stuff'
  1954. ]
  1955. ];
  1956. $this->assertHtml($expected, $result);
  1957. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1958. $expected = ['input' => [
  1959. 'type' => 'hidden',
  1960. 'name' => 'hidden',
  1961. 'value' => '0'
  1962. ]];
  1963. $this->assertHtml($expected, $result);
  1964. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1965. $expected = [
  1966. 'div' => ['class' => 'input checkbox'],
  1967. ['input' => [
  1968. 'type' => 'hidden',
  1969. 'name' => 'something',
  1970. 'value' => '0'
  1971. ]],
  1972. 'label' => ['for' => 'something'],
  1973. ['input' => [
  1974. 'type' => 'checkbox',
  1975. 'name' => 'something',
  1976. 'value' => '1',
  1977. 'id' => 'something'
  1978. ]],
  1979. 'Something',
  1980. '/label',
  1981. '/div'
  1982. ];
  1983. $this->assertHtml($expected, $result);
  1984. $result = $this->Form->fields;
  1985. $expectedFields = [
  1986. 'ratio',
  1987. 'population',
  1988. 'published',
  1989. 'other',
  1990. 'stuff' => '',
  1991. 'hidden' => '0',
  1992. 'something'
  1993. ];
  1994. $this->assertEquals($expectedFields, $result);
  1995. $result = $this->Form->secure($this->Form->fields);
  1996. $tokenDebug = urlencode(json_encode([
  1997. '/articles/add',
  1998. $expectedFields,
  1999. []
  2000. ]));
  2001. $expected = [
  2002. 'div' => ['style' => 'display:none;'],
  2003. ['input' => [
  2004. 'type' => 'hidden',
  2005. 'name' => '_Token[fields]',
  2006. 'value',
  2007. 'autocomplete'
  2008. ]],
  2009. ['input' => [
  2010. 'type' => 'hidden',
  2011. 'name' => '_Token[unlocked]',
  2012. 'value' => '',
  2013. 'autocomplete' => 'off'
  2014. ]],
  2015. ['input' => [
  2016. 'type' => 'hidden', 'name' => '_Token[debug]',
  2017. 'value' => $tokenDebug,
  2018. 'autocomplete' => 'off'
  2019. ]],
  2020. '/div'
  2021. ];
  2022. $this->assertHtml($expected, $result);
  2023. }
  2024. /**
  2025. * testSecuredControlCustomName method
  2026. *
  2027. * Test secured inputs with custom names.
  2028. *
  2029. * @return void
  2030. */
  2031. public function testSecuredControlCustomName()
  2032. {
  2033. $this->Form->request->params['_Token'] = 'testKey';
  2034. $this->assertEquals([], $this->Form->fields);
  2035. $this->Form->text('text_input', [
  2036. 'name' => 'Option[General.default_role]',
  2037. ]);
  2038. $expected = ['Option.General.default_role'];
  2039. $this->assertEquals($expected, $this->Form->fields);
  2040. $this->Form->select('select_box', [1, 2], [
  2041. 'name' => 'Option[General.select_role]',
  2042. ]);
  2043. $expected[] = 'Option.General.select_role';
  2044. $this->assertEquals($expected, $this->Form->fields);
  2045. $this->Form->text('other.things[]');
  2046. $expected[] = 'other.things';
  2047. $this->assertEquals($expected, $this->Form->fields);
  2048. }
  2049. /**
  2050. * testSecuredControlDuplicate method
  2051. *
  2052. * Test that a hidden field followed by a visible field
  2053. * undoes the hidden field locking.
  2054. *
  2055. * @return void
  2056. */
  2057. public function testSecuredControlDuplicate()
  2058. {
  2059. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2060. $this->assertEquals([], $this->Form->fields);
  2061. $this->Form->control('text_val', [
  2062. 'type' => 'hidden',
  2063. 'value' => 'some text',
  2064. ]);
  2065. $expected = ['text_val' => 'some text'];
  2066. $this->assertEquals($expected, $this->Form->fields);
  2067. $this->Form->control('text_val', [
  2068. 'type' => 'text',
  2069. ]);
  2070. $expected = ['text_val'];
  2071. $this->assertEquals($expected, $this->Form->fields);
  2072. }
  2073. /**
  2074. * testFormSecuredFileControl method
  2075. *
  2076. * Tests that the correct keys are added to the field hash index.
  2077. *
  2078. * @return void
  2079. */
  2080. public function testFormSecuredFileControl()
  2081. {
  2082. $this->assertEquals([], $this->Form->fields);
  2083. $this->Form->file('Attachment.file');
  2084. $expected = [
  2085. 'Attachment.file.name', 'Attachment.file.type',
  2086. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2087. 'Attachment.file.size'
  2088. ];
  2089. $this->assertEquals($expected, $this->Form->fields);
  2090. }
  2091. /**
  2092. * testFormSecuredMultipleSelect method
  2093. *
  2094. * Test that multiple selects keys are added to field hash.
  2095. *
  2096. * @return void
  2097. */
  2098. public function testFormSecuredMultipleSelect()
  2099. {
  2100. $this->Form->request->params['_csrfToken'] = 'testKey';
  2101. $this->assertEquals([], $this->Form->fields);
  2102. $options = ['1' => 'one', '2' => 'two'];
  2103. $this->Form->select('Model.select', $options);
  2104. $expected = ['Model.select'];
  2105. $this->assertEquals($expected, $this->Form->fields);
  2106. $this->Form->fields = [];
  2107. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2108. $this->assertEquals($expected, $this->Form->fields);
  2109. }
  2110. /**
  2111. * testFormSecuredRadio method
  2112. *
  2113. * @return void
  2114. */
  2115. public function testFormSecuredRadio()
  2116. {
  2117. $this->Form->request->params['_Token'] = 'testKey';
  2118. $this->assertEquals([], $this->Form->fields);
  2119. $options = ['1' => 'option1', '2' => 'option2'];
  2120. $this->Form->radio('Test.test', $options);
  2121. $expected = ['Test.test'];
  2122. $this->assertEquals($expected, $this->Form->fields);
  2123. $this->Form->radio('Test.all', $options, [
  2124. 'disabled' => ['option1', 'option2']
  2125. ]);
  2126. $expected = ['Test.test', 'Test.all' => ''];
  2127. $this->assertEquals($expected, $this->Form->fields);
  2128. $this->Form->radio('Test.some', $options, [
  2129. 'disabled' => ['option1']
  2130. ]);
  2131. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2132. $this->assertEquals($expected, $this->Form->fields);
  2133. }
  2134. /**
  2135. * testFormSecuredAndDisabledNotAssoc method
  2136. *
  2137. * Test that when disabled is in a list based attribute array it works.
  2138. *
  2139. * @return void
  2140. */
  2141. public function testFormSecuredAndDisabledNotAssoc()
  2142. {
  2143. $this->Form->request->params['_Token'] = 'testKey';
  2144. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2145. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2146. $this->Form->text('Model.text', ['disabled']);
  2147. $this->Form->textarea('Model.textarea', ['disabled']);
  2148. $this->Form->password('Model.password', ['disabled']);
  2149. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2150. $expected = [
  2151. 'Model.radio' => ''
  2152. ];
  2153. $this->assertEquals($expected, $this->Form->fields);
  2154. }
  2155. /**
  2156. * testFormSecuredAndDisabled method
  2157. *
  2158. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2159. * hashing.
  2160. *
  2161. * @return void
  2162. */
  2163. public function testFormSecuredAndDisabled()
  2164. {
  2165. $this->Form->request->params['_Token'] = 'testKey';
  2166. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2167. $this->Form->text('Model.text', ['disabled' => true]);
  2168. $this->Form->password('Model.text', ['disabled' => true]);
  2169. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2170. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2171. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2172. $this->Form->year('Model.year', ['disabled' => true]);
  2173. $this->Form->month('Model.month', ['disabled' => true]);
  2174. $this->Form->day('Model.day', ['disabled' => true]);
  2175. $this->Form->hour('Model.hour', ['disabled' => true]);
  2176. $this->Form->minute('Model.minute', ['disabled' => true]);
  2177. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2178. $expected = [
  2179. 'Model.radio' => ''
  2180. ];
  2181. $this->assertEquals($expected, $this->Form->fields);
  2182. }
  2183. /**
  2184. * testDisableSecurityUsingForm method
  2185. *
  2186. * @return void
  2187. */
  2188. public function testDisableSecurityUsingForm()
  2189. {
  2190. $this->Form->request->params['_Token'] = [
  2191. 'disabledFields' => []
  2192. ];
  2193. $this->Form->create();
  2194. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2195. $this->Form->text('Addresses.title');
  2196. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2197. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2198. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2199. $result = $this->Form->fields;
  2200. $expected = [
  2201. 'Addresses.id' => '123456', 'Addresses.title',
  2202. ];
  2203. $this->assertEquals($expected, $result);
  2204. }
  2205. /**
  2206. * testUnlockFieldAddsToList method
  2207. *
  2208. * Test disableField.
  2209. *
  2210. * @return void
  2211. */
  2212. public function testUnlockFieldAddsToList()
  2213. {
  2214. $this->Form->request->params['_Token'] = [
  2215. 'unlockedFields' => []
  2216. ];
  2217. $this->Form->unlockField('Contact.name');
  2218. $this->Form->text('Contact.name');
  2219. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2220. $this->assertEquals([], $this->Form->fields);
  2221. }
  2222. /**
  2223. * testUnlockFieldRemovingFromFields method
  2224. *
  2225. * Test unlockField removing from fields array.
  2226. *
  2227. * @return void
  2228. */
  2229. public function testUnlockFieldRemovingFromFields()
  2230. {
  2231. $this->Form->request->params['_Token'] = [
  2232. 'unlockedFields' => []
  2233. ];
  2234. $this->Form->create($this->article);
  2235. $this->Form->hidden('Article.id', ['value' => 1]);
  2236. $this->Form->text('Article.title');
  2237. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2238. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  2239. $this->Form->unlockField('Article.title');
  2240. $this->Form->unlockField('Article.id');
  2241. $this->assertEquals([], $this->Form->fields);
  2242. }
  2243. /**
  2244. * testResetUnlockFields method
  2245. *
  2246. * Test reset unlockFields, when create new form.
  2247. *
  2248. * @return void
  2249. */
  2250. public function testResetUnlockFields()
  2251. {
  2252. $this->Form->request->params['_Token'] = [
  2253. 'key' => 'testKey',
  2254. 'unlockedFields' => []
  2255. ];
  2256. $this->Form->unlockField('Contact.id');
  2257. $this->Form->create('Contact');
  2258. $this->Form->hidden('Contact.id', ['value' => 1]);
  2259. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2260. $this->Form->end();
  2261. $this->Form->create('Contact');
  2262. $this->Form->hidden('Contact.id', ['value' => 1]);
  2263. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2264. }
  2265. /**
  2266. * testSecuredFormUrlIgnoresHost method
  2267. *
  2268. * Test that only the path + query elements of a form's URL show up in their hash.
  2269. *
  2270. * @return void
  2271. */
  2272. public function testSecuredFormUrlIgnoresHost()
  2273. {
  2274. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2275. $expected = '8312b8faa7e74c6f36e05c8d188eda58b39fab20%3A';
  2276. $this->Form->create($this->article, [
  2277. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2278. ]);
  2279. $result = $this->Form->secure();
  2280. $this->assertContains($expected, $result);
  2281. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2282. $result = $this->Form->secure();
  2283. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2284. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2285. $result = $this->Form->secure();
  2286. $this->assertContains($expected, $result, 'URL path + query should work.');
  2287. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2288. $result = $this->Form->secure();
  2289. $this->assertNotContains($expected, $result, 'URL is different');
  2290. }
  2291. /**
  2292. * testSecuredFormUrlHasHtmlAndIdentifier method
  2293. *
  2294. * Test that URL, HTML and identifier show up in their hashes.
  2295. *
  2296. * @return void
  2297. */
  2298. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2299. {
  2300. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2301. $expected = '93acdc2336947d62cf057a17275264c1fecc2443%3A';
  2302. $this->Form->create($this->article, [
  2303. 'url' => [
  2304. 'controller' => 'articles',
  2305. 'action' => 'view',
  2306. '?' => [
  2307. 'page' => 1,
  2308. 'limit' => 10,
  2309. 'html' => '<>"',
  2310. ],
  2311. '#' => 'result',
  2312. ],
  2313. ]);
  2314. $result = $this->Form->secure();
  2315. $this->assertContains($expected, $result);
  2316. $this->Form->create($this->article, [
  2317. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2318. ]);
  2319. $result = $this->Form->secure();
  2320. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2321. $this->Form->create($this->article, [
  2322. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2323. ]);
  2324. $result = $this->Form->secure();
  2325. $this->assertContains($expected, $result, 'URL path + query should work.');
  2326. }
  2327. /**
  2328. * testErrorMessageDisplay method
  2329. *
  2330. * Test error message display.
  2331. *
  2332. * @return void
  2333. */
  2334. public function testErrorMessageDisplay()
  2335. {
  2336. $this->article['errors'] = [
  2337. 'Article' => [
  2338. 'title' => 'error message',
  2339. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2340. ]
  2341. ];
  2342. $this->Form->create($this->article);
  2343. $result = $this->Form->control('Article.title');
  2344. $expected = [
  2345. 'div' => ['class' => 'input text error'],
  2346. 'label' => ['for' => 'article-title'],
  2347. 'Title',
  2348. '/label',
  2349. 'input' => [
  2350. 'type' => 'text', 'name' => 'Article[title]',
  2351. 'id' => 'article-title', 'class' => 'form-error'
  2352. ],
  2353. ['div' => ['class' => 'error-message']],
  2354. 'error message',
  2355. '/div',
  2356. '/div'
  2357. ];
  2358. $this->assertHtml($expected, $result);
  2359. $result = $this->Form->control('Article.title', [
  2360. 'templates' => [
  2361. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2362. ]
  2363. ]);
  2364. $expected = [
  2365. 'div' => ['class' => 'input text error'],
  2366. 'label' => ['for' => 'article-title'],
  2367. 'Title',
  2368. '/label',
  2369. 'input' => [
  2370. 'type' => 'text', 'name' => 'Article[title]',
  2371. 'id' => 'article-title', 'class' => 'form-error'
  2372. ],
  2373. '/div'
  2374. ];
  2375. $this->assertHtml($expected, $result);
  2376. $result = $this->Form->control('Article.content');
  2377. $expected = [
  2378. 'div' => ['class' => 'input text error'],
  2379. 'label' => ['for' => 'article-content'],
  2380. 'Content',
  2381. '/label',
  2382. 'input' => [
  2383. 'type' => 'text', 'name' => 'Article[content]',
  2384. 'id' => 'article-content', 'class' => 'form-error'
  2385. ],
  2386. ['div' => ['class' => 'error-message']],
  2387. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2388. '/div',
  2389. '/div'
  2390. ];
  2391. $this->assertHtml($expected, $result);
  2392. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2393. $expected = [
  2394. 'div' => ['class' => 'input text error'],
  2395. 'label' => ['for' => 'article-content'],
  2396. 'Content',
  2397. '/label',
  2398. 'input' => [
  2399. 'type' => 'text', 'name' => 'Article[content]',
  2400. 'id' => 'article-content', 'class' => 'form-error'
  2401. ],
  2402. ['div' => ['class' => 'error-message']],
  2403. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2404. '/div',
  2405. '/div'
  2406. ];
  2407. $this->assertHtml($expected, $result);
  2408. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2409. $expected = [
  2410. 'div' => ['class' => 'input text error'],
  2411. 'label' => ['for' => 'article-content'],
  2412. 'Content',
  2413. '/label',
  2414. 'input' => [
  2415. 'type' => 'text', 'name' => 'Article[content]',
  2416. 'id' => 'article-content', 'class' => 'form-error'
  2417. ],
  2418. ['div' => ['class' => 'error-message']],
  2419. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2420. '/div',
  2421. '/div'
  2422. ];
  2423. $this->assertHtml($expected, $result);
  2424. }
  2425. /**
  2426. * testEmptyErrorValidation method
  2427. *
  2428. * Test validation errors, when validation message is an empty string.
  2429. *
  2430. * @return void
  2431. */
  2432. public function testEmptyErrorValidation()
  2433. {
  2434. $this->article['errors'] = [
  2435. 'Article' => ['title' => '']
  2436. ];
  2437. $this->Form->create($this->article);
  2438. $result = $this->Form->control('Article.title');
  2439. $expected = [
  2440. 'div' => ['class' => 'input text error'],
  2441. 'label' => ['for' => 'article-title'],
  2442. 'Title',
  2443. '/label',
  2444. 'input' => [
  2445. 'type' => 'text', 'name' => 'Article[title]',
  2446. 'id' => 'article-title', 'class' => 'form-error'
  2447. ],
  2448. ['div' => ['class' => 'error-message']],
  2449. [],
  2450. '/div',
  2451. '/div'
  2452. ];
  2453. $this->assertHtml($expected, $result);
  2454. }
  2455. /**
  2456. * testEmptyControlErrorValidation method
  2457. *
  2458. * Test validation errors, when calling control() overriding validation message by an empty string.
  2459. *
  2460. * @return void
  2461. */
  2462. public function testEmptyControlErrorValidation()
  2463. {
  2464. $this->article['errors'] = [
  2465. 'Article' => ['title' => 'error message']
  2466. ];
  2467. $this->Form->create($this->article);
  2468. $result = $this->Form->control('Article.title', ['error' => '']);
  2469. $expected = [
  2470. 'div' => ['class' => 'input text error'],
  2471. 'label' => ['for' => 'article-title'],
  2472. 'Title',
  2473. '/label',
  2474. 'input' => [
  2475. 'type' => 'text', 'name' => 'Article[title]',
  2476. 'id' => 'article-title', 'class' => 'form-error'
  2477. ],
  2478. ['div' => ['class' => 'error-message']],
  2479. [],
  2480. '/div',
  2481. '/div'
  2482. ];
  2483. $this->assertHtml($expected, $result);
  2484. }
  2485. /**
  2486. * testControlErrorMessage method
  2487. *
  2488. * Test validation errors, when calling control() overriding validation messages.
  2489. *
  2490. * @return void
  2491. */
  2492. public function testControlErrorMessage()
  2493. {
  2494. $this->article['errors'] = [
  2495. 'title' => ['error message']
  2496. ];
  2497. $this->Form->create($this->article);
  2498. $result = $this->Form->control('title', [
  2499. 'error' => 'Custom error!'
  2500. ]);
  2501. $expected = [
  2502. 'div' => ['class' => 'input text required error'],
  2503. 'label' => ['for' => 'title'],
  2504. 'Title',
  2505. '/label',
  2506. 'input' => [
  2507. 'type' => 'text', 'name' => 'title',
  2508. 'id' => 'title', 'class' => 'form-error',
  2509. 'required' => 'required',
  2510. ],
  2511. ['div' => ['class' => 'error-message']],
  2512. 'Custom error!',
  2513. '/div',
  2514. '/div'
  2515. ];
  2516. $this->assertHtml($expected, $result);
  2517. $result = $this->Form->control('title', [
  2518. 'error' => ['error message' => 'Custom error!']
  2519. ]);
  2520. $expected = [
  2521. 'div' => ['class' => 'input text required error'],
  2522. 'label' => ['for' => 'title'],
  2523. 'Title',
  2524. '/label',
  2525. 'input' => [
  2526. 'type' => 'text',
  2527. 'name' => 'title',
  2528. 'id' => 'title',
  2529. 'class' => 'form-error',
  2530. 'required' => 'required'
  2531. ],
  2532. ['div' => ['class' => 'error-message']],
  2533. 'Custom error!',
  2534. '/div',
  2535. '/div'
  2536. ];
  2537. $this->assertHtml($expected, $result);
  2538. }
  2539. /**
  2540. * testFormValidationAssociated method
  2541. *
  2542. * Tests displaying errors for nested entities.
  2543. *
  2544. * @return void
  2545. */
  2546. public function testFormValidationAssociated()
  2547. {
  2548. $nested = new Entity(['foo' => 'bar']);
  2549. $nested->errors('foo', ['not a valid bar']);
  2550. $entity = new Entity(['nested' => $nested]);
  2551. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2552. $result = $this->Form->error('nested.foo');
  2553. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2554. }
  2555. /**
  2556. * testFormValidationAssociatedSecondLevel method
  2557. *
  2558. * Test form error display with associated model.
  2559. *
  2560. * @return void
  2561. */
  2562. public function testFormValidationAssociatedSecondLevel()
  2563. {
  2564. $inner = new Entity(['bar' => 'baz']);
  2565. $nested = new Entity(['foo' => $inner]);
  2566. $entity = new Entity(['nested' => $nested]);
  2567. $inner->errors('bar', ['not a valid one']);
  2568. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2569. $result = $this->Form->error('nested.foo.bar');
  2570. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2571. }
  2572. /**
  2573. * testFormValidationMultiRecord method
  2574. *
  2575. * Test form error display with multiple records.
  2576. *
  2577. * @return void
  2578. */
  2579. public function testFormValidationMultiRecord()
  2580. {
  2581. $one = new Entity();
  2582. $two = new Entity();
  2583. TableRegistry::get('Contacts', [
  2584. 'className' => __NAMESPACE__ . '\ContactsTable'
  2585. ]);
  2586. $one->set('email', '');
  2587. $one->errors('email', ['invalid email']);
  2588. $two->set('name', '');
  2589. $two->errors('name', ['This is wrong']);
  2590. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2591. $result = $this->Form->control('0.email');
  2592. $expected = [
  2593. 'div' => ['class' => 'input email error'],
  2594. 'label' => ['for' => '0-email'],
  2595. 'Email',
  2596. '/label',
  2597. 'input' => [
  2598. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2599. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2600. ],
  2601. ['div' => ['class' => 'error-message']],
  2602. 'invalid email',
  2603. '/div',
  2604. '/div'
  2605. ];
  2606. $this->assertHtml($expected, $result);
  2607. $result = $this->Form->control('1.name');
  2608. $expected = [
  2609. 'div' => ['class' => 'input text error'],
  2610. 'label' => ['for' => '1-name'],
  2611. 'Name',
  2612. '/label',
  2613. 'input' => [
  2614. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2615. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2616. ],
  2617. ['div' => ['class' => 'error-message']],
  2618. 'This is wrong',
  2619. '/div',
  2620. '/div'
  2621. ];
  2622. $this->assertHtml($expected, $result);
  2623. }
  2624. /**
  2625. * testControl method
  2626. *
  2627. * Test various incarnations of control().
  2628. *
  2629. * @return void
  2630. */
  2631. public function testControl()
  2632. {
  2633. TableRegistry::get('ValidateUsers', [
  2634. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2635. ]);
  2636. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2637. $result = $this->Form->control('ValidateUsers.balance');
  2638. $expected = [
  2639. 'div' => ['class'],
  2640. 'label' => ['for'],
  2641. 'Balance',
  2642. '/label',
  2643. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2644. '/div',
  2645. ];
  2646. $this->assertHtml($expected, $result);
  2647. $result = $this->Form->control('ValidateUser.cost_decimal');
  2648. $expected = [
  2649. 'div' => ['class'],
  2650. 'label' => ['for'],
  2651. 'Cost Decimal',
  2652. '/label',
  2653. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2654. '/div',
  2655. ];
  2656. $this->assertHtml($expected, $result);
  2657. $result = $this->Form->control('ValidateUser.null_decimal');
  2658. $expected = [
  2659. 'div' => ['class'],
  2660. 'label' => ['for'],
  2661. 'Null Decimal',
  2662. '/label',
  2663. 'input' => ['name', 'type' => 'number', 'id'],
  2664. '/div',
  2665. ];
  2666. $this->assertHtml($expected, $result);
  2667. }
  2668. /**
  2669. * testControlCustomization method
  2670. *
  2671. * Tests the input method and passing custom options.
  2672. *
  2673. * @return void
  2674. */
  2675. public function testControlCustomization()
  2676. {
  2677. TableRegistry::get('Contacts', [
  2678. 'className' => __NAMESPACE__ . '\ContactsTable'
  2679. ]);
  2680. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2681. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2682. $expected = [
  2683. 'div' => ['class' => 'input email'],
  2684. 'label' => ['for' => 'custom'],
  2685. 'Email',
  2686. '/label',
  2687. ['input' => [
  2688. 'type' => 'email', 'name' => 'Contact[email]',
  2689. 'id' => 'custom', 'maxlength' => 255
  2690. ]],
  2691. '/div'
  2692. ];
  2693. $this->assertHtml($expected, $result);
  2694. $result = $this->Form->control('Contact.email', [
  2695. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2696. ]);
  2697. $expected = [
  2698. '<div',
  2699. 'label' => ['for' => 'contact-email'],
  2700. 'Email',
  2701. '/label',
  2702. ['input' => [
  2703. 'type' => 'email', 'name' => 'Contact[email]',
  2704. 'id' => 'contact-email', 'maxlength' => 255
  2705. ]],
  2706. '/div'
  2707. ];
  2708. $this->assertHtml($expected, $result);
  2709. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2710. $expected = [
  2711. 'div' => ['class' => 'input text'],
  2712. 'label' => ['for' => 'contact-email'],
  2713. 'Email',
  2714. '/label',
  2715. ['input' => [
  2716. 'type' => 'text', 'name' => 'Contact[email]',
  2717. 'id' => 'contact-email', 'maxlength' => '255'
  2718. ]],
  2719. '/div'
  2720. ];
  2721. $this->assertHtml($expected, $result);
  2722. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2723. $expected = [
  2724. 'div' => ['class' => 'input text'],
  2725. 'label' => ['for' => 'contact-5-email'],
  2726. 'Email',
  2727. '/label',
  2728. ['input' => [
  2729. 'type' => 'text', 'name' => 'Contact[5][email]',
  2730. 'id' => 'contact-5-email', 'maxlength' => '255'
  2731. ]],
  2732. '/div'
  2733. ];
  2734. $this->assertHtml($expected, $result);
  2735. $result = $this->Form->control('Contact.password');
  2736. $expected = [
  2737. 'div' => ['class' => 'input password'],
  2738. 'label' => ['for' => 'contact-password'],
  2739. 'Password',
  2740. '/label',
  2741. ['input' => [
  2742. 'type' => 'password', 'name' => 'Contact[password]',
  2743. 'id' => 'contact-password'
  2744. ]],
  2745. '/div'
  2746. ];
  2747. $this->assertHtml($expected, $result);
  2748. $result = $this->Form->control('Contact.email', [
  2749. 'type' => 'file', 'class' => 'textbox'
  2750. ]);
  2751. $expected = [
  2752. 'div' => ['class' => 'input file'],
  2753. 'label' => ['for' => 'contact-email'],
  2754. 'Email',
  2755. '/label',
  2756. ['input' => [
  2757. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2758. 'id' => 'contact-email'
  2759. ]],
  2760. '/div'
  2761. ];
  2762. $this->assertHtml($expected, $result);
  2763. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2764. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2765. $result = $this->Form->control('phone');
  2766. $expected = [
  2767. 'div' => ['class' => 'input tel'],
  2768. 'label' => ['for' => 'phone'],
  2769. 'Phone',
  2770. '/label',
  2771. ['input' => [
  2772. 'type' => 'tel', 'name' => 'phone',
  2773. 'value' => 'Hello &amp; World &gt; weird chars',
  2774. 'id' => 'phone', 'maxlength' => 255
  2775. ]],
  2776. '/div'
  2777. ];
  2778. $this->assertHtml($expected, $result);
  2779. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2780. $this->Form->create();
  2781. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2782. $expected = [
  2783. 'div' => ['class' => 'input text'],
  2784. 'label' => ['for' => 'myId'],
  2785. 'Field',
  2786. '/label',
  2787. 'input' => [
  2788. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2789. 'value' => 'My value', 'id' => 'myId'
  2790. ],
  2791. '/div'
  2792. ];
  2793. $this->assertHtml($expected, $result);
  2794. $this->Form->request->data = [];
  2795. $entity->errors('field', 'Badness!');
  2796. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2797. $result = $this->Form->control('field');
  2798. $expected = [
  2799. 'div' => ['class' => 'input text error'],
  2800. 'label' => ['for' => 'field'],
  2801. 'Field',
  2802. '/label',
  2803. 'input' => [
  2804. 'type' => 'text', 'name' => 'field',
  2805. 'id' => 'field', 'class' => 'form-error'
  2806. ],
  2807. ['div' => ['class' => 'error-message']],
  2808. 'Badness!',
  2809. '/div',
  2810. '/div'
  2811. ];
  2812. $this->assertHtml($expected, $result);
  2813. $result = $this->Form->control('field', [
  2814. 'templates' => [
  2815. 'inputContainerError' => '{{content}}{{error}}',
  2816. 'error' => '<span class="error-message">{{content}}</span>'
  2817. ]
  2818. ]);
  2819. $expected = [
  2820. 'label' => ['for' => 'field'],
  2821. 'Field',
  2822. '/label',
  2823. 'input' => [
  2824. 'type' => 'text', 'name' => 'field',
  2825. 'id' => 'field', 'class' => 'form-error'
  2826. ],
  2827. ['span' => ['class' => 'error-message']],
  2828. 'Badness!',
  2829. '/span'
  2830. ];
  2831. $this->assertHtml($expected, $result);
  2832. $entity->errors('field', ['minLength'], true);
  2833. $result = $this->Form->control('field', [
  2834. 'error' => [
  2835. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2836. 'maxLength' => 'login too large'
  2837. ]
  2838. ]);
  2839. $expected = [
  2840. 'div' => ['class' => 'input text error'],
  2841. 'label' => ['for' => 'field'],
  2842. 'Field',
  2843. '/label',
  2844. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2845. ['div' => ['class' => 'error-message']],
  2846. 'Le login doit contenir au moins 2 caractères',
  2847. '/div',
  2848. '/div'
  2849. ];
  2850. $this->assertHtml($expected, $result);
  2851. $entity->errors('field', ['maxLength'], true);
  2852. $result = $this->Form->control('field', [
  2853. 'error' => [
  2854. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2855. 'maxLength' => 'login too large',
  2856. ]
  2857. ]);
  2858. $expected = [
  2859. 'div' => ['class' => 'input text error'],
  2860. 'label' => ['for' => 'field'],
  2861. 'Field',
  2862. '/label',
  2863. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2864. ['div' => ['class' => 'error-message']],
  2865. 'login too large',
  2866. '/div',
  2867. '/div'
  2868. ];
  2869. $this->assertHtml($expected, $result);
  2870. }
  2871. /**
  2872. * testControlWithTemplateFile method
  2873. *
  2874. * Test that control() accepts a template file.
  2875. *
  2876. * @return void
  2877. */
  2878. public function testControlWithTemplateFile()
  2879. {
  2880. $result = $this->Form->control('field', [
  2881. 'templates' => 'htmlhelper_tags'
  2882. ]);
  2883. $expected = [
  2884. 'label' => ['for' => 'field'],
  2885. 'Field',
  2886. '/label',
  2887. 'input' => [
  2888. 'type' => 'text', 'name' => 'field',
  2889. 'id' => 'field'
  2890. ],
  2891. ];
  2892. $this->assertHtml($expected, $result);
  2893. }
  2894. /**
  2895. * testNestedControlsEndWithBrackets method
  2896. *
  2897. * Test that nested inputs end with brackets.
  2898. *
  2899. * @return void
  2900. */
  2901. public function testNestedControlsEndWithBrackets()
  2902. {
  2903. $result = $this->Form->text('nested.text[]');
  2904. $expected = [
  2905. 'input' => [
  2906. 'type' => 'text', 'name' => 'nested[text][]'
  2907. ],
  2908. ];
  2909. $this->assertHtml($expected, $result);
  2910. $result = $this->Form->file('nested.file[]');
  2911. $expected = [
  2912. 'input' => [
  2913. 'type' => 'file', 'name' => 'nested[file][]'
  2914. ],
  2915. ];
  2916. $this->assertHtml($expected, $result);
  2917. }
  2918. /**
  2919. * testCreateIdPrefix method
  2920. *
  2921. * Test id prefix.
  2922. *
  2923. * @return void
  2924. */
  2925. public function testCreateIdPrefix()
  2926. {
  2927. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2928. $result = $this->Form->control('field');
  2929. $expected = [
  2930. 'div' => ['class' => 'input text'],
  2931. 'label' => ['for' => 'prefix-field'],
  2932. 'Field',
  2933. '/label',
  2934. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2935. '/div'
  2936. ];
  2937. $this->assertHtml($expected, $result);
  2938. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2939. $expected = [
  2940. 'div' => ['class' => 'input text'],
  2941. 'label' => ['for' => 'custom-id'],
  2942. 'Field',
  2943. '/label',
  2944. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2945. '/div'
  2946. ];
  2947. $this->assertHtml($expected, $result);
  2948. $result = $this->Form->radio('Model.field', ['option A']);
  2949. $expected = [
  2950. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2951. 'label' => ['for' => 'prefix-model-field-0'],
  2952. ['input' => [
  2953. 'type' => 'radio',
  2954. 'name' => 'Model[field]',
  2955. 'value' => '0',
  2956. 'id' => 'prefix-model-field-0'
  2957. ]],
  2958. 'option A',
  2959. '/label'
  2960. ];
  2961. $this->assertHtml($expected, $result);
  2962. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2963. $expected = [
  2964. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2965. 'label' => ['for' => 'prefix-model-field-0'],
  2966. ['input' => [
  2967. 'type' => 'radio',
  2968. 'name' => 'Model[field]',
  2969. 'value' => '0',
  2970. 'id' => 'prefix-model-field-0'
  2971. ]],
  2972. 'option A',
  2973. '/label'
  2974. ];
  2975. $this->assertHtml($expected, $result);
  2976. $result = $this->Form->select(
  2977. 'Model.multi_field',
  2978. ['first'],
  2979. ['multiple' => 'checkbox']
  2980. );
  2981. $expected = [
  2982. 'input' => [
  2983. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2984. ],
  2985. ['div' => ['class' => 'checkbox']],
  2986. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2987. ['input' => [
  2988. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2989. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2990. ]],
  2991. 'first',
  2992. '/label',
  2993. '/div',
  2994. ];
  2995. $this->assertHtml($expected, $result);
  2996. $this->Form->end();
  2997. $result = $this->Form->control('field');
  2998. $expected = [
  2999. 'div' => ['class' => 'input text'],
  3000. 'label' => ['for' => 'field'],
  3001. 'Field',
  3002. '/label',
  3003. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  3004. '/div'
  3005. ];
  3006. $this->assertHtml($expected, $result);
  3007. }
  3008. /**
  3009. * testControlZero method
  3010. *
  3011. * Test that inputs with 0 can be created.
  3012. *
  3013. * @return void
  3014. */
  3015. public function testControlZero()
  3016. {
  3017. TableRegistry::get('Contacts', [
  3018. 'className' => __NAMESPACE__ . '\ContactsTable'
  3019. ]);
  3020. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  3021. $result = $this->Form->control('0');
  3022. $expected = [
  3023. 'div' => ['class' => 'input text'],
  3024. 'label' => ['for' => '0'], '/label',
  3025. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  3026. '/div'
  3027. ];
  3028. $this->assertHtml($expected, $result);
  3029. }
  3030. /**
  3031. * testControlCheckbox method
  3032. *
  3033. * Test control() with checkbox creation.
  3034. *
  3035. * @return void
  3036. */
  3037. public function testControlCheckbox()
  3038. {
  3039. $articles = TableRegistry::get('Articles');
  3040. $articles->schema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3041. $article = $articles->newEntity();
  3042. $this->Form->create($article);
  3043. $result = $this->Form->control('Articles.active');
  3044. $expected = [
  3045. 'div' => ['class' => 'input checkbox'],
  3046. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3047. 'label' => ['for' => 'articles-active'],
  3048. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3049. 'Active',
  3050. '/label',
  3051. '/div'
  3052. ];
  3053. $this->assertHtml($expected, $result);
  3054. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3055. $expected = [
  3056. 'div' => ['class' => 'input checkbox'],
  3057. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3058. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3059. '/div'
  3060. ];
  3061. $this->assertHtml($expected, $result);
  3062. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3063. $expected = [
  3064. 'div' => ['class' => 'input checkbox'],
  3065. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3066. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3067. '/div'
  3068. ];
  3069. $this->assertHtml($expected, $result);
  3070. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3071. $expected = [
  3072. 'div' => ['class' => 'input checkbox'],
  3073. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3074. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3075. '/div'
  3076. ];
  3077. $this->assertHtml($expected, $result);
  3078. $result = $this->Form->control('Articles.disabled', [
  3079. 'label' => 'Disabled',
  3080. 'type' => 'checkbox',
  3081. 'data-foo' => 'disabled'
  3082. ]);
  3083. $expected = [
  3084. 'div' => ['class' => 'input checkbox'],
  3085. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3086. 'label' => ['for' => 'articles-disabled'],
  3087. ['input' => [
  3088. 'type' => 'checkbox',
  3089. 'name' => 'Articles[disabled]',
  3090. 'value' => '1',
  3091. 'id' => 'articles-disabled',
  3092. 'data-foo' => 'disabled'
  3093. ]],
  3094. 'Disabled',
  3095. '/label',
  3096. '/div'
  3097. ];
  3098. $this->assertHtml($expected, $result);
  3099. $result = $this->Form->control('Articles.confirm', [
  3100. 'label' => 'Confirm <b>me</b>!',
  3101. 'type' => 'checkbox',
  3102. 'escape' => false
  3103. ]);
  3104. $expected = [
  3105. 'div' => ['class' => 'input checkbox'],
  3106. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3107. 'label' => ['for' => 'articles-confirm'],
  3108. ['input' => [
  3109. 'type' => 'checkbox',
  3110. 'name' => 'Articles[confirm]',
  3111. 'value' => '1',
  3112. 'id' => 'articles-confirm',
  3113. ]],
  3114. 'Confirm <b>me</b>!',
  3115. '/label',
  3116. '/div'
  3117. ];
  3118. $this->assertHtml($expected, $result);
  3119. }
  3120. /**
  3121. * testControlHidden method
  3122. *
  3123. * Test that control() does not create wrapping div and label tag for hidden fields.
  3124. *
  3125. * @return void
  3126. */
  3127. public function testControlHidden()
  3128. {
  3129. TableRegistry::get('ValidateUsers', [
  3130. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3131. ]);
  3132. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3133. $result = $this->Form->control('ValidateUser.id');
  3134. $expected = [
  3135. 'input' => ['name', 'type' => 'hidden', 'id']
  3136. ];
  3137. $this->assertHtml($expected, $result);
  3138. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3139. $expected = [
  3140. 'input' => ['name', 'type' => 'hidden', 'id']
  3141. ];
  3142. $this->assertHtml($expected, $result);
  3143. }
  3144. /**
  3145. * testControlDatetime method
  3146. *
  3147. * Test form->control() with datetime.
  3148. *
  3149. * @return void
  3150. */
  3151. public function testControlDatetime()
  3152. {
  3153. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3154. ->setMethods(['datetime'])
  3155. ->setConstructorArgs([new View()])
  3156. ->getMock();
  3157. $this->Form->expects($this->once())->method('datetime')
  3158. ->with('prueba', [
  3159. 'type' => 'datetime',
  3160. 'timeFormat' => 24,
  3161. 'minYear' => 2008,
  3162. 'maxYear' => 2011,
  3163. 'interval' => 15,
  3164. 'options' => null,
  3165. 'empty' => false,
  3166. 'id' => 'prueba',
  3167. 'required' => false,
  3168. 'templateVars' => []
  3169. ])
  3170. ->will($this->returnValue('This is it!'));
  3171. $result = $this->Form->control('prueba', [
  3172. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3173. 'maxYear' => 2011, 'interval' => 15
  3174. ]);
  3175. $expected = [
  3176. 'div' => ['class' => 'input datetime'],
  3177. '<label',
  3178. 'Prueba',
  3179. '/label',
  3180. 'This is it!',
  3181. '/div'
  3182. ];
  3183. $this->assertHtml($expected, $result);
  3184. }
  3185. /**
  3186. * testControlDatetimeIdPrefix method
  3187. *
  3188. * Test form->control() with datetime with id prefix.
  3189. *
  3190. * @return void
  3191. */
  3192. public function testControlDatetimeIdPrefix()
  3193. {
  3194. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3195. ->setMethods(['datetime'])
  3196. ->setConstructorArgs([new View()])
  3197. ->getMock();
  3198. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3199. $this->Form->expects($this->once())->method('datetime')
  3200. ->with('prueba', [
  3201. 'type' => 'datetime',
  3202. 'timeFormat' => 24,
  3203. 'minYear' => 2008,
  3204. 'maxYear' => 2011,
  3205. 'interval' => 15,
  3206. 'options' => null,
  3207. 'empty' => false,
  3208. 'id' => 'prefix-prueba',
  3209. 'required' => false,
  3210. 'templateVars' => []
  3211. ])
  3212. ->will($this->returnValue('This is it!'));
  3213. $result = $this->Form->control('prueba', [
  3214. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3215. 'maxYear' => 2011, 'interval' => 15
  3216. ]);
  3217. $expected = [
  3218. 'div' => ['class' => 'input datetime'],
  3219. '<label',
  3220. 'Prueba',
  3221. '/label',
  3222. 'This is it!',
  3223. '/div'
  3224. ];
  3225. $this->assertHtml($expected, $result);
  3226. }
  3227. /**
  3228. * testControlCheckboxWithDisabledElements method
  3229. *
  3230. * Test generating checkboxes with disabled elements.
  3231. *
  3232. * @return void
  3233. */
  3234. public function testControlCheckboxWithDisabledElements()
  3235. {
  3236. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3237. $result = $this->Form->control('Contact.multiple', [
  3238. 'multiple' => 'checkbox',
  3239. 'disabled' => 'disabled',
  3240. 'options' => $options
  3241. ]);
  3242. $expected = [
  3243. ['div' => ['class' => 'input select']],
  3244. ['label' => ['for' => 'contact-multiple']],
  3245. 'Multiple',
  3246. '/label',
  3247. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3248. ['div' => ['class' => 'checkbox']],
  3249. ['label' => ['for' => 'contact-multiple-1']],
  3250. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3251. 'One',
  3252. '/label',
  3253. '/div',
  3254. ['div' => ['class' => 'checkbox']],
  3255. ['label' => ['for' => 'contact-multiple-2']],
  3256. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3257. 'Two',
  3258. '/label',
  3259. '/div',
  3260. ['div' => ['class' => 'checkbox']],
  3261. ['label' => ['for' => 'contact-multiple-3']],
  3262. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3263. 'Three',
  3264. '/label',
  3265. '/div',
  3266. '/div'
  3267. ];
  3268. $this->assertHtml($expected, $result);
  3269. // make sure 50 does only disable 50, and not 50f5c0cf
  3270. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3271. $disabled = [50];
  3272. $expected = [
  3273. ['div' => ['class' => 'input select']],
  3274. ['label' => ['for' => 'contact-multiple']],
  3275. 'Multiple',
  3276. '/label',
  3277. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3278. ['div' => ['class' => 'checkbox']],
  3279. ['label' => ['for' => 'contact-multiple-50']],
  3280. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3281. 'Fifty',
  3282. '/label',
  3283. '/div',
  3284. ['div' => ['class' => 'checkbox']],
  3285. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3286. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3287. 'Stringy',
  3288. '/label',
  3289. '/div',
  3290. '/div'
  3291. ];
  3292. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3293. $this->assertHtml($expected, $result);
  3294. }
  3295. /**
  3296. * testControlWithLeadingInteger method
  3297. *
  3298. * Test input name with leading integer, ensure attributes are generated correctly.
  3299. *
  3300. * @return void
  3301. */
  3302. public function testControlWithLeadingInteger()
  3303. {
  3304. $result = $this->Form->text('0.Node.title');
  3305. $expected = [
  3306. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3307. ];
  3308. $this->assertHtml($expected, $result);
  3309. }
  3310. /**
  3311. * testControlSelectType method
  3312. *
  3313. * Test form->control() with select type inputs.
  3314. *
  3315. * @return void
  3316. */
  3317. public function testControlSelectType()
  3318. {
  3319. $result = $this->Form->control(
  3320. 'email',
  3321. [
  3322. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3323. );
  3324. $expected = [
  3325. 'div' => ['class' => 'input select'],
  3326. 'label' => ['for' => 'email'],
  3327. 'Email',
  3328. '/label',
  3329. ['select' => ['name' => 'email', 'id' => 'email']],
  3330. ['option' => ['value' => '']],
  3331. '/option',
  3332. ['option' => ['value' => 'è']],
  3333. 'Firést',
  3334. '/option',
  3335. ['option' => ['value' => 'é']],
  3336. 'Secoènd',
  3337. '/option',
  3338. '/select',
  3339. '/div'
  3340. ];
  3341. $this->assertHtml($expected, $result);
  3342. $result = $this->Form->control(
  3343. 'email',
  3344. [
  3345. 'options' => ['First', 'Second'], 'empty' => true]
  3346. );
  3347. $expected = [
  3348. 'div' => ['class' => 'input select'],
  3349. 'label' => ['for' => 'email'],
  3350. 'Email',
  3351. '/label',
  3352. ['select' => ['name' => 'email', 'id' => 'email']],
  3353. ['option' => ['value' => '']],
  3354. '/option',
  3355. ['option' => ['value' => '0']],
  3356. 'First',
  3357. '/option',
  3358. ['option' => ['value' => '1']],
  3359. 'Second',
  3360. '/option',
  3361. '/select',
  3362. '/div'
  3363. ];
  3364. $this->assertHtml($expected, $result);
  3365. $result = $this->Form->control('email', [
  3366. 'type' => 'select',
  3367. 'options' => new \ArrayObject(['First', 'Second']),
  3368. 'empty' => true
  3369. ]);
  3370. $this->assertHtml($expected, $result);
  3371. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3372. $this->Form->request->data = ['Model' => ['user_id' => 'value']];
  3373. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3374. $expected = [
  3375. 'div' => ['class' => 'input select'],
  3376. 'label' => ['for' => 'model-user-id'],
  3377. 'User',
  3378. '/label',
  3379. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3380. ['option' => ['value' => '']],
  3381. '/option',
  3382. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3383. 'good',
  3384. '/option',
  3385. ['option' => ['value' => 'other']],
  3386. 'bad',
  3387. '/option',
  3388. '/select',
  3389. '/div'
  3390. ];
  3391. $this->assertHtml($expected, $result);
  3392. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3393. $this->Form->request->data = ['Thing' => ['user_id' => null]];
  3394. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3395. $expected = [
  3396. 'div' => ['class' => 'input select'],
  3397. 'label' => ['for' => 'thing-user-id'],
  3398. 'User',
  3399. '/label',
  3400. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3401. ['option' => ['value' => '']],
  3402. 'Some Empty',
  3403. '/option',
  3404. ['option' => ['value' => 'value']],
  3405. 'good',
  3406. '/option',
  3407. ['option' => ['value' => 'other']],
  3408. 'bad',
  3409. '/option',
  3410. '/select',
  3411. '/div'
  3412. ];
  3413. $this->assertHtml($expected, $result);
  3414. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3415. $this->Form->request->data = ['Thing' => ['user_id' => 'value']];
  3416. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3417. $expected = [
  3418. 'div' => ['class' => 'input select'],
  3419. 'label' => ['for' => 'thing-user-id'],
  3420. 'User',
  3421. '/label',
  3422. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3423. ['option' => ['value' => '']],
  3424. 'Some Empty',
  3425. '/option',
  3426. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3427. 'good',
  3428. '/option',
  3429. ['option' => ['value' => 'other']],
  3430. 'bad',
  3431. '/option',
  3432. '/select',
  3433. '/div'
  3434. ];
  3435. $this->assertHtml($expected, $result);
  3436. $this->Form->data = [];
  3437. $result = $this->Form->control('Publisher.id', [
  3438. 'label' => 'Publisher',
  3439. 'type' => 'select',
  3440. 'multiple' => 'checkbox',
  3441. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3442. ]);
  3443. $expected = [
  3444. ['div' => ['class' => 'input select']],
  3445. ['label' => ['for' => 'publisher-id']],
  3446. 'Publisher',
  3447. '/label',
  3448. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3449. ['div' => ['class' => 'checkbox']],
  3450. ['label' => ['for' => 'publisher-id-value-1']],
  3451. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3452. 'Label 1',
  3453. '/label',
  3454. '/div',
  3455. ['div' => ['class' => 'checkbox']],
  3456. ['label' => ['for' => 'publisher-id-value-2']],
  3457. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3458. 'Label 2',
  3459. '/label',
  3460. '/div',
  3461. '/div'
  3462. ];
  3463. $this->assertHtml($expected, $result);
  3464. }
  3465. /**
  3466. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3467. *
  3468. * Test that control() and a non standard primary key makes a hidden input by default.
  3469. *
  3470. * @return void
  3471. */
  3472. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3473. {
  3474. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3475. $this->Form->create($this->article);
  3476. $result = $this->Form->control('title');
  3477. $expected = [
  3478. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3479. ];
  3480. $this->assertHtml($expected, $result);
  3481. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3482. $this->Form->create($this->article);
  3483. $result = $this->Form->control('title');
  3484. $expected = [
  3485. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3486. ];
  3487. $this->assertHtml($expected, $result);
  3488. $result = $this->Form->control('body');
  3489. $expected = [
  3490. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3491. ];
  3492. $this->assertHtml($expected, $result);
  3493. }
  3494. /**
  3495. * testControlOverridingMagicSelectType method
  3496. *
  3497. * Test that overriding the magic select type widget is possible.
  3498. *
  3499. * @return void
  3500. */
  3501. public function testControlOverridingMagicSelectType()
  3502. {
  3503. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3504. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3505. $expected = [
  3506. 'div' => ['class' => 'input text'],
  3507. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3508. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3509. '/div'
  3510. ];
  3511. $this->assertHtml($expected, $result);
  3512. //Check that magic types still work for plural/singular vars
  3513. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3514. $result = $this->Form->control('Model.type');
  3515. $expected = [
  3516. 'div' => ['class' => 'input select'],
  3517. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3518. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3519. ['option' => ['value' => 'value']], 'good', '/option',
  3520. ['option' => ['value' => 'other']], 'bad', '/option',
  3521. '/select',
  3522. '/div'
  3523. ];
  3524. $this->assertHtml($expected, $result);
  3525. }
  3526. /**
  3527. * testControlMagicTypeDoesNotOverride method
  3528. *
  3529. * Test that inferred types do not override developer input.
  3530. *
  3531. * @return void
  3532. */
  3533. public function testControlMagicTypeDoesNotOverride()
  3534. {
  3535. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3536. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3537. $expected = [
  3538. 'div' => ['class' => 'input checkbox'],
  3539. ['input' => [
  3540. 'type' => 'hidden',
  3541. 'name' => 'Model[user]',
  3542. 'value' => 0,
  3543. ]],
  3544. 'label' => ['for' => 'model-user'],
  3545. ['input' => [
  3546. 'name' => 'Model[user]',
  3547. 'type' => 'checkbox',
  3548. 'id' => 'model-user',
  3549. 'value' => 1
  3550. ]],
  3551. 'User',
  3552. '/label',
  3553. '/div'
  3554. ];
  3555. $this->assertHtml($expected, $result);
  3556. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3557. $options = [
  3558. 1 => 'blue',
  3559. 2 => 'red'
  3560. ];
  3561. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3562. $expected = [
  3563. 'div' => ['class' => 'input select'],
  3564. 'label' => ['for' => 'tags-ids'],
  3565. 'Tags',
  3566. '/label',
  3567. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3568. ['div' => ['class' => 'checkbox']],
  3569. ['label' => ['for' => 'tags-ids-1']],
  3570. ['input' => [
  3571. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3572. 'value' => '1', 'name' => 'tags[_ids][]'
  3573. ]],
  3574. 'blue',
  3575. '/label',
  3576. '/div',
  3577. ['div' => ['class' => 'checkbox']],
  3578. ['label' => ['for' => 'tags-ids-2']],
  3579. ['input' => [
  3580. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3581. 'value' => '2', 'name' => 'tags[_ids][]'
  3582. ]],
  3583. 'red',
  3584. '/label',
  3585. '/div',
  3586. '/div'
  3587. ];
  3588. $this->assertHtml($expected, $result);
  3589. }
  3590. /**
  3591. * testControlMagicSelectForTypeNumber method
  3592. *
  3593. * Test that magic control() selects are created for type=number.
  3594. *
  3595. * @return void
  3596. */
  3597. public function testControlMagicSelectForTypeNumber()
  3598. {
  3599. TableRegistry::get('ValidateUsers', [
  3600. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3601. ]);
  3602. $entity = new Entity(['balance' => 1]);
  3603. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3604. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3605. $result = $this->Form->control('balance');
  3606. $expected = [
  3607. 'div' => ['class' => 'input select'],
  3608. 'label' => ['for' => 'balance'],
  3609. 'Balance',
  3610. '/label',
  3611. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3612. ['option' => ['value' => '0']],
  3613. 'nothing',
  3614. '/option',
  3615. ['option' => ['value' => '1', 'selected' => 'selected']],
  3616. 'some',
  3617. '/option',
  3618. ['option' => ['value' => '100']],
  3619. 'a lot',
  3620. '/option',
  3621. '/select',
  3622. '/div'
  3623. ];
  3624. $this->assertHtml($expected, $result);
  3625. }
  3626. /**
  3627. * testInvalidControlTypeOption method
  3628. *
  3629. * Test invalid 'input' type option to control() function.
  3630. *
  3631. * @expectedException \RuntimeException
  3632. * @expectedExceptionMessage Invalid type 'input' used for field 'text'
  3633. * @return void
  3634. */
  3635. public function testInvalidControlTypeOption()
  3636. {
  3637. $this->Form->control('text', ['type' => 'input']);
  3638. }
  3639. /**
  3640. * testControlMagicSelectChangeToRadio method
  3641. *
  3642. * Test that magic control() selects can easily be converted into radio types without error.
  3643. *
  3644. * @return void
  3645. */
  3646. public function testControlMagicSelectChangeToRadio()
  3647. {
  3648. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3649. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3650. $this->assertContains('input type="radio"', $result);
  3651. }
  3652. /**
  3653. * testFormControlSubmit method
  3654. *
  3655. * Test correct results for form::control() and type submit.
  3656. *
  3657. * @return void
  3658. */
  3659. public function testFormControlSubmit()
  3660. {
  3661. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3662. $expected = [
  3663. 'div' => ['class' => 'submit'],
  3664. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3665. '/div'
  3666. ];
  3667. $this->assertHtml($expected, $result);
  3668. }
  3669. /**
  3670. * testFormControls method
  3671. *
  3672. * Test correct results from Form::controls().
  3673. *
  3674. * @return void
  3675. */
  3676. public function testFormControlsLegendFieldset()
  3677. {
  3678. $this->Form->create($this->article);
  3679. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3680. $expected = [
  3681. '<fieldset',
  3682. '<legend',
  3683. 'The Legend',
  3684. '/legend',
  3685. '*/fieldset',
  3686. ];
  3687. $this->assertHtml($expected, $result);
  3688. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3689. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3690. $this->assertContains('<fieldset>', $result);
  3691. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3692. $this->assertNotContains('<legend>', $result);
  3693. $this->assertNotContains('<fieldset>', $result);
  3694. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3695. $this->assertNotContains('<legend>', $result);
  3696. $this->assertNotContains('<fieldset>', $result);
  3697. $this->Form->create($this->article);
  3698. $this->Form->request->params['prefix'] = 'admin';
  3699. $this->Form->request->params['action'] = 'admin_edit';
  3700. $this->Form->request->params['controller'] = 'articles';
  3701. $result = $this->Form->allControls();
  3702. $expected = [
  3703. '<fieldset',
  3704. '<legend',
  3705. 'New Article',
  3706. '/legend',
  3707. '*/fieldset',
  3708. ];
  3709. $this->assertHtml($expected, $result);
  3710. $this->Form->create($this->article);
  3711. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3712. $expected = [
  3713. '<fieldset',
  3714. '<legend',
  3715. 'The Legend',
  3716. '/legend',
  3717. '*/fieldset',
  3718. ];
  3719. $this->assertHtml($expected, $result);
  3720. $this->Form->create($this->article);
  3721. $result = $this->Form->allControls([], [
  3722. 'fieldset' => [
  3723. 'class' => 'some-class some-other-class',
  3724. 'disabled' => true,
  3725. 'data-param' => 'a-param'
  3726. ],
  3727. 'legend' => 'The Legend'
  3728. ]);
  3729. $expected = [
  3730. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3731. '<legend',
  3732. 'The Legend',
  3733. '/legend',
  3734. '*/fieldset',
  3735. ];
  3736. $this->assertHtml($expected, $result);
  3737. }
  3738. /**
  3739. * testFormControls method
  3740. *
  3741. * Test the controls() method.
  3742. *
  3743. * @return void
  3744. */
  3745. public function testFormControls()
  3746. {
  3747. $this->Form->create($this->article);
  3748. $result = $this->Form->allControls();
  3749. $expected = [
  3750. '<fieldset',
  3751. '<legend', 'New Article', '/legend',
  3752. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3753. ['div' => ['class' => 'input select required']],
  3754. '*/div',
  3755. ['div' => ['class' => 'input text required']],
  3756. '*/div',
  3757. ['div' => ['class' => 'input text']],
  3758. '*/div',
  3759. ['div' => ['class' => 'input text']],
  3760. '*/div',
  3761. '/fieldset',
  3762. ];
  3763. $this->assertHtml($expected, $result);
  3764. $result = $this->Form->allControls([
  3765. 'published' => ['type' => 'boolean']
  3766. ]);
  3767. $expected = [
  3768. '<fieldset',
  3769. '<legend', 'New Article', '/legend',
  3770. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3771. ['div' => ['class' => 'input select required']],
  3772. '*/div',
  3773. ['div' => ['class' => 'input text required']],
  3774. '*/div',
  3775. ['div' => ['class' => 'input text']],
  3776. '*/div',
  3777. ['div' => ['class' => 'input boolean']],
  3778. '*/div',
  3779. '/fieldset',
  3780. ];
  3781. $this->assertHtml($expected, $result);
  3782. $this->Form->create($this->article);
  3783. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3784. $expected = [
  3785. 'fieldset' => [],
  3786. 'legend' => [],
  3787. 'Hello',
  3788. '/legend',
  3789. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3790. ['div' => ['class' => 'input select required']],
  3791. '*/div',
  3792. ['div' => ['class' => 'input text required']],
  3793. '*/div',
  3794. ['div' => ['class' => 'input text']],
  3795. '*/div',
  3796. ['div' => ['class' => 'input text']],
  3797. '*/div',
  3798. '/fieldset'
  3799. ];
  3800. $this->assertHtml($expected, $result);
  3801. $this->Form->create(false);
  3802. $expected = [
  3803. 'fieldset' => [],
  3804. ['div' => ['class' => 'input text']],
  3805. 'label' => ['for' => 'foo'],
  3806. 'Foo',
  3807. '/label',
  3808. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3809. '*/div',
  3810. '/fieldset'
  3811. ];
  3812. $result = $this->Form->allControls(
  3813. ['foo' => ['type' => 'text']],
  3814. ['legend' => false]
  3815. );
  3816. $this->assertHtml($expected, $result);
  3817. }
  3818. /**
  3819. * testFormControlsBlacklist method
  3820. *
  3821. * @return void
  3822. */
  3823. public function testFormControlsBlacklist()
  3824. {
  3825. $this->Form->create($this->article);
  3826. $result = $this->Form->allControls([
  3827. 'id' => false
  3828. ]);
  3829. $expected = [
  3830. '<fieldset',
  3831. '<legend', 'New Article', '/legend',
  3832. ['div' => ['class' => 'input select required']],
  3833. '*/div',
  3834. ['div' => ['class' => 'input text required']],
  3835. '*/div',
  3836. ['div' => ['class' => 'input text']],
  3837. '*/div',
  3838. ['div' => ['class' => 'input text']],
  3839. '*/div',
  3840. '/fieldset',
  3841. ];
  3842. $this->assertHtml($expected, $result);
  3843. $this->Form->create($this->article);
  3844. $result = $this->Form->allControls([
  3845. 'id' => []
  3846. ]);
  3847. $expected = [
  3848. '<fieldset',
  3849. '<legend', 'New Article', '/legend',
  3850. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3851. ['div' => ['class' => 'input select required']],
  3852. '*/div',
  3853. ['div' => ['class' => 'input text required']],
  3854. '*/div',
  3855. ['div' => ['class' => 'input text']],
  3856. '*/div',
  3857. ['div' => ['class' => 'input text']],
  3858. '*/div',
  3859. '/fieldset',
  3860. ];
  3861. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3862. }
  3863. /**
  3864. * testSelectAsCheckbox method
  3865. *
  3866. * Test multi-select widget with checkbox formatting.
  3867. *
  3868. * @return void
  3869. */
  3870. public function testSelectAsCheckbox()
  3871. {
  3872. $result = $this->Form->select(
  3873. 'Model.multi_field',
  3874. ['first', 'second', 'third'],
  3875. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3876. );
  3877. $expected = [
  3878. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3879. ['div' => ['class' => 'checkbox']],
  3880. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3881. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3882. 'first',
  3883. '/label',
  3884. '/div',
  3885. ['div' => ['class' => 'checkbox']],
  3886. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3887. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3888. 'second',
  3889. '/label',
  3890. '/div',
  3891. ['div' => ['class' => 'checkbox']],
  3892. ['label' => ['for' => 'model-multi-field-2']],
  3893. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3894. 'third',
  3895. '/label',
  3896. '/div',
  3897. ];
  3898. $this->assertHtml($expected, $result);
  3899. $result = $this->Form->select(
  3900. 'Model.multi_field',
  3901. ['1/2' => 'half'],
  3902. ['multiple' => 'checkbox']
  3903. );
  3904. $expected = [
  3905. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3906. ['div' => ['class' => 'checkbox']],
  3907. ['label' => ['for' => 'model-multi-field-1-2']],
  3908. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3909. 'half',
  3910. '/label',
  3911. '/div',
  3912. ];
  3913. $this->assertHtml($expected, $result);
  3914. }
  3915. /**
  3916. * testLabel method
  3917. *
  3918. * Test label generation.
  3919. *
  3920. * @return void
  3921. */
  3922. public function testLabel()
  3923. {
  3924. $result = $this->Form->label('Person.name');
  3925. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3926. $this->assertHtml($expected, $result);
  3927. $result = $this->Form->label('Person.name');
  3928. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3929. $this->assertHtml($expected, $result);
  3930. $result = $this->Form->label('Person.first_name');
  3931. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3932. $this->assertHtml($expected, $result);
  3933. $result = $this->Form->label('Person.first_name', 'Your first name');
  3934. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3935. $this->assertHtml($expected, $result);
  3936. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3937. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3938. $this->assertHtml($expected, $result);
  3939. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3940. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3941. $this->assertHtml($expected, $result);
  3942. $result = $this->Form->label('Person.first_name', '');
  3943. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3944. $this->assertHtml($expected, $result);
  3945. $result = $this->Form->label('Person.2.name', '');
  3946. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3947. $this->assertHtml($expected, $result);
  3948. }
  3949. /**
  3950. * testLabelContainControl method
  3951. *
  3952. * Test that label() can accept an input with the correct template vars.
  3953. *
  3954. * @return void
  3955. */
  3956. public function testLabelContainControl()
  3957. {
  3958. $this->Form->templates([
  3959. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3960. ]);
  3961. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3962. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3963. ]);
  3964. $expected = [
  3965. 'label' => ['for' => 'person-accept-terms'],
  3966. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3967. 'Accept',
  3968. '/label',
  3969. ];
  3970. $this->assertHtml($expected, $result);
  3971. }
  3972. /**
  3973. * testTextbox method
  3974. *
  3975. * Test textbox element generation.
  3976. *
  3977. * @return void
  3978. */
  3979. public function testTextbox()
  3980. {
  3981. $result = $this->Form->text('Model.field');
  3982. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3983. $this->assertHtml($expected, $result);
  3984. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3985. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3986. $this->assertHtml($expected, $result);
  3987. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3988. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3989. $this->assertHtml($expected, $result);
  3990. }
  3991. /**
  3992. * testTextBoxDataAndError method
  3993. *
  3994. * Test that text() hooks up with request data and error fields.
  3995. *
  3996. * @return void
  3997. */
  3998. public function testTextBoxDataAndError()
  3999. {
  4000. $this->article['errors'] = [
  4001. 'Contact' => ['text' => 'wrong']
  4002. ];
  4003. $this->Form->create($this->article);
  4004. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  4005. $result = $this->Form->text('Model.text');
  4006. $expected = [
  4007. 'input' => [
  4008. 'type' => 'text',
  4009. 'name' => 'Model[text]',
  4010. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  4011. ]
  4012. ];
  4013. $this->assertHtml($expected, $result);
  4014. $this->Form->request->data['Contact']['text'] = 'test';
  4015. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  4016. $expected = [
  4017. 'input' => [
  4018. 'type' => 'text',
  4019. 'name' => 'Contact[text]',
  4020. 'value' => 'test',
  4021. 'id' => 'theID',
  4022. 'class' => 'form-error'
  4023. ]
  4024. ];
  4025. $this->assertHtml($expected, $result);
  4026. }
  4027. /**
  4028. * testDefaultValue method
  4029. *
  4030. * Test default value setting.
  4031. *
  4032. * @return void
  4033. */
  4034. public function testTextDefaultValue()
  4035. {
  4036. $this->Form->request->data['Model']['field'] = 'test';
  4037. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4038. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  4039. $this->assertHtml($expected, $result);
  4040. unset($this->Form->request->data['Model']['field']);
  4041. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4042. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4043. $this->assertHtml($expected, $result);
  4044. $Articles = TableRegistry::get('Articles');
  4045. $title = $Articles->schema()->column('title');
  4046. $Articles->schema()->addColumn(
  4047. 'title',
  4048. ['default' => 'default title'] + $title
  4049. );
  4050. $entity = $Articles->newEntity();
  4051. $this->Form->create($entity);
  4052. // Get default value from schema
  4053. $result = $this->Form->text('title');
  4054. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4055. $this->assertHtml($expected, $result);
  4056. // Don't get value from schema
  4057. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4058. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4059. $this->assertHtml($expected, $result);
  4060. // Custom default value overrides default value from schema
  4061. $result = $this->Form->text('title', ['default' => 'override default']);
  4062. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4063. $this->assertHtml($expected, $result);
  4064. // Default value from schema is used only for new entities.
  4065. $entity->isNew(false);
  4066. $result = $this->Form->text('title');
  4067. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4068. $this->assertHtml($expected, $result);
  4069. }
  4070. /**
  4071. * testError method
  4072. *
  4073. * Test field error generation.
  4074. *
  4075. * @return void
  4076. */
  4077. public function testError()
  4078. {
  4079. $this->article['errors'] = [
  4080. 'Article' => ['field' => 'email']
  4081. ];
  4082. $this->Form->create($this->article);
  4083. $result = $this->Form->error('Article.field');
  4084. $expected = [
  4085. ['div' => ['class' => 'error-message']],
  4086. 'email',
  4087. '/div',
  4088. ];
  4089. $this->assertHtml($expected, $result);
  4090. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4091. $expected = [
  4092. ['div' => ['class' => 'error-message']],
  4093. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4094. '/div',
  4095. ];
  4096. $this->assertHtml($expected, $result);
  4097. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4098. $expected = [
  4099. ['div' => ['class' => 'error-message']],
  4100. '<strong', 'Badness!', '/strong',
  4101. '/div',
  4102. ];
  4103. $this->assertHtml($expected, $result);
  4104. }
  4105. /**
  4106. * testErrorRuleName method
  4107. *
  4108. * Test error translation can use rule names for translating.
  4109. *
  4110. * @return void
  4111. */
  4112. public function testErrorRuleName()
  4113. {
  4114. $this->article['errors'] = [
  4115. 'Article' => [
  4116. 'field' => ['email' => 'Your email was not good']
  4117. ]
  4118. ];
  4119. $this->Form->create($this->article);
  4120. $result = $this->Form->error('Article.field');
  4121. $expected = [
  4122. ['div' => ['class' => 'error-message']],
  4123. 'Your email was not good',
  4124. '/div',
  4125. ];
  4126. $this->assertHtml($expected, $result);
  4127. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4128. $expected = [
  4129. ['div' => ['class' => 'error-message']],
  4130. 'Email in use',
  4131. '/div',
  4132. ];
  4133. $this->assertHtml($expected, $result);
  4134. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4135. $expected = [
  4136. ['div' => ['class' => 'error-message']],
  4137. 'Email in use',
  4138. '/div',
  4139. ];
  4140. $this->assertHtml($expected, $result);
  4141. $result = $this->Form->error('Article.field', [
  4142. 'email' => 'Key is preferred',
  4143. 'Your email was not good' => 'Email in use'
  4144. ]);
  4145. $expected = [
  4146. ['div' => ['class' => 'error-message']],
  4147. 'Key is preferred',
  4148. '/div',
  4149. ];
  4150. $this->assertHtml($expected, $result);
  4151. }
  4152. /**
  4153. * testErrorMessages method
  4154. *
  4155. * Test error with nested lists.
  4156. *
  4157. * @return void
  4158. */
  4159. public function testErrorMessages()
  4160. {
  4161. $this->article['errors'] = [
  4162. 'Article' => ['field' => 'email']
  4163. ];
  4164. $this->Form->create($this->article);
  4165. $result = $this->Form->error('Article.field', [
  4166. 'email' => 'No good!'
  4167. ]);
  4168. $expected = [
  4169. 'div' => ['class' => 'error-message'],
  4170. 'No good!',
  4171. '/div'
  4172. ];
  4173. $this->assertHtml($expected, $result);
  4174. }
  4175. /**
  4176. * testErrorMultipleMessages method
  4177. *
  4178. * Test error() with multiple messages.
  4179. *
  4180. * @return void
  4181. */
  4182. public function testErrorMultipleMessages()
  4183. {
  4184. $this->article['errors'] = [
  4185. 'field' => ['notBlank', 'email', 'Something else']
  4186. ];
  4187. $this->Form->create($this->article);
  4188. $result = $this->Form->error('field', [
  4189. 'notBlank' => 'Cannot be empty',
  4190. 'email' => 'No good!'
  4191. ]);
  4192. $expected = [
  4193. 'div' => ['class' => 'error-message'],
  4194. 'ul' => [],
  4195. '<li', 'Cannot be empty', '/li',
  4196. '<li', 'No good!', '/li',
  4197. '<li', 'Something else', '/li',
  4198. '/ul',
  4199. '/div'
  4200. ];
  4201. $this->assertHtml($expected, $result);
  4202. }
  4203. /**
  4204. * testPassword method
  4205. *
  4206. * Test password element generation.
  4207. *
  4208. * @return void
  4209. */
  4210. public function testPassword()
  4211. {
  4212. $this->article['errors'] = [
  4213. 'Contact' => [
  4214. 'passwd' => 1
  4215. ]
  4216. ];
  4217. $this->Form->create($this->article);
  4218. $result = $this->Form->password('Contact.field');
  4219. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4220. $this->assertHtml($expected, $result);
  4221. $this->Form->request->data['Contact']['passwd'] = 'test';
  4222. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4223. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4224. $this->assertHtml($expected, $result);
  4225. }
  4226. /**
  4227. * testRadio method
  4228. *
  4229. * Test radio element set generation.
  4230. *
  4231. * @return void
  4232. */
  4233. public function testRadio()
  4234. {
  4235. $result = $this->Form->radio('Model.field', ['option A']);
  4236. $expected = [
  4237. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4238. 'label' => ['for' => 'model-field-0'],
  4239. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4240. 'option A',
  4241. '/label'
  4242. ];
  4243. $this->assertHtml($expected, $result);
  4244. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4245. $this->assertHtml($expected, $result);
  4246. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4247. $expected = [
  4248. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4249. ['label' => ['for' => 'model-field-0']],
  4250. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4251. 'option A',
  4252. '/label',
  4253. ['label' => ['for' => 'model-field-1']],
  4254. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4255. 'option B',
  4256. '/label',
  4257. ];
  4258. $this->assertHtml($expected, $result);
  4259. $result = $this->Form->radio(
  4260. 'Employee.gender',
  4261. ['male' => 'Male', 'female' => 'Female'],
  4262. ['form' => 'my-form']
  4263. );
  4264. $expected = [
  4265. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4266. ['label' => ['for' => 'employee-gender-male']],
  4267. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4268. 'Male',
  4269. '/label',
  4270. ['label' => ['for' => 'employee-gender-female']],
  4271. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4272. 'Female',
  4273. '/label',
  4274. ];
  4275. $this->assertHtml($expected, $result);
  4276. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4277. $expected = [
  4278. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4279. ['label' => ['for' => 'model-custom-0']],
  4280. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4281. 'option A',
  4282. '/label',
  4283. ['label' => ['for' => 'model-custom-1']],
  4284. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4285. 'option B',
  4286. '/label',
  4287. ];
  4288. $this->assertHtml($expected, $result);
  4289. $result = $this->Form->radio(
  4290. 'Employee.gender',
  4291. [
  4292. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4293. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4294. ]
  4295. );
  4296. $expected = [
  4297. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4298. ['label' => ['for' => 'employee-gender-male']],
  4299. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4300. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4301. 'Male',
  4302. '/label',
  4303. ['label' => ['for' => 'employee-gender-female']],
  4304. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4305. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4306. 'Female',
  4307. '/label',
  4308. ];
  4309. $this->assertHtml($expected, $result);
  4310. }
  4311. /**
  4312. * Test radio with complex options and empty disabled data.
  4313. *
  4314. * @return void
  4315. */
  4316. public function testRadioComplexDisabled()
  4317. {
  4318. $options = [
  4319. ['value' => 'r', 'text' => 'red'],
  4320. ['value' => 'b', 'text' => 'blue'],
  4321. ];
  4322. $attrs = ['disabled' => []];
  4323. $result = $this->Form->radio('Model.field', $options, $attrs);
  4324. $expected = [
  4325. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4326. ['label' => ['for' => 'model-field-r']],
  4327. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4328. 'red',
  4329. '/label',
  4330. ['label' => ['for' => 'model-field-b']],
  4331. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4332. 'blue',
  4333. '/label',
  4334. ];
  4335. $this->assertHtml($expected, $result);
  4336. $attrs = ['disabled' => ['r']];
  4337. $result = $this->Form->radio('Model.field', $options, $attrs);
  4338. $this->assertContains('disabled="disabled"', $result);
  4339. }
  4340. /**
  4341. * testRadioDefaultValue method
  4342. *
  4343. * Test default value setting on radio() method.
  4344. *
  4345. * @return void
  4346. */
  4347. public function testRadioDefaultValue()
  4348. {
  4349. $Articles = TableRegistry::get('Articles');
  4350. $title = $Articles->schema()->column('title');
  4351. $Articles->schema()->addColumn(
  4352. 'title',
  4353. ['default' => '1'] + $title
  4354. );
  4355. $this->Form->create($Articles->newEntity());
  4356. $result = $this->Form->radio('title', ['option A', 'option B']);
  4357. $expected = [
  4358. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4359. ['label' => ['for' => 'title-0']],
  4360. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4361. 'option A',
  4362. '/label',
  4363. ['label' => ['for' => 'title-1']],
  4364. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4365. 'option B',
  4366. '/label',
  4367. ];
  4368. $this->assertHtml($expected, $result);
  4369. }
  4370. /**
  4371. * Test setting a hiddenField value on radio buttons.
  4372. *
  4373. * @return void
  4374. */
  4375. public function testRadioHiddenFieldValue()
  4376. {
  4377. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4378. $expected = [
  4379. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4380. 'label' => ['for' => 'title-0'],
  4381. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4382. 'option A',
  4383. '/label',
  4384. ];
  4385. $this->assertHtml($expected, $result);
  4386. }
  4387. /**
  4388. * testControlRadio method
  4389. *
  4390. * Test that input works with radio types.
  4391. *
  4392. * @return void
  4393. */
  4394. public function testControlRadio()
  4395. {
  4396. $result = $this->Form->control('test', [
  4397. 'type' => 'radio',
  4398. 'options' => ['A', 'B'],
  4399. ]);
  4400. $expected = [
  4401. ['div' => ['class' => 'input radio']],
  4402. '<label',
  4403. 'Test',
  4404. '/label',
  4405. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4406. ['label' => ['for' => 'test-0']],
  4407. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4408. 'A',
  4409. '/label',
  4410. ['label' => ['for' => 'test-1']],
  4411. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4412. 'B',
  4413. '/label',
  4414. '/div',
  4415. ];
  4416. $this->assertHtml($expected, $result);
  4417. $result = $this->Form->control('test', [
  4418. 'type' => 'radio',
  4419. 'options' => ['A', 'B'],
  4420. 'value' => '0'
  4421. ]);
  4422. $expected = [
  4423. ['div' => ['class' => 'input radio']],
  4424. '<label',
  4425. 'Test',
  4426. '/label',
  4427. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4428. ['label' => ['for' => 'test-0']],
  4429. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4430. 'A',
  4431. '/label',
  4432. ['label' => ['for' => 'test-1']],
  4433. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4434. 'B',
  4435. '/label',
  4436. '/div',
  4437. ];
  4438. $this->assertHtml($expected, $result);
  4439. $result = $this->Form->control('test', [
  4440. 'type' => 'radio',
  4441. 'options' => ['A', 'B'],
  4442. 'label' => false
  4443. ]);
  4444. $expected = [
  4445. ['div' => ['class' => 'input radio']],
  4446. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4447. ['label' => ['for' => 'test-0']],
  4448. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4449. 'A',
  4450. '/label',
  4451. ['label' => ['for' => 'test-1']],
  4452. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4453. 'B',
  4454. '/label',
  4455. '/div',
  4456. ];
  4457. $this->assertHtml($expected, $result);
  4458. }
  4459. /**
  4460. * testRadioNoLabel method
  4461. *
  4462. * Test that radio() works with label = false.
  4463. *
  4464. * @return void
  4465. */
  4466. public function testRadioNoLabel()
  4467. {
  4468. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4469. $expected = [
  4470. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4471. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4472. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4473. ];
  4474. $this->assertHtml($expected, $result);
  4475. }
  4476. /**
  4477. * testRadioControlInsideLabel method
  4478. *
  4479. * Test generating radio input inside label ala twitter bootstrap.
  4480. *
  4481. * @return void
  4482. */
  4483. public function testRadioControlInsideLabel()
  4484. {
  4485. $this->Form->templates([
  4486. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4487. 'radioWrapper' => '{{label}}'
  4488. ]);
  4489. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4490. //@codingStandardsIgnoreStart
  4491. $expected = [
  4492. ['input' => [
  4493. 'type' => 'hidden',
  4494. 'name' => 'Model[field]',
  4495. 'value' => ''
  4496. ]],
  4497. ['label' => ['for' => 'model-field-0']],
  4498. ['input' => [
  4499. 'type' => 'radio',
  4500. 'name' => 'Model[field]',
  4501. 'value' => '0',
  4502. 'id' => 'model-field-0'
  4503. ]],
  4504. 'option A',
  4505. '/label',
  4506. ['label' => ['for' => 'model-field-1']],
  4507. ['input' => [
  4508. 'type' => 'radio',
  4509. 'name' => 'Model[field]',
  4510. 'value' => '1',
  4511. 'id' => 'model-field-1'
  4512. ]],
  4513. 'option B',
  4514. '/label',
  4515. ];
  4516. //@codingStandardsIgnoreEnd
  4517. $this->assertHtml($expected, $result);
  4518. }
  4519. /**
  4520. * testRadioHiddenControlDisabling method
  4521. *
  4522. * Test disabling the hidden input for radio buttons.
  4523. *
  4524. * @return void
  4525. */
  4526. public function testRadioHiddenControlDisabling()
  4527. {
  4528. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4529. $expected = [
  4530. 'label' => ['for' => 'model-1-field-0'],
  4531. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4532. 'option A',
  4533. '/label'
  4534. ];
  4535. $this->assertHtml($expected, $result);
  4536. }
  4537. /**
  4538. * testRadioOutOfRange method
  4539. *
  4540. * Test radio element set generation.
  4541. *
  4542. * @return void
  4543. */
  4544. public function testRadioOutOfRange()
  4545. {
  4546. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4547. $expected = [
  4548. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4549. 'label' => ['for' => 'model-field-v'],
  4550. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4551. 'value',
  4552. '/label'
  4553. ];
  4554. $this->assertHtml($expected, $result);
  4555. }
  4556. /**
  4557. * testSelect method
  4558. *
  4559. * Test select element generation.
  4560. *
  4561. * @return void
  4562. */
  4563. public function testSelect()
  4564. {
  4565. $result = $this->Form->select('Model.field', []);
  4566. $expected = [
  4567. 'select' => ['name' => 'Model[field]'],
  4568. '/select'
  4569. ];
  4570. $this->assertHtml($expected, $result);
  4571. $this->Form->request->data = ['Model' => ['field' => 'value']];
  4572. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4573. $expected = [
  4574. 'select' => ['name' => 'Model[field]'],
  4575. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4576. 'good',
  4577. '/option',
  4578. ['option' => ['value' => 'other']],
  4579. 'bad',
  4580. '/option',
  4581. '/select'
  4582. ];
  4583. $this->assertHtml($expected, $result);
  4584. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4585. $this->assertHtml($expected, $result);
  4586. $this->Form->request->data = [];
  4587. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4588. $expected = [
  4589. 'select' => ['name' => 'Model[field]'],
  4590. ['option' => ['value' => 'value']],
  4591. 'good',
  4592. '/option',
  4593. ['option' => ['value' => 'other']],
  4594. 'bad',
  4595. '/option',
  4596. '/select'
  4597. ];
  4598. $this->assertHtml($expected, $result);
  4599. $options = [
  4600. ['value' => 'first', 'text' => 'First'],
  4601. ['value' => 'first', 'text' => 'Another First'],
  4602. ];
  4603. $result = $this->Form->select(
  4604. 'Model.field',
  4605. $options,
  4606. ['escape' => false, 'empty' => false]
  4607. );
  4608. $expected = [
  4609. 'select' => ['name' => 'Model[field]'],
  4610. ['option' => ['value' => 'first']],
  4611. 'First',
  4612. '/option',
  4613. ['option' => ['value' => 'first']],
  4614. 'Another First',
  4615. '/option',
  4616. '/select'
  4617. ];
  4618. $this->assertHtml($expected, $result);
  4619. $this->Form->request->data = ['Model' => ['contact_id' => 228]];
  4620. $result = $this->Form->select(
  4621. 'Model.contact_id',
  4622. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4623. ['escape' => false, 'empty' => 'pick something']
  4624. );
  4625. $expected = [
  4626. 'select' => ['name' => 'Model[contact_id]'],
  4627. ['option' => ['value' => '']], 'pick something', '/option',
  4628. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4629. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4630. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4631. '/select'
  4632. ];
  4633. $this->assertHtml($expected, $result);
  4634. $this->Form->request->data['Model']['field'] = 0;
  4635. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4636. $expected = [
  4637. 'select' => ['name' => 'Model[field]'],
  4638. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4639. ['option' => ['value' => '1']], 'Yes', '/option',
  4640. '/select'
  4641. ];
  4642. $this->assertHtml($expected, $result);
  4643. }
  4644. /**
  4645. * testSelectEscapeHtml method
  4646. *
  4647. * Test that select() escapes HTML.
  4648. *
  4649. * @return void
  4650. */
  4651. public function testSelectEscapeHtml()
  4652. {
  4653. $result = $this->Form->select(
  4654. 'Model.field',
  4655. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4656. ['empty' => false]
  4657. );
  4658. $expected = [
  4659. 'select' => ['name' => 'Model[field]'],
  4660. ['option' => ['value' => 'first']],
  4661. 'first &quot;html&quot; &lt;chars&gt;',
  4662. '/option',
  4663. ['option' => ['value' => 'second']],
  4664. 'value',
  4665. '/option',
  4666. '/select'
  4667. ];
  4668. $this->assertHtml($expected, $result);
  4669. $result = $this->Form->select(
  4670. 'Model.field',
  4671. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4672. ['escape' => false, 'empty' => false]
  4673. );
  4674. $expected = [
  4675. 'select' => ['name' => 'Model[field]'],
  4676. ['option' => ['value' => 'first']],
  4677. 'first "html" <chars>',
  4678. '/option',
  4679. ['option' => ['value' => 'second']],
  4680. 'value',
  4681. '/option',
  4682. '/select'
  4683. ];
  4684. $this->assertHtml($expected, $result);
  4685. }
  4686. /**
  4687. * testSelectRequired method
  4688. *
  4689. * Test select() with required and disabled attributes.
  4690. *
  4691. * @return void
  4692. */
  4693. public function testSelectRequired()
  4694. {
  4695. $this->article['required'] = [
  4696. 'user_id' => true
  4697. ];
  4698. $this->Form->create($this->article);
  4699. $result = $this->Form->select('user_id', ['option A']);
  4700. $expected = [
  4701. 'select' => [
  4702. 'name' => 'user_id',
  4703. 'required' => 'required'
  4704. ],
  4705. ['option' => ['value' => '0']], 'option A', '/option',
  4706. '/select'
  4707. ];
  4708. $this->assertHtml($expected, $result);
  4709. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4710. $expected = [
  4711. 'select' => [
  4712. 'name' => 'user_id',
  4713. 'disabled' => 'disabled'
  4714. ],
  4715. ['option' => ['value' => '0']], 'option A', '/option',
  4716. '/select'
  4717. ];
  4718. $this->assertHtml($expected, $result);
  4719. }
  4720. /**
  4721. * testNestedSelect method
  4722. *
  4723. * Test select element generation with optgroups.
  4724. *
  4725. * @return void
  4726. */
  4727. public function testNestedSelect()
  4728. {
  4729. $result = $this->Form->select(
  4730. 'Model.field',
  4731. [1 => 'One', 2 => 'Two', 'Three' => [
  4732. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4733. ]],
  4734. ['empty' => false]
  4735. );
  4736. $expected = [
  4737. 'select' => ['name' => 'Model[field]'],
  4738. ['option' => ['value' => 1]],
  4739. 'One',
  4740. '/option',
  4741. ['option' => ['value' => 2]],
  4742. 'Two',
  4743. '/option',
  4744. ['optgroup' => ['label' => 'Three']],
  4745. ['option' => ['value' => 3]],
  4746. 'Three',
  4747. '/option',
  4748. ['option' => ['value' => 4]],
  4749. 'Four',
  4750. '/option',
  4751. ['option' => ['value' => 5]],
  4752. 'Five',
  4753. '/option',
  4754. '/optgroup',
  4755. '/select'
  4756. ];
  4757. $this->assertHtml($expected, $result);
  4758. }
  4759. /**
  4760. * testSelectMultiple method
  4761. *
  4762. * Test generation of multiple select elements.
  4763. *
  4764. * @return void
  4765. */
  4766. public function testSelectMultiple()
  4767. {
  4768. $options = ['first', 'second', 'third'];
  4769. $result = $this->Form->select(
  4770. 'Model.multi_field',
  4771. $options,
  4772. ['form' => 'my-form', 'multiple' => true]
  4773. );
  4774. $expected = [
  4775. 'input' => [
  4776. 'type' => 'hidden',
  4777. 'name' => 'Model[multi_field]',
  4778. 'value' => '',
  4779. 'form' => 'my-form',
  4780. ],
  4781. 'select' => [
  4782. 'name' => 'Model[multi_field][]',
  4783. 'multiple' => 'multiple',
  4784. 'form' => 'my-form',
  4785. ],
  4786. ['option' => ['value' => '0']],
  4787. 'first',
  4788. '/option',
  4789. ['option' => ['value' => '1']],
  4790. 'second',
  4791. '/option',
  4792. ['option' => ['value' => '2']],
  4793. 'third',
  4794. '/option',
  4795. '/select'
  4796. ];
  4797. $this->assertHtml($expected, $result);
  4798. $result = $this->Form->select(
  4799. 'Model.multi_field',
  4800. $options,
  4801. ['multiple' => 'multiple', 'form' => 'my-form']
  4802. );
  4803. $this->assertHtml($expected, $result);
  4804. }
  4805. /**
  4806. * testCheckboxZeroValue method
  4807. *
  4808. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4809. *
  4810. * @return void
  4811. */
  4812. public function testCheckboxZeroValue()
  4813. {
  4814. $result = $this->Form->control('User.get_spam', [
  4815. 'type' => 'checkbox',
  4816. 'value' => '0',
  4817. 'hiddenField' => '1',
  4818. ]);
  4819. $expected = [
  4820. 'div' => ['class' => 'input checkbox'],
  4821. 'label' => ['for' => 'user-get-spam'],
  4822. ['input' => [
  4823. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4824. 'value' => '1'
  4825. ]],
  4826. ['input' => [
  4827. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4828. 'value' => '0', 'id' => 'user-get-spam'
  4829. ]],
  4830. 'Get Spam',
  4831. '/label',
  4832. '/div'
  4833. ];
  4834. $this->assertHtml($expected, $result);
  4835. }
  4836. /**
  4837. * testHabtmSelectBox method
  4838. *
  4839. * Test generation of habtm select boxes.
  4840. *
  4841. * @return void
  4842. */
  4843. public function testHabtmSelectBox()
  4844. {
  4845. $this->loadFixtures('Articles');
  4846. $options = [
  4847. 1 => 'blue',
  4848. 2 => 'red',
  4849. 3 => 'green'
  4850. ];
  4851. $tags = [
  4852. new Entity(['id' => 1, 'name' => 'blue']),
  4853. new Entity(['id' => 3, 'name' => 'green'])
  4854. ];
  4855. $article = new Article(['tags' => $tags]);
  4856. $this->Form->create($article);
  4857. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4858. $expected = [
  4859. 'div' => ['class' => 'input select'],
  4860. 'label' => ['for' => 'tags-ids'],
  4861. 'Tags',
  4862. '/label',
  4863. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4864. 'select' => [
  4865. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4866. 'multiple' => 'multiple'
  4867. ],
  4868. ['option' => ['value' => '1', 'selected' => 'selected']],
  4869. 'blue',
  4870. '/option',
  4871. ['option' => ['value' => '2']],
  4872. 'red',
  4873. '/option',
  4874. ['option' => ['value' => '3', 'selected' => 'selected']],
  4875. 'green',
  4876. '/option',
  4877. '/select',
  4878. '/div'
  4879. ];
  4880. $this->assertHtml($expected, $result);
  4881. // make sure only 50 is selected, and not 50f5c0cf
  4882. $options = [
  4883. '1' => 'blue',
  4884. '50f5c0cf' => 'red',
  4885. '50' => 'green'
  4886. ];
  4887. $tags = [
  4888. new Entity(['id' => 1, 'name' => 'blue']),
  4889. new Entity(['id' => 50, 'name' => 'green'])
  4890. ];
  4891. $article = new Article(['tags' => $tags]);
  4892. $this->Form->create($article);
  4893. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4894. $expected = [
  4895. 'div' => ['class' => 'input select'],
  4896. 'label' => ['for' => 'tags-ids'],
  4897. 'Tags',
  4898. '/label',
  4899. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4900. 'select' => [
  4901. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4902. 'multiple' => 'multiple'
  4903. ],
  4904. ['option' => ['value' => '1', 'selected' => 'selected']],
  4905. 'blue',
  4906. '/option',
  4907. ['option' => ['value' => '50f5c0cf']],
  4908. 'red',
  4909. '/option',
  4910. ['option' => ['value' => '50', 'selected' => 'selected']],
  4911. 'green',
  4912. '/option',
  4913. '/select',
  4914. '/div'
  4915. ];
  4916. $this->assertHtml($expected, $result);
  4917. $spacecraft = [
  4918. 1 => 'Orion',
  4919. 2 => 'Helios'
  4920. ];
  4921. $this->View->viewVars['spacecraft'] = $spacecraft;
  4922. $this->Form->create();
  4923. $result = $this->Form->control('spacecraft._ids');
  4924. $expected = [
  4925. 'div' => ['class' => 'input select'],
  4926. 'label' => ['for' => 'spacecraft-ids'],
  4927. 'Spacecraft',
  4928. '/label',
  4929. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4930. 'select' => [
  4931. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4932. 'multiple' => 'multiple'
  4933. ],
  4934. ['option' => ['value' => '1']],
  4935. 'Orion',
  4936. '/option',
  4937. ['option' => ['value' => '2']],
  4938. 'Helios',
  4939. '/option',
  4940. '/select',
  4941. '/div'
  4942. ];
  4943. $this->assertHtml($expected, $result);
  4944. }
  4945. /**
  4946. * testErrorsForBelongsToManySelect method
  4947. *
  4948. * Tests that errors for belongsToMany select fields are being
  4949. * picked up properly.
  4950. *
  4951. * @return void
  4952. */
  4953. public function testErrorsForBelongsToManySelect()
  4954. {
  4955. $spacecraft = [
  4956. 1 => 'Orion',
  4957. 2 => 'Helios'
  4958. ];
  4959. $this->View->viewVars['spacecraft'] = $spacecraft;
  4960. $article = new Article();
  4961. $article->errors('spacecraft', ['Invalid']);
  4962. $this->Form->create($article);
  4963. $result = $this->Form->control('spacecraft._ids');
  4964. $expected = [
  4965. ['div' => ['class' => 'input select error']],
  4966. 'label' => ['for' => 'spacecraft-ids'],
  4967. 'Spacecraft',
  4968. '/label',
  4969. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4970. 'select' => [
  4971. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4972. 'multiple' => 'multiple'
  4973. ],
  4974. ['option' => ['value' => '1']],
  4975. 'Orion',
  4976. '/option',
  4977. ['option' => ['value' => '2']],
  4978. 'Helios',
  4979. '/option',
  4980. '/select',
  4981. ['div' => ['class' => 'error-message']],
  4982. 'Invalid',
  4983. '/div',
  4984. '/div'
  4985. ];
  4986. $this->assertHtml($expected, $result);
  4987. }
  4988. /**
  4989. * testSelectMultipleCheckboxes method
  4990. *
  4991. * Test generation of multi select elements in checkbox format.
  4992. *
  4993. * @return void
  4994. */
  4995. public function testSelectMultipleCheckboxes()
  4996. {
  4997. $result = $this->Form->select(
  4998. 'Model.multi_field',
  4999. ['first', 'second', 'third'],
  5000. ['multiple' => 'checkbox']
  5001. );
  5002. $expected = [
  5003. 'input' => [
  5004. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5005. ],
  5006. ['div' => ['class' => 'checkbox']],
  5007. ['label' => ['for' => 'model-multi-field-0']],
  5008. ['input' => [
  5009. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5010. 'value' => '0', 'id' => 'model-multi-field-0'
  5011. ]],
  5012. 'first',
  5013. '/label',
  5014. '/div',
  5015. ['div' => ['class' => 'checkbox']],
  5016. ['label' => ['for' => 'model-multi-field-1']],
  5017. ['input' => [
  5018. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5019. 'value' => '1', 'id' => 'model-multi-field-1'
  5020. ]],
  5021. 'second',
  5022. '/label',
  5023. '/div',
  5024. ['div' => ['class' => 'checkbox']],
  5025. ['label' => ['for' => 'model-multi-field-2']],
  5026. ['input' => [
  5027. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5028. 'value' => '2', 'id' => 'model-multi-field-2'
  5029. ]],
  5030. 'third',
  5031. '/label',
  5032. '/div'
  5033. ];
  5034. $this->assertHtml($expected, $result);
  5035. $result = $this->Form->select(
  5036. 'Model.multi_field',
  5037. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5038. ['multiple' => 'checkbox']
  5039. );
  5040. $expected = [
  5041. 'input' => [
  5042. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5043. ],
  5044. ['div' => ['class' => 'checkbox']],
  5045. ['label' => ['for' => 'model-multi-field-a+']],
  5046. ['input' => [
  5047. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5048. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  5049. ]],
  5050. 'first',
  5051. '/label',
  5052. '/div',
  5053. ['div' => ['class' => 'checkbox']],
  5054. ['label' => ['for' => 'model-multi-field-a++']],
  5055. ['input' => [
  5056. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5057. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  5058. ]],
  5059. 'second',
  5060. '/label',
  5061. '/div',
  5062. ['div' => ['class' => 'checkbox']],
  5063. ['label' => ['for' => 'model-multi-field-a+++']],
  5064. ['input' => [
  5065. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5066. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  5067. ]],
  5068. 'third',
  5069. '/label',
  5070. '/div'
  5071. ];
  5072. $this->assertHtml($expected, $result);
  5073. $result = $this->Form->select(
  5074. 'Model.multi_field',
  5075. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5076. ['multiple' => 'checkbox']
  5077. );
  5078. $expected = [
  5079. 'input' => [
  5080. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5081. ],
  5082. ['div' => ['class' => 'checkbox']],
  5083. ['label' => ['for' => 'model-multi-field-a-b']],
  5084. ['input' => [
  5085. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5086. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  5087. ]],
  5088. 'first',
  5089. '/label',
  5090. '/div',
  5091. ['div' => ['class' => 'checkbox']],
  5092. ['label' => ['for' => 'model-multi-field-a-b1']],
  5093. ['input' => [
  5094. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5095. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  5096. ]],
  5097. 'second',
  5098. '/label',
  5099. '/div',
  5100. ['div' => ['class' => 'checkbox']],
  5101. ['label' => ['for' => 'model-multi-field-a-b2']],
  5102. ['input' => [
  5103. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5104. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  5105. ]],
  5106. 'third',
  5107. '/label',
  5108. '/div'
  5109. ];
  5110. $this->assertHtml($expected, $result);
  5111. }
  5112. /**
  5113. * testSelectMultipleCheckboxRequestData method
  5114. *
  5115. * Ensure that multiCheckbox reads from the request data.
  5116. *
  5117. * @return void
  5118. */
  5119. public function testSelectMultipleCheckboxRequestData()
  5120. {
  5121. $this->Form->request->data = ['Model' => ['tags' => [1]]];
  5122. $result = $this->Form->select(
  5123. 'Model.tags',
  5124. ['1' => 'first', 'Array' => 'Array'],
  5125. ['multiple' => 'checkbox']
  5126. );
  5127. $expected = [
  5128. 'input' => [
  5129. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  5130. ],
  5131. ['div' => ['class' => 'checkbox']],
  5132. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5133. ['input' => [
  5134. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5135. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  5136. ]],
  5137. 'first',
  5138. '/label',
  5139. '/div',
  5140. ['div' => ['class' => 'checkbox']],
  5141. ['label' => ['for' => 'model-tags-array']],
  5142. ['input' => [
  5143. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5144. 'value' => 'Array', 'id' => 'model-tags-array'
  5145. ]],
  5146. 'Array',
  5147. '/label',
  5148. '/div'
  5149. ];
  5150. $this->assertHtml($expected, $result);
  5151. }
  5152. /**
  5153. * testSelectMultipleCheckboxSecurity method
  5154. *
  5155. * Checks the security hash array generated for multiple-input checkbox elements.
  5156. *
  5157. * @return void
  5158. */
  5159. public function testSelectMultipleCheckboxSecurity()
  5160. {
  5161. $this->Form->request->params['_Token'] = 'testKey';
  5162. $this->assertEquals([], $this->Form->fields);
  5163. $this->Form->select(
  5164. 'Model.multi_field',
  5165. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5166. ['multiple' => 'checkbox']
  5167. );
  5168. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5169. $result = $this->Form->secure($this->Form->fields);
  5170. $key = '3cecbba5b65c8792d963b0498c67741466e61d47%3A';
  5171. $this->assertRegExp('/"' . $key . '"/', $result);
  5172. }
  5173. /**
  5174. * testSelectMultipleSecureWithNoOptions method
  5175. *
  5176. * Multiple select elements should always be secured as they always participate
  5177. * in the POST data.
  5178. *
  5179. * @return void
  5180. */
  5181. public function testSelectMultipleSecureWithNoOptions()
  5182. {
  5183. $this->assertEquals([], $this->Form->fields);
  5184. $this->Form->select(
  5185. 'Model.select',
  5186. [],
  5187. ['multiple' => true]
  5188. );
  5189. $this->assertEquals(['Model.select'], $this->Form->fields);
  5190. }
  5191. /**
  5192. * testSelectNoSecureWithNoOptions method
  5193. *
  5194. * When a select box has no options it should not be added to the fields list
  5195. * as it always fail post validation.
  5196. *
  5197. * @return void
  5198. */
  5199. public function testSelectNoSecureWithNoOptions()
  5200. {
  5201. $this->Form->request->params['_Token'] = 'testkey';
  5202. $this->assertEquals([], $this->Form->fields);
  5203. $this->Form->select(
  5204. 'Model.select',
  5205. []
  5206. );
  5207. $this->assertEquals([], $this->Form->fields);
  5208. $this->Form->select(
  5209. 'Model.user_id',
  5210. [],
  5211. ['empty' => true]
  5212. );
  5213. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5214. }
  5215. /**
  5216. * testControlMultipleCheckboxes method
  5217. *
  5218. * Test control() resulting in multi select elements being generated.
  5219. *
  5220. * @return void
  5221. */
  5222. public function testControlMultipleCheckboxes()
  5223. {
  5224. $result = $this->Form->control('Model.multi_field', [
  5225. 'options' => ['first', 'second', 'third'],
  5226. 'multiple' => 'checkbox'
  5227. ]);
  5228. $expected = [
  5229. ['div' => ['class' => 'input select']],
  5230. ['label' => ['for' => 'model-multi-field']],
  5231. 'Multi Field',
  5232. '/label',
  5233. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5234. ['div' => ['class' => 'checkbox']],
  5235. ['label' => ['for' => 'model-multi-field-0']],
  5236. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5237. 'first',
  5238. '/label',
  5239. '/div',
  5240. ['div' => ['class' => 'checkbox']],
  5241. ['label' => ['for' => 'model-multi-field-1']],
  5242. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5243. 'second',
  5244. '/label',
  5245. '/div',
  5246. ['div' => ['class' => 'checkbox']],
  5247. ['label' => ['for' => 'model-multi-field-2']],
  5248. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5249. 'third',
  5250. '/label',
  5251. '/div',
  5252. '/div'
  5253. ];
  5254. $this->assertHtml($expected, $result);
  5255. $result = $this->Form->control('Model.multi_field', [
  5256. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5257. 'multiple' => 'checkbox'
  5258. ]);
  5259. $expected = [
  5260. ['div' => ['class' => 'input select']],
  5261. ['label' => ['for' => 'model-multi-field']],
  5262. 'Multi Field',
  5263. '/label',
  5264. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5265. ['div' => ['class' => 'checkbox']],
  5266. ['label' => ['for' => 'model-multi-field-a']],
  5267. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5268. 'first',
  5269. '/label',
  5270. '/div',
  5271. ['div' => ['class' => 'checkbox']],
  5272. ['label' => ['for' => 'model-multi-field-b']],
  5273. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5274. 'second',
  5275. '/label',
  5276. '/div',
  5277. ['div' => ['class' => 'checkbox']],
  5278. ['label' => ['for' => 'model-multi-field-c']],
  5279. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5280. 'third',
  5281. '/label',
  5282. '/div',
  5283. '/div'
  5284. ];
  5285. $this->assertHtml($expected, $result);
  5286. }
  5287. /**
  5288. * testSelectHiddenFieldOmission method
  5289. *
  5290. * Test that select() with 'hiddenField' => false omits the hidden field.
  5291. *
  5292. * @return void
  5293. */
  5294. public function testSelectHiddenFieldOmission()
  5295. {
  5296. $result = $this->Form->select(
  5297. 'Model.multi_field',
  5298. ['first', 'second'],
  5299. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5300. );
  5301. $this->assertNotContains('type="hidden"', $result);
  5302. }
  5303. /**
  5304. * testSelectCheckboxMultipleOverrideName method
  5305. *
  5306. * Test that select() with multiple = checkbox works with overriding name attribute.
  5307. *
  5308. * @return void
  5309. */
  5310. public function testSelectCheckboxMultipleOverrideName()
  5311. {
  5312. $result = $this->Form->select('category', ['1', '2'], [
  5313. 'multiple' => 'checkbox',
  5314. 'name' => 'fish',
  5315. ]);
  5316. $expected = [
  5317. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5318. ['div' => ['class' => 'checkbox']],
  5319. ['label' => ['for' => 'fish-0']],
  5320. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5321. '1',
  5322. '/label',
  5323. '/div',
  5324. ['div' => ['class' => 'checkbox']],
  5325. ['label' => ['for' => 'fish-1']],
  5326. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5327. '2',
  5328. '/label',
  5329. '/div'
  5330. ];
  5331. $this->assertHtml($expected, $result);
  5332. $result = $this->Form->multiCheckbox(
  5333. 'category',
  5334. new Collection(['1', '2']),
  5335. ['name' => 'fish']
  5336. );
  5337. $this->assertHtml($expected, $result);
  5338. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5339. 'name' => 'fish',
  5340. ]);
  5341. $this->assertHtml($expected, $result);
  5342. }
  5343. /**
  5344. * testControlMultiCheckbox method
  5345. *
  5346. * Test that control() works with multicheckbox.
  5347. *
  5348. * @return void
  5349. */
  5350. public function testControlMultiCheckbox()
  5351. {
  5352. $result = $this->Form->control('category', [
  5353. 'type' => 'multicheckbox',
  5354. 'options' => ['1', '2'],
  5355. ]);
  5356. $expected = [
  5357. ['div' => ['class' => 'input multicheckbox']],
  5358. '<label',
  5359. 'Category',
  5360. '/label',
  5361. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5362. ['div' => ['class' => 'checkbox']],
  5363. ['label' => ['for' => 'category-0']],
  5364. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5365. '1',
  5366. '/label',
  5367. '/div',
  5368. ['div' => ['class' => 'checkbox']],
  5369. ['label' => ['for' => 'category-1']],
  5370. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5371. '2',
  5372. '/label',
  5373. '/div',
  5374. '/div',
  5375. ];
  5376. $this->assertHtml($expected, $result);
  5377. }
  5378. /**
  5379. * testCheckbox method
  5380. *
  5381. * Test generation of checkboxes.
  5382. *
  5383. * @return void
  5384. */
  5385. public function testCheckbox()
  5386. {
  5387. $result = $this->Form->checkbox('Model.field');
  5388. $expected = [
  5389. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5390. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5391. ];
  5392. $this->assertHtml($expected, $result);
  5393. $result = $this->Form->checkbox('Model.field', [
  5394. 'id' => 'theID',
  5395. 'value' => 'myvalue',
  5396. 'form' => 'my-form',
  5397. ]);
  5398. $expected = [
  5399. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5400. ['input' => [
  5401. 'type' => 'checkbox', 'name' => 'Model[field]',
  5402. 'value' => 'myvalue', 'id' => 'theID',
  5403. 'form' => 'my-form',
  5404. ]]
  5405. ];
  5406. $this->assertHtml($expected, $result);
  5407. }
  5408. /**
  5409. * testCheckboxDefaultValue method
  5410. *
  5411. * Test default value setting on checkbox() method.
  5412. *
  5413. * @return void
  5414. */
  5415. public function testCheckboxDefaultValue()
  5416. {
  5417. $this->Form->request->data['Model']['field'] = false;
  5418. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5419. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5420. $this->assertHtml($expected, $result);
  5421. unset($this->Form->request->data['Model']['field']);
  5422. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5423. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5424. $this->assertHtml($expected, $result);
  5425. $this->Form->request->data['Model']['field'] = true;
  5426. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5427. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5428. $this->assertHtml($expected, $result);
  5429. unset($this->Form->request->data['Model']['field']);
  5430. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5431. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5432. $this->assertHtml($expected, $result);
  5433. $Articles = TableRegistry::get('Articles');
  5434. $Articles->schema()->addColumn(
  5435. 'published',
  5436. ['type' => 'boolean', 'null' => false, 'default' => true]
  5437. );
  5438. $this->Form->create($Articles->newEntity());
  5439. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5440. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5441. $this->assertHtml($expected, $result);
  5442. }
  5443. /**
  5444. * testCheckboxCheckedAndError method
  5445. *
  5446. * Test checkbox being checked or having errors.
  5447. *
  5448. * @return void
  5449. */
  5450. public function testCheckboxCheckedAndError()
  5451. {
  5452. $this->article['errors'] = [
  5453. 'published' => true
  5454. ];
  5455. $this->Form->request->data['published'] = 'myvalue';
  5456. $this->Form->create($this->article);
  5457. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5458. $expected = [
  5459. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5460. ['input' => [
  5461. 'type' => 'checkbox',
  5462. 'name' => 'published',
  5463. 'value' => 'myvalue',
  5464. 'id' => 'theID',
  5465. 'checked' => 'checked',
  5466. 'class' => 'form-error'
  5467. ]]
  5468. ];
  5469. $this->assertHtml($expected, $result);
  5470. $this->Form->request->data['published'] = '';
  5471. $result = $this->Form->checkbox('published');
  5472. $expected = [
  5473. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5474. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5475. ];
  5476. $this->assertHtml($expected, $result);
  5477. }
  5478. /**
  5479. * testCheckboxCustomNameAttribute method
  5480. *
  5481. * Test checkbox() with a custom name attribute.
  5482. *
  5483. * @return void
  5484. */
  5485. public function testCheckboxCustomNameAttribute()
  5486. {
  5487. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5488. $expected = [
  5489. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5490. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5491. ];
  5492. $this->assertHtml($expected, $result);
  5493. }
  5494. /**
  5495. * testCheckboxHiddenField method
  5496. *
  5497. * Test that the hidden input for checkboxes can be omitted or set to a
  5498. * specific value.
  5499. *
  5500. * @return void
  5501. */
  5502. public function testCheckboxHiddenField()
  5503. {
  5504. $result = $this->Form->checkbox('UserForm.something', [
  5505. 'hiddenField' => false
  5506. ]);
  5507. $expected = [
  5508. 'input' => [
  5509. 'type' => 'checkbox',
  5510. 'name' => 'UserForm[something]',
  5511. 'value' => '1'
  5512. ],
  5513. ];
  5514. $this->assertHtml($expected, $result);
  5515. $result = $this->Form->checkbox('UserForm.something', [
  5516. 'value' => 'Y',
  5517. 'hiddenField' => 'N',
  5518. ]);
  5519. $expected = [
  5520. ['input' => [
  5521. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5522. 'value' => 'N'
  5523. ]],
  5524. ['input' => [
  5525. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5526. 'value' => 'Y'
  5527. ]],
  5528. ];
  5529. $this->assertHtml($expected, $result);
  5530. }
  5531. /**
  5532. * testTime method
  5533. *
  5534. * Test the time type.
  5535. *
  5536. * @return void
  5537. */
  5538. public function testTime()
  5539. {
  5540. $result = $this->Form->time('start_time', [
  5541. 'timeFormat' => 12,
  5542. 'interval' => 5,
  5543. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5544. ]);
  5545. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5546. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5547. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5548. $this->assertNotContains('year', $result);
  5549. $this->assertNotContains('month', $result);
  5550. $this->assertNotContains('day', $result);
  5551. $result = $this->Form->time('start_time', [
  5552. 'timeFormat' => 12,
  5553. 'interval' => 5,
  5554. 'value' => '2014-03-08 16:30:00'
  5555. ]);
  5556. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5557. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5558. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5559. $this->assertNotContains('year', $result);
  5560. $this->assertNotContains('month', $result);
  5561. $this->assertNotContains('day', $result);
  5562. }
  5563. /**
  5564. * testTimeFormat24NoMeridian method
  5565. *
  5566. * Ensure that timeFormat=24 has no merdian.
  5567. *
  5568. * @return void.
  5569. */
  5570. public function testTimeFormat24NoMeridian()
  5571. {
  5572. $result = $this->Form->time('start_time', [
  5573. 'timeFormat' => 24,
  5574. 'interval' => 5,
  5575. 'value' => '2014-03-08 16:30:00'
  5576. ]);
  5577. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5578. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5579. $this->assertNotContains('meridian', $result);
  5580. $this->assertNotContains('pm', $result);
  5581. $this->assertNotContains('year', $result);
  5582. $this->assertNotContains('month', $result);
  5583. $this->assertNotContains('day', $result);
  5584. }
  5585. /**
  5586. * testDate method
  5587. *
  5588. * Test the date type.
  5589. *
  5590. * @return void
  5591. */
  5592. public function testDate()
  5593. {
  5594. $result = $this->Form->date('start_day', [
  5595. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5596. ]);
  5597. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5598. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5599. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5600. $this->assertNotContains('hour', $result);
  5601. $this->assertNotContains('minute', $result);
  5602. $this->assertNotContains('second', $result);
  5603. $this->assertNotContains('meridian', $result);
  5604. }
  5605. /**
  5606. * testDateTime method
  5607. *
  5608. * Test generation of date/time select elements.
  5609. *
  5610. * @return void
  5611. */
  5612. public function testDateTime()
  5613. {
  5614. extract($this->dateRegex);
  5615. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5616. $now = strtotime('now');
  5617. $expected = [
  5618. ['select' => ['name' => 'Contact[date][year]']],
  5619. ['option' => ['value' => '']],
  5620. '/option',
  5621. $yearsRegex,
  5622. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5623. date('Y', $now),
  5624. '/option',
  5625. '*/select',
  5626. ['select' => ['name' => 'Contact[date][month]']],
  5627. ['option' => ['value' => '']],
  5628. '/option',
  5629. $monthsRegex,
  5630. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5631. date('F', $now),
  5632. '/option',
  5633. '*/select',
  5634. ['select' => ['name' => 'Contact[date][day]']],
  5635. ['option' => ['value' => '']],
  5636. '/option',
  5637. $daysRegex,
  5638. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5639. date('j', $now),
  5640. '/option',
  5641. '*/select',
  5642. ['select' => ['name' => 'Contact[date][hour]']],
  5643. ['option' => ['value' => '']],
  5644. '/option',
  5645. $hoursRegex,
  5646. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5647. date('G', $now),
  5648. '/option',
  5649. '*/select',
  5650. ['select' => ['name' => 'Contact[date][minute]']],
  5651. ['option' => ['value' => '']],
  5652. '/option',
  5653. $minutesRegex,
  5654. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5655. date('i', $now),
  5656. '/option',
  5657. '*/select',
  5658. ];
  5659. $this->assertHtml($expected, $result);
  5660. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5661. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5662. $now = strtotime('now');
  5663. $expected = [
  5664. ['select' => ['name' => 'Contact[date][year]']],
  5665. $yearsRegex,
  5666. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5667. date('Y', $now),
  5668. '/option',
  5669. '*/select',
  5670. ['select' => ['name' => 'Contact[date][month]']],
  5671. $monthsRegex,
  5672. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5673. date('F', $now),
  5674. '/option',
  5675. '*/select',
  5676. ['select' => ['name' => 'Contact[date][day]']],
  5677. $daysRegex,
  5678. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5679. date('j', $now),
  5680. '/option',
  5681. '*/select',
  5682. ['select' => ['name' => 'Contact[date][hour]']],
  5683. $hoursRegex,
  5684. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5685. date('G', $now),
  5686. '/option',
  5687. '*/select',
  5688. ['select' => ['name' => 'Contact[date][minute]']],
  5689. $minutesRegex,
  5690. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5691. date('i', $now),
  5692. '/option',
  5693. '*/select',
  5694. ];
  5695. $this->assertHtml($expected, $result);
  5696. }
  5697. /**
  5698. * testDateTimeSecured method
  5699. *
  5700. * Test that datetime fields are added to protected fields list.
  5701. *
  5702. * @return void
  5703. */
  5704. public function testDateTimeSecured()
  5705. {
  5706. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5707. $this->Form->dateTime('Contact.date');
  5708. $expected = [
  5709. 'Contact.date.year',
  5710. 'Contact.date.month',
  5711. 'Contact.date.day',
  5712. 'Contact.date.hour',
  5713. 'Contact.date.minute',
  5714. ];
  5715. $this->assertEquals($expected, $this->Form->fields);
  5716. $this->Form->fields = [];
  5717. $this->Form->date('Contact.published');
  5718. $expected = [
  5719. 'Contact.published.year',
  5720. 'Contact.published.month',
  5721. 'Contact.published.day',
  5722. ];
  5723. $this->assertEquals($expected, $this->Form->fields);
  5724. }
  5725. /**
  5726. * testDateTimeSecuredDisabled method
  5727. *
  5728. * Test that datetime fields are added to protected fields list.
  5729. *
  5730. * @return void
  5731. */
  5732. public function testDateTimeSecuredDisabled()
  5733. {
  5734. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5735. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5736. $expected = [];
  5737. $this->assertEquals($expected, $this->Form->fields);
  5738. $this->Form->fields = [];
  5739. $this->Form->date('Contact.published', ['secure' => false]);
  5740. $expected = [];
  5741. $this->assertEquals($expected, $this->Form->fields);
  5742. }
  5743. /**
  5744. * testDatetimeEmpty method
  5745. *
  5746. * Test empty defaulting to true for datetime.
  5747. *
  5748. * @return void
  5749. */
  5750. public function testDatetimeEmpty()
  5751. {
  5752. extract($this->dateRegex);
  5753. $result = $this->Form->dateTime('Contact.date', [
  5754. 'timeFormat' => 12,
  5755. 'empty' => true,
  5756. 'default' => true
  5757. ]);
  5758. $expected = [
  5759. ['select' => ['name' => 'Contact[date][year]']],
  5760. $yearsRegex,
  5761. ['option' => ['value' => '']],
  5762. '/option',
  5763. '*/select',
  5764. ['select' => ['name' => 'Contact[date][month]']],
  5765. $monthsRegex,
  5766. ['option' => ['value' => '']],
  5767. '/option',
  5768. '*/select',
  5769. ['select' => ['name' => 'Contact[date][day]']],
  5770. $daysRegex,
  5771. ['option' => ['value' => '']],
  5772. '/option',
  5773. '*/select',
  5774. ['select' => ['name' => 'Contact[date][hour]']],
  5775. $hoursRegex,
  5776. ['option' => ['value' => '']],
  5777. '/option',
  5778. '*/select',
  5779. ['select' => ['name' => 'Contact[date][minute]']],
  5780. $minutesRegex,
  5781. ['option' => ['value' => '']],
  5782. '/option',
  5783. '*/select',
  5784. ['select' => ['name' => 'Contact[date][meridian]']],
  5785. $meridianRegex,
  5786. ['option' => ['value' => '']],
  5787. '/option',
  5788. '*/select'
  5789. ];
  5790. $this->assertHtml($expected, $result);
  5791. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5792. }
  5793. /**
  5794. * testDatetimeMinuteInterval method
  5795. *
  5796. * Test datetime with interval option.
  5797. *
  5798. * @return void
  5799. */
  5800. public function testDatetimeMinuteInterval()
  5801. {
  5802. extract($this->dateRegex);
  5803. $result = $this->Form->dateTime('Contact.date', [
  5804. 'interval' => 5,
  5805. 'value' => ''
  5806. ]);
  5807. $expected = [
  5808. ['select' => ['name' => 'Contact[date][year]']],
  5809. $yearsRegex,
  5810. ['option' => ['selected' => 'selected', 'value' => '']],
  5811. '/option',
  5812. '*/select',
  5813. ['select' => ['name' => 'Contact[date][month]']],
  5814. $monthsRegex,
  5815. ['option' => ['selected' => 'selected', 'value' => '']],
  5816. '/option',
  5817. '*/select',
  5818. ['select' => ['name' => 'Contact[date][day]']],
  5819. $daysRegex,
  5820. ['option' => ['selected' => 'selected', 'value' => '']],
  5821. '/option',
  5822. '*/select',
  5823. ['select' => ['name' => 'Contact[date][hour]']],
  5824. $hoursRegex,
  5825. ['option' => ['selected' => 'selected', 'value' => '']],
  5826. '/option',
  5827. '*/select',
  5828. ['select' => ['name' => 'Contact[date][minute]']],
  5829. $minutesRegex,
  5830. ['option' => ['selected' => 'selected', 'value' => '']],
  5831. '/option',
  5832. ['option' => ['value' => '00']],
  5833. '00',
  5834. '/option',
  5835. ['option' => ['value' => '05']],
  5836. '05',
  5837. '/option',
  5838. ['option' => ['value' => '10']],
  5839. '10',
  5840. '/option',
  5841. '*/select',
  5842. ];
  5843. $this->assertHtml($expected, $result);
  5844. }
  5845. /**
  5846. * testDateTimeRounding method
  5847. *
  5848. * Test dateTime with rounding.
  5849. *
  5850. * @return void
  5851. */
  5852. public function testDateTimeRounding()
  5853. {
  5854. $this->Form->request->data['Contact'] = [
  5855. 'date' => [
  5856. 'day' => '13',
  5857. 'month' => '12',
  5858. 'year' => '2010',
  5859. 'hour' => '04',
  5860. 'minute' => '19',
  5861. 'meridian' => 'AM'
  5862. ]
  5863. ];
  5864. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5865. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5866. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5867. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5868. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5869. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5870. }
  5871. /**
  5872. * testDatetimeWithDefault method
  5873. *
  5874. * Test that datetime() and default values work.
  5875. *
  5876. * @return void
  5877. */
  5878. public function testDatetimeWithDefault()
  5879. {
  5880. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5881. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5882. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5883. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5884. $result = $this->Form->dateTime('Contact.updated', [
  5885. 'default' => '2009-06-01 11:15:30'
  5886. ]);
  5887. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5888. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5889. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5890. }
  5891. /**
  5892. * testDateTimeAllZeros method
  5893. *
  5894. * Test datetime() with all zeros.
  5895. *
  5896. * @return void
  5897. */
  5898. public function testDateTimeAllZeros()
  5899. {
  5900. $result = $this->Form->dateTime('Contact.date', [
  5901. 'timeFormat' => false,
  5902. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5903. 'value' => '0000-00-00'
  5904. ]);
  5905. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5906. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5907. }
  5908. /**
  5909. * testDateTimeEmptyAsArray method
  5910. *
  5911. * @return void
  5912. */
  5913. public function testDateTimeEmptyAsArray()
  5914. {
  5915. $result = $this->Form->dateTime('Contact.date', [
  5916. 'empty' => [
  5917. 'day' => 'DAY',
  5918. 'month' => 'MONTH',
  5919. 'year' => 'YEAR',
  5920. 'hour' => 'HOUR',
  5921. 'minute' => 'MINUTE',
  5922. 'meridian' => false
  5923. ],
  5924. 'default' => true
  5925. ]);
  5926. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5927. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5928. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5929. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5930. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5931. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5932. $result = $this->Form->dateTime('Contact.date', [
  5933. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5934. 'default' => true
  5935. ]);
  5936. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5937. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5938. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5939. }
  5940. /**
  5941. * testFormDateTimeMulti method
  5942. *
  5943. * Test multiple datetime element generation.
  5944. *
  5945. * @return void
  5946. */
  5947. public function testFormDateTimeMulti()
  5948. {
  5949. extract($this->dateRegex);
  5950. $result = $this->Form->dateTime('Contact.1.updated');
  5951. $this->assertContains('Contact[1][updated][month]', $result);
  5952. $this->assertContains('Contact[1][updated][day]', $result);
  5953. $this->assertContains('Contact[1][updated][year]', $result);
  5954. $this->assertContains('Contact[1][updated][hour]', $result);
  5955. $this->assertContains('Contact[1][updated][minute]', $result);
  5956. }
  5957. /**
  5958. * testDateTimeLabelIdMatchesFirstControl method
  5959. *
  5960. * When changing the date format, the label should always focus the first select box when
  5961. * clicked.
  5962. *
  5963. * @return void
  5964. */
  5965. public function testDateTimeLabelIdMatchesFirstControl()
  5966. {
  5967. $result = $this->Form->control('Model.date', ['type' => 'date']);
  5968. $this->assertContains('<label>Date</label>', $result);
  5969. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  5970. $this->assertContains('<label>Date</label>', $result);
  5971. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  5972. $this->assertContains('<label>Date</label>', $result);
  5973. }
  5974. /**
  5975. * testDateTimeSecondOptions method
  5976. *
  5977. * Test datetime second=true.
  5978. *
  5979. * @return void
  5980. */
  5981. public function testDateTimeSecondOptions()
  5982. {
  5983. $result = $this->Form->dateTime('updated', ['second' => true]);
  5984. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5985. $result = $this->Form->dateTime('updated', ['second' => []]);
  5986. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5987. $result = $this->Form->dateTime('updated', ['second' => null]);
  5988. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5989. $result = $this->Form->dateTime('updated', ['second' => false]);
  5990. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5991. }
  5992. /**
  5993. * testMonth method
  5994. *
  5995. * Test generation of a month input.
  5996. *
  5997. * @return void
  5998. */
  5999. public function testMonth()
  6000. {
  6001. $result = $this->Form->month('Model.field', ['value' => '']);
  6002. $expected = [
  6003. ['select' => ['name' => 'Model[field][month]']],
  6004. ['option' => ['value' => '', 'selected' => 'selected']],
  6005. '/option',
  6006. ['option' => ['value' => '01']],
  6007. date('F', strtotime('2008-01-01 00:00:00')),
  6008. '/option',
  6009. ['option' => ['value' => '02']],
  6010. date('F', strtotime('2008-02-01 00:00:00')),
  6011. '/option',
  6012. '*/select',
  6013. ];
  6014. $this->assertHtml($expected, $result);
  6015. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  6016. $expected = [
  6017. ['select' => ['name' => 'Model[field][month]']],
  6018. ['option' => ['selected' => 'selected', 'value' => '']],
  6019. '/option',
  6020. ['option' => ['value' => '01']],
  6021. date('F', strtotime('2008-01-01 00:00:00')),
  6022. '/option',
  6023. ['option' => ['value' => '02']],
  6024. date('F', strtotime('2008-02-01 00:00:00')),
  6025. '/option',
  6026. '*/select',
  6027. ];
  6028. $this->assertHtml($expected, $result);
  6029. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  6030. $expected = [
  6031. ['select' => ['name' => 'Model[field][month]']],
  6032. ['option' => ['selected' => 'selected', 'value' => '']],
  6033. '/option',
  6034. ['option' => ['value' => '01']],
  6035. '1',
  6036. '/option',
  6037. ['option' => ['value' => '02']],
  6038. '2',
  6039. '/option',
  6040. '*/select',
  6041. ];
  6042. $this->assertHtml($expected, $result);
  6043. $monthNames = [
  6044. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6045. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  6046. ];
  6047. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  6048. $expected = [
  6049. ['select' => ['name' => 'Model[field][month]']],
  6050. ['option' => ['value' => '']],
  6051. '/option',
  6052. ['option' => ['value' => '01', 'selected' => 'selected']],
  6053. 'Jan',
  6054. '/option',
  6055. ['option' => ['value' => '02']],
  6056. 'Feb',
  6057. '/option',
  6058. '*/select',
  6059. ];
  6060. $this->assertHtml($expected, $result);
  6061. $this->Form->request->data['Project']['release'] = '2050-02-10';
  6062. $result = $this->Form->month('Project.release');
  6063. $expected = [
  6064. ['select' => ['name' => 'Project[release][month]']],
  6065. ['option' => ['value' => '']],
  6066. '/option',
  6067. ['option' => ['value' => '01']],
  6068. 'January',
  6069. '/option',
  6070. ['option' => ['value' => '02', 'selected' => 'selected']],
  6071. 'February',
  6072. '/option',
  6073. '*/select',
  6074. ];
  6075. $this->assertHtml($expected, $result);
  6076. $result = $this->Form->month('Contact.published', [
  6077. 'empty' => 'Published on',
  6078. ]);
  6079. $this->assertContains('Published on', $result);
  6080. }
  6081. /**
  6082. * testDay method
  6083. *
  6084. * Test generation of a day input.
  6085. *
  6086. * @return void
  6087. */
  6088. public function testDay()
  6089. {
  6090. extract($this->dateRegex);
  6091. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  6092. $expected = [
  6093. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  6094. ['option' => ['selected' => 'selected', 'value' => '']],
  6095. '/option',
  6096. ['option' => ['value' => '01']],
  6097. '1',
  6098. '/option',
  6099. ['option' => ['value' => '02']],
  6100. '2',
  6101. '/option',
  6102. $daysRegex,
  6103. '/select',
  6104. ];
  6105. $this->assertHtml($expected, $result);
  6106. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6107. $result = $this->Form->day('Model.field');
  6108. $expected = [
  6109. ['select' => ['name' => 'Model[field][day]']],
  6110. ['option' => ['value' => '']],
  6111. '/option',
  6112. ['option' => ['value' => '01']],
  6113. '1',
  6114. '/option',
  6115. ['option' => ['value' => '02']],
  6116. '2',
  6117. '/option',
  6118. $daysRegex,
  6119. ['option' => ['value' => '10', 'selected' => 'selected']],
  6120. '10',
  6121. '/option',
  6122. $daysRegex,
  6123. '/select',
  6124. ];
  6125. $this->assertHtml($expected, $result);
  6126. $this->Form->request->data['Model']['field'] = '';
  6127. $result = $this->Form->day('Model.field', ['value' => '10']);
  6128. $expected = [
  6129. ['select' => ['name' => 'Model[field][day]']],
  6130. ['option' => ['value' => '']],
  6131. '/option',
  6132. ['option' => ['value' => '01']],
  6133. '1',
  6134. '/option',
  6135. ['option' => ['value' => '02']],
  6136. '2',
  6137. '/option',
  6138. $daysRegex,
  6139. ['option' => ['value' => '10', 'selected' => 'selected']],
  6140. '10',
  6141. '/option',
  6142. $daysRegex,
  6143. '/select',
  6144. ];
  6145. $this->assertHtml($expected, $result);
  6146. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6147. $result = $this->Form->day('Project.release');
  6148. $expected = [
  6149. ['select' => ['name' => 'Project[release][day]']],
  6150. ['option' => ['value' => '']],
  6151. '/option',
  6152. ['option' => ['value' => '01']],
  6153. '1',
  6154. '/option',
  6155. ['option' => ['value' => '02']],
  6156. '2',
  6157. '/option',
  6158. $daysRegex,
  6159. ['option' => ['value' => '10', 'selected' => 'selected']],
  6160. '10',
  6161. '/option',
  6162. $daysRegex,
  6163. '/select',
  6164. ];
  6165. $this->assertHtml($expected, $result);
  6166. $result = $this->Form->day('Contact.published', [
  6167. 'empty' => 'Published on',
  6168. ]);
  6169. $this->assertContains('Published on', $result);
  6170. }
  6171. /**
  6172. * testMinute method
  6173. *
  6174. * Test generation of a minute input.
  6175. *
  6176. * @return void
  6177. */
  6178. public function testMinute()
  6179. {
  6180. extract($this->dateRegex);
  6181. $result = $this->Form->minute('Model.field', ['value' => '']);
  6182. $expected = [
  6183. ['select' => ['name' => 'Model[field][minute]']],
  6184. ['option' => ['selected' => 'selected', 'value' => '']],
  6185. '/option',
  6186. ['option' => ['value' => '00']],
  6187. '00',
  6188. '/option',
  6189. ['option' => ['value' => '01']],
  6190. '01',
  6191. '/option',
  6192. ['option' => ['value' => '02']],
  6193. '02',
  6194. '/option',
  6195. $minutesRegex,
  6196. '/select',
  6197. ];
  6198. $this->assertHtml($expected, $result);
  6199. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6200. $result = $this->Form->minute('Model.field');
  6201. $expected = [
  6202. ['select' => ['name' => 'Model[field][minute]']],
  6203. ['option' => ['value' => '']],
  6204. '/option',
  6205. ['option' => ['value' => '00']],
  6206. '00',
  6207. '/option',
  6208. ['option' => ['value' => '01']],
  6209. '01',
  6210. '/option',
  6211. ['option' => ['value' => '02']],
  6212. '02',
  6213. '/option',
  6214. $minutesRegex,
  6215. ['option' => ['value' => '12', 'selected' => 'selected']],
  6216. '12',
  6217. '/option',
  6218. $minutesRegex,
  6219. '/select',
  6220. ];
  6221. $this->assertHtml($expected, $result);
  6222. $this->Form->request->data['Model']['field'] = '';
  6223. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6224. $expected = [
  6225. ['select' => ['name' => 'Model[field][minute]']],
  6226. ['option' => ['selected' => 'selected', 'value' => '']],
  6227. '/option',
  6228. ['option' => ['value' => '00']],
  6229. '00',
  6230. '/option',
  6231. ['option' => ['value' => '05']],
  6232. '05',
  6233. '/option',
  6234. ['option' => ['value' => '10']],
  6235. '10',
  6236. '/option',
  6237. $minutesRegex,
  6238. '/select',
  6239. ];
  6240. $this->assertHtml($expected, $result);
  6241. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6242. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6243. $expected = [
  6244. ['select' => ['name' => 'Model[field][minute]']],
  6245. ['option' => ['value' => '']],
  6246. '/option',
  6247. ['option' => ['value' => '00']],
  6248. '00',
  6249. '/option',
  6250. ['option' => ['value' => '05']],
  6251. '05',
  6252. '/option',
  6253. ['option' => ['value' => '10', 'selected' => 'selected']],
  6254. '10',
  6255. '/option',
  6256. $minutesRegex,
  6257. '/select',
  6258. ];
  6259. $this->assertHtml($expected, $result);
  6260. }
  6261. /**
  6262. * testMeridian method
  6263. *
  6264. * Test generating an input for the meridian.
  6265. *
  6266. * @return void
  6267. */
  6268. public function testMeridian()
  6269. {
  6270. extract($this->dateRegex);
  6271. $now = new \DateTime();
  6272. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6273. $expected = [
  6274. ['select' => ['name' => 'Model[field][meridian]']],
  6275. ['option' => ['value' => '']],
  6276. '/option',
  6277. $meridianRegex,
  6278. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6279. $now->format('a'),
  6280. '/option',
  6281. '*/select'
  6282. ];
  6283. $this->assertHtml($expected, $result);
  6284. }
  6285. /**
  6286. * testHour method
  6287. *
  6288. * Test generation of an hour input.
  6289. *
  6290. * @return void
  6291. */
  6292. public function testHour()
  6293. {
  6294. extract($this->dateRegex);
  6295. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6296. $expected = [
  6297. ['select' => ['name' => 'Model[field][hour]']],
  6298. ['option' => ['selected' => 'selected', 'value' => '']],
  6299. '/option',
  6300. ['option' => ['value' => '01']],
  6301. '1',
  6302. '/option',
  6303. ['option' => ['value' => '02']],
  6304. '2',
  6305. '/option',
  6306. $hoursRegex,
  6307. '/select',
  6308. ];
  6309. $this->assertHtml($expected, $result);
  6310. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6311. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6312. $expected = [
  6313. ['select' => ['name' => 'Model[field][hour]']],
  6314. ['option' => ['value' => '']],
  6315. '/option',
  6316. ['option' => ['value' => '01']],
  6317. '1',
  6318. '/option',
  6319. ['option' => ['value' => '02']],
  6320. '2',
  6321. '/option',
  6322. $hoursRegex,
  6323. ['option' => ['value' => '12', 'selected' => 'selected']],
  6324. '12',
  6325. '/option',
  6326. '/select',
  6327. ];
  6328. $this->assertHtml($expected, $result);
  6329. $this->Form->request->data['Model']['field'] = '';
  6330. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6331. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6332. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6333. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6334. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6335. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6336. $expected = [
  6337. ['select' => ['name' => 'Model[field][hour]']],
  6338. ['option' => ['value' => '']],
  6339. '/option',
  6340. ['option' => ['value' => '00', 'selected' => 'selected']],
  6341. '0',
  6342. '/option',
  6343. ['option' => ['value' => '01']],
  6344. '1',
  6345. '/option',
  6346. ['option' => ['value' => '02']],
  6347. '2',
  6348. '/option',
  6349. $hoursRegex,
  6350. '/select',
  6351. ];
  6352. $this->assertHtml($expected, $result);
  6353. unset($this->Form->request->data['Model']['field']);
  6354. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6355. $thisHour = date('H');
  6356. $optValue = date('G');
  6357. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6358. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6359. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6360. $expected = [
  6361. ['select' => ['name' => 'Model[field][hour]']],
  6362. ['option' => ['value' => '']],
  6363. '/option',
  6364. ['option' => ['value' => '00']],
  6365. '0',
  6366. '/option',
  6367. ['option' => ['value' => '01', 'selected' => 'selected']],
  6368. '1',
  6369. '/option',
  6370. ['option' => ['value' => '02']],
  6371. '2',
  6372. '/option',
  6373. $hoursRegex,
  6374. '/select',
  6375. ];
  6376. $this->assertHtml($expected, $result);
  6377. }
  6378. /**
  6379. * testYear method
  6380. *
  6381. * Test generation of a year input.
  6382. *
  6383. * @return void
  6384. */
  6385. public function testYear()
  6386. {
  6387. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6388. $expected = [
  6389. ['select' => ['name' => 'Model[field][year]']],
  6390. ['option' => ['selected' => 'selected', 'value' => '']],
  6391. '/option',
  6392. ['option' => ['value' => '2007']],
  6393. '2007',
  6394. '/option',
  6395. ['option' => ['value' => '2006']],
  6396. '2006',
  6397. '/option',
  6398. '/select',
  6399. ];
  6400. $this->assertHtml($expected, $result);
  6401. $result = $this->Form->year('Model.field', [
  6402. 'value' => '',
  6403. 'minYear' => 2006,
  6404. 'maxYear' => 2007,
  6405. 'orderYear' => 'asc'
  6406. ]);
  6407. $expected = [
  6408. ['select' => ['name' => 'Model[field][year]']],
  6409. ['option' => ['selected' => 'selected', 'value' => '']],
  6410. '/option',
  6411. ['option' => ['value' => '2006']],
  6412. '2006',
  6413. '/option',
  6414. ['option' => ['value' => '2007']],
  6415. '2007',
  6416. '/option',
  6417. '/select',
  6418. ];
  6419. $this->assertHtml($expected, $result);
  6420. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6421. $result = $this->Form->year('Contact.published', [
  6422. 'empty' => false,
  6423. 'minYear' => 2006,
  6424. 'maxYear' => 2007,
  6425. ]);
  6426. $expected = [
  6427. ['select' => ['name' => 'Contact[published][year]']],
  6428. ['option' => ['value' => '2007']],
  6429. '2007',
  6430. '/option',
  6431. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6432. '2006',
  6433. '/option',
  6434. '/select',
  6435. ];
  6436. $this->assertHtml($expected, $result);
  6437. $result = $this->Form->year('Contact.published', [
  6438. 'empty' => 'Published on',
  6439. ]);
  6440. $this->assertContains('Published on', $result);
  6441. }
  6442. /**
  6443. * testControlDatetimePreEpoch method
  6444. *
  6445. * Test minYear being prior to the unix epoch.
  6446. *
  6447. * @return void
  6448. */
  6449. public function testControlDatetimePreEpoch()
  6450. {
  6451. $start = date('Y') - 80;
  6452. $end = date('Y') - 18;
  6453. $result = $this->Form->control('birth_year', [
  6454. 'type' => 'date',
  6455. 'label' => 'Birth Year',
  6456. 'minYear' => $start,
  6457. 'maxYear' => $end,
  6458. 'month' => false,
  6459. 'day' => false,
  6460. ]);
  6461. $this->assertContains('value="' . $start . '">' . $start, $result);
  6462. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6463. $this->assertNotContains('value="00">00', $result);
  6464. }
  6465. /**
  6466. * testYearAutoExpandRange method
  6467. *
  6468. * @return void
  6469. */
  6470. public function testYearAutoExpandRange()
  6471. {
  6472. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6473. $result = $this->Form->year('User.birthday');
  6474. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6475. $result = $matches[1];
  6476. $expected = range(date('Y') + 5, 1930);
  6477. $this->assertEquals($expected, $result);
  6478. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6479. $result = $this->Form->year('Project.release');
  6480. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6481. $result = $matches[1];
  6482. $expected = range(2050, date('Y') - 5);
  6483. $this->assertEquals($expected, $result);
  6484. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6485. $result = $this->Form->year('Project.release', [
  6486. 'minYear' => 1890,
  6487. 'maxYear' => 1900
  6488. ]);
  6489. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6490. $result = $matches[1];
  6491. $expected = range(1900, 1881);
  6492. $this->assertEquals($expected, $result);
  6493. }
  6494. /**
  6495. * testControlDate method
  6496. *
  6497. * Test that control() accepts the type of date and passes options in.
  6498. *
  6499. * @return void
  6500. */
  6501. public function testControlDate()
  6502. {
  6503. $this->Form->request->data = [
  6504. 'month_year' => ['month' => date('m')],
  6505. ];
  6506. $this->Form->create($this->article);
  6507. $result = $this->Form->control('month_year', [
  6508. 'label' => false,
  6509. 'type' => 'date',
  6510. 'minYear' => 2006,
  6511. 'maxYear' => 2008
  6512. ]);
  6513. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6514. $this->assertNotContains('value="2008" selected="selected"', $result);
  6515. }
  6516. /**
  6517. * testControlLabelFalse method
  6518. *
  6519. * Test the label option being set to false.
  6520. *
  6521. * @return void
  6522. */
  6523. public function testControlLabelFalse()
  6524. {
  6525. $this->Form->create($this->article);
  6526. $result = $this->Form->control('title', ['label' => false]);
  6527. $expected = [
  6528. 'div' => ['class' => 'input text required'],
  6529. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6530. '/div'
  6531. ];
  6532. $this->assertHtml($expected, $result);
  6533. }
  6534. /**
  6535. * testControlDateMaxYear method
  6536. *
  6537. * Let's say we want to only allow users born from 2006 to 2008 to register
  6538. * This being the first signup page, we still don't have any data.
  6539. *
  6540. * @return void
  6541. */
  6542. public function testControlDateMaxYear()
  6543. {
  6544. $this->Form->request->data = [];
  6545. $this->Form->create($this->article);
  6546. $result = $this->Form->control('birthday', [
  6547. 'label' => false,
  6548. 'type' => 'date',
  6549. 'minYear' => 2006,
  6550. 'maxYear' => 2008,
  6551. 'default' => true
  6552. ]);
  6553. $this->assertContains('value="2008" selected="selected"', $result);
  6554. $this->assertContains('value="2006"', $result);
  6555. $this->assertNotContains('value="2005"', $result);
  6556. $this->assertNotContains('value="2009"', $result);
  6557. }
  6558. /**
  6559. * testTextArea method
  6560. *
  6561. * Test generation of a textarea input.
  6562. *
  6563. * @return void
  6564. */
  6565. public function testTextArea()
  6566. {
  6567. $this->Form->request->data = ['field' => 'some test data'];
  6568. $result = $this->Form->textarea('field');
  6569. $expected = [
  6570. 'textarea' => ['name' => 'field', 'rows' => 5],
  6571. 'some test data',
  6572. '/textarea',
  6573. ];
  6574. $this->assertHtml($expected, $result);
  6575. $result = $this->Form->textarea('user.bio');
  6576. $expected = [
  6577. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6578. '/textarea',
  6579. ];
  6580. $this->assertHtml($expected, $result);
  6581. $this->Form->request->data = ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'];
  6582. $result = $this->Form->textarea('field');
  6583. $expected = [
  6584. 'textarea' => ['name' => 'field', 'rows' => 5],
  6585. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6586. '/textarea',
  6587. ];
  6588. $this->assertHtml($expected, $result);
  6589. $this->Form->request->data = [
  6590. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  6591. ];
  6592. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6593. $expected = [
  6594. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6595. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6596. '/textarea',
  6597. ];
  6598. $this->assertHtml($expected, $result);
  6599. $result = $this->Form->textarea('0.OtherModel.field');
  6600. $expected = [
  6601. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6602. '/textarea'
  6603. ];
  6604. $this->assertHtml($expected, $result);
  6605. }
  6606. /**
  6607. * testTextAreaWithStupidCharacters method
  6608. *
  6609. * Test text area with non-ascii characters.
  6610. *
  6611. * @return void
  6612. */
  6613. public function testTextAreaWithStupidCharacters()
  6614. {
  6615. $result = $this->Form->textarea('Post.content', [
  6616. 'value' => 'GREAT®',
  6617. 'rows' => '15',
  6618. 'cols' => '75'
  6619. ]);
  6620. $expected = [
  6621. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6622. 'GREAT®',
  6623. '/textarea',
  6624. ];
  6625. $this->assertHtml($expected, $result);
  6626. }
  6627. /**
  6628. * testTextAreaMaxLength method
  6629. *
  6630. * Test textareas maxlength read from schema.
  6631. *
  6632. * @return void
  6633. */
  6634. public function testTextAreaMaxLength()
  6635. {
  6636. $this->Form->create([
  6637. 'schema' => [
  6638. 'stuff' => ['type' => 'string', 'length' => 10],
  6639. ]
  6640. ]);
  6641. $result = $this->Form->control('other', ['type' => 'textarea']);
  6642. $expected = [
  6643. 'div' => ['class' => 'input textarea'],
  6644. 'label' => ['for' => 'other'],
  6645. 'Other',
  6646. '/label',
  6647. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6648. '/textarea',
  6649. '/div'
  6650. ];
  6651. $this->assertHtml($expected, $result);
  6652. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6653. $expected = [
  6654. 'div' => ['class' => 'input textarea'],
  6655. 'label' => ['for' => 'stuff'],
  6656. 'Stuff',
  6657. '/label',
  6658. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6659. '/textarea',
  6660. '/div'
  6661. ];
  6662. $this->assertHtml($expected, $result);
  6663. }
  6664. /**
  6665. * testHiddenField method
  6666. *
  6667. * Test generation of a hidden input.
  6668. *
  6669. * @return void
  6670. */
  6671. public function testHiddenField()
  6672. {
  6673. $this->article['errors'] = [
  6674. 'field' => true
  6675. ];
  6676. $this->Form->request->data['field'] = 'test';
  6677. $this->Form->create($this->article);
  6678. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6679. $expected = [
  6680. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6681. $this->assertHtml($expected, $result);
  6682. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6683. $expected = [
  6684. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6685. ];
  6686. $this->assertHtml($expected, $result);
  6687. }
  6688. /**
  6689. * testFileUploadField method
  6690. *
  6691. * Test generation of a file upload input.
  6692. *
  6693. * @return void
  6694. */
  6695. public function testFileUploadField()
  6696. {
  6697. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6698. $result = $this->Form->file('Model.upload');
  6699. $this->assertHtml($expected, $result);
  6700. $this->Form->request->data['Model']['upload'] = [
  6701. 'name' => '', 'type' => '', 'tmp_name' => '',
  6702. 'error' => 4, 'size' => 0
  6703. ];
  6704. $result = $this->Form->file('Model.upload');
  6705. $this->assertHtml($expected, $result);
  6706. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6707. $result = $this->Form->file('Model.upload');
  6708. $this->assertHtml($expected, $result);
  6709. }
  6710. /**
  6711. * testFileUploadOnOtherModel method
  6712. *
  6713. * Test File upload input on a model not used in create().
  6714. *
  6715. * @return void
  6716. */
  6717. public function testFileUploadOnOtherModel()
  6718. {
  6719. $this->Form->create($this->article, ['type' => 'file']);
  6720. $result = $this->Form->file('ValidateProfile.city');
  6721. $expected = [
  6722. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6723. ];
  6724. $this->assertHtml($expected, $result);
  6725. }
  6726. /**
  6727. * testButton method
  6728. *
  6729. * Test generation of a form button.
  6730. *
  6731. * @return void
  6732. */
  6733. public function testButton()
  6734. {
  6735. $result = $this->Form->button('Hi');
  6736. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6737. $this->assertHtml($expected, $result);
  6738. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6739. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6740. $this->assertHtml($expected, $result);
  6741. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6742. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6743. $this->assertHtml($expected, $result);
  6744. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6745. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6746. $this->assertHtml($expected, $result);
  6747. $result = $this->Form->button('No type', ['type' => false]);
  6748. $expected = ['button' => [], 'No type', '/button'];
  6749. $this->assertHtml($expected, $result);
  6750. $result = $this->Form->button('Upload Text', [
  6751. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6752. 'escape' => false
  6753. ]);
  6754. $this->assertNotRegExp('/\&039/', $result);
  6755. }
  6756. /**
  6757. * testButtonUnlockedByDefault method
  6758. *
  6759. * Test that button() makes unlocked fields by default.
  6760. *
  6761. * @return void
  6762. */
  6763. public function testButtonUnlockedByDefault()
  6764. {
  6765. $this->Form->request->params['_csrfToken'] = 'secured';
  6766. $this->Form->button('Save', ['name' => 'save']);
  6767. $this->Form->button('Clear');
  6768. $result = $this->Form->unlockField();
  6769. $this->assertEquals(['save'], $result);
  6770. }
  6771. /**
  6772. * testPostButton method
  6773. *
  6774. * @return void
  6775. */
  6776. public function testPostButton()
  6777. {
  6778. $result = $this->Form->postButton('Hi', '/controller/action');
  6779. $expected = [
  6780. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6781. 'div' => ['style' => 'display:none;'],
  6782. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6783. '/div',
  6784. 'button' => ['type' => 'submit'],
  6785. 'Hi',
  6786. '/button',
  6787. '/form'
  6788. ];
  6789. $this->assertHtml($expected, $result);
  6790. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6791. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  6792. }
  6793. /**
  6794. * testPostButtonMethodType method
  6795. *
  6796. * @return void
  6797. */
  6798. public function testPostButtonMethodType()
  6799. {
  6800. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6801. $expected = [
  6802. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6803. 'div' => ['style' => 'display:none;'],
  6804. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6805. '/div',
  6806. 'button' => ['type' => 'submit'],
  6807. 'Hi',
  6808. '/button',
  6809. '/form'
  6810. ];
  6811. $this->assertHtml($expected, $result);
  6812. }
  6813. /**
  6814. * testPostButtonFormOptions method
  6815. *
  6816. * @return void
  6817. */
  6818. public function testPostButtonFormOptions()
  6819. {
  6820. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6821. $expected = [
  6822. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6823. 'div' => ['style' => 'display:none;'],
  6824. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6825. '/div',
  6826. 'button' => ['type' => 'submit'],
  6827. 'Hi',
  6828. '/button',
  6829. '/form'
  6830. ];
  6831. $this->assertHtml($expected, $result);
  6832. }
  6833. /**
  6834. * testPostButtonNestedData method
  6835. *
  6836. * Test using postButton with N dimensional data.
  6837. *
  6838. * @return void
  6839. */
  6840. public function testPostButtonNestedData()
  6841. {
  6842. $data = [
  6843. 'one' => [
  6844. 'two' => [
  6845. 3, 4, 5
  6846. ]
  6847. ]
  6848. ];
  6849. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6850. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6851. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6852. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6853. }
  6854. /**
  6855. * testSecurePostButton method
  6856. *
  6857. * Test that postButton adds _Token fields.
  6858. *
  6859. * @return void
  6860. */
  6861. public function testSecurePostButton()
  6862. {
  6863. $this->Form->request->params['_csrfToken'] = 'testkey';
  6864. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  6865. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6866. $tokenDebug = urlencode(json_encode([
  6867. '/posts/delete/1',
  6868. [],
  6869. []
  6870. ]));
  6871. $expected = [
  6872. 'form' => [
  6873. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6874. ],
  6875. ['div' => ['style' => 'display:none;']],
  6876. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6877. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6878. '/div',
  6879. 'button' => ['type' => 'submit'],
  6880. 'Delete',
  6881. '/button',
  6882. ['div' => ['style' => 'display:none;']],
  6883. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6884. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6885. ['input' => [
  6886. 'type' => 'hidden',
  6887. 'name' => '_Token[debug]',
  6888. 'value' => $tokenDebug,
  6889. 'autocomplete' => 'off',
  6890. ]],
  6891. '/div',
  6892. '/form',
  6893. ];
  6894. $this->assertHtml($expected, $result);
  6895. }
  6896. /**
  6897. * testPostLink method
  6898. *
  6899. * @return void
  6900. */
  6901. public function testPostLink()
  6902. {
  6903. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6904. $expected = [
  6905. 'form' => [
  6906. 'method' => 'post', 'action' => '/posts/delete/1',
  6907. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6908. ],
  6909. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6910. '/form',
  6911. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6912. 'Delete',
  6913. '/a'
  6914. ];
  6915. $this->assertHtml($expected, $result);
  6916. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  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' => 'DELETE'],
  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(
  6930. 'Delete',
  6931. '/posts/delete/1',
  6932. ['target' => '_blank', 'class' => 'btn btn-danger']
  6933. );
  6934. $expected = [
  6935. 'form' => [
  6936. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6937. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6938. ],
  6939. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6940. '/form',
  6941. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6942. 'Delete',
  6943. '/a'
  6944. ];
  6945. $this->assertHtml($expected, $result);
  6946. }
  6947. /**
  6948. * testPostLinkWithConfirm method
  6949. *
  6950. * Test the confirm option for postLink().
  6951. *
  6952. * @return void
  6953. */
  6954. public function testPostLinkWithConfirm()
  6955. {
  6956. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6957. $expected = [
  6958. 'form' => [
  6959. 'method' => 'post', 'action' => '/posts/delete/1',
  6960. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6961. ],
  6962. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6963. '/form',
  6964. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  6965. 'Delete',
  6966. '/a'
  6967. ];
  6968. $this->assertHtml($expected, $result);
  6969. $result = $this->Form->postLink(
  6970. 'Delete',
  6971. '/posts/delete/1',
  6972. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  6973. );
  6974. $expected = [
  6975. 'form' => [
  6976. 'method' => 'post', 'action' => '/posts/delete/1',
  6977. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6978. ],
  6979. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6980. '/form',
  6981. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  6982. 'Delete',
  6983. '/a'
  6984. ];
  6985. $this->assertHtml($expected, $result);
  6986. }
  6987. /**
  6988. * testPostLinkWithQuery method
  6989. *
  6990. * Test postLink() with query string args.
  6991. *
  6992. * @return void
  6993. */
  6994. public function testPostLinkWithQuery()
  6995. {
  6996. $result = $this->Form->postLink(
  6997. 'Delete',
  6998. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6999. );
  7000. $expected = [
  7001. 'form' => [
  7002. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  7003. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7004. ],
  7005. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7006. '/form',
  7007. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7008. 'Delete',
  7009. '/a'
  7010. ];
  7011. $this->assertHtml($expected, $result);
  7012. }
  7013. /**
  7014. * testPostLinkWithData method
  7015. *
  7016. * Test postLink with additional data.
  7017. *
  7018. * @return void
  7019. */
  7020. public function testPostLinkWithData()
  7021. {
  7022. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  7023. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  7024. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  7025. $this->Form->create($entity);
  7026. $this->Form->end();
  7027. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  7028. $this->assertContains(
  7029. '<input type="hidden" name="name" value="show"',
  7030. $result,
  7031. 'should not contain entity data.'
  7032. );
  7033. }
  7034. /**
  7035. * testPostLinkSecurityHash method
  7036. *
  7037. * Test that security hashes for postLink include the url.
  7038. *
  7039. * @return void
  7040. */
  7041. public function testPostLinkSecurityHash()
  7042. {
  7043. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']), Security::getSalt());
  7044. $hash .= '%3Aid';
  7045. $this->Form->request->params['_Token']['key'] = 'test';
  7046. $result = $this->Form->postLink(
  7047. 'Delete',
  7048. '/posts/delete/1',
  7049. ['data' => ['id' => 1]]
  7050. );
  7051. $tokenDebug = urlencode(json_encode([
  7052. '/posts/delete/1',
  7053. [
  7054. 'id' => 1
  7055. ],
  7056. []
  7057. ]));
  7058. $expected = [
  7059. 'form' => [
  7060. 'method' => 'post', 'action' => '/posts/delete/1',
  7061. 'name', 'style' => 'display:none;'
  7062. ],
  7063. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7064. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7065. 'div' => ['style' => 'display:none;'],
  7066. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7067. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7068. ['input' => [
  7069. 'type' => 'hidden',
  7070. 'name' => '_Token[debug]',
  7071. 'value' => $tokenDebug,
  7072. 'autocomplete' => 'off',
  7073. ]],
  7074. '/div',
  7075. '/form',
  7076. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7077. 'Delete',
  7078. '/a'
  7079. ];
  7080. $this->assertHtml($expected, $result);
  7081. }
  7082. /**
  7083. * testPostLinkSecurityHashBlockMode method
  7084. *
  7085. * Test that postLink doesn't modify the fields in the containing form.
  7086. *
  7087. * postLink() calls inside open forms should not modify the field list
  7088. * for the form.
  7089. *
  7090. * @return void
  7091. */
  7092. public function testPostLinkSecurityHashBlockMode()
  7093. {
  7094. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]), Security::getSalt());
  7095. $hash .= '%3A';
  7096. $this->Form->request->params['_Token']['key'] = 'test';
  7097. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  7098. $this->Form->control('title');
  7099. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7100. $result = $this->View->fetch('postLink');
  7101. $this->assertEquals(['title'], $this->Form->fields);
  7102. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  7103. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  7104. }
  7105. /**
  7106. * testPostLinkSecurityHashNoDebugMode method
  7107. *
  7108. * Test that security does not include debug token if debug is false.
  7109. *
  7110. * @return void
  7111. */
  7112. public function testPostLinkSecurityHashNoDebugMode()
  7113. {
  7114. Configure::write('debug', false);
  7115. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']), Security::getSalt());
  7116. $hash .= '%3Aid';
  7117. $this->Form->request->params['_Token']['key'] = 'test';
  7118. $result = $this->Form->postLink(
  7119. 'Delete',
  7120. '/posts/delete/1',
  7121. ['data' => ['id' => 1]]
  7122. );
  7123. $expected = [
  7124. 'form' => [
  7125. 'method' => 'post', 'action' => '/posts/delete/1',
  7126. 'name', 'style' => 'display:none;'
  7127. ],
  7128. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7129. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7130. 'div' => ['style' => 'display:none;'],
  7131. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7132. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7133. '/div',
  7134. '/form',
  7135. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7136. 'Delete',
  7137. '/a'
  7138. ];
  7139. $this->assertHtml($expected, $result);
  7140. }
  7141. /**
  7142. * testPostLinkNestedData method
  7143. *
  7144. * Test using postLink with N dimensional data.
  7145. *
  7146. * @return void
  7147. */
  7148. public function testPostLinkNestedData()
  7149. {
  7150. $data = [
  7151. 'one' => [
  7152. 'two' => [
  7153. 3, 4, 5
  7154. ]
  7155. ]
  7156. ];
  7157. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7158. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7159. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7160. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7161. }
  7162. /**
  7163. * testPostLinkAfterGetForm method
  7164. *
  7165. * Test creating postLinks after a GET form.
  7166. *
  7167. * @return void
  7168. */
  7169. public function testPostLinkAfterGetForm()
  7170. {
  7171. $this->Form->request->params['_csrfToken'] = 'testkey';
  7172. $this->Form->request->params['_Token'] = 'val';
  7173. $this->Form->create($this->article, ['type' => 'get']);
  7174. $this->Form->end();
  7175. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7176. $tokenDebug = urlencode(json_encode([
  7177. '/posts/delete/1',
  7178. [],
  7179. []
  7180. ]));
  7181. $expected = [
  7182. 'form' => [
  7183. 'method' => 'post', 'action' => '/posts/delete/1',
  7184. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7185. ],
  7186. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7187. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7188. 'div' => ['style' => 'display:none;'],
  7189. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7190. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7191. ['input' => [
  7192. 'type' => 'hidden', 'name' => '_Token[debug]',
  7193. 'value' => $tokenDebug,
  7194. 'autocomplete' => 'off'
  7195. ]],
  7196. '/div',
  7197. '/form',
  7198. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7199. 'Delete',
  7200. '/a'
  7201. ];
  7202. $this->assertHtml($expected, $result);
  7203. }
  7204. /**
  7205. * testPostLinkFormBuffer method
  7206. *
  7207. * Test that postLink adds form tags to view block.
  7208. *
  7209. * @return void
  7210. */
  7211. public function testPostLinkFormBuffer()
  7212. {
  7213. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7214. $expected = [
  7215. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7216. 'Delete',
  7217. '/a'
  7218. ];
  7219. $this->assertHtml($expected, $result);
  7220. $result = $this->View->fetch('postLink');
  7221. $expected = [
  7222. 'form' => [
  7223. 'method' => 'post', 'action' => '/posts/delete/1',
  7224. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7225. ],
  7226. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7227. '/form'
  7228. ];
  7229. $this->assertHtml($expected, $result);
  7230. $result = $this->Form->postLink(
  7231. 'Delete',
  7232. '/posts/delete/2',
  7233. ['block' => true, 'method' => 'DELETE']
  7234. );
  7235. $expected = [
  7236. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7237. 'Delete',
  7238. '/a'
  7239. ];
  7240. $this->assertHtml($expected, $result);
  7241. $result = $this->View->fetch('postLink');
  7242. $expected = [
  7243. 'form' => [
  7244. 'method' => 'post', 'action' => '/posts/delete/1',
  7245. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7246. ],
  7247. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7248. '/form',
  7249. [
  7250. 'form' => [
  7251. 'method' => 'post', 'action' => '/posts/delete/2',
  7252. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7253. ],
  7254. ],
  7255. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7256. '/form'
  7257. ];
  7258. $this->assertHtml($expected, $result);
  7259. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7260. $expected = [
  7261. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7262. 'Delete',
  7263. '/a'
  7264. ];
  7265. $this->assertHtml($expected, $result);
  7266. $result = $this->View->fetch('foobar');
  7267. $expected = [
  7268. 'form' => [
  7269. 'method' => 'post', 'action' => '/posts/delete/1',
  7270. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7271. ],
  7272. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7273. '/form'
  7274. ];
  7275. $this->assertHtml($expected, $result);
  7276. }
  7277. /**
  7278. * testSubmitButton method
  7279. *
  7280. * @return void
  7281. */
  7282. public function testSubmitButton()
  7283. {
  7284. $result = $this->Form->submit('');
  7285. $expected = [
  7286. 'div' => ['class' => 'submit'],
  7287. 'input' => ['type' => 'submit', 'value' => ''],
  7288. '/div'
  7289. ];
  7290. $this->assertHtml($expected, $result);
  7291. $result = $this->Form->submit('Test Submit');
  7292. $expected = [
  7293. 'div' => ['class' => 'submit'],
  7294. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7295. '/div'
  7296. ];
  7297. $this->assertHtml($expected, $result);
  7298. $result = $this->Form->submit('Next >');
  7299. $expected = [
  7300. 'div' => ['class' => 'submit'],
  7301. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7302. '/div'
  7303. ];
  7304. $this->assertHtml($expected, $result);
  7305. $result = $this->Form->submit('Next >', ['escape' => false]);
  7306. $expected = [
  7307. 'div' => ['class' => 'submit'],
  7308. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7309. '/div'
  7310. ];
  7311. $this->assertHtml($expected, $result);
  7312. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7313. $expected = [
  7314. 'div' => ['class' => 'submit'],
  7315. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7316. '/div'
  7317. ];
  7318. $this->assertHtml($expected, $result);
  7319. }
  7320. /**
  7321. * testSubmitImage method
  7322. *
  7323. * Test image submit types.
  7324. *
  7325. * @return void
  7326. */
  7327. public function testSubmitImage()
  7328. {
  7329. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7330. $expected = [
  7331. 'div' => ['class' => 'submit'],
  7332. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7333. '/div'
  7334. ];
  7335. $this->assertHtml($expected, $result);
  7336. $result = $this->Form->submit('/relative/cake.power.gif');
  7337. $expected = [
  7338. 'div' => ['class' => 'submit'],
  7339. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7340. '/div'
  7341. ];
  7342. $this->assertHtml($expected, $result);
  7343. $result = $this->Form->submit('cake.power.gif');
  7344. $expected = [
  7345. 'div' => ['class' => 'submit'],
  7346. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7347. '/div'
  7348. ];
  7349. $this->assertHtml($expected, $result);
  7350. $result = $this->Form->submit('Not.an.image');
  7351. $expected = [
  7352. 'div' => ['class' => 'submit'],
  7353. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7354. '/div'
  7355. ];
  7356. $this->assertHtml($expected, $result);
  7357. }
  7358. /**
  7359. * testSubmitUnlockedByDefault method
  7360. *
  7361. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7362. * be submitted at a time.
  7363. *
  7364. * @return void
  7365. */
  7366. public function testSubmitUnlockedByDefault()
  7367. {
  7368. $this->Form->request->params['_Token'] = 'secured';
  7369. $this->Form->submit('Go go');
  7370. $this->Form->submit('Save', ['name' => 'save']);
  7371. $result = $this->Form->unlockField();
  7372. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7373. }
  7374. /**
  7375. * testSubmitImageTimestamp method
  7376. *
  7377. * Test submit image with timestamps.
  7378. *
  7379. * @return void
  7380. */
  7381. public function testSubmitImageTimestamp()
  7382. {
  7383. Configure::write('Asset.timestamp', 'force');
  7384. $result = $this->Form->submit('cake.power.gif');
  7385. $expected = [
  7386. 'div' => ['class' => 'submit'],
  7387. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7388. '/div'
  7389. ];
  7390. $this->assertHtml($expected, $result);
  7391. }
  7392. /**
  7393. * testDateTimeWithGetForms method
  7394. *
  7395. * Test that datetime() works with GET style forms.
  7396. *
  7397. * @return void
  7398. */
  7399. public function testDateTimeWithGetForms()
  7400. {
  7401. extract($this->dateRegex);
  7402. $this->Form->create($this->article, ['type' => 'get']);
  7403. $result = $this->Form->datetime('created');
  7404. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7405. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7406. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7407. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7408. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7409. }
  7410. /**
  7411. * testForMagicControlNonExistingNorValidated method
  7412. *
  7413. * @return void
  7414. */
  7415. public function testForMagicControlNonExistingNorValidated()
  7416. {
  7417. $this->Form->create($this->article);
  7418. $this->Form->templates(['inputContainer' => '{{content}}']);
  7419. $result = $this->Form->control('non_existing_nor_validated');
  7420. $expected = [
  7421. 'label' => ['for' => 'non-existing-nor-validated'],
  7422. 'Non Existing Nor Validated',
  7423. '/label',
  7424. 'input' => [
  7425. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7426. 'id' => 'non-existing-nor-validated'
  7427. ]
  7428. ];
  7429. $this->assertHtml($expected, $result);
  7430. $result = $this->Form->control('non_existing_nor_validated', [
  7431. 'val' => 'my value'
  7432. ]);
  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. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7440. ]
  7441. ];
  7442. $this->assertHtml($expected, $result);
  7443. $this->Form->request->data = ['non_existing_nor_validated' => 'CakePHP magic'];
  7444. $result = $this->Form->control('non_existing_nor_validated');
  7445. $expected = [
  7446. 'label' => ['for' => 'non-existing-nor-validated'],
  7447. 'Non Existing Nor Validated',
  7448. '/label',
  7449. 'input' => [
  7450. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7451. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7452. ]
  7453. ];
  7454. $this->assertHtml($expected, $result);
  7455. }
  7456. /**
  7457. * testFormMagicControlLabel method
  7458. *
  7459. * @return void
  7460. */
  7461. public function testFormMagicControlLabel()
  7462. {
  7463. TableRegistry::get('Contacts', [
  7464. 'className' => __NAMESPACE__ . '\ContactsTable'
  7465. ]);
  7466. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7467. $this->Form->templates(['inputContainer' => '{{content}}']);
  7468. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7469. $expected = [
  7470. 'label' => ['for' => 'contacts-name'],
  7471. 'My label',
  7472. '/label',
  7473. 'input' => [
  7474. 'type' => 'text', 'name' => 'Contacts[name]',
  7475. 'id' => 'contacts-name', 'maxlength' => '255'
  7476. ]
  7477. ];
  7478. $this->assertHtml($expected, $result);
  7479. $result = $this->Form->control('name', [
  7480. 'label' => ['class' => 'mandatory']
  7481. ]);
  7482. $expected = [
  7483. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7484. 'Name',
  7485. '/label',
  7486. 'input' => [
  7487. 'type' => 'text', 'name' => 'name',
  7488. 'id' => 'name', 'maxlength' => '255'
  7489. ]
  7490. ];
  7491. $this->assertHtml($expected, $result);
  7492. $result = $this->Form->control('name', [
  7493. 'div' => false,
  7494. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7495. ]);
  7496. $expected = [
  7497. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7498. 'My label',
  7499. '/label',
  7500. 'input' => [
  7501. 'type' => 'text', 'name' => 'name',
  7502. 'id' => 'name', 'maxlength' => '255'
  7503. ]
  7504. ];
  7505. $this->assertHtml($expected, $result);
  7506. $result = $this->Form->control('Contact.name', [
  7507. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7508. ]);
  7509. $expected = [
  7510. 'label' => ['for' => 'my_id'],
  7511. 'Name',
  7512. '/label',
  7513. 'input' => [
  7514. 'type' => 'text', 'name' => 'Contact[name]',
  7515. 'id' => 'my_id', 'maxlength' => '255'
  7516. ]
  7517. ];
  7518. $this->assertHtml($expected, $result);
  7519. $result = $this->Form->control('1.id');
  7520. $expected = ['input' => [
  7521. 'type' => 'hidden', 'name' => '1[id]',
  7522. 'id' => '1-id'
  7523. ]];
  7524. $this->assertHtml($expected, $result);
  7525. $result = $this->Form->control('1.name');
  7526. $expected = [
  7527. 'label' => ['for' => '1-name'],
  7528. 'Name',
  7529. '/label',
  7530. 'input' => [
  7531. 'type' => 'text', 'name' => '1[name]',
  7532. 'id' => '1-name', 'maxlength' => '255'
  7533. ]
  7534. ];
  7535. $this->assertHtml($expected, $result);
  7536. }
  7537. /**
  7538. * testFormEnd method
  7539. *
  7540. * @return void
  7541. */
  7542. public function testFormEnd()
  7543. {
  7544. $this->assertEquals('</form>', $this->Form->end());
  7545. }
  7546. /**
  7547. * testMultiRecordForm method
  7548. *
  7549. * Test the generation of fields for a multi record form.
  7550. *
  7551. * @return void
  7552. */
  7553. public function testMultiRecordForm()
  7554. {
  7555. $this->loadFixtures('Articles', 'Comments');
  7556. $articles = TableRegistry::get('Articles');
  7557. $articles->hasMany('Comments');
  7558. $comment = new Entity(['comment' => 'Value']);
  7559. $article = new Article(['comments' => [$comment]]);
  7560. $this->Form->create([$article]);
  7561. $result = $this->Form->control('0.comments.1.comment');
  7562. //@codingStandardsIgnoreStart
  7563. $expected = [
  7564. 'div' => ['class' => 'input textarea'],
  7565. 'label' => ['for' => '0-comments-1-comment'],
  7566. 'Comment',
  7567. '/label',
  7568. 'textarea' => [
  7569. 'name',
  7570. 'id' => '0-comments-1-comment',
  7571. 'rows' => 5
  7572. ],
  7573. '/textarea',
  7574. '/div'
  7575. ];
  7576. //@codingStandardsIgnoreEnd
  7577. $this->assertHtml($expected, $result);
  7578. $result = $this->Form->control('0.comments.0.comment');
  7579. //@codingStandardsIgnoreStart
  7580. $expected = [
  7581. 'div' => ['class' => 'input textarea'],
  7582. 'label' => ['for' => '0-comments-0-comment'],
  7583. 'Comment',
  7584. '/label',
  7585. 'textarea' => [
  7586. 'name',
  7587. 'id' => '0-comments-0-comment',
  7588. 'rows' => 5
  7589. ],
  7590. 'Value',
  7591. '/textarea',
  7592. '/div'
  7593. ];
  7594. //@codingStandardsIgnoreEnd
  7595. $this->assertHtml($expected, $result);
  7596. $comment->errors('comment', ['Not valid']);
  7597. $result = $this->Form->control('0.comments.0.comment');
  7598. //@codingStandardsIgnoreStart
  7599. $expected = [
  7600. 'div' => ['class' => 'input textarea error'],
  7601. 'label' => ['for' => '0-comments-0-comment'],
  7602. 'Comment',
  7603. '/label',
  7604. 'textarea' => [
  7605. 'name',
  7606. 'class' => 'form-error',
  7607. 'id' => '0-comments-0-comment',
  7608. 'rows' => 5
  7609. ],
  7610. 'Value',
  7611. '/textarea',
  7612. ['div' => ['class' => 'error-message']],
  7613. 'Not valid',
  7614. '/div',
  7615. '/div'
  7616. ];
  7617. //@codingStandardsIgnoreEnd
  7618. $this->assertHtml($expected, $result);
  7619. TableRegistry::get('Comments')
  7620. ->validator('default')
  7621. ->allowEmpty('comment', false);
  7622. $result = $this->Form->control('0.comments.1.comment');
  7623. //@codingStandardsIgnoreStart
  7624. $expected = [
  7625. 'div' => ['class' => 'input textarea required'],
  7626. 'label' => ['for' => '0-comments-1-comment'],
  7627. 'Comment',
  7628. '/label',
  7629. 'textarea' => [
  7630. 'name',
  7631. 'required' => 'required',
  7632. 'id' => '0-comments-1-comment',
  7633. 'rows' => 5
  7634. ],
  7635. '/textarea',
  7636. '/div'
  7637. ];
  7638. //@codingStandardsIgnoreEnd
  7639. $this->assertHtml($expected, $result);
  7640. }
  7641. /**
  7642. * testHtml5Controls method
  7643. *
  7644. * Test that some html5 inputs + FormHelper::__call() work.
  7645. *
  7646. * @return void
  7647. */
  7648. public function testHtml5Controls()
  7649. {
  7650. $result = $this->Form->email('User.email');
  7651. $expected = [
  7652. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7653. ];
  7654. $this->assertHtml($expected, $result);
  7655. $result = $this->Form->search('User.query');
  7656. $expected = [
  7657. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7658. ];
  7659. $this->assertHtml($expected, $result);
  7660. $result = $this->Form->search('User.query', ['value' => 'test']);
  7661. $expected = [
  7662. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7663. ];
  7664. $this->assertHtml($expected, $result);
  7665. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7666. $expected = [
  7667. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7668. ];
  7669. $this->assertHtml($expected, $result);
  7670. }
  7671. /**
  7672. * testHtml5ControlWithControl method
  7673. *
  7674. * Test accessing html5 inputs through control().
  7675. *
  7676. * @return void
  7677. */
  7678. public function testHtml5ControlWithControl()
  7679. {
  7680. $this->Form->create();
  7681. $this->Form->templates(['inputContainer' => '{{content}}']);
  7682. $result = $this->Form->control('website', [
  7683. 'type' => 'url',
  7684. 'val' => 'http://domain.tld',
  7685. 'label' => false
  7686. ]);
  7687. $expected = [
  7688. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7689. ];
  7690. $this->assertHtml($expected, $result);
  7691. }
  7692. /**
  7693. * testHtml5ControlException method
  7694. *
  7695. * Test errors when field name is missing.
  7696. *
  7697. * @expectedException \Cake\Core\Exception\Exception
  7698. * @return void
  7699. */
  7700. public function testHtml5ControlException()
  7701. {
  7702. $this->Form->email();
  7703. }
  7704. /**
  7705. * testRequiredAttribute method
  7706. *
  7707. * Tests that formhelper sets required attributes.
  7708. *
  7709. * @return void
  7710. */
  7711. public function testRequiredAttribute()
  7712. {
  7713. $this->article['required'] = [
  7714. 'title' => true,
  7715. 'body' => false,
  7716. ];
  7717. $this->Form->create($this->article);
  7718. $result = $this->Form->control('title');
  7719. $expected = [
  7720. 'div' => ['class' => 'input text required'],
  7721. 'label' => ['for' => 'title'],
  7722. 'Title',
  7723. '/label',
  7724. 'input' => [
  7725. 'type' => 'text',
  7726. 'name' => 'title',
  7727. 'id' => 'title',
  7728. 'required' => 'required',
  7729. ],
  7730. '/div'
  7731. ];
  7732. $this->assertHtml($expected, $result);
  7733. $result = $this->Form->control('title', ['required' => false]);
  7734. $this->assertNotContains('required', $result);
  7735. $result = $this->Form->control('body');
  7736. $expected = [
  7737. 'div' => ['class' => 'input text'],
  7738. 'label' => ['for' => 'body'],
  7739. 'Body',
  7740. '/label',
  7741. 'input' => [
  7742. 'type' => 'text',
  7743. 'name' => 'body',
  7744. 'id' => 'body',
  7745. ],
  7746. '/div'
  7747. ];
  7748. $this->assertHtml($expected, $result);
  7749. $result = $this->Form->control('body', ['required' => true]);
  7750. $this->assertContains('required', $result);
  7751. }
  7752. /**
  7753. * testControlsNotNested method
  7754. *
  7755. * Tests that it is possible to put inputs outside of the label.
  7756. *
  7757. * @return void
  7758. */
  7759. public function testControlsNotNested()
  7760. {
  7761. $this->Form->templates([
  7762. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7763. 'formGroup' => '{{input}}{{label}}',
  7764. ]);
  7765. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7766. $expected = [
  7767. 'div' => ['class' => 'input checkbox'],
  7768. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7769. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7770. 'label' => ['for' => 'foo'],
  7771. 'Foo',
  7772. '/label',
  7773. '/div'
  7774. ];
  7775. $this->assertHtml($expected, $result);
  7776. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7777. $expected = [
  7778. 'div' => ['class' => 'input checkbox'],
  7779. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7780. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7781. '/div'
  7782. ];
  7783. $this->assertHtml($expected, $result);
  7784. $result = $this->Form->control('confirm', [
  7785. 'type' => 'radio',
  7786. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7787. ]);
  7788. $expected = [
  7789. 'div' => ['class' => 'input radio'],
  7790. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7791. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7792. ['label' => ['for' => 'confirm-y']],
  7793. 'Yes',
  7794. '/label',
  7795. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7796. ['label' => ['for' => 'confirm-n']],
  7797. 'No',
  7798. '/label',
  7799. '<label',
  7800. 'Confirm',
  7801. '/label',
  7802. '/div',
  7803. ];
  7804. $this->assertHtml($expected, $result);
  7805. $result = $this->Form->select('category', ['1', '2'], [
  7806. 'multiple' => 'checkbox',
  7807. 'name' => 'fish',
  7808. ]);
  7809. $expected = [
  7810. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7811. ['div' => ['class' => 'checkbox']],
  7812. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7813. ['label' => ['for' => 'fish-0']],
  7814. '1',
  7815. '/label',
  7816. '/div',
  7817. ['div' => ['class' => 'checkbox']],
  7818. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7819. ['label' => ['for' => 'fish-1']],
  7820. '2',
  7821. '/label',
  7822. '/div'
  7823. ];
  7824. $this->assertHtml($expected, $result);
  7825. }
  7826. /**
  7827. * testControlContainerTemplates method
  7828. *
  7829. * Test that *Container templates are used by input.
  7830. *
  7831. * @return void
  7832. */
  7833. public function testControlContainerTemplates()
  7834. {
  7835. $this->Form->templates([
  7836. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7837. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7838. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7839. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7840. ]);
  7841. $this->article['errors'] = [
  7842. 'Article' => ['published' => 'error message']
  7843. ];
  7844. $this->Form->create($this->article);
  7845. $result = $this->Form->control('accept', [
  7846. 'type' => 'checkbox'
  7847. ]);
  7848. $expected = [
  7849. 'div' => ['class' => 'check'],
  7850. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7851. 'label' => ['for' => 'accept'],
  7852. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7853. 'Accept',
  7854. '/label',
  7855. '/div'
  7856. ];
  7857. $this->assertHtml($expected, $result);
  7858. $result = $this->Form->control('accept', [
  7859. 'type' => 'radio',
  7860. 'options' => ['Y', 'N']
  7861. ]);
  7862. $this->assertContains('<div class="rad">', $result);
  7863. $result = $this->Form->control('Article.published', [
  7864. 'type' => 'radio',
  7865. 'options' => ['Y', 'N']
  7866. ]);
  7867. $this->assertContains('<div class="rad err">', $result);
  7868. $result = $this->Form->control('Article.created', [
  7869. 'type' => 'datetime',
  7870. ]);
  7871. $this->assertContains('<div class="dt">', $result);
  7872. }
  7873. /**
  7874. * testFormGroupTemplates method
  7875. *
  7876. * Test that *Container templates are used by input.
  7877. *
  7878. * @return void
  7879. */
  7880. public function testFormGroupTemplates()
  7881. {
  7882. $this->Form->templates([
  7883. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7884. ]);
  7885. $this->Form->create($this->article);
  7886. $result = $this->Form->control('accept', [
  7887. 'type' => 'radio',
  7888. 'options' => ['Y', 'N']
  7889. ]);
  7890. $this->assertContains('<div class="radio">', $result);
  7891. }
  7892. /**
  7893. * testResetTemplates method
  7894. *
  7895. * Test resetting templates.
  7896. *
  7897. * @return void
  7898. */
  7899. public function testResetTemplates()
  7900. {
  7901. $this->Form->templates(['input' => '<input/>']);
  7902. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  7903. $this->Form->resetTemplates();
  7904. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7905. }
  7906. /**
  7907. * testContext method
  7908. *
  7909. * Test the context method.
  7910. *
  7911. * @return void
  7912. */
  7913. public function testContext()
  7914. {
  7915. $result = $this->Form->context();
  7916. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7917. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7918. $this->assertSame($mock, $this->Form->context($mock));
  7919. $this->assertSame($mock, $this->Form->context());
  7920. }
  7921. /**
  7922. * testAutoDomId method
  7923. *
  7924. * @return void
  7925. */
  7926. public function testAutoDomId()
  7927. {
  7928. $result = $this->Form->text('field', ['id' => true]);
  7929. $expected = [
  7930. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7931. ];
  7932. $this->assertHtml($expected, $result);
  7933. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7934. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7935. $expected = [
  7936. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7937. ['label' => ['for' => 'field-0']],
  7938. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7939. 'option A',
  7940. '/label',
  7941. ['label' => ['for' => 'field-1']],
  7942. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7943. 'option B',
  7944. '/label',
  7945. ];
  7946. $this->assertHtml($expected, $result);
  7947. $result = $this->Form->select(
  7948. 'multi_field',
  7949. ['first', 'second'],
  7950. ['multiple' => 'checkbox', 'id' => true]
  7951. );
  7952. $expected = [
  7953. 'input' => [
  7954. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  7955. ],
  7956. ['div' => ['class' => 'checkbox']],
  7957. ['label' => ['for' => 'multi-field-0']],
  7958. ['input' => [
  7959. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7960. 'value' => '0', 'id' => 'multi-field-0'
  7961. ]],
  7962. 'first',
  7963. '/label',
  7964. '/div',
  7965. ['div' => ['class' => 'checkbox']],
  7966. ['label' => ['for' => 'multi-field-1']],
  7967. ['input' => [
  7968. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7969. 'value' => '1', 'id' => 'multi-field-1'
  7970. ]],
  7971. 'second',
  7972. '/label',
  7973. '/div',
  7974. ];
  7975. $this->assertHtml($expected, $result);
  7976. }
  7977. /**
  7978. * Test the basic setters and getters for value sources
  7979. *
  7980. * @return void
  7981. */
  7982. public function testFormValueSourcesSettersGetters()
  7983. {
  7984. $expected = ['context'];
  7985. $result = $this->Form->getValueSources();
  7986. $this->assertEquals($expected, $result);
  7987. $expected = null;
  7988. $result = $this->Form->getSourceValue('id');
  7989. $this->assertEquals($expected, $result);
  7990. $expected = ['query', 'data', 'context'];
  7991. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7992. $result = $this->Form->getValueSources();
  7993. $this->assertEquals($expected, $result);
  7994. $this->Form->request->data['id'] = '1';
  7995. $this->Form->request->query['id'] = '2';
  7996. $this->Form->setValueSources(['context']);
  7997. $expected = '1';
  7998. $result = $this->Form->getSourceValue('id');
  7999. $this->assertEquals($expected, $result);
  8000. $this->Form->setValueSources('query');
  8001. $expected = ['query'];
  8002. $result = $this->Form->getValueSources();
  8003. $this->assertEquals($expected, $result);
  8004. $expected = '2';
  8005. $result = $this->Form->getSourceValue('id');
  8006. $this->assertEquals($expected, $result);
  8007. $this->Form->setValueSources(['data']);
  8008. $expected = '1';
  8009. $result = $this->Form->getSourceValue('id');
  8010. $this->assertEquals($expected, $result);
  8011. $this->Form->setValueSources(['query', 'data']);
  8012. $expected = '2';
  8013. $result = $this->Form->getSourceValue('id');
  8014. $this->assertEquals($expected, $result);
  8015. }
  8016. /**
  8017. * Tests the different input rendering values based on sources values switching
  8018. *
  8019. * @return void
  8020. */
  8021. public function testFormValueSourcesSingleSwitchRendering()
  8022. {
  8023. $this->loadFixtures('Articles');
  8024. $articles = TableRegistry::get('Articles');
  8025. $article = new Article();
  8026. $articles->patchEntity($article, ['id' => '3']);
  8027. $this->Form->create($article);
  8028. $this->Form->setValueSources(['context']);
  8029. $result = $this->Form->control('id');
  8030. $expected = [
  8031. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8032. ];
  8033. $this->assertHtml($expected, $result);
  8034. $this->Form->request->query['id'] = '5';
  8035. $this->Form->setValueSources(['query']);
  8036. $result = $this->Form->control('id');
  8037. $expected = [
  8038. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8039. ];
  8040. $this->assertHtml($expected, $result);
  8041. $this->Form->request->query['id'] = '5a';
  8042. $this->Form->request->data['id'] = '5b';
  8043. $this->Form->setValueSources(['context']);
  8044. $this->Form->create($article);
  8045. $result = $this->Form->control('id');
  8046. $expected = [
  8047. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8048. ];
  8049. $this->assertHtml($expected, $result);
  8050. $this->Form->setValueSources(['data']);
  8051. $this->Form->create($article);
  8052. $result = $this->Form->control('id');
  8053. $expected = [
  8054. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8055. ];
  8056. $this->assertHtml($expected, $result);
  8057. $this->Form->setValueSources(['query']);
  8058. $result = $this->Form->control('id');
  8059. $expected = [
  8060. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  8061. ];
  8062. $this->assertHtml($expected, $result);
  8063. }
  8064. /**
  8065. * Tests the different input rendering values based on sources values switching while supplying
  8066. * an entity (base context) and multiple sources (such as data, query)
  8067. *
  8068. * @return void
  8069. */
  8070. public function testFormValueSourcesListSwitchRendering()
  8071. {
  8072. $this->loadFixtures('Articles');
  8073. $articles = TableRegistry::get('Articles');
  8074. $article = new Article();
  8075. $articles->patchEntity($article, ['id' => '3']);
  8076. $this->Form->request->query['id'] = '9';
  8077. $this->Form->create($article);
  8078. $this->Form->setValueSources(['context', 'query']);
  8079. $result = $this->Form->control('id');
  8080. $expected = [
  8081. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8082. ];
  8083. $this->assertHtml($expected, $result);
  8084. $this->Form->setValueSources(['query', 'context']);
  8085. $result = $this->Form->control('id');
  8086. $expected = [
  8087. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8088. ];
  8089. $this->assertHtml($expected, $result);
  8090. $this->Form->setValueSources(['data', 'query', 'context']);
  8091. $result = $this->Form->control('id');
  8092. $expected = [
  8093. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8094. ];
  8095. $this->assertHtml($expected, $result);
  8096. $this->Form->request->data['id'] = '8';
  8097. $this->Form->request->query['id'] = '9';
  8098. $this->Form->setValueSources(['data', 'query', 'context']);
  8099. $result = $this->Form->control('id');
  8100. $expected = [
  8101. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  8102. ];
  8103. $this->assertHtml($expected, $result);
  8104. }
  8105. /**
  8106. * Test the different form input renderings based on values sources switchings through form options
  8107. *
  8108. * @return void
  8109. */
  8110. public function testFormValueSourcesSwitchViaOptionsRendering()
  8111. {
  8112. $this->loadFixtures('Articles');
  8113. $articles = TableRegistry::get('Articles');
  8114. $article = new Article();
  8115. $articles->patchEntity($article, ['id' => '3']);
  8116. $this->Form->request->data['id'] = '4';
  8117. $this->Form->request->query['id'] = '5';
  8118. $this->Form->create($article, ['valueSources' => 'query']);
  8119. $result = $this->Form->control('id');
  8120. $expected = [
  8121. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8122. ];
  8123. $this->assertHtml($expected, $result);
  8124. $result = $this->Form->getSourceValue('id');
  8125. $this->assertEquals('5', $result);
  8126. $this->Form->setValueSources(['context']);
  8127. $this->Form->create($article, ['valueSources' => 'query']);
  8128. $result = $this->Form->control('id');
  8129. $expected = [
  8130. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8131. ];
  8132. $this->assertHtml($expected, $result);
  8133. $result = $this->Form->getSourceValue('id');
  8134. $this->assertEquals('5', $result);
  8135. $this->Form->setValueSources(['query']);
  8136. $this->Form->create($article, ['valueSources' => 'data']);
  8137. $result = $this->Form->control('id');
  8138. $expected = [
  8139. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8140. ];
  8141. $this->assertHtml($expected, $result);
  8142. $result = $this->Form->getSourceValue('id');
  8143. $this->assertEquals('4', $result);
  8144. $this->Form->setValueSources(['query']);
  8145. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8146. $result = $this->Form->control('id');
  8147. $expected = [
  8148. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8149. ];
  8150. $this->assertHtml($expected, $result);
  8151. $result = $this->Form->getSourceValue('id');
  8152. $this->assertEquals('4', $result);
  8153. }
  8154. /**
  8155. * Test the different form input renderings based on values sources switchings through form options
  8156. *
  8157. * @return void
  8158. */
  8159. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8160. {
  8161. $this->loadFixtures('Articles');
  8162. $articles = TableRegistry::get('Articles');
  8163. $article = new Article();
  8164. $articles->patchEntity($article, ['id' => '3']);
  8165. $this->Form->request->data['id'] = '10';
  8166. $this->Form->request->query['id'] = '11';
  8167. $this->Form->setValueSources(['context'])
  8168. ->create($article, ['valueSources' => ['query', 'data']]);
  8169. $result = $this->Form->control('id');
  8170. $expected = [
  8171. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8172. ];
  8173. $this->assertHtml($expected, $result);
  8174. $result = $this->Form->getSourceValue('id');
  8175. $this->assertEquals('11', $result);
  8176. unset($this->Form->request->query['id']);
  8177. $this->Form->setValueSources(['context'])
  8178. ->create($article, ['valueSources' => ['query', 'data']]);
  8179. $result = $this->Form->control('id');
  8180. $expected = [
  8181. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8182. ];
  8183. $this->assertHtml($expected, $result);
  8184. $result = $this->Form->getSourceValue('id');
  8185. $this->assertEquals('10', $result);
  8186. }
  8187. /**
  8188. * Test the different form values sources resetting through From::end();
  8189. *
  8190. * @return void
  8191. */
  8192. public function testFormValueSourcesResetViaEnd()
  8193. {
  8194. $expected = ['context'];
  8195. $result = $this->Form->getValueSources();
  8196. $this->assertEquals($expected, $result);
  8197. $expected = ['query', 'context', 'data'];
  8198. $this->Form->setValueSources(['query', 'context', 'data']);
  8199. $result = $this->Form->getValueSources();
  8200. $this->assertEquals($expected, $result);
  8201. $this->Form->create();
  8202. $result = $this->Form->getValueSources();
  8203. $this->assertEquals($expected, $result);
  8204. $this->Form->end();
  8205. $result = $this->Form->getValueSources();
  8206. $this->assertEquals(['context'], $result);
  8207. }
  8208. /**
  8209. * Test sources values defaults handling
  8210. *
  8211. * @return void
  8212. */
  8213. public function testFormValueSourcesDefaults()
  8214. {
  8215. $this->Form->request->query['password'] = 'open Sesame';
  8216. $this->Form->create();
  8217. $result = $this->Form->password('password');
  8218. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8219. $this->assertHtml($expected, $result);
  8220. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8221. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8222. $this->assertHtml($expected, $result);
  8223. $this->Form->setValueSources('query');
  8224. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8225. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8226. $this->assertHtml($expected, $result);
  8227. $this->Form->setValueSources('data');
  8228. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8229. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8230. $this->assertHtml($expected, $result);
  8231. }
  8232. /**
  8233. * Test sources values schema defaults handling
  8234. *
  8235. * @return void
  8236. */
  8237. public function testSourcesValueDoesntExistPassThrough()
  8238. {
  8239. $this->Form->request->query['category'] = 'sesame-cookies';
  8240. $articles = TableRegistry::get('Articles');
  8241. $entity = $articles->newEntity();
  8242. $this->Form->create($entity);
  8243. $this->Form->setValueSources(['query', 'context']);
  8244. $result = $this->Form->getSourceValue('category');
  8245. $this->assertEquals('sesame-cookies', $result);
  8246. $this->Form->setValueSources(['context', 'query']);
  8247. $result = $this->Form->getSourceValue('category');
  8248. $this->assertEquals('sesame-cookies', $result);
  8249. }
  8250. /**
  8251. * testNestedLabelInput method
  8252. *
  8253. * Test the `nestedInput` parameter
  8254. *
  8255. * @return void
  8256. */
  8257. public function testNestedLabelInput()
  8258. {
  8259. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8260. $expected = [
  8261. 'div' => ['class' => 'input text'],
  8262. 'label' => ['for' => 'foo'],
  8263. ['input' => [
  8264. 'type' => 'text',
  8265. 'name' => 'foo',
  8266. 'id' => 'foo'
  8267. ]],
  8268. 'Foo',
  8269. '/label',
  8270. '/div'
  8271. ];
  8272. $this->assertHtml($expected, $result);
  8273. }
  8274. /**
  8275. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8276. *
  8277. * This test makes sure `false` excludes the label from the render
  8278. *
  8279. * @return void
  8280. */
  8281. public function testInputLabelManipulationDisableLabels()
  8282. {
  8283. $result = $this->Form->input('test', [
  8284. 'type' => 'radio',
  8285. 'options' => ['A', 'B'],
  8286. 'labelOptions' => false
  8287. ]);
  8288. $expected = [
  8289. ['div' => ['class' => 'input radio']],
  8290. '<label',
  8291. 'Test',
  8292. '/label',
  8293. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8294. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8295. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8296. '/div',
  8297. ];
  8298. $this->assertHtml($expected, $result);
  8299. $result = $this->Form->input('checkbox1', [
  8300. 'label' => 'My checkboxes',
  8301. 'multiple' => 'checkbox',
  8302. 'type' => 'select',
  8303. 'options' => [
  8304. ['text' => 'First Checkbox', 'value' => 1],
  8305. ['text' => 'Second Checkbox', 'value' => 2]
  8306. ],
  8307. 'labelOptions' => false
  8308. ]);
  8309. $expected = [
  8310. ['div' => ['class' => 'input select']],
  8311. ['label' => ['for' => 'checkbox1']],
  8312. 'My checkboxes',
  8313. '/label',
  8314. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8315. ['div' => ['class' => 'checkbox']],
  8316. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8317. '/div',
  8318. ['div' => ['class' => 'checkbox']],
  8319. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8320. '/div',
  8321. '/div'
  8322. ];
  8323. $this->assertHtml($expected, $result);
  8324. }
  8325. /**
  8326. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8327. *
  8328. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8329. * added to the class if checked.
  8330. *
  8331. * Also checks to make sure any custom attributes are rendered correctly
  8332. *
  8333. * @return void
  8334. */
  8335. public function testInputLabelManipulationRadios()
  8336. {
  8337. $result = $this->Form->input('test', [
  8338. 'type' => 'radio',
  8339. 'options' => ['A', 'B'],
  8340. 'labelOptions' => ['class' => 'custom-class']
  8341. ]);
  8342. $expected = [
  8343. ['div' => ['class' => 'input radio']],
  8344. '<label',
  8345. 'Test',
  8346. '/label',
  8347. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8348. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8349. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8350. 'A',
  8351. '/label',
  8352. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8353. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8354. 'B',
  8355. '/label',
  8356. '/div',
  8357. ];
  8358. $this->assertHtml($expected, $result);
  8359. $result = $this->Form->input('test', [
  8360. 'type' => 'radio',
  8361. 'options' => ['A', 'B'],
  8362. 'value' => 1,
  8363. 'labelOptions' => ['class' => 'custom-class']
  8364. ]);
  8365. $expected = [
  8366. ['div' => ['class' => 'input radio']],
  8367. '<label',
  8368. 'Test',
  8369. '/label',
  8370. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8371. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8372. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8373. 'A',
  8374. '/label',
  8375. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8376. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8377. 'B',
  8378. '/label',
  8379. '/div',
  8380. ];
  8381. $this->assertHtml($expected, $result);
  8382. $result = $this->Form->input('test', [
  8383. 'type' => 'radio',
  8384. 'options' => ['A', 'B'],
  8385. 'value' => 1,
  8386. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']]
  8387. ]);
  8388. $expected = [
  8389. ['div' => ['class' => 'input radio']],
  8390. '<label',
  8391. 'Test',
  8392. '/label',
  8393. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8394. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8395. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8396. 'A',
  8397. '/label',
  8398. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8399. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8400. 'B',
  8401. '/label',
  8402. '/div',
  8403. ];
  8404. $this->assertHtml($expected, $result);
  8405. $result = $this->Form->radio('test', ['A', 'B'], [
  8406. 'label' => [
  8407. 'class' => ['custom-class', 'another-class'],
  8408. 'data-name' => 'bob'
  8409. ],
  8410. 'value' => 1
  8411. ]);
  8412. $expected = [
  8413. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8414. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8415. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8416. 'A',
  8417. '/label',
  8418. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8419. ['input' => [
  8420. 'type' => 'radio',
  8421. 'name' => 'test',
  8422. 'value' => '1',
  8423. 'id' => 'test-1',
  8424. 'checked' => 'checked'
  8425. ]],
  8426. 'B',
  8427. '/label',
  8428. ];
  8429. $this->assertHtml($expected, $result);
  8430. }
  8431. /**
  8432. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8433. *
  8434. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8435. * added to the class if checked.
  8436. *
  8437. * Also checks to make sure any custom attributes are rendered correctly
  8438. *
  8439. * @return void
  8440. */
  8441. public function testInputLabelManipulationCheckboxes()
  8442. {
  8443. $result = $this->Form->input('checkbox1', [
  8444. 'label' => 'My checkboxes',
  8445. 'multiple' => 'checkbox',
  8446. 'type' => 'select',
  8447. 'options' => [
  8448. ['text' => 'First Checkbox', 'value' => 1],
  8449. ['text' => 'Second Checkbox', 'value' => 2]
  8450. ],
  8451. 'labelOptions' => ['class' => 'custom-class'],
  8452. 'value' => ['1']
  8453. ]);
  8454. $expected = [
  8455. ['div' => ['class' => 'input select']],
  8456. ['label' => ['for' => 'checkbox1']],
  8457. 'My checkboxes',
  8458. '/label',
  8459. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8460. ['div' => ['class' => 'checkbox']],
  8461. ['label' => [
  8462. 'class' => 'custom-class selected',
  8463. 'for' => 'checkbox1-1'
  8464. ]],
  8465. ['input' => [
  8466. 'type' => 'checkbox',
  8467. 'name' => 'checkbox1[]',
  8468. 'value' => '1',
  8469. 'id' => 'checkbox1-1',
  8470. 'checked' => 'checked'
  8471. ]],
  8472. 'First Checkbox',
  8473. '/label',
  8474. '/div',
  8475. ['div' => ['class' => 'checkbox']],
  8476. ['label' => [
  8477. 'class' => 'custom-class',
  8478. 'for' => 'checkbox1-2'
  8479. ]],
  8480. ['input' => [
  8481. 'type' => 'checkbox',
  8482. 'name' => 'checkbox1[]',
  8483. 'value' => '2',
  8484. 'id' => 'checkbox1-2'
  8485. ]],
  8486. 'Second Checkbox',
  8487. '/label',
  8488. '/div',
  8489. '/div'
  8490. ];
  8491. $this->assertHtml($expected, $result);
  8492. $result = $this->Form->input('checkbox1', [
  8493. 'label' => 'My checkboxes',
  8494. 'multiple' => 'checkbox',
  8495. 'type' => 'select',
  8496. 'options' => [
  8497. ['text' => 'First Checkbox', 'value' => 1],
  8498. ['text' => 'Second Checkbox', 'value' => 2]
  8499. ],
  8500. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8501. 'value' => ['1']
  8502. ]);
  8503. $expected = [
  8504. ['div' => ['class' => 'input select']],
  8505. ['label' => ['for' => 'checkbox1']],
  8506. 'My checkboxes',
  8507. '/label',
  8508. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8509. ['div' => ['class' => 'checkbox']],
  8510. ['label' => [
  8511. 'class' => 'custom-class another-class selected',
  8512. 'data-name' => 'bob',
  8513. 'for' => 'checkbox1-1'
  8514. ]],
  8515. ['input' => [
  8516. 'type' => 'checkbox',
  8517. 'name' => 'checkbox1[]',
  8518. 'value' => '1',
  8519. 'id' => 'checkbox1-1',
  8520. 'checked' => 'checked'
  8521. ]],
  8522. 'First Checkbox',
  8523. '/label',
  8524. '/div',
  8525. ['div' => ['class' => 'checkbox']],
  8526. ['label' => [
  8527. 'class' => 'custom-class another-class',
  8528. 'data-name' => 'bob',
  8529. 'for' => 'checkbox1-2'
  8530. ]],
  8531. ['input' => [
  8532. 'type' => 'checkbox',
  8533. 'name' => 'checkbox1[]',
  8534. 'value' => '2',
  8535. 'id' => 'checkbox1-2'
  8536. ]],
  8537. 'Second Checkbox',
  8538. '/label',
  8539. '/div',
  8540. '/div'
  8541. ];
  8542. $this->assertHtml($expected, $result);
  8543. }
  8544. }