FormHelperTest.php 305 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299
  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. * @return void
  289. */
  290. public function testAddWidgetInvalid()
  291. {
  292. $this->expectException(\RuntimeException::class);
  293. $mock = new \StdClass();
  294. $this->Form->addWidget('test', $mock);
  295. $this->Form->widget('test');
  296. }
  297. /**
  298. * Test adding a new context class.
  299. *
  300. * @return void
  301. */
  302. public function testAddContextProvider()
  303. {
  304. $context = 'My data';
  305. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  306. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  307. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  308. $this->assertEquals($context, $data['entity']);
  309. return $stub;
  310. });
  311. $this->Form->create($context);
  312. $result = $this->Form->context();
  313. $this->assertSame($stub, $result);
  314. }
  315. /**
  316. * Test replacing a context class.
  317. *
  318. * @return void
  319. */
  320. public function testAddContextProviderReplace()
  321. {
  322. $entity = new Article();
  323. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  324. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  325. return $stub;
  326. });
  327. $this->Form->create($entity);
  328. $result = $this->Form->context();
  329. $this->assertSame($stub, $result);
  330. }
  331. /**
  332. * Test overriding a context class.
  333. *
  334. * @return void
  335. */
  336. public function testAddContextProviderAdd()
  337. {
  338. $entity = new Article();
  339. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  340. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  341. if ($data['entity'] instanceof Entity) {
  342. return $stub;
  343. }
  344. });
  345. $this->Form->create($entity);
  346. $result = $this->Form->context();
  347. $this->assertSame($stub, $result);
  348. }
  349. /**
  350. * Test adding an invalid context class.
  351. *
  352. * @return void
  353. */
  354. public function testAddContextProviderInvalid()
  355. {
  356. $this->expectException(\RuntimeException::class);
  357. $this->expectExceptionMessage('Context providers must return object implementing Cake\View\Form\ContextInterface. Got "stdClass" instead.');
  358. $context = 'My data';
  359. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  360. return new \StdClass();
  361. });
  362. $this->Form->create($context);
  363. }
  364. /**
  365. * Provides context options for create().
  366. *
  367. * @return array
  368. */
  369. public function contextSelectionProvider()
  370. {
  371. $entity = new Article();
  372. $collection = new Collection([$entity]);
  373. $emptyCollection = new Collection([]);
  374. $arrayObject = new \ArrayObject([]);
  375. $data = [
  376. 'schema' => [
  377. 'title' => ['type' => 'string']
  378. ]
  379. ];
  380. $form = new Form();
  381. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  382. return [
  383. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  384. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  385. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  386. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  387. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  388. 'form' => [$form, 'Cake\View\Form\FormContext'],
  389. 'none' => [null, 'Cake\View\Form\NullContext'],
  390. 'false' => [false, 'Cake\View\Form\NullContext'],
  391. 'custom' => [$custom, get_class($custom)]
  392. ];
  393. }
  394. /**
  395. * Test default context selection in create()
  396. *
  397. * @dataProvider contextSelectionProvider
  398. * @return void
  399. */
  400. public function testCreateContextSelectionBuiltIn($data, $class)
  401. {
  402. $this->loadFixtures('Articles');
  403. $this->Form->create($data);
  404. $this->assertInstanceOf($class, $this->Form->context());
  405. }
  406. /**
  407. * Data provider for type option.
  408. *
  409. * @return array
  410. */
  411. public static function requestTypeProvider()
  412. {
  413. return [
  414. // type, method, override
  415. ['post', 'post', 'POST'],
  416. ['put', 'post', 'PUT'],
  417. ['patch', 'post', 'PATCH'],
  418. ['delete', 'post', 'DELETE'],
  419. ];
  420. }
  421. /**
  422. * Test creating file forms.
  423. *
  424. * @return void
  425. */
  426. public function testCreateFile()
  427. {
  428. $encoding = strtolower(Configure::read('App.encoding'));
  429. $result = $this->Form->create(false, ['type' => 'file']);
  430. $expected = [
  431. 'form' => [
  432. 'method' => 'post', 'action' => '/articles/add',
  433. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  434. ],
  435. 'div' => ['style' => 'display:none;'],
  436. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  437. '/div'
  438. ];
  439. $this->assertHtml($expected, $result);
  440. }
  441. /**
  442. * Test creating GET forms.
  443. *
  444. * @return void
  445. */
  446. public function testCreateGet()
  447. {
  448. $encoding = strtolower(Configure::read('App.encoding'));
  449. $result = $this->Form->create(false, ['type' => 'get']);
  450. $expected = ['form' => [
  451. 'method' => 'get', 'action' => '/articles/add',
  452. 'accept-charset' => $encoding
  453. ]];
  454. $this->assertHtml($expected, $result);
  455. }
  456. /**
  457. * Test explicit method/enctype options.
  458. *
  459. * Explicit method overwrites inferred method from 'type'
  460. *
  461. * @return void
  462. */
  463. public function testCreateExplicitMethodEnctype()
  464. {
  465. $encoding = strtolower(Configure::read('App.encoding'));
  466. $result = $this->Form->create(false, [
  467. 'type' => 'get',
  468. 'method' => 'put',
  469. 'enctype' => 'multipart/form-data'
  470. ]);
  471. $expected = ['form' => [
  472. 'method' => 'put',
  473. 'action' => '/articles/add',
  474. 'enctype' => 'multipart/form-data',
  475. 'accept-charset' => $encoding
  476. ]];
  477. $this->assertHtml($expected, $result);
  478. }
  479. /**
  480. * Test create() with the templates option.
  481. *
  482. * @return void
  483. */
  484. public function testCreateTemplatesArray()
  485. {
  486. $result = $this->Form->create($this->article, [
  487. 'templates' => [
  488. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  489. ]
  490. ]);
  491. $expected = [
  492. 'form' => [
  493. 'class' => 'form-horizontal',
  494. 'method' => 'post',
  495. 'action' => '/articles/add',
  496. 'accept-charset' => 'utf-8'
  497. ]
  498. ];
  499. $this->assertHtml($expected, $result);
  500. }
  501. /**
  502. * Test create() with the templates option.
  503. *
  504. * @return void
  505. */
  506. public function testCreateTemplatesFile()
  507. {
  508. $result = $this->Form->create($this->article, [
  509. 'templates' => 'htmlhelper_tags',
  510. ]);
  511. $expected = [
  512. 'start form',
  513. 'div' => ['class' => 'hidden'],
  514. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  515. '/div'
  516. ];
  517. $this->assertHtml($expected, $result);
  518. }
  519. /**
  520. * Test that create() and end() restore templates.
  521. *
  522. * @return void
  523. */
  524. public function testCreateEndRestoreTemplates()
  525. {
  526. $this->Form->create($this->article, [
  527. 'templates' => ['input' => 'custom input element']
  528. ]);
  529. $this->Form->end();
  530. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  531. }
  532. /**
  533. * Test using template vars in various templates used by control() method.
  534. *
  535. * @return void
  536. */
  537. public function testControlTemplateVars()
  538. {
  539. $result = $this->Form->control('text', [
  540. 'templates' => [
  541. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  542. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  543. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  544. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  545. ],
  546. 'templateVars' => [
  547. 'forinput' => 'in-input',
  548. 'forlabel' => 'in-label',
  549. 'forgroup' => 'in-group',
  550. 'forcontainer' => 'in-container'
  551. ]
  552. ]);
  553. $expected = [
  554. 'div' => ['class'],
  555. 'label' => ['for'],
  556. 'Text in-label',
  557. '/label',
  558. 'in-group',
  559. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  560. 'in-container',
  561. '/div',
  562. ];
  563. $this->assertHtml($expected, $result);
  564. }
  565. /**
  566. * Test ensuring template variables work in template files loaded
  567. * during control().
  568. *
  569. * @return void
  570. */
  571. public function testControlTemplatesFromFile()
  572. {
  573. $result = $this->Form->control('title', [
  574. 'templates' => 'test_templates',
  575. 'templateVars' => [
  576. 'forcontainer' => 'container-data'
  577. ]
  578. ]);
  579. $expected = [
  580. 'div' => ['class'],
  581. 'label' => ['for'],
  582. 'Title',
  583. '/label',
  584. 'input' => ['name', 'type' => 'text', 'id'],
  585. 'container-data',
  586. '/div',
  587. ];
  588. $this->assertHtml($expected, $result);
  589. }
  590. /**
  591. * Test using template vars in inputSubmit and submitContainer template.
  592. *
  593. * @return void
  594. */
  595. public function testSubmitTemplateVars()
  596. {
  597. $this->Form->templates([
  598. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  599. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  600. ]);
  601. $result = $this->Form->submit('Submit', [
  602. 'templateVars' => [
  603. 'forinput' => 'in-input',
  604. 'forcontainer' => 'in-container'
  605. ]
  606. ]);
  607. $expected = [
  608. 'div' => ['class'],
  609. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  610. 'in-container',
  611. '/div',
  612. ];
  613. $this->assertHtml($expected, $result);
  614. }
  615. /**
  616. * test the create() method
  617. *
  618. * @dataProvider requestTypeProvider
  619. * @return void
  620. */
  621. public function testCreateTypeOptions($type, $method, $override)
  622. {
  623. $encoding = strtolower(Configure::read('App.encoding'));
  624. $result = $this->Form->create(false, ['type' => $type]);
  625. $expected = [
  626. 'form' => [
  627. 'method' => $method, 'action' => '/articles/add',
  628. 'accept-charset' => $encoding
  629. ],
  630. 'div' => ['style' => 'display:none;'],
  631. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  632. '/div'
  633. ];
  634. $this->assertHtml($expected, $result);
  635. }
  636. /**
  637. * Test using template vars in Create (formStart template)
  638. *
  639. * @return void
  640. */
  641. public function testCreateTemplateVars()
  642. {
  643. $result = $this->Form->create($this->article, [
  644. 'templates' => [
  645. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  646. ],
  647. 'templateVars' => ['header' => 'headertext']
  648. ]);
  649. $expected = [
  650. 'h4' => ['class'],
  651. 'headertext',
  652. '/h4',
  653. 'form' => [
  654. 'method' => 'post',
  655. 'action' => '/articles/add',
  656. 'accept-charset' => 'utf-8'
  657. ]
  658. ];
  659. $this->assertHtml($expected, $result);
  660. }
  661. /**
  662. * Test opening a form for an update operation.
  663. *
  664. * @return void
  665. */
  666. public function testCreateUpdateForm()
  667. {
  668. $encoding = strtolower(Configure::read('App.encoding'));
  669. $this->Form->request->here = '/articles/edit/1';
  670. $this->Form->request->params['action'] = 'edit';
  671. $this->article['defaults']['id'] = 1;
  672. $result = $this->Form->create($this->article);
  673. $expected = [
  674. 'form' => [
  675. 'method' => 'post',
  676. 'action' => '/articles/edit/1',
  677. 'accept-charset' => $encoding
  678. ],
  679. 'div' => ['style' => 'display:none;'],
  680. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  681. '/div'
  682. ];
  683. $this->assertHtml($expected, $result);
  684. }
  685. /**
  686. * test create() with automatic url generation
  687. *
  688. * @return void
  689. */
  690. public function testCreateAutoUrl()
  691. {
  692. $encoding = strtolower(Configure::read('App.encoding'));
  693. $this->Form->request->params['action'] = 'delete';
  694. $this->Form->request->here = '/articles/delete/10';
  695. $this->Form->request->base = '';
  696. $result = $this->Form->create($this->article);
  697. $expected = [
  698. 'form' => [
  699. 'method' => 'post', 'action' => '/articles/delete/10',
  700. 'accept-charset' => $encoding
  701. ],
  702. 'div' => ['style' => 'display:none;'],
  703. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  704. '/div'
  705. ];
  706. $this->assertHtml($expected, $result);
  707. $this->article['defaults'] = ['id' => 1];
  708. $this->Form->request->here = '/articles/edit/1';
  709. $this->Form->request->params['action'] = 'delete';
  710. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  711. $expected = [
  712. 'form' => [
  713. 'method' => 'post',
  714. 'action' => '/articles/edit/1',
  715. 'accept-charset' => $encoding
  716. ],
  717. 'div' => ['style' => 'display:none;'],
  718. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  719. '/div'
  720. ];
  721. $this->assertHtml($expected, $result);
  722. $this->Form->request->params['action'] = 'add';
  723. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  724. $expected = [
  725. 'form' => [
  726. 'method' => 'post',
  727. 'action' => '/articles/publish/1',
  728. 'accept-charset' => $encoding
  729. ],
  730. 'div' => ['style' => 'display:none;'],
  731. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  732. '/div'
  733. ];
  734. $this->assertHtml($expected, $result);
  735. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  736. $expected = [
  737. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  738. 'div' => ['style' => 'display:none;'],
  739. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  740. '/div'
  741. ];
  742. $this->assertHtml($expected, $result);
  743. $this->Form->request->params['controller'] = 'Pages';
  744. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  745. $expected = [
  746. 'form' => [
  747. 'method' => 'post', 'action' => '/Pages/signup/1',
  748. 'accept-charset' => $encoding
  749. ],
  750. 'div' => ['style' => 'display:none;'],
  751. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  752. '/div'
  753. ];
  754. $this->assertHtml($expected, $result);
  755. }
  756. /**
  757. * Test create() with no URL (no "action" attribute for <form> tag)
  758. *
  759. * @return void
  760. */
  761. public function testCreateNoUrl()
  762. {
  763. $result = $this->Form->create(false, ['url' => false]);
  764. $expected = [
  765. 'form' => [
  766. 'method' => 'post',
  767. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  768. ],
  769. 'div' => ['style' => 'display:none;'],
  770. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  771. '/div'
  772. ];
  773. $this->assertHtml($expected, $result);
  774. }
  775. /**
  776. * test create() with a custom route
  777. *
  778. * @return void
  779. */
  780. public function testCreateCustomRoute()
  781. {
  782. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  783. $encoding = strtolower(Configure::read('App.encoding'));
  784. $this->Form->request->params['controller'] = 'users';
  785. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  786. $expected = [
  787. 'form' => [
  788. 'method' => 'post', 'action' => '/login',
  789. 'accept-charset' => $encoding
  790. ],
  791. 'div' => ['style' => 'display:none;'],
  792. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  793. '/div'
  794. ];
  795. $this->assertHtml($expected, $result);
  796. Router::connect(
  797. '/new-article',
  798. ['controller' => 'articles', 'action' => 'myaction'],
  799. ['_name' => 'my-route']
  800. );
  801. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  802. $expected = [
  803. 'form' => [
  804. 'method' => 'post', 'action' => '/new-article',
  805. 'accept-charset' => $encoding,
  806. ],
  807. 'div' => ['style' => 'display:none;'],
  808. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  809. '/div'
  810. ];
  811. $this->assertHtml($expected, $result);
  812. }
  813. /**
  814. * test automatic accept-charset overriding
  815. *
  816. * @return void
  817. */
  818. public function testCreateWithAcceptCharset()
  819. {
  820. $result = $this->Form->create(
  821. $this->article,
  822. [
  823. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  824. ]
  825. );
  826. $expected = [
  827. 'form' => [
  828. 'method' => 'post', 'action' => '/articles',
  829. 'accept-charset' => 'iso-8859-1'
  830. ],
  831. 'div' => ['style' => 'display:none;'],
  832. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  833. '/div'
  834. ];
  835. $this->assertHtml($expected, $result);
  836. }
  837. /**
  838. * Test base form URL when url param is passed with multiple parameters (&)
  839. *
  840. */
  841. public function testCreateQueryStringRequest()
  842. {
  843. $encoding = strtolower(Configure::read('App.encoding'));
  844. $result = $this->Form->create($this->article, [
  845. 'type' => 'post',
  846. 'escape' => false,
  847. 'url' => [
  848. 'controller' => 'controller',
  849. 'action' => 'action',
  850. '?' => ['param1' => 'value1', 'param2' => 'value2']
  851. ]
  852. ]);
  853. $expected = [
  854. 'form' => [
  855. 'method' => 'post',
  856. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  857. 'accept-charset' => $encoding
  858. ],
  859. 'div' => ['style' => 'display:none;'],
  860. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  861. '/div'
  862. ];
  863. $this->assertHtml($expected, $result);
  864. $result = $this->Form->create($this->article, [
  865. 'type' => 'post',
  866. 'url' => [
  867. 'controller' => 'controller',
  868. 'action' => 'action',
  869. '?' => ['param1' => 'value1', 'param2' => 'value2']
  870. ]
  871. ]);
  872. $this->assertHtml($expected, $result);
  873. }
  874. /**
  875. * test that create() doesn't cause errors by multiple id's being in the primary key
  876. * as could happen with multiple select or checkboxes.
  877. *
  878. * @return void
  879. */
  880. public function testCreateWithMultipleIdInData()
  881. {
  882. $encoding = strtolower(Configure::read('App.encoding'));
  883. $this->Form->request->data['Article']['id'] = [1, 2];
  884. $result = $this->Form->create($this->article);
  885. $expected = [
  886. 'form' => [
  887. 'method' => 'post',
  888. 'action' => '/articles/add',
  889. 'accept-charset' => $encoding
  890. ],
  891. 'div' => ['style' => 'display:none;'],
  892. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  893. '/div'
  894. ];
  895. $this->assertHtml($expected, $result);
  896. }
  897. /**
  898. * test that create() doesn't add in extra passed params.
  899. *
  900. * @return void
  901. */
  902. public function testCreatePassedArgs()
  903. {
  904. $encoding = strtolower(Configure::read('App.encoding'));
  905. $this->Form->request->data['Article']['id'] = 1;
  906. $result = $this->Form->create($this->article, [
  907. 'type' => 'post',
  908. 'escape' => false,
  909. 'url' => [
  910. 'action' => 'edit',
  911. 'myparam'
  912. ]
  913. ]);
  914. $expected = [
  915. 'form' => [
  916. 'method' => 'post',
  917. 'action' => '/articles/edit/myparam',
  918. 'accept-charset' => $encoding
  919. ],
  920. 'div' => ['style' => 'display:none;'],
  921. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  922. '/div'
  923. ];
  924. $this->assertHtml($expected, $result);
  925. }
  926. /**
  927. * test creating a get form, and get form inputs.
  928. *
  929. * @return void
  930. */
  931. public function testGetFormCreate()
  932. {
  933. $encoding = strtolower(Configure::read('App.encoding'));
  934. $result = $this->Form->create($this->article, ['type' => 'get']);
  935. $expected = ['form' => [
  936. 'method' => 'get', 'action' => '/articles/add',
  937. 'accept-charset' => $encoding
  938. ]];
  939. $this->assertHtml($expected, $result);
  940. $result = $this->Form->text('title');
  941. $expected = ['input' => [
  942. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  943. ]];
  944. $this->assertHtml($expected, $result);
  945. $result = $this->Form->password('password');
  946. $expected = ['input' => [
  947. 'name' => 'password', 'type' => 'password'
  948. ]];
  949. $this->assertHtml($expected, $result);
  950. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  951. $result = $this->Form->text('user_form');
  952. $expected = ['input' => [
  953. 'name' => 'user_form', 'type' => 'text'
  954. ]];
  955. $this->assertHtml($expected, $result);
  956. }
  957. /**
  958. * test get form, and inputs when the model param is false
  959. *
  960. * @return void
  961. */
  962. public function testGetFormWithFalseModel()
  963. {
  964. $encoding = strtolower(Configure::read('App.encoding'));
  965. $this->Form->request->params['controller'] = 'contact_test';
  966. $result = $this->Form->create(false, [
  967. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  968. ]);
  969. $expected = ['form' => [
  970. 'method' => 'get', 'action' => '/contact_test/add',
  971. 'accept-charset' => $encoding
  972. ]];
  973. $this->assertHtml($expected, $result);
  974. $result = $this->Form->text('reason');
  975. $expected = [
  976. 'input' => ['type' => 'text', 'name' => 'reason']
  977. ];
  978. $this->assertHtml($expected, $result);
  979. }
  980. /**
  981. * testFormCreateWithSecurity method
  982. *
  983. * Test form->create() with security key.
  984. *
  985. * @return void
  986. */
  987. public function testCreateWithSecurity()
  988. {
  989. $this->Form->request->params['_csrfToken'] = 'testKey';
  990. $encoding = strtolower(Configure::read('App.encoding'));
  991. $result = $this->Form->create($this->article, [
  992. 'url' => '/articles/publish',
  993. ]);
  994. $expected = [
  995. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  996. 'div' => ['style' => 'display:none;'],
  997. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  998. ['input' => [
  999. 'type' => 'hidden',
  1000. 'name' => '_csrfToken',
  1001. 'value' => 'testKey',
  1002. 'autocomplete' => 'off',
  1003. ]],
  1004. '/div'
  1005. ];
  1006. $this->assertHtml($expected, $result);
  1007. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1008. $expected['form']['id'] = 'MyForm';
  1009. $this->assertHtml($expected, $result);
  1010. }
  1011. /**
  1012. * testFormCreateGetNoSecurity method
  1013. *
  1014. * Test form->create() with no security key as its a get form
  1015. *
  1016. * @return void
  1017. */
  1018. public function testCreateEndGetNoSecurity()
  1019. {
  1020. $this->Form->request->params['_csrfToken'] = 'testKey';
  1021. $article = new Article();
  1022. $result = $this->Form->create($article, [
  1023. 'type' => 'get',
  1024. 'url' => '/contacts/add'
  1025. ]);
  1026. $this->assertNotContains('testKey', $result);
  1027. $result = $this->Form->end();
  1028. $this->assertNotContains('testKey', $result);
  1029. }
  1030. /**
  1031. * test that create() clears the fields property so it starts fresh
  1032. *
  1033. * @return void
  1034. */
  1035. public function testCreateClearingFields()
  1036. {
  1037. $this->Form->fields = ['model_id'];
  1038. $this->Form->create($this->article);
  1039. $this->assertEquals([], $this->Form->fields);
  1040. }
  1041. /**
  1042. * Tests form hash generation with model-less data
  1043. *
  1044. * @return void
  1045. */
  1046. public function testValidateHashNoModel()
  1047. {
  1048. $this->Form->request->params['_Token'] = 'foo';
  1049. $result = $this->Form->secure(['anything']);
  1050. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  1051. }
  1052. /**
  1053. * Tests that hidden fields generated for checkboxes don't get locked
  1054. *
  1055. * @return void
  1056. */
  1057. public function testNoCheckboxLocking()
  1058. {
  1059. $this->Form->request->params['_Token'] = 'foo';
  1060. $this->assertSame([], $this->Form->fields);
  1061. $this->Form->checkbox('check', ['value' => '1']);
  1062. $this->assertSame($this->Form->fields, ['check']);
  1063. }
  1064. /**
  1065. * testFormSecurityFields method
  1066. *
  1067. * Test generation of secure form hash generation.
  1068. *
  1069. * @return void
  1070. */
  1071. public function testFormSecurityFields()
  1072. {
  1073. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1074. $this->Form->request->params['_Token'] = 'testKey';
  1075. $result = $this->Form->secure($fields);
  1076. $hash = Security::hash(serialize($fields) . Security::salt());
  1077. $hash .= ':' . 'Model.valid';
  1078. $hash = urlencode($hash);
  1079. $tokenDebug = urlencode(json_encode([
  1080. '',
  1081. $fields,
  1082. []
  1083. ]));
  1084. $expected = [
  1085. 'div' => ['style' => 'display:none;'],
  1086. ['input' => [
  1087. 'type' => 'hidden',
  1088. 'name' => '_Token[fields]',
  1089. 'value' => $hash,
  1090. 'autocomplete' => 'off',
  1091. ]],
  1092. ['input' => [
  1093. 'type' => 'hidden',
  1094. 'name' => '_Token[unlocked]',
  1095. 'value' => '',
  1096. 'autocomplete' => 'off',
  1097. ]],
  1098. ['input' => [
  1099. 'type' => 'hidden',
  1100. 'name' => '_Token[debug]',
  1101. 'value' => $tokenDebug,
  1102. 'autocomplete' => 'off',
  1103. ]],
  1104. '/div'
  1105. ];
  1106. $this->assertHtml($expected, $result);
  1107. }
  1108. /**
  1109. * testFormSecurityFields method
  1110. *
  1111. * Test debug token is not generated if debug is false
  1112. *
  1113. * @return void
  1114. */
  1115. public function testFormSecurityFieldsNoDebugMode()
  1116. {
  1117. Configure::write('debug', false);
  1118. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1119. $this->Form->request->params['_Token'] = 'testKey';
  1120. $result = $this->Form->secure($fields);
  1121. $hash = Security::hash(serialize($fields) . Security::salt());
  1122. $hash .= ':' . 'Model.valid';
  1123. $hash = urlencode($hash);
  1124. $expected = [
  1125. 'div' => ['style' => 'display:none;'],
  1126. ['input' => [
  1127. 'type' => 'hidden',
  1128. 'name' => '_Token[fields]',
  1129. 'autocomplete' => 'off',
  1130. 'value' => $hash
  1131. ]],
  1132. ['input' => [
  1133. 'type' => 'hidden',
  1134. 'name' => '_Token[unlocked]',
  1135. 'autocomplete' => 'off',
  1136. 'value' => '',
  1137. ]],
  1138. '/div'
  1139. ];
  1140. $this->assertHtml($expected, $result);
  1141. }
  1142. /**
  1143. * Tests correct generation of number fields for smallint
  1144. *
  1145. * @return void
  1146. */
  1147. public function testTextFieldGenerationForSmallint()
  1148. {
  1149. $this->article['schema'] = [
  1150. 'foo' => [
  1151. 'type' => 'smallinteger',
  1152. 'null' => false,
  1153. 'default' => null,
  1154. 'length' => 10
  1155. ]
  1156. ];
  1157. $this->Form->create($this->article);
  1158. $result = $this->Form->control('foo');
  1159. $this->assertContains('class="input number"', $result);
  1160. $this->assertContains('type="number"', $result);
  1161. }
  1162. /**
  1163. * Tests correct generation of number fields for tinyint
  1164. *
  1165. * @return void
  1166. */
  1167. public function testTextFieldGenerationForTinyint()
  1168. {
  1169. $this->article['schema'] = [
  1170. 'foo' => [
  1171. 'type' => 'tinyinteger',
  1172. 'null' => false,
  1173. 'default' => null,
  1174. 'length' => 10
  1175. ]
  1176. ];
  1177. $this->Form->create($this->article);
  1178. $result = $this->Form->control('foo');
  1179. $this->assertContains('class="input number"', $result);
  1180. $this->assertContains('type="number"', $result);
  1181. }
  1182. /**
  1183. * Tests correct generation of number fields for double and float fields
  1184. *
  1185. * @return void
  1186. */
  1187. public function testTextFieldGenerationForFloats()
  1188. {
  1189. $this->article['schema'] = [
  1190. 'foo' => [
  1191. 'type' => 'float',
  1192. 'null' => false,
  1193. 'default' => null,
  1194. 'length' => 10
  1195. ]
  1196. ];
  1197. $this->Form->create($this->article);
  1198. $result = $this->Form->control('foo');
  1199. $expected = [
  1200. 'div' => ['class' => 'input number'],
  1201. 'label' => ['for' => 'foo'],
  1202. 'Foo',
  1203. '/label',
  1204. ['input' => [
  1205. 'type' => 'number',
  1206. 'name' => 'foo',
  1207. 'id' => 'foo',
  1208. 'step' => 'any'
  1209. ]],
  1210. '/div'
  1211. ];
  1212. $this->assertHtml($expected, $result);
  1213. $result = $this->Form->control('foo', ['step' => 0.5]);
  1214. $expected = [
  1215. 'div' => ['class' => 'input number'],
  1216. 'label' => ['for' => 'foo'],
  1217. 'Foo',
  1218. '/label',
  1219. ['input' => [
  1220. 'type' => 'number',
  1221. 'name' => 'foo',
  1222. 'id' => 'foo',
  1223. 'step' => '0.5'
  1224. ]],
  1225. '/div'
  1226. ];
  1227. $this->assertHtml($expected, $result);
  1228. }
  1229. /**
  1230. * Tests correct generation of number fields for integer fields
  1231. *
  1232. * @return void
  1233. */
  1234. public function testTextFieldTypeNumberGenerationForIntegers()
  1235. {
  1236. TableRegistry::get('Contacts', [
  1237. 'className' => __NAMESPACE__ . '\ContactsTable'
  1238. ]);
  1239. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1240. $result = $this->Form->control('age');
  1241. $expected = [
  1242. 'div' => ['class' => 'input number'],
  1243. 'label' => ['for' => 'age'],
  1244. 'Age',
  1245. '/label',
  1246. ['input' => [
  1247. 'type' => 'number', 'name' => 'age',
  1248. 'id' => 'age'
  1249. ]],
  1250. '/div'
  1251. ];
  1252. $this->assertHtml($expected, $result);
  1253. }
  1254. /**
  1255. * Tests correct generation of file upload fields for binary fields
  1256. *
  1257. * @return void
  1258. */
  1259. public function testFileUploadFieldTypeGenerationForBinaries()
  1260. {
  1261. $table = TableRegistry::get('Contacts', [
  1262. 'className' => __NAMESPACE__ . '\ContactsTable'
  1263. ]);
  1264. $table->schema(['foo' => [
  1265. 'type' => 'binary',
  1266. 'null' => false,
  1267. 'default' => null,
  1268. 'length' => 1024
  1269. ]]);
  1270. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1271. $result = $this->Form->control('foo');
  1272. $expected = [
  1273. 'div' => ['class' => 'input file'],
  1274. 'label' => ['for' => 'foo'],
  1275. 'Foo',
  1276. '/label',
  1277. ['input' => [
  1278. 'type' => 'file', 'name' => 'foo',
  1279. 'id' => 'foo'
  1280. ]],
  1281. '/div'
  1282. ];
  1283. $this->assertHtml($expected, $result);
  1284. }
  1285. /**
  1286. * testFormSecurityMultipleFields method
  1287. *
  1288. * Test secure() with multiple row form. Ensure hash is correct.
  1289. *
  1290. * @return void
  1291. */
  1292. public function testFormSecurityMultipleFields()
  1293. {
  1294. $this->Form->request->params['_Token'] = 'foo';
  1295. $fields = [
  1296. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1297. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1298. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1299. ];
  1300. $result = $this->Form->secure($fields);
  1301. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  1302. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  1303. $tokenDebug = urlencode(json_encode([
  1304. '',
  1305. $fields,
  1306. []
  1307. ]));
  1308. $expected = [
  1309. 'div' => ['style' => 'display:none;'],
  1310. ['input' => [
  1311. 'type' => 'hidden',
  1312. 'name' => '_Token[fields]',
  1313. 'value' => $hash,
  1314. 'autocomplete' => 'off',
  1315. ]],
  1316. ['input' => [
  1317. 'type' => 'hidden',
  1318. 'name' => '_Token[unlocked]',
  1319. 'autocomplete' => 'off',
  1320. 'value' => ''
  1321. ]],
  1322. ['input' => [
  1323. 'type' => 'hidden',
  1324. 'name' => '_Token[debug]',
  1325. 'value' => $tokenDebug,
  1326. 'autocomplete' => 'off',
  1327. ]],
  1328. '/div'
  1329. ];
  1330. $this->assertHtml($expected, $result);
  1331. }
  1332. /**
  1333. * testFormSecurityMultipleSubmitButtons
  1334. *
  1335. * test form submit generation and ensure that _Token is only created on end()
  1336. *
  1337. * @return void
  1338. */
  1339. public function testFormSecurityMultipleSubmitButtons()
  1340. {
  1341. $this->Form->request->params['_Token'] = 'testKey';
  1342. $this->Form->create($this->article);
  1343. $this->Form->text('Address.title');
  1344. $this->Form->text('Address.first_name');
  1345. $result = $this->Form->submit('Save', ['name' => 'save']);
  1346. $expected = [
  1347. 'div' => ['class' => 'submit'],
  1348. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1349. '/div',
  1350. ];
  1351. $this->assertHtml($expected, $result);
  1352. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1353. $expected = [
  1354. 'div' => ['class' => 'submit'],
  1355. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1356. '/div',
  1357. ];
  1358. $this->assertHtml($expected, $result);
  1359. $result = $this->Form->end();
  1360. $tokenDebug = urlencode(json_encode([
  1361. '/articles/add',
  1362. [
  1363. 'Address.title',
  1364. 'Address.first_name',
  1365. ],
  1366. ['save', 'cancel']
  1367. ]));
  1368. $expected = [
  1369. 'div' => ['style' => 'display:none;'],
  1370. ['input' => [
  1371. 'type' => 'hidden',
  1372. 'name' => '_Token[fields]',
  1373. 'autocomplete',
  1374. 'value'
  1375. ]],
  1376. ['input' => [
  1377. 'type' => 'hidden',
  1378. 'name' => '_Token[unlocked]',
  1379. 'value' => 'cancel%7Csave',
  1380. 'autocomplete' => 'off',
  1381. ]],
  1382. ['input' => [
  1383. 'type' => 'hidden',
  1384. 'name' => '_Token[debug]',
  1385. 'value' => $tokenDebug,
  1386. 'autocomplete' => 'off',
  1387. ]],
  1388. '/div'
  1389. ];
  1390. $this->assertHtml($expected, $result);
  1391. }
  1392. /**
  1393. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1394. *
  1395. * @return void
  1396. */
  1397. public function testSecurityButtonNestedNamed()
  1398. {
  1399. $key = 'testKey';
  1400. $this->Form->request->params['_csrfToken'] = $key;
  1401. $this->Form->create('Addresses');
  1402. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1403. $result = $this->Form->unlockField();
  1404. $this->assertEquals(['Address.button'], $result);
  1405. }
  1406. /**
  1407. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1408. *
  1409. * @return void
  1410. */
  1411. public function testSecuritySubmitNestedNamed()
  1412. {
  1413. $this->Form->request->params['_Token'] = 'testKey';
  1414. $this->Form->create($this->article);
  1415. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1416. $result = $this->Form->unlockField();
  1417. $this->assertEquals(['Address.button'], $result);
  1418. }
  1419. /**
  1420. * Test that the correct fields are unlocked for image submits with no names.
  1421. *
  1422. * @return void
  1423. */
  1424. public function testSecuritySubmitImageNoName()
  1425. {
  1426. $this->Form->request->params['_Token'] = 'testKey';
  1427. $this->Form->create(false);
  1428. $result = $this->Form->submit('save.png');
  1429. $expected = [
  1430. 'div' => ['class' => 'submit'],
  1431. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1432. '/div'
  1433. ];
  1434. $this->assertHtml($expected, $result);
  1435. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1436. }
  1437. /**
  1438. * Test that the correct fields are unlocked for image submits with names.
  1439. *
  1440. * @return void
  1441. */
  1442. public function testSecuritySubmitImageName()
  1443. {
  1444. $this->Form->request->params['_Token'] = 'testKey';
  1445. $this->Form->create(null);
  1446. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1447. $expected = [
  1448. 'div' => ['class' => 'submit'],
  1449. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1450. '/div'
  1451. ];
  1452. $this->assertHtml($expected, $result);
  1453. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1454. }
  1455. /**
  1456. * testFormSecurityMultipleControlFields method
  1457. *
  1458. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1459. *
  1460. * @return void
  1461. */
  1462. public function testFormSecurityMultipleControlFields()
  1463. {
  1464. $this->Form->request->params['_Token'] = 'testKey';
  1465. $this->Form->create();
  1466. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1467. $this->Form->control('Addresses.0.title');
  1468. $this->Form->control('Addresses.0.first_name');
  1469. $this->Form->control('Addresses.0.last_name');
  1470. $this->Form->control('Addresses.0.address');
  1471. $this->Form->control('Addresses.0.city');
  1472. $this->Form->control('Addresses.0.phone');
  1473. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1474. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1475. $this->Form->control('Addresses.1.title');
  1476. $this->Form->control('Addresses.1.first_name');
  1477. $this->Form->control('Addresses.1.last_name');
  1478. $this->Form->control('Addresses.1.address');
  1479. $this->Form->control('Addresses.1.city');
  1480. $this->Form->control('Addresses.1.phone');
  1481. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1482. $result = $this->Form->secure($this->Form->fields);
  1483. $hash = '8bd3911b07b507408b1a969b31ee90c47b7d387e%3AAddresses.0.id%7CAddresses.1.id';
  1484. $tokenDebug = urlencode(json_encode([
  1485. '/articles/add',
  1486. [
  1487. 'Addresses.0.id' => '123456',
  1488. 'Addresses.0.title',
  1489. 'Addresses.0.first_name',
  1490. 'Addresses.0.last_name',
  1491. 'Addresses.0.address',
  1492. 'Addresses.0.city',
  1493. 'Addresses.0.phone',
  1494. 'Addresses.0.primary',
  1495. 'Addresses.1.id' => '654321',
  1496. 'Addresses.1.title',
  1497. 'Addresses.1.first_name',
  1498. 'Addresses.1.last_name',
  1499. 'Addresses.1.address',
  1500. 'Addresses.1.city',
  1501. 'Addresses.1.phone',
  1502. 'Addresses.1.primary',
  1503. ],
  1504. []
  1505. ]));
  1506. $expected = [
  1507. 'div' => ['style' => 'display:none;'],
  1508. ['input' => [
  1509. 'type' => 'hidden',
  1510. 'name' => '_Token[fields]',
  1511. 'value' => $hash,
  1512. 'autocomplete' => 'off',
  1513. ]],
  1514. ['input' => [
  1515. 'type' => 'hidden',
  1516. 'name' => '_Token[unlocked]',
  1517. 'autocomplete' => 'off',
  1518. 'value' => ''
  1519. ]],
  1520. ['input' => [
  1521. 'type' => 'hidden',
  1522. 'name' => '_Token[debug]',
  1523. 'value' => $tokenDebug,
  1524. 'autocomplete' => 'off',
  1525. ]],
  1526. '/div'
  1527. ];
  1528. $this->assertHtml($expected, $result);
  1529. }
  1530. /**
  1531. * testFormSecurityArrayFields method
  1532. *
  1533. * Test form security with Model.field.0 style inputs.
  1534. *
  1535. * @return void
  1536. */
  1537. public function testFormSecurityArrayFields()
  1538. {
  1539. $this->Form->request->params['_Token'] = 'testKey';
  1540. $this->Form->create();
  1541. $this->Form->text('Address.primary.1');
  1542. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1543. $this->Form->text('Address.secondary.1.0');
  1544. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1545. }
  1546. /**
  1547. * testFormSecurityMultipleControlDisabledFields method
  1548. *
  1549. * Test secure form generation with multiple records and disabled fields.
  1550. *
  1551. * @return void
  1552. */
  1553. public function testFormSecurityMultipleControlDisabledFields()
  1554. {
  1555. $this->Form->request->params['_Token'] = [
  1556. 'unlockedFields' => ['first_name', 'address']
  1557. ];
  1558. $this->Form->create();
  1559. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1560. $this->Form->text('Addresses.0.title');
  1561. $this->Form->text('Addresses.0.first_name');
  1562. $this->Form->text('Addresses.0.last_name');
  1563. $this->Form->text('Addresses.0.address');
  1564. $this->Form->text('Addresses.0.city');
  1565. $this->Form->text('Addresses.0.phone');
  1566. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1567. $this->Form->text('Addresses.1.title');
  1568. $this->Form->text('Addresses.1.first_name');
  1569. $this->Form->text('Addresses.1.last_name');
  1570. $this->Form->text('Addresses.1.address');
  1571. $this->Form->text('Addresses.1.city');
  1572. $this->Form->text('Addresses.1.phone');
  1573. $result = $this->Form->secure($this->Form->fields);
  1574. $hash = '4fb10b46873df4ddd4ef5c3a19944a2f29b38991%3AAddresses.0.id%7CAddresses.1.id';
  1575. $tokenDebug = urlencode(json_encode([
  1576. '/articles/add',
  1577. [
  1578. 'Addresses.0.id' => '123456',
  1579. 'Addresses.0.title',
  1580. 'Addresses.0.last_name',
  1581. 'Addresses.0.city',
  1582. 'Addresses.0.phone',
  1583. 'Addresses.1.id' => '654321',
  1584. 'Addresses.1.title',
  1585. 'Addresses.1.last_name',
  1586. 'Addresses.1.city',
  1587. 'Addresses.1.phone'
  1588. ],
  1589. [
  1590. 'first_name',
  1591. 'address'
  1592. ]
  1593. ]));
  1594. $expected = [
  1595. 'div' => ['style' => 'display:none;'],
  1596. ['input' => [
  1597. 'type' => 'hidden',
  1598. 'name' => '_Token[fields]',
  1599. 'autocomplete' => 'off',
  1600. 'value' => $hash
  1601. ]],
  1602. ['input' => [
  1603. 'type' => 'hidden',
  1604. 'name' => '_Token[unlocked]',
  1605. 'autocomplete' => 'off',
  1606. 'value' => 'address%7Cfirst_name',
  1607. ]],
  1608. ['input' => [
  1609. 'type' => 'hidden',
  1610. 'name' => '_Token[debug]',
  1611. 'autocomplete' => 'off',
  1612. 'value' => $tokenDebug,
  1613. ]],
  1614. '/div'
  1615. ];
  1616. $this->assertHtml($expected, $result);
  1617. }
  1618. /**
  1619. * testFormSecurityControlDisabledFields method
  1620. *
  1621. * Test single record form with disabled fields.
  1622. *
  1623. * @return void
  1624. */
  1625. public function testFormSecurityControlUnlockedFields()
  1626. {
  1627. $this->Form->request->params['_Token'] = [
  1628. 'unlockedFields' => ['first_name', 'address']
  1629. ];
  1630. $this->Form->create();
  1631. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1632. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1633. $this->Form->text('Addresses.title');
  1634. $this->Form->text('Addresses.first_name');
  1635. $this->Form->text('Addresses.last_name');
  1636. $this->Form->text('Addresses.address');
  1637. $this->Form->text('Addresses.city');
  1638. $this->Form->text('Addresses.phone');
  1639. $result = $this->Form->fields;
  1640. $expected = [
  1641. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1642. 'Addresses.city', 'Addresses.phone'
  1643. ];
  1644. $this->assertEquals($expected, $result);
  1645. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1646. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1647. $tokenDebug = urlencode(json_encode([
  1648. '/articles/add',
  1649. [
  1650. 'Addresses.id' => '123456',
  1651. 'Addresses.title',
  1652. 'Addresses.last_name',
  1653. 'Addresses.city',
  1654. 'Addresses.phone'
  1655. ],
  1656. [
  1657. 'first_name',
  1658. 'address'
  1659. ]
  1660. ]));
  1661. $expected = [
  1662. 'div' => ['style' => 'display:none;'],
  1663. ['input' => [
  1664. 'type' => 'hidden',
  1665. 'name' => '_Token[fields]',
  1666. 'value' => $hash,
  1667. 'autocomplete' => 'off',
  1668. 'data-foo' => 'bar',
  1669. ]],
  1670. ['input' => [
  1671. 'type' => 'hidden',
  1672. 'name' => '_Token[unlocked]',
  1673. 'value' => 'address%7Cfirst_name',
  1674. 'autocomplete' => 'off',
  1675. 'data-foo' => 'bar',
  1676. ]],
  1677. ['input' => [
  1678. 'type' => 'hidden', 'name' => '_Token[debug]',
  1679. 'value' => $tokenDebug,
  1680. 'autocomplete' => 'off',
  1681. 'data-foo' => 'bar'
  1682. ]],
  1683. '/div'
  1684. ];
  1685. $this->assertHtml($expected, $result);
  1686. }
  1687. /**
  1688. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1689. *
  1690. * Test single record form with debugSecurity param.
  1691. *
  1692. * @return void
  1693. */
  1694. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1695. {
  1696. $this->Form->request->params['_Token'] = [
  1697. 'unlockedFields' => ['first_name', 'address']
  1698. ];
  1699. $this->Form->create();
  1700. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1701. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1702. $this->Form->text('Addresses.title');
  1703. $this->Form->text('Addresses.first_name');
  1704. $this->Form->text('Addresses.last_name');
  1705. $this->Form->text('Addresses.address');
  1706. $this->Form->text('Addresses.city');
  1707. $this->Form->text('Addresses.phone');
  1708. $result = $this->Form->fields;
  1709. $expected = [
  1710. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1711. 'Addresses.city', 'Addresses.phone'
  1712. ];
  1713. $this->assertEquals($expected, $result);
  1714. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1715. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1716. $tokenDebug = urlencode(json_encode([
  1717. '/articles/add',
  1718. [
  1719. 'Addresses.id' => '123456',
  1720. 'Addresses.title',
  1721. 'Addresses.last_name',
  1722. 'Addresses.city',
  1723. 'Addresses.phone'
  1724. ],
  1725. [
  1726. 'first_name',
  1727. 'address'
  1728. ]
  1729. ]));
  1730. $expected = [
  1731. 'div' => ['style' => 'display:none;'],
  1732. ['input' => [
  1733. 'type' => 'hidden',
  1734. 'name' => '_Token[fields]',
  1735. 'value' => $hash,
  1736. 'autocomplete' => 'off',
  1737. 'data-foo' => 'bar',
  1738. ]],
  1739. ['input' => [
  1740. 'type' => 'hidden',
  1741. 'name' => '_Token[unlocked]',
  1742. 'value' => 'address%7Cfirst_name',
  1743. 'autocomplete' => 'off',
  1744. 'data-foo' => 'bar',
  1745. ]],
  1746. ['input' => [
  1747. 'type' => 'hidden', 'name' => '_Token[debug]',
  1748. 'value' => $tokenDebug,
  1749. 'autocomplete' => 'off',
  1750. 'data-foo' => 'bar'
  1751. ]],
  1752. '/div'
  1753. ];
  1754. $this->assertHtml($expected, $result);
  1755. }
  1756. /**
  1757. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1758. *
  1759. * Debug is false, debugSecurity is true -> no debug
  1760. *
  1761. * @return void
  1762. */
  1763. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1764. {
  1765. $this->Form->request->params['_Token'] = [
  1766. 'unlockedFields' => ['first_name', 'address']
  1767. ];
  1768. $this->Form->create();
  1769. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1770. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1771. $this->Form->text('Addresses.title');
  1772. $this->Form->text('Addresses.first_name');
  1773. $this->Form->text('Addresses.last_name');
  1774. $this->Form->text('Addresses.address');
  1775. $this->Form->text('Addresses.city');
  1776. $this->Form->text('Addresses.phone');
  1777. $result = $this->Form->fields;
  1778. $expected = [
  1779. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1780. 'Addresses.city', 'Addresses.phone'
  1781. ];
  1782. $this->assertEquals($expected, $result);
  1783. Configure::write('debug', false);
  1784. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1785. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1786. $expected = [
  1787. 'div' => ['style' => 'display:none;'],
  1788. ['input' => [
  1789. 'type' => 'hidden',
  1790. 'name' => '_Token[fields]',
  1791. 'value' => $hash,
  1792. 'autocomplete' => 'off',
  1793. 'data-foo' => 'bar',
  1794. ]],
  1795. ['input' => [
  1796. 'type' => 'hidden',
  1797. 'name' => '_Token[unlocked]',
  1798. 'value' => 'address%7Cfirst_name',
  1799. 'autocomplete' => 'off',
  1800. 'data-foo' => 'bar',
  1801. ]],
  1802. '/div'
  1803. ];
  1804. $this->assertHtml($expected, $result);
  1805. }
  1806. /**
  1807. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1808. *
  1809. * Test single record form with debugSecurity param.
  1810. *
  1811. * @return void
  1812. */
  1813. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1814. {
  1815. $this->Form->request->params['_Token'] = [
  1816. 'unlockedFields' => ['first_name', 'address']
  1817. ];
  1818. $this->Form->create();
  1819. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1820. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1821. $this->Form->text('Addresses.title');
  1822. $this->Form->text('Addresses.first_name');
  1823. $this->Form->text('Addresses.last_name');
  1824. $this->Form->text('Addresses.address');
  1825. $this->Form->text('Addresses.city');
  1826. $this->Form->text('Addresses.phone');
  1827. $result = $this->Form->fields;
  1828. $expected = [
  1829. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1830. 'Addresses.city', 'Addresses.phone'
  1831. ];
  1832. $this->assertEquals($expected, $result);
  1833. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1834. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1835. $expected = [
  1836. 'div' => ['style' => 'display:none;'],
  1837. ['input' => [
  1838. 'type' => 'hidden',
  1839. 'name' => '_Token[fields]',
  1840. 'value' => $hash,
  1841. 'autocomplete' => 'off',
  1842. 'data-foo' => 'bar',
  1843. ]],
  1844. ['input' => [
  1845. 'type' => 'hidden',
  1846. 'name' => '_Token[unlocked]',
  1847. 'value' => 'address%7Cfirst_name',
  1848. 'autocomplete' => 'off',
  1849. 'data-foo' => 'bar',
  1850. ]],
  1851. '/div'
  1852. ];
  1853. $this->assertHtml($expected, $result);
  1854. }
  1855. /**
  1856. * testFormSecureWithCustomNameAttribute method
  1857. *
  1858. * Test securing inputs with custom name attributes.
  1859. *
  1860. * @return void
  1861. */
  1862. public function testFormSecureWithCustomNameAttribute()
  1863. {
  1864. $this->Form->request->params['_Token'] = 'testKey';
  1865. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1866. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1867. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1868. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1869. }
  1870. /**
  1871. * testFormSecuredControl method
  1872. *
  1873. * Test generation of entire secure form, assertions made on control() output.
  1874. *
  1875. * @return void
  1876. */
  1877. public function testFormSecuredControl()
  1878. {
  1879. $this->Form->request->params['_csrfToken'] = 'testKey';
  1880. $this->Form->request->params['_Token'] = 'stuff';
  1881. $this->article['schema'] = [
  1882. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1883. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1884. ];
  1885. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1886. $encoding = strtolower(Configure::read('App.encoding'));
  1887. $expected = [
  1888. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1889. 'div' => ['style' => 'display:none;'],
  1890. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1891. ['input' => [
  1892. 'type' => 'hidden',
  1893. 'name' => '_csrfToken',
  1894. 'value' => 'testKey',
  1895. 'autocomplete' => 'off'
  1896. ]],
  1897. '/div'
  1898. ];
  1899. $this->assertHtml($expected, $result);
  1900. $result = $this->Form->control('ratio');
  1901. $expected = [
  1902. 'div' => ['class'],
  1903. 'label' => ['for'],
  1904. 'Ratio',
  1905. '/label',
  1906. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1907. '/div',
  1908. ];
  1909. $this->assertHtml($expected, $result);
  1910. $result = $this->Form->control('population');
  1911. $expected = [
  1912. 'div' => ['class'],
  1913. 'label' => ['for'],
  1914. 'Population',
  1915. '/label',
  1916. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1917. '/div',
  1918. ];
  1919. $this->assertHtml($expected, $result);
  1920. $result = $this->Form->control('published', ['type' => 'text']);
  1921. $expected = [
  1922. 'div' => ['class' => 'input text'],
  1923. 'label' => ['for' => 'published'],
  1924. 'Published',
  1925. '/label',
  1926. ['input' => [
  1927. 'type' => 'text',
  1928. 'name' => 'published',
  1929. 'id' => 'published'
  1930. ]],
  1931. '/div'
  1932. ];
  1933. $this->assertHtml($expected, $result);
  1934. $result = $this->Form->control('other', ['type' => 'text']);
  1935. $expected = [
  1936. 'div' => ['class' => 'input text'],
  1937. 'label' => ['for' => 'other'],
  1938. 'Other',
  1939. '/label',
  1940. ['input' => [
  1941. 'type' => 'text',
  1942. 'name' => 'other',
  1943. 'id',
  1944. ]],
  1945. '/div'
  1946. ];
  1947. $this->assertHtml($expected, $result);
  1948. $result = $this->Form->hidden('stuff');
  1949. $expected = [
  1950. 'input' => [
  1951. 'type' => 'hidden',
  1952. 'name' => 'stuff'
  1953. ]
  1954. ];
  1955. $this->assertHtml($expected, $result);
  1956. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1957. $expected = ['input' => [
  1958. 'type' => 'hidden',
  1959. 'name' => 'hidden',
  1960. 'value' => '0'
  1961. ]];
  1962. $this->assertHtml($expected, $result);
  1963. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1964. $expected = [
  1965. 'div' => ['class' => 'input checkbox'],
  1966. ['input' => [
  1967. 'type' => 'hidden',
  1968. 'name' => 'something',
  1969. 'value' => '0'
  1970. ]],
  1971. 'label' => ['for' => 'something'],
  1972. ['input' => [
  1973. 'type' => 'checkbox',
  1974. 'name' => 'something',
  1975. 'value' => '1',
  1976. 'id' => 'something'
  1977. ]],
  1978. 'Something',
  1979. '/label',
  1980. '/div'
  1981. ];
  1982. $this->assertHtml($expected, $result);
  1983. $result = $this->Form->fields;
  1984. $expectedFields = [
  1985. 'ratio',
  1986. 'population',
  1987. 'published',
  1988. 'other',
  1989. 'stuff' => '',
  1990. 'hidden' => '0',
  1991. 'something'
  1992. ];
  1993. $this->assertEquals($expectedFields, $result);
  1994. $result = $this->Form->secure($this->Form->fields);
  1995. $tokenDebug = urlencode(json_encode([
  1996. '/articles/add',
  1997. $expectedFields,
  1998. []
  1999. ]));
  2000. $expected = [
  2001. 'div' => ['style' => 'display:none;'],
  2002. ['input' => [
  2003. 'type' => 'hidden',
  2004. 'name' => '_Token[fields]',
  2005. 'value',
  2006. 'autocomplete'
  2007. ]],
  2008. ['input' => [
  2009. 'type' => 'hidden',
  2010. 'name' => '_Token[unlocked]',
  2011. 'value' => '',
  2012. 'autocomplete' => 'off'
  2013. ]],
  2014. ['input' => [
  2015. 'type' => 'hidden', 'name' => '_Token[debug]',
  2016. 'value' => $tokenDebug,
  2017. 'autocomplete' => 'off'
  2018. ]],
  2019. '/div'
  2020. ];
  2021. $this->assertHtml($expected, $result);
  2022. }
  2023. /**
  2024. * testSecuredControlCustomName method
  2025. *
  2026. * Test secured inputs with custom names.
  2027. *
  2028. * @return void
  2029. */
  2030. public function testSecuredControlCustomName()
  2031. {
  2032. $this->Form->request->params['_Token'] = 'testKey';
  2033. $this->assertEquals([], $this->Form->fields);
  2034. $this->Form->text('text_input', [
  2035. 'name' => 'Option[General.default_role]',
  2036. ]);
  2037. $expected = ['Option.General.default_role'];
  2038. $this->assertEquals($expected, $this->Form->fields);
  2039. $this->Form->select('select_box', [1, 2], [
  2040. 'name' => 'Option[General.select_role]',
  2041. ]);
  2042. $expected[] = 'Option.General.select_role';
  2043. $this->assertEquals($expected, $this->Form->fields);
  2044. $this->Form->text('other.things[]');
  2045. $expected[] = 'other.things';
  2046. $this->assertEquals($expected, $this->Form->fields);
  2047. }
  2048. /**
  2049. * testSecuredControlDuplicate method
  2050. *
  2051. * Test that a hidden field followed by a visible field
  2052. * undoes the hidden field locking.
  2053. *
  2054. * @return void
  2055. */
  2056. public function testSecuredControlDuplicate()
  2057. {
  2058. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2059. $this->assertEquals([], $this->Form->fields);
  2060. $this->Form->control('text_val', [
  2061. 'type' => 'hidden',
  2062. 'value' => 'some text',
  2063. ]);
  2064. $expected = ['text_val' => 'some text'];
  2065. $this->assertEquals($expected, $this->Form->fields);
  2066. $this->Form->control('text_val', [
  2067. 'type' => 'text',
  2068. ]);
  2069. $expected = ['text_val'];
  2070. $this->assertEquals($expected, $this->Form->fields);
  2071. }
  2072. /**
  2073. * testFormSecuredFileControl method
  2074. *
  2075. * Tests that the correct keys are added to the field hash index.
  2076. *
  2077. * @return void
  2078. */
  2079. public function testFormSecuredFileControl()
  2080. {
  2081. $this->assertEquals([], $this->Form->fields);
  2082. $this->Form->file('Attachment.file');
  2083. $expected = [
  2084. 'Attachment.file.name', 'Attachment.file.type',
  2085. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2086. 'Attachment.file.size'
  2087. ];
  2088. $this->assertEquals($expected, $this->Form->fields);
  2089. }
  2090. /**
  2091. * testFormSecuredMultipleSelect method
  2092. *
  2093. * Test that multiple selects keys are added to field hash.
  2094. *
  2095. * @return void
  2096. */
  2097. public function testFormSecuredMultipleSelect()
  2098. {
  2099. $this->Form->request->params['_csrfToken'] = 'testKey';
  2100. $this->assertEquals([], $this->Form->fields);
  2101. $options = ['1' => 'one', '2' => 'two'];
  2102. $this->Form->select('Model.select', $options);
  2103. $expected = ['Model.select'];
  2104. $this->assertEquals($expected, $this->Form->fields);
  2105. $this->Form->fields = [];
  2106. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2107. $this->assertEquals($expected, $this->Form->fields);
  2108. }
  2109. /**
  2110. * testFormSecuredRadio method
  2111. *
  2112. * @return void
  2113. */
  2114. public function testFormSecuredRadio()
  2115. {
  2116. $this->Form->request->params['_Token'] = 'testKey';
  2117. $this->assertEquals([], $this->Form->fields);
  2118. $options = ['1' => 'option1', '2' => 'option2'];
  2119. $this->Form->radio('Test.test', $options);
  2120. $expected = ['Test.test'];
  2121. $this->assertEquals($expected, $this->Form->fields);
  2122. $this->Form->radio('Test.all', $options, [
  2123. 'disabled' => ['option1', 'option2']
  2124. ]);
  2125. $expected = ['Test.test', 'Test.all' => ''];
  2126. $this->assertEquals($expected, $this->Form->fields);
  2127. $this->Form->radio('Test.some', $options, [
  2128. 'disabled' => ['option1']
  2129. ]);
  2130. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2131. $this->assertEquals($expected, $this->Form->fields);
  2132. }
  2133. /**
  2134. * testFormSecuredAndDisabledNotAssoc method
  2135. *
  2136. * Test that when disabled is in a list based attribute array it works.
  2137. *
  2138. * @return void
  2139. */
  2140. public function testFormSecuredAndDisabledNotAssoc()
  2141. {
  2142. $this->Form->request->params['_Token'] = 'testKey';
  2143. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2144. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2145. $this->Form->text('Model.text', ['disabled']);
  2146. $this->Form->textarea('Model.textarea', ['disabled']);
  2147. $this->Form->password('Model.password', ['disabled']);
  2148. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2149. $expected = [
  2150. 'Model.radio' => ''
  2151. ];
  2152. $this->assertEquals($expected, $this->Form->fields);
  2153. }
  2154. /**
  2155. * testFormSecuredAndDisabled method
  2156. *
  2157. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2158. * hashing.
  2159. *
  2160. * @return void
  2161. */
  2162. public function testFormSecuredAndDisabled()
  2163. {
  2164. $this->Form->request->params['_Token'] = 'testKey';
  2165. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2166. $this->Form->text('Model.text', ['disabled' => true]);
  2167. $this->Form->password('Model.text', ['disabled' => true]);
  2168. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2169. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2170. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2171. $this->Form->year('Model.year', ['disabled' => true]);
  2172. $this->Form->month('Model.month', ['disabled' => true]);
  2173. $this->Form->day('Model.day', ['disabled' => true]);
  2174. $this->Form->hour('Model.hour', ['disabled' => true]);
  2175. $this->Form->minute('Model.minute', ['disabled' => true]);
  2176. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2177. $expected = [
  2178. 'Model.radio' => ''
  2179. ];
  2180. $this->assertEquals($expected, $this->Form->fields);
  2181. }
  2182. /**
  2183. * testDisableSecurityUsingForm method
  2184. *
  2185. * @return void
  2186. */
  2187. public function testDisableSecurityUsingForm()
  2188. {
  2189. $this->Form->request->params['_Token'] = [
  2190. 'disabledFields' => []
  2191. ];
  2192. $this->Form->create();
  2193. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2194. $this->Form->text('Addresses.title');
  2195. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2196. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2197. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2198. $result = $this->Form->fields;
  2199. $expected = [
  2200. 'Addresses.id' => '123456', 'Addresses.title',
  2201. ];
  2202. $this->assertEquals($expected, $result);
  2203. }
  2204. /**
  2205. * testUnlockFieldAddsToList method
  2206. *
  2207. * Test disableField.
  2208. *
  2209. * @return void
  2210. */
  2211. public function testUnlockFieldAddsToList()
  2212. {
  2213. $this->Form->request->params['_Token'] = [
  2214. 'unlockedFields' => []
  2215. ];
  2216. $this->Form->unlockField('Contact.name');
  2217. $this->Form->text('Contact.name');
  2218. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2219. $this->assertEquals([], $this->Form->fields);
  2220. }
  2221. /**
  2222. * testUnlockFieldRemovingFromFields method
  2223. *
  2224. * Test unlockField removing from fields array.
  2225. *
  2226. * @return void
  2227. */
  2228. public function testUnlockFieldRemovingFromFields()
  2229. {
  2230. $this->Form->request->params['_Token'] = [
  2231. 'unlockedFields' => []
  2232. ];
  2233. $this->Form->create($this->article);
  2234. $this->Form->hidden('Article.id', ['value' => 1]);
  2235. $this->Form->text('Article.title');
  2236. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2237. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  2238. $this->Form->unlockField('Article.title');
  2239. $this->Form->unlockField('Article.id');
  2240. $this->assertEquals([], $this->Form->fields);
  2241. }
  2242. /**
  2243. * testResetUnlockFields method
  2244. *
  2245. * Test reset unlockFields, when create new form.
  2246. *
  2247. * @return void
  2248. */
  2249. public function testResetUnlockFields()
  2250. {
  2251. $this->Form->request->params['_Token'] = [
  2252. 'key' => 'testKey',
  2253. 'unlockedFields' => []
  2254. ];
  2255. $this->Form->unlockField('Contact.id');
  2256. $this->Form->create('Contact');
  2257. $this->Form->hidden('Contact.id', ['value' => 1]);
  2258. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2259. $this->Form->end();
  2260. $this->Form->create('Contact');
  2261. $this->Form->hidden('Contact.id', ['value' => 1]);
  2262. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2263. }
  2264. /**
  2265. * testSecuredFormUrlIgnoresHost method
  2266. *
  2267. * Test that only the path + query elements of a form's URL show up in their hash.
  2268. *
  2269. * @return void
  2270. */
  2271. public function testSecuredFormUrlIgnoresHost()
  2272. {
  2273. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2274. $expected = '0ff0c85cd70584d8fd18fa136846d22c66c21e2d%3A';
  2275. $this->Form->create($this->article, [
  2276. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2277. ]);
  2278. $result = $this->Form->secure();
  2279. $this->assertContains($expected, $result);
  2280. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2281. $result = $this->Form->secure();
  2282. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2283. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2284. $result = $this->Form->secure();
  2285. $this->assertContains($expected, $result, 'URL path + query should work.');
  2286. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2287. $result = $this->Form->secure();
  2288. $this->assertNotContains($expected, $result, 'URL is different');
  2289. }
  2290. /**
  2291. * testSecuredFormUrlHasHtmlAndIdentifier method
  2292. *
  2293. * Test that URL, HTML and identifier show up in their hashes.
  2294. *
  2295. * @return void
  2296. */
  2297. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2298. {
  2299. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2300. $expected = 'ece0693fb1b19ca116133db1832ac29baaf41ce5%3A';
  2301. $this->Form->create($this->article, [
  2302. 'url' => [
  2303. 'controller' => 'articles',
  2304. 'action' => 'view',
  2305. '?' => [
  2306. 'page' => 1,
  2307. 'limit' => 10,
  2308. 'html' => '<>"',
  2309. ],
  2310. '#' => 'result',
  2311. ],
  2312. ]);
  2313. $result = $this->Form->secure();
  2314. $this->assertContains($expected, $result);
  2315. $this->Form->create($this->article, [
  2316. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2317. ]);
  2318. $result = $this->Form->secure();
  2319. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2320. $this->Form->create($this->article, [
  2321. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2322. ]);
  2323. $result = $this->Form->secure();
  2324. $this->assertContains($expected, $result, 'URL path + query should work.');
  2325. }
  2326. /**
  2327. * testErrorMessageDisplay method
  2328. *
  2329. * Test error message display.
  2330. *
  2331. * @return void
  2332. */
  2333. public function testErrorMessageDisplay()
  2334. {
  2335. $this->article['errors'] = [
  2336. 'Article' => [
  2337. 'title' => 'error message',
  2338. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2339. ]
  2340. ];
  2341. $this->Form->create($this->article);
  2342. $result = $this->Form->control('Article.title');
  2343. $expected = [
  2344. 'div' => ['class' => 'input text error'],
  2345. 'label' => ['for' => 'article-title'],
  2346. 'Title',
  2347. '/label',
  2348. 'input' => [
  2349. 'type' => 'text', 'name' => 'Article[title]',
  2350. 'id' => 'article-title', 'class' => 'form-error'
  2351. ],
  2352. ['div' => ['class' => 'error-message']],
  2353. 'error message',
  2354. '/div',
  2355. '/div'
  2356. ];
  2357. $this->assertHtml($expected, $result);
  2358. $result = $this->Form->control('Article.title', [
  2359. 'templates' => [
  2360. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2361. ]
  2362. ]);
  2363. $expected = [
  2364. 'div' => ['class' => 'input text error'],
  2365. 'label' => ['for' => 'article-title'],
  2366. 'Title',
  2367. '/label',
  2368. 'input' => [
  2369. 'type' => 'text', 'name' => 'Article[title]',
  2370. 'id' => 'article-title', 'class' => 'form-error'
  2371. ],
  2372. '/div'
  2373. ];
  2374. $this->assertHtml($expected, $result);
  2375. $result = $this->Form->control('Article.content');
  2376. $expected = [
  2377. 'div' => ['class' => 'input text error'],
  2378. 'label' => ['for' => 'article-content'],
  2379. 'Content',
  2380. '/label',
  2381. 'input' => [
  2382. 'type' => 'text', 'name' => 'Article[content]',
  2383. 'id' => 'article-content', 'class' => 'form-error'
  2384. ],
  2385. ['div' => ['class' => 'error-message']],
  2386. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2387. '/div',
  2388. '/div'
  2389. ];
  2390. $this->assertHtml($expected, $result);
  2391. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2392. $expected = [
  2393. 'div' => ['class' => 'input text error'],
  2394. 'label' => ['for' => 'article-content'],
  2395. 'Content',
  2396. '/label',
  2397. 'input' => [
  2398. 'type' => 'text', 'name' => 'Article[content]',
  2399. 'id' => 'article-content', 'class' => 'form-error'
  2400. ],
  2401. ['div' => ['class' => 'error-message']],
  2402. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2403. '/div',
  2404. '/div'
  2405. ];
  2406. $this->assertHtml($expected, $result);
  2407. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2408. $expected = [
  2409. 'div' => ['class' => 'input text error'],
  2410. 'label' => ['for' => 'article-content'],
  2411. 'Content',
  2412. '/label',
  2413. 'input' => [
  2414. 'type' => 'text', 'name' => 'Article[content]',
  2415. 'id' => 'article-content', 'class' => 'form-error'
  2416. ],
  2417. ['div' => ['class' => 'error-message']],
  2418. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2419. '/div',
  2420. '/div'
  2421. ];
  2422. $this->assertHtml($expected, $result);
  2423. }
  2424. /**
  2425. * testEmptyErrorValidation method
  2426. *
  2427. * Test validation errors, when validation message is an empty string.
  2428. *
  2429. * @return void
  2430. */
  2431. public function testEmptyErrorValidation()
  2432. {
  2433. $this->article['errors'] = [
  2434. 'Article' => ['title' => '']
  2435. ];
  2436. $this->Form->create($this->article);
  2437. $result = $this->Form->control('Article.title');
  2438. $expected = [
  2439. 'div' => ['class' => 'input text error'],
  2440. 'label' => ['for' => 'article-title'],
  2441. 'Title',
  2442. '/label',
  2443. 'input' => [
  2444. 'type' => 'text', 'name' => 'Article[title]',
  2445. 'id' => 'article-title', 'class' => 'form-error'
  2446. ],
  2447. ['div' => ['class' => 'error-message']],
  2448. [],
  2449. '/div',
  2450. '/div'
  2451. ];
  2452. $this->assertHtml($expected, $result);
  2453. }
  2454. /**
  2455. * testEmptyControlErrorValidation method
  2456. *
  2457. * Test validation errors, when calling control() overriding validation message by an empty string.
  2458. *
  2459. * @return void
  2460. */
  2461. public function testEmptyControlErrorValidation()
  2462. {
  2463. $this->article['errors'] = [
  2464. 'Article' => ['title' => 'error message']
  2465. ];
  2466. $this->Form->create($this->article);
  2467. $result = $this->Form->control('Article.title', ['error' => '']);
  2468. $expected = [
  2469. 'div' => ['class' => 'input text error'],
  2470. 'label' => ['for' => 'article-title'],
  2471. 'Title',
  2472. '/label',
  2473. 'input' => [
  2474. 'type' => 'text', 'name' => 'Article[title]',
  2475. 'id' => 'article-title', 'class' => 'form-error'
  2476. ],
  2477. ['div' => ['class' => 'error-message']],
  2478. [],
  2479. '/div',
  2480. '/div'
  2481. ];
  2482. $this->assertHtml($expected, $result);
  2483. }
  2484. /**
  2485. * testControlErrorMessage method
  2486. *
  2487. * Test validation errors, when calling control() overriding validation messages.
  2488. *
  2489. * @return void
  2490. */
  2491. public function testControlErrorMessage()
  2492. {
  2493. $this->article['errors'] = [
  2494. 'title' => ['error message']
  2495. ];
  2496. $this->Form->create($this->article);
  2497. $result = $this->Form->control('title', [
  2498. 'error' => 'Custom error!'
  2499. ]);
  2500. $expected = [
  2501. 'div' => ['class' => 'input text required error'],
  2502. 'label' => ['for' => 'title'],
  2503. 'Title',
  2504. '/label',
  2505. 'input' => [
  2506. 'type' => 'text', 'name' => 'title',
  2507. 'id' => 'title', 'class' => 'form-error',
  2508. 'required' => 'required',
  2509. ],
  2510. ['div' => ['class' => 'error-message']],
  2511. 'Custom error!',
  2512. '/div',
  2513. '/div'
  2514. ];
  2515. $this->assertHtml($expected, $result);
  2516. $result = $this->Form->control('title', [
  2517. 'error' => ['error message' => 'Custom error!']
  2518. ]);
  2519. $expected = [
  2520. 'div' => ['class' => 'input text required error'],
  2521. 'label' => ['for' => 'title'],
  2522. 'Title',
  2523. '/label',
  2524. 'input' => [
  2525. 'type' => 'text',
  2526. 'name' => 'title',
  2527. 'id' => 'title',
  2528. 'class' => 'form-error',
  2529. 'required' => 'required'
  2530. ],
  2531. ['div' => ['class' => 'error-message']],
  2532. 'Custom error!',
  2533. '/div',
  2534. '/div'
  2535. ];
  2536. $this->assertHtml($expected, $result);
  2537. }
  2538. /**
  2539. * testFormValidationAssociated method
  2540. *
  2541. * Tests displaying errors for nested entities.
  2542. *
  2543. * @return void
  2544. */
  2545. public function testFormValidationAssociated()
  2546. {
  2547. $nested = new Entity(['foo' => 'bar']);
  2548. $nested->errors('foo', ['not a valid bar']);
  2549. $entity = new Entity(['nested' => $nested]);
  2550. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2551. $result = $this->Form->error('nested.foo');
  2552. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2553. }
  2554. /**
  2555. * testFormValidationAssociatedSecondLevel method
  2556. *
  2557. * Test form error display with associated model.
  2558. *
  2559. * @return void
  2560. */
  2561. public function testFormValidationAssociatedSecondLevel()
  2562. {
  2563. $inner = new Entity(['bar' => 'baz']);
  2564. $nested = new Entity(['foo' => $inner]);
  2565. $entity = new Entity(['nested' => $nested]);
  2566. $inner->errors('bar', ['not a valid one']);
  2567. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2568. $result = $this->Form->error('nested.foo.bar');
  2569. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2570. }
  2571. /**
  2572. * testFormValidationMultiRecord method
  2573. *
  2574. * Test form error display with multiple records.
  2575. *
  2576. * @return void
  2577. */
  2578. public function testFormValidationMultiRecord()
  2579. {
  2580. $one = new Entity();
  2581. $two = new Entity();
  2582. TableRegistry::get('Contacts', [
  2583. 'className' => __NAMESPACE__ . '\ContactsTable'
  2584. ]);
  2585. $one->set('email', '');
  2586. $one->errors('email', ['invalid email']);
  2587. $two->set('name', '');
  2588. $two->errors('name', ['This is wrong']);
  2589. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2590. $result = $this->Form->control('0.email');
  2591. $expected = [
  2592. 'div' => ['class' => 'input email error'],
  2593. 'label' => ['for' => '0-email'],
  2594. 'Email',
  2595. '/label',
  2596. 'input' => [
  2597. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2598. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2599. ],
  2600. ['div' => ['class' => 'error-message']],
  2601. 'invalid email',
  2602. '/div',
  2603. '/div'
  2604. ];
  2605. $this->assertHtml($expected, $result);
  2606. $result = $this->Form->control('1.name');
  2607. $expected = [
  2608. 'div' => ['class' => 'input text error'],
  2609. 'label' => ['for' => '1-name'],
  2610. 'Name',
  2611. '/label',
  2612. 'input' => [
  2613. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2614. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2615. ],
  2616. ['div' => ['class' => 'error-message']],
  2617. 'This is wrong',
  2618. '/div',
  2619. '/div'
  2620. ];
  2621. $this->assertHtml($expected, $result);
  2622. }
  2623. /**
  2624. * testControl method
  2625. *
  2626. * Test various incarnations of control().
  2627. *
  2628. * @return void
  2629. */
  2630. public function testControl()
  2631. {
  2632. TableRegistry::get('ValidateUsers', [
  2633. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2634. ]);
  2635. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2636. $result = $this->Form->control('ValidateUsers.balance');
  2637. $expected = [
  2638. 'div' => ['class'],
  2639. 'label' => ['for'],
  2640. 'Balance',
  2641. '/label',
  2642. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2643. '/div',
  2644. ];
  2645. $this->assertHtml($expected, $result);
  2646. $result = $this->Form->control('ValidateUser.cost_decimal');
  2647. $expected = [
  2648. 'div' => ['class'],
  2649. 'label' => ['for'],
  2650. 'Cost Decimal',
  2651. '/label',
  2652. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2653. '/div',
  2654. ];
  2655. $this->assertHtml($expected, $result);
  2656. $result = $this->Form->control('ValidateUser.null_decimal');
  2657. $expected = [
  2658. 'div' => ['class'],
  2659. 'label' => ['for'],
  2660. 'Null Decimal',
  2661. '/label',
  2662. 'input' => ['name', 'type' => 'number', 'id'],
  2663. '/div',
  2664. ];
  2665. $this->assertHtml($expected, $result);
  2666. }
  2667. /**
  2668. * testControlCustomization method
  2669. *
  2670. * Tests the input method and passing custom options.
  2671. *
  2672. * @return void
  2673. */
  2674. public function testControlCustomization()
  2675. {
  2676. TableRegistry::get('Contacts', [
  2677. 'className' => __NAMESPACE__ . '\ContactsTable'
  2678. ]);
  2679. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2680. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2681. $expected = [
  2682. 'div' => ['class' => 'input email'],
  2683. 'label' => ['for' => 'custom'],
  2684. 'Email',
  2685. '/label',
  2686. ['input' => [
  2687. 'type' => 'email', 'name' => 'Contact[email]',
  2688. 'id' => 'custom', 'maxlength' => 255
  2689. ]],
  2690. '/div'
  2691. ];
  2692. $this->assertHtml($expected, $result);
  2693. $result = $this->Form->control('Contact.email', [
  2694. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2695. ]);
  2696. $expected = [
  2697. '<div',
  2698. 'label' => ['for' => 'contact-email'],
  2699. 'Email',
  2700. '/label',
  2701. ['input' => [
  2702. 'type' => 'email', 'name' => 'Contact[email]',
  2703. 'id' => 'contact-email', 'maxlength' => 255
  2704. ]],
  2705. '/div'
  2706. ];
  2707. $this->assertHtml($expected, $result);
  2708. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2709. $expected = [
  2710. 'div' => ['class' => 'input text'],
  2711. 'label' => ['for' => 'contact-email'],
  2712. 'Email',
  2713. '/label',
  2714. ['input' => [
  2715. 'type' => 'text', 'name' => 'Contact[email]',
  2716. 'id' => 'contact-email', 'maxlength' => '255'
  2717. ]],
  2718. '/div'
  2719. ];
  2720. $this->assertHtml($expected, $result);
  2721. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2722. $expected = [
  2723. 'div' => ['class' => 'input text'],
  2724. 'label' => ['for' => 'contact-5-email'],
  2725. 'Email',
  2726. '/label',
  2727. ['input' => [
  2728. 'type' => 'text', 'name' => 'Contact[5][email]',
  2729. 'id' => 'contact-5-email', 'maxlength' => '255'
  2730. ]],
  2731. '/div'
  2732. ];
  2733. $this->assertHtml($expected, $result);
  2734. $result = $this->Form->control('Contact.password');
  2735. $expected = [
  2736. 'div' => ['class' => 'input password'],
  2737. 'label' => ['for' => 'contact-password'],
  2738. 'Password',
  2739. '/label',
  2740. ['input' => [
  2741. 'type' => 'password', 'name' => 'Contact[password]',
  2742. 'id' => 'contact-password'
  2743. ]],
  2744. '/div'
  2745. ];
  2746. $this->assertHtml($expected, $result);
  2747. $result = $this->Form->control('Contact.email', [
  2748. 'type' => 'file', 'class' => 'textbox'
  2749. ]);
  2750. $expected = [
  2751. 'div' => ['class' => 'input file'],
  2752. 'label' => ['for' => 'contact-email'],
  2753. 'Email',
  2754. '/label',
  2755. ['input' => [
  2756. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2757. 'id' => 'contact-email'
  2758. ]],
  2759. '/div'
  2760. ];
  2761. $this->assertHtml($expected, $result);
  2762. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2763. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2764. $result = $this->Form->control('phone');
  2765. $expected = [
  2766. 'div' => ['class' => 'input tel'],
  2767. 'label' => ['for' => 'phone'],
  2768. 'Phone',
  2769. '/label',
  2770. ['input' => [
  2771. 'type' => 'tel', 'name' => 'phone',
  2772. 'value' => 'Hello &amp; World &gt; weird chars',
  2773. 'id' => 'phone', 'maxlength' => 255
  2774. ]],
  2775. '/div'
  2776. ];
  2777. $this->assertHtml($expected, $result);
  2778. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2779. $this->Form->create();
  2780. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2781. $expected = [
  2782. 'div' => ['class' => 'input text'],
  2783. 'label' => ['for' => 'myId'],
  2784. 'Field',
  2785. '/label',
  2786. 'input' => [
  2787. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2788. 'value' => 'My value', 'id' => 'myId'
  2789. ],
  2790. '/div'
  2791. ];
  2792. $this->assertHtml($expected, $result);
  2793. $this->Form->request->data = [];
  2794. $entity->errors('field', 'Badness!');
  2795. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2796. $result = $this->Form->control('field');
  2797. $expected = [
  2798. 'div' => ['class' => 'input text error'],
  2799. 'label' => ['for' => 'field'],
  2800. 'Field',
  2801. '/label',
  2802. 'input' => [
  2803. 'type' => 'text', 'name' => 'field',
  2804. 'id' => 'field', 'class' => 'form-error'
  2805. ],
  2806. ['div' => ['class' => 'error-message']],
  2807. 'Badness!',
  2808. '/div',
  2809. '/div'
  2810. ];
  2811. $this->assertHtml($expected, $result);
  2812. $result = $this->Form->control('field', [
  2813. 'templates' => [
  2814. 'inputContainerError' => '{{content}}{{error}}',
  2815. 'error' => '<span class="error-message">{{content}}</span>'
  2816. ]
  2817. ]);
  2818. $expected = [
  2819. 'label' => ['for' => 'field'],
  2820. 'Field',
  2821. '/label',
  2822. 'input' => [
  2823. 'type' => 'text', 'name' => 'field',
  2824. 'id' => 'field', 'class' => 'form-error'
  2825. ],
  2826. ['span' => ['class' => 'error-message']],
  2827. 'Badness!',
  2828. '/span'
  2829. ];
  2830. $this->assertHtml($expected, $result);
  2831. $entity->errors('field', ['minLength'], true);
  2832. $result = $this->Form->control('field', [
  2833. 'error' => [
  2834. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2835. 'maxLength' => 'login too large'
  2836. ]
  2837. ]);
  2838. $expected = [
  2839. 'div' => ['class' => 'input text error'],
  2840. 'label' => ['for' => 'field'],
  2841. 'Field',
  2842. '/label',
  2843. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2844. ['div' => ['class' => 'error-message']],
  2845. 'Le login doit contenir au moins 2 caractères',
  2846. '/div',
  2847. '/div'
  2848. ];
  2849. $this->assertHtml($expected, $result);
  2850. $entity->errors('field', ['maxLength'], true);
  2851. $result = $this->Form->control('field', [
  2852. 'error' => [
  2853. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2854. 'maxLength' => 'login too large',
  2855. ]
  2856. ]);
  2857. $expected = [
  2858. 'div' => ['class' => 'input text error'],
  2859. 'label' => ['for' => 'field'],
  2860. 'Field',
  2861. '/label',
  2862. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2863. ['div' => ['class' => 'error-message']],
  2864. 'login too large',
  2865. '/div',
  2866. '/div'
  2867. ];
  2868. $this->assertHtml($expected, $result);
  2869. }
  2870. /**
  2871. * testControlWithTemplateFile method
  2872. *
  2873. * Test that control() accepts a template file.
  2874. *
  2875. * @return void
  2876. */
  2877. public function testControlWithTemplateFile()
  2878. {
  2879. $result = $this->Form->control('field', [
  2880. 'templates' => 'htmlhelper_tags'
  2881. ]);
  2882. $expected = [
  2883. 'label' => ['for' => 'field'],
  2884. 'Field',
  2885. '/label',
  2886. 'input' => [
  2887. 'type' => 'text', 'name' => 'field',
  2888. 'id' => 'field'
  2889. ],
  2890. ];
  2891. $this->assertHtml($expected, $result);
  2892. }
  2893. /**
  2894. * testNestedControlsEndWithBrackets method
  2895. *
  2896. * Test that nested inputs end with brackets.
  2897. *
  2898. * @return void
  2899. */
  2900. public function testNestedControlsEndWithBrackets()
  2901. {
  2902. $result = $this->Form->text('nested.text[]');
  2903. $expected = [
  2904. 'input' => [
  2905. 'type' => 'text', 'name' => 'nested[text][]'
  2906. ],
  2907. ];
  2908. $this->assertHtml($expected, $result);
  2909. $result = $this->Form->file('nested.file[]');
  2910. $expected = [
  2911. 'input' => [
  2912. 'type' => 'file', 'name' => 'nested[file][]'
  2913. ],
  2914. ];
  2915. $this->assertHtml($expected, $result);
  2916. }
  2917. /**
  2918. * testCreateIdPrefix method
  2919. *
  2920. * Test id prefix.
  2921. *
  2922. * @return void
  2923. */
  2924. public function testCreateIdPrefix()
  2925. {
  2926. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2927. $result = $this->Form->control('field');
  2928. $expected = [
  2929. 'div' => ['class' => 'input text'],
  2930. 'label' => ['for' => 'prefix-field'],
  2931. 'Field',
  2932. '/label',
  2933. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2934. '/div'
  2935. ];
  2936. $this->assertHtml($expected, $result);
  2937. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2938. $expected = [
  2939. 'div' => ['class' => 'input text'],
  2940. 'label' => ['for' => 'custom-id'],
  2941. 'Field',
  2942. '/label',
  2943. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2944. '/div'
  2945. ];
  2946. $this->assertHtml($expected, $result);
  2947. $result = $this->Form->radio('Model.field', ['option A']);
  2948. $expected = [
  2949. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2950. 'label' => ['for' => 'prefix-model-field-0'],
  2951. ['input' => [
  2952. 'type' => 'radio',
  2953. 'name' => 'Model[field]',
  2954. 'value' => '0',
  2955. 'id' => 'prefix-model-field-0'
  2956. ]],
  2957. 'option A',
  2958. '/label'
  2959. ];
  2960. $this->assertHtml($expected, $result);
  2961. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2962. $expected = [
  2963. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2964. 'label' => ['for' => 'prefix-model-field-0'],
  2965. ['input' => [
  2966. 'type' => 'radio',
  2967. 'name' => 'Model[field]',
  2968. 'value' => '0',
  2969. 'id' => 'prefix-model-field-0'
  2970. ]],
  2971. 'option A',
  2972. '/label'
  2973. ];
  2974. $this->assertHtml($expected, $result);
  2975. $result = $this->Form->select(
  2976. 'Model.multi_field',
  2977. ['first'],
  2978. ['multiple' => 'checkbox']
  2979. );
  2980. $expected = [
  2981. 'input' => [
  2982. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2983. ],
  2984. ['div' => ['class' => 'checkbox']],
  2985. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2986. ['input' => [
  2987. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2988. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2989. ]],
  2990. 'first',
  2991. '/label',
  2992. '/div',
  2993. ];
  2994. $this->assertHtml($expected, $result);
  2995. $this->Form->end();
  2996. $result = $this->Form->control('field');
  2997. $expected = [
  2998. 'div' => ['class' => 'input text'],
  2999. 'label' => ['for' => 'field'],
  3000. 'Field',
  3001. '/label',
  3002. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  3003. '/div'
  3004. ];
  3005. $this->assertHtml($expected, $result);
  3006. }
  3007. /**
  3008. * testControlZero method
  3009. *
  3010. * Test that inputs with 0 can be created.
  3011. *
  3012. * @return void
  3013. */
  3014. public function testControlZero()
  3015. {
  3016. TableRegistry::get('Contacts', [
  3017. 'className' => __NAMESPACE__ . '\ContactsTable'
  3018. ]);
  3019. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  3020. $result = $this->Form->control('0');
  3021. $expected = [
  3022. 'div' => ['class' => 'input text'],
  3023. 'label' => ['for' => '0'], '/label',
  3024. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  3025. '/div'
  3026. ];
  3027. $this->assertHtml($expected, $result);
  3028. }
  3029. /**
  3030. * testControlCheckbox method
  3031. *
  3032. * Test control() with checkbox creation.
  3033. *
  3034. * @return void
  3035. */
  3036. public function testControlCheckbox()
  3037. {
  3038. $articles = TableRegistry::get('Articles');
  3039. $articles->schema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3040. $article = $articles->newEntity();
  3041. $this->Form->create($article);
  3042. $result = $this->Form->control('Articles.active');
  3043. $expected = [
  3044. 'div' => ['class' => 'input checkbox'],
  3045. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3046. 'label' => ['for' => 'articles-active'],
  3047. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3048. 'Active',
  3049. '/label',
  3050. '/div'
  3051. ];
  3052. $this->assertHtml($expected, $result);
  3053. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3054. $expected = [
  3055. 'div' => ['class' => 'input checkbox'],
  3056. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3057. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3058. '/div'
  3059. ];
  3060. $this->assertHtml($expected, $result);
  3061. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3062. $expected = [
  3063. 'div' => ['class' => 'input checkbox'],
  3064. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3065. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3066. '/div'
  3067. ];
  3068. $this->assertHtml($expected, $result);
  3069. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3070. $expected = [
  3071. 'div' => ['class' => 'input checkbox'],
  3072. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3073. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3074. '/div'
  3075. ];
  3076. $this->assertHtml($expected, $result);
  3077. $result = $this->Form->control('Articles.disabled', [
  3078. 'label' => 'Disabled',
  3079. 'type' => 'checkbox',
  3080. 'data-foo' => 'disabled'
  3081. ]);
  3082. $expected = [
  3083. 'div' => ['class' => 'input checkbox'],
  3084. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3085. 'label' => ['for' => 'articles-disabled'],
  3086. ['input' => [
  3087. 'type' => 'checkbox',
  3088. 'name' => 'Articles[disabled]',
  3089. 'value' => '1',
  3090. 'id' => 'articles-disabled',
  3091. 'data-foo' => 'disabled'
  3092. ]],
  3093. 'Disabled',
  3094. '/label',
  3095. '/div'
  3096. ];
  3097. $this->assertHtml($expected, $result);
  3098. $result = $this->Form->control('Articles.confirm', [
  3099. 'label' => 'Confirm <b>me</b>!',
  3100. 'type' => 'checkbox',
  3101. 'escape' => false
  3102. ]);
  3103. $expected = [
  3104. 'div' => ['class' => 'input checkbox'],
  3105. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3106. 'label' => ['for' => 'articles-confirm'],
  3107. ['input' => [
  3108. 'type' => 'checkbox',
  3109. 'name' => 'Articles[confirm]',
  3110. 'value' => '1',
  3111. 'id' => 'articles-confirm',
  3112. ]],
  3113. 'Confirm <b>me</b>!',
  3114. '/label',
  3115. '/div'
  3116. ];
  3117. $this->assertHtml($expected, $result);
  3118. }
  3119. /**
  3120. * testControlHidden method
  3121. *
  3122. * Test that control() does not create wrapping div and label tag for hidden fields.
  3123. *
  3124. * @return void
  3125. */
  3126. public function testControlHidden()
  3127. {
  3128. TableRegistry::get('ValidateUsers', [
  3129. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3130. ]);
  3131. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3132. $result = $this->Form->control('ValidateUser.id');
  3133. $expected = [
  3134. 'input' => ['name', 'type' => 'hidden', 'id']
  3135. ];
  3136. $this->assertHtml($expected, $result);
  3137. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3138. $expected = [
  3139. 'input' => ['name', 'type' => 'hidden', 'id']
  3140. ];
  3141. $this->assertHtml($expected, $result);
  3142. }
  3143. /**
  3144. * testControlDatetime method
  3145. *
  3146. * Test form->control() with datetime.
  3147. *
  3148. * @return void
  3149. */
  3150. public function testControlDatetime()
  3151. {
  3152. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3153. ->setMethods(['datetime'])
  3154. ->setConstructorArgs([new View()])
  3155. ->getMock();
  3156. $this->Form->expects($this->once())->method('datetime')
  3157. ->with('prueba', [
  3158. 'type' => 'datetime',
  3159. 'timeFormat' => 24,
  3160. 'minYear' => 2008,
  3161. 'maxYear' => 2011,
  3162. 'interval' => 15,
  3163. 'options' => null,
  3164. 'empty' => false,
  3165. 'id' => 'prueba',
  3166. 'required' => false,
  3167. 'templateVars' => []
  3168. ])
  3169. ->will($this->returnValue('This is it!'));
  3170. $result = $this->Form->control('prueba', [
  3171. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3172. 'maxYear' => 2011, 'interval' => 15
  3173. ]);
  3174. $expected = [
  3175. 'div' => ['class' => 'input datetime'],
  3176. '<label',
  3177. 'Prueba',
  3178. '/label',
  3179. 'This is it!',
  3180. '/div'
  3181. ];
  3182. $this->assertHtml($expected, $result);
  3183. }
  3184. /**
  3185. * testControlDatetimeIdPrefix method
  3186. *
  3187. * Test form->control() with datetime with id prefix.
  3188. *
  3189. * @return void
  3190. */
  3191. public function testControlDatetimeIdPrefix()
  3192. {
  3193. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3194. ->setMethods(['datetime'])
  3195. ->setConstructorArgs([new View()])
  3196. ->getMock();
  3197. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3198. $this->Form->expects($this->once())->method('datetime')
  3199. ->with('prueba', [
  3200. 'type' => 'datetime',
  3201. 'timeFormat' => 24,
  3202. 'minYear' => 2008,
  3203. 'maxYear' => 2011,
  3204. 'interval' => 15,
  3205. 'options' => null,
  3206. 'empty' => false,
  3207. 'id' => 'prefix-prueba',
  3208. 'required' => false,
  3209. 'templateVars' => []
  3210. ])
  3211. ->will($this->returnValue('This is it!'));
  3212. $result = $this->Form->control('prueba', [
  3213. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3214. 'maxYear' => 2011, 'interval' => 15
  3215. ]);
  3216. $expected = [
  3217. 'div' => ['class' => 'input datetime'],
  3218. '<label',
  3219. 'Prueba',
  3220. '/label',
  3221. 'This is it!',
  3222. '/div'
  3223. ];
  3224. $this->assertHtml($expected, $result);
  3225. }
  3226. /**
  3227. * testControlCheckboxWithDisabledElements method
  3228. *
  3229. * Test generating checkboxes with disabled elements.
  3230. *
  3231. * @return void
  3232. */
  3233. public function testControlCheckboxWithDisabledElements()
  3234. {
  3235. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3236. $result = $this->Form->control('Contact.multiple', [
  3237. 'multiple' => 'checkbox',
  3238. 'disabled' => 'disabled',
  3239. 'options' => $options
  3240. ]);
  3241. $expected = [
  3242. ['div' => ['class' => 'input select']],
  3243. ['label' => ['for' => 'contact-multiple']],
  3244. 'Multiple',
  3245. '/label',
  3246. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3247. ['div' => ['class' => 'checkbox']],
  3248. ['label' => ['for' => 'contact-multiple-1']],
  3249. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3250. 'One',
  3251. '/label',
  3252. '/div',
  3253. ['div' => ['class' => 'checkbox']],
  3254. ['label' => ['for' => 'contact-multiple-2']],
  3255. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3256. 'Two',
  3257. '/label',
  3258. '/div',
  3259. ['div' => ['class' => 'checkbox']],
  3260. ['label' => ['for' => 'contact-multiple-3']],
  3261. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3262. 'Three',
  3263. '/label',
  3264. '/div',
  3265. '/div'
  3266. ];
  3267. $this->assertHtml($expected, $result);
  3268. // make sure 50 does only disable 50, and not 50f5c0cf
  3269. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3270. $disabled = [50];
  3271. $expected = [
  3272. ['div' => ['class' => 'input select']],
  3273. ['label' => ['for' => 'contact-multiple']],
  3274. 'Multiple',
  3275. '/label',
  3276. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3277. ['div' => ['class' => 'checkbox']],
  3278. ['label' => ['for' => 'contact-multiple-50']],
  3279. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3280. 'Fifty',
  3281. '/label',
  3282. '/div',
  3283. ['div' => ['class' => 'checkbox']],
  3284. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3285. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3286. 'Stringy',
  3287. '/label',
  3288. '/div',
  3289. '/div'
  3290. ];
  3291. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3292. $this->assertHtml($expected, $result);
  3293. }
  3294. /**
  3295. * testControlWithLeadingInteger method
  3296. *
  3297. * Test input name with leading integer, ensure attributes are generated correctly.
  3298. *
  3299. * @return void
  3300. */
  3301. public function testControlWithLeadingInteger()
  3302. {
  3303. $result = $this->Form->text('0.Node.title');
  3304. $expected = [
  3305. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3306. ];
  3307. $this->assertHtml($expected, $result);
  3308. }
  3309. /**
  3310. * testControlSelectType method
  3311. *
  3312. * Test form->control() with select type inputs.
  3313. *
  3314. * @return void
  3315. */
  3316. public function testControlSelectType()
  3317. {
  3318. $result = $this->Form->control(
  3319. 'email',
  3320. [
  3321. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3322. );
  3323. $expected = [
  3324. 'div' => ['class' => 'input select'],
  3325. 'label' => ['for' => 'email'],
  3326. 'Email',
  3327. '/label',
  3328. ['select' => ['name' => 'email', 'id' => 'email']],
  3329. ['option' => ['value' => '']],
  3330. '/option',
  3331. ['option' => ['value' => 'è']],
  3332. 'Firést',
  3333. '/option',
  3334. ['option' => ['value' => 'é']],
  3335. 'Secoènd',
  3336. '/option',
  3337. '/select',
  3338. '/div'
  3339. ];
  3340. $this->assertHtml($expected, $result);
  3341. $result = $this->Form->control(
  3342. 'email',
  3343. [
  3344. 'options' => ['First', 'Second'], 'empty' => true]
  3345. );
  3346. $expected = [
  3347. 'div' => ['class' => 'input select'],
  3348. 'label' => ['for' => 'email'],
  3349. 'Email',
  3350. '/label',
  3351. ['select' => ['name' => 'email', 'id' => 'email']],
  3352. ['option' => ['value' => '']],
  3353. '/option',
  3354. ['option' => ['value' => '0']],
  3355. 'First',
  3356. '/option',
  3357. ['option' => ['value' => '1']],
  3358. 'Second',
  3359. '/option',
  3360. '/select',
  3361. '/div'
  3362. ];
  3363. $this->assertHtml($expected, $result);
  3364. $result = $this->Form->control('email', [
  3365. 'type' => 'select',
  3366. 'options' => new \ArrayObject(['First', 'Second']),
  3367. 'empty' => true
  3368. ]);
  3369. $this->assertHtml($expected, $result);
  3370. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3371. $this->Form->request->data = ['Model' => ['user_id' => 'value']];
  3372. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3373. $expected = [
  3374. 'div' => ['class' => 'input select'],
  3375. 'label' => ['for' => 'model-user-id'],
  3376. 'User',
  3377. '/label',
  3378. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3379. ['option' => ['value' => '']],
  3380. '/option',
  3381. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3382. 'good',
  3383. '/option',
  3384. ['option' => ['value' => 'other']],
  3385. 'bad',
  3386. '/option',
  3387. '/select',
  3388. '/div'
  3389. ];
  3390. $this->assertHtml($expected, $result);
  3391. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3392. $this->Form->request->data = ['Thing' => ['user_id' => null]];
  3393. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3394. $expected = [
  3395. 'div' => ['class' => 'input select'],
  3396. 'label' => ['for' => 'thing-user-id'],
  3397. 'User',
  3398. '/label',
  3399. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3400. ['option' => ['value' => '']],
  3401. 'Some Empty',
  3402. '/option',
  3403. ['option' => ['value' => 'value']],
  3404. 'good',
  3405. '/option',
  3406. ['option' => ['value' => 'other']],
  3407. 'bad',
  3408. '/option',
  3409. '/select',
  3410. '/div'
  3411. ];
  3412. $this->assertHtml($expected, $result);
  3413. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3414. $this->Form->request->data = ['Thing' => ['user_id' => 'value']];
  3415. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3416. $expected = [
  3417. 'div' => ['class' => 'input select'],
  3418. 'label' => ['for' => 'thing-user-id'],
  3419. 'User',
  3420. '/label',
  3421. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3422. ['option' => ['value' => '']],
  3423. 'Some Empty',
  3424. '/option',
  3425. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3426. 'good',
  3427. '/option',
  3428. ['option' => ['value' => 'other']],
  3429. 'bad',
  3430. '/option',
  3431. '/select',
  3432. '/div'
  3433. ];
  3434. $this->assertHtml($expected, $result);
  3435. $this->Form->data = [];
  3436. $result = $this->Form->control('Publisher.id', [
  3437. 'label' => 'Publisher',
  3438. 'type' => 'select',
  3439. 'multiple' => 'checkbox',
  3440. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3441. ]);
  3442. $expected = [
  3443. ['div' => ['class' => 'input select']],
  3444. ['label' => ['for' => 'publisher-id']],
  3445. 'Publisher',
  3446. '/label',
  3447. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3448. ['div' => ['class' => 'checkbox']],
  3449. ['label' => ['for' => 'publisher-id-value-1']],
  3450. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3451. 'Label 1',
  3452. '/label',
  3453. '/div',
  3454. ['div' => ['class' => 'checkbox']],
  3455. ['label' => ['for' => 'publisher-id-value-2']],
  3456. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3457. 'Label 2',
  3458. '/label',
  3459. '/div',
  3460. '/div'
  3461. ];
  3462. $this->assertHtml($expected, $result);
  3463. }
  3464. /**
  3465. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3466. *
  3467. * Test that control() and a non standard primary key makes a hidden input by default.
  3468. *
  3469. * @return void
  3470. */
  3471. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3472. {
  3473. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3474. $this->Form->create($this->article);
  3475. $result = $this->Form->control('title');
  3476. $expected = [
  3477. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3478. ];
  3479. $this->assertHtml($expected, $result);
  3480. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3481. $this->Form->create($this->article);
  3482. $result = $this->Form->control('title');
  3483. $expected = [
  3484. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3485. ];
  3486. $this->assertHtml($expected, $result);
  3487. $result = $this->Form->control('body');
  3488. $expected = [
  3489. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3490. ];
  3491. $this->assertHtml($expected, $result);
  3492. }
  3493. /**
  3494. * testControlOverridingMagicSelectType method
  3495. *
  3496. * Test that overriding the magic select type widget is possible.
  3497. *
  3498. * @return void
  3499. */
  3500. public function testControlOverridingMagicSelectType()
  3501. {
  3502. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3503. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3504. $expected = [
  3505. 'div' => ['class' => 'input text'],
  3506. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3507. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3508. '/div'
  3509. ];
  3510. $this->assertHtml($expected, $result);
  3511. //Check that magic types still work for plural/singular vars
  3512. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3513. $result = $this->Form->control('Model.type');
  3514. $expected = [
  3515. 'div' => ['class' => 'input select'],
  3516. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3517. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3518. ['option' => ['value' => 'value']], 'good', '/option',
  3519. ['option' => ['value' => 'other']], 'bad', '/option',
  3520. '/select',
  3521. '/div'
  3522. ];
  3523. $this->assertHtml($expected, $result);
  3524. }
  3525. /**
  3526. * testControlMagicTypeDoesNotOverride method
  3527. *
  3528. * Test that inferred types do not override developer input.
  3529. *
  3530. * @return void
  3531. */
  3532. public function testControlMagicTypeDoesNotOverride()
  3533. {
  3534. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3535. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3536. $expected = [
  3537. 'div' => ['class' => 'input checkbox'],
  3538. ['input' => [
  3539. 'type' => 'hidden',
  3540. 'name' => 'Model[user]',
  3541. 'value' => 0,
  3542. ]],
  3543. 'label' => ['for' => 'model-user'],
  3544. ['input' => [
  3545. 'name' => 'Model[user]',
  3546. 'type' => 'checkbox',
  3547. 'id' => 'model-user',
  3548. 'value' => 1
  3549. ]],
  3550. 'User',
  3551. '/label',
  3552. '/div'
  3553. ];
  3554. $this->assertHtml($expected, $result);
  3555. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3556. $options = [
  3557. 1 => 'blue',
  3558. 2 => 'red'
  3559. ];
  3560. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3561. $expected = [
  3562. 'div' => ['class' => 'input select'],
  3563. 'label' => ['for' => 'tags-ids'],
  3564. 'Tags',
  3565. '/label',
  3566. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3567. ['div' => ['class' => 'checkbox']],
  3568. ['label' => ['for' => 'tags-ids-1']],
  3569. ['input' => [
  3570. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3571. 'value' => '1', 'name' => 'tags[_ids][]'
  3572. ]],
  3573. 'blue',
  3574. '/label',
  3575. '/div',
  3576. ['div' => ['class' => 'checkbox']],
  3577. ['label' => ['for' => 'tags-ids-2']],
  3578. ['input' => [
  3579. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3580. 'value' => '2', 'name' => 'tags[_ids][]'
  3581. ]],
  3582. 'red',
  3583. '/label',
  3584. '/div',
  3585. '/div'
  3586. ];
  3587. $this->assertHtml($expected, $result);
  3588. }
  3589. /**
  3590. * testControlMagicSelectForTypeNumber method
  3591. *
  3592. * Test that magic control() selects are created for type=number.
  3593. *
  3594. * @return void
  3595. */
  3596. public function testControlMagicSelectForTypeNumber()
  3597. {
  3598. TableRegistry::get('ValidateUsers', [
  3599. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3600. ]);
  3601. $entity = new Entity(['balance' => 1]);
  3602. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3603. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3604. $result = $this->Form->control('balance');
  3605. $expected = [
  3606. 'div' => ['class' => 'input select'],
  3607. 'label' => ['for' => 'balance'],
  3608. 'Balance',
  3609. '/label',
  3610. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3611. ['option' => ['value' => '0']],
  3612. 'nothing',
  3613. '/option',
  3614. ['option' => ['value' => '1', 'selected' => 'selected']],
  3615. 'some',
  3616. '/option',
  3617. ['option' => ['value' => '100']],
  3618. 'a lot',
  3619. '/option',
  3620. '/select',
  3621. '/div'
  3622. ];
  3623. $this->assertHtml($expected, $result);
  3624. }
  3625. /**
  3626. * testInvalidControlTypeOption method
  3627. *
  3628. * Test invalid 'input' type option to control() function.
  3629. *
  3630. * @return void
  3631. */
  3632. public function testInvalidControlTypeOption()
  3633. {
  3634. $this->expectException(\RuntimeException::class);
  3635. $this->expectExceptionMessage('Invalid type \'input\' used for field \'text\'');
  3636. $this->Form->control('text', ['type' => 'input']);
  3637. }
  3638. /**
  3639. * testControlMagicSelectChangeToRadio method
  3640. *
  3641. * Test that magic control() selects can easily be converted into radio types without error.
  3642. *
  3643. * @return void
  3644. */
  3645. public function testControlMagicSelectChangeToRadio()
  3646. {
  3647. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3648. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3649. $this->assertContains('input type="radio"', $result);
  3650. }
  3651. /**
  3652. * testFormControlSubmit method
  3653. *
  3654. * Test correct results for form::control() and type submit.
  3655. *
  3656. * @return void
  3657. */
  3658. public function testFormControlSubmit()
  3659. {
  3660. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3661. $expected = [
  3662. 'div' => ['class' => 'submit'],
  3663. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3664. '/div'
  3665. ];
  3666. $this->assertHtml($expected, $result);
  3667. }
  3668. /**
  3669. * testFormControls method
  3670. *
  3671. * Test correct results from Form::controls().
  3672. *
  3673. * @return void
  3674. */
  3675. public function testFormControlsLegendFieldset()
  3676. {
  3677. $this->Form->create($this->article);
  3678. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3679. $expected = [
  3680. '<fieldset',
  3681. '<legend',
  3682. 'The Legend',
  3683. '/legend',
  3684. '*/fieldset',
  3685. ];
  3686. $this->assertHtml($expected, $result);
  3687. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3688. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3689. $this->assertContains('<fieldset>', $result);
  3690. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3691. $this->assertNotContains('<legend>', $result);
  3692. $this->assertNotContains('<fieldset>', $result);
  3693. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3694. $this->assertNotContains('<legend>', $result);
  3695. $this->assertNotContains('<fieldset>', $result);
  3696. $this->Form->create($this->article);
  3697. $this->Form->request->params['prefix'] = 'admin';
  3698. $this->Form->request->params['action'] = 'admin_edit';
  3699. $this->Form->request->params['controller'] = 'articles';
  3700. $result = $this->Form->allControls();
  3701. $expected = [
  3702. '<fieldset',
  3703. '<legend',
  3704. 'New Article',
  3705. '/legend',
  3706. '*/fieldset',
  3707. ];
  3708. $this->assertHtml($expected, $result);
  3709. $this->Form->create($this->article);
  3710. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3711. $expected = [
  3712. '<fieldset',
  3713. '<legend',
  3714. 'The Legend',
  3715. '/legend',
  3716. '*/fieldset',
  3717. ];
  3718. $this->assertHtml($expected, $result);
  3719. $this->Form->create($this->article);
  3720. $result = $this->Form->allControls([], [
  3721. 'fieldset' => [
  3722. 'class' => 'some-class some-other-class',
  3723. 'disabled' => true,
  3724. 'data-param' => 'a-param'
  3725. ],
  3726. 'legend' => 'The Legend'
  3727. ]);
  3728. $expected = [
  3729. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3730. '<legend',
  3731. 'The Legend',
  3732. '/legend',
  3733. '*/fieldset',
  3734. ];
  3735. $this->assertHtml($expected, $result);
  3736. }
  3737. /**
  3738. * testFormControls method
  3739. *
  3740. * Test the controls() method.
  3741. *
  3742. * @return void
  3743. */
  3744. public function testFormControls()
  3745. {
  3746. $this->Form->create($this->article);
  3747. $result = $this->Form->allControls();
  3748. $expected = [
  3749. '<fieldset',
  3750. '<legend', 'New Article', '/legend',
  3751. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3752. ['div' => ['class' => 'input select required']],
  3753. '*/div',
  3754. ['div' => ['class' => 'input text required']],
  3755. '*/div',
  3756. ['div' => ['class' => 'input text']],
  3757. '*/div',
  3758. ['div' => ['class' => 'input text']],
  3759. '*/div',
  3760. '/fieldset',
  3761. ];
  3762. $this->assertHtml($expected, $result);
  3763. $result = $this->Form->allControls([
  3764. 'published' => ['type' => 'boolean']
  3765. ]);
  3766. $expected = [
  3767. '<fieldset',
  3768. '<legend', 'New Article', '/legend',
  3769. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3770. ['div' => ['class' => 'input select required']],
  3771. '*/div',
  3772. ['div' => ['class' => 'input text required']],
  3773. '*/div',
  3774. ['div' => ['class' => 'input text']],
  3775. '*/div',
  3776. ['div' => ['class' => 'input boolean']],
  3777. '*/div',
  3778. '/fieldset',
  3779. ];
  3780. $this->assertHtml($expected, $result);
  3781. $this->Form->create($this->article);
  3782. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3783. $expected = [
  3784. 'fieldset' => [],
  3785. 'legend' => [],
  3786. 'Hello',
  3787. '/legend',
  3788. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3789. ['div' => ['class' => 'input select required']],
  3790. '*/div',
  3791. ['div' => ['class' => 'input text required']],
  3792. '*/div',
  3793. ['div' => ['class' => 'input text']],
  3794. '*/div',
  3795. ['div' => ['class' => 'input text']],
  3796. '*/div',
  3797. '/fieldset'
  3798. ];
  3799. $this->assertHtml($expected, $result);
  3800. $this->Form->create(false);
  3801. $expected = [
  3802. 'fieldset' => [],
  3803. ['div' => ['class' => 'input text']],
  3804. 'label' => ['for' => 'foo'],
  3805. 'Foo',
  3806. '/label',
  3807. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3808. '*/div',
  3809. '/fieldset'
  3810. ];
  3811. $result = $this->Form->allControls(
  3812. ['foo' => ['type' => 'text']],
  3813. ['legend' => false]
  3814. );
  3815. $this->assertHtml($expected, $result);
  3816. }
  3817. /**
  3818. * testFormControlsBlacklist method
  3819. *
  3820. * @return void
  3821. */
  3822. public function testFormControlsBlacklist()
  3823. {
  3824. $this->Form->create($this->article);
  3825. $result = $this->Form->allControls([
  3826. 'id' => false
  3827. ]);
  3828. $expected = [
  3829. '<fieldset',
  3830. '<legend', 'New Article', '/legend',
  3831. ['div' => ['class' => 'input select required']],
  3832. '*/div',
  3833. ['div' => ['class' => 'input text required']],
  3834. '*/div',
  3835. ['div' => ['class' => 'input text']],
  3836. '*/div',
  3837. ['div' => ['class' => 'input text']],
  3838. '*/div',
  3839. '/fieldset',
  3840. ];
  3841. $this->assertHtml($expected, $result);
  3842. $this->Form->create($this->article);
  3843. $result = $this->Form->allControls([
  3844. 'id' => []
  3845. ]);
  3846. $expected = [
  3847. '<fieldset',
  3848. '<legend', 'New Article', '/legend',
  3849. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3850. ['div' => ['class' => 'input select required']],
  3851. '*/div',
  3852. ['div' => ['class' => 'input text required']],
  3853. '*/div',
  3854. ['div' => ['class' => 'input text']],
  3855. '*/div',
  3856. ['div' => ['class' => 'input text']],
  3857. '*/div',
  3858. '/fieldset',
  3859. ];
  3860. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3861. }
  3862. /**
  3863. * testSelectAsCheckbox method
  3864. *
  3865. * Test multi-select widget with checkbox formatting.
  3866. *
  3867. * @return void
  3868. */
  3869. public function testSelectAsCheckbox()
  3870. {
  3871. $result = $this->Form->select(
  3872. 'Model.multi_field',
  3873. ['first', 'second', 'third'],
  3874. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3875. );
  3876. $expected = [
  3877. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3878. ['div' => ['class' => 'checkbox']],
  3879. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3880. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3881. 'first',
  3882. '/label',
  3883. '/div',
  3884. ['div' => ['class' => 'checkbox']],
  3885. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3886. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3887. 'second',
  3888. '/label',
  3889. '/div',
  3890. ['div' => ['class' => 'checkbox']],
  3891. ['label' => ['for' => 'model-multi-field-2']],
  3892. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3893. 'third',
  3894. '/label',
  3895. '/div',
  3896. ];
  3897. $this->assertHtml($expected, $result);
  3898. $result = $this->Form->select(
  3899. 'Model.multi_field',
  3900. ['1/2' => 'half'],
  3901. ['multiple' => 'checkbox']
  3902. );
  3903. $expected = [
  3904. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3905. ['div' => ['class' => 'checkbox']],
  3906. ['label' => ['for' => 'model-multi-field-1-2']],
  3907. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3908. 'half',
  3909. '/label',
  3910. '/div',
  3911. ];
  3912. $this->assertHtml($expected, $result);
  3913. }
  3914. /**
  3915. * testLabel method
  3916. *
  3917. * Test label generation.
  3918. *
  3919. * @return void
  3920. */
  3921. public function testLabel()
  3922. {
  3923. $result = $this->Form->label('Person.name');
  3924. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3925. $this->assertHtml($expected, $result);
  3926. $result = $this->Form->label('Person.name');
  3927. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3928. $this->assertHtml($expected, $result);
  3929. $result = $this->Form->label('Person.first_name');
  3930. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3931. $this->assertHtml($expected, $result);
  3932. $result = $this->Form->label('Person.first_name', 'Your first name');
  3933. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3934. $this->assertHtml($expected, $result);
  3935. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3936. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3937. $this->assertHtml($expected, $result);
  3938. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3939. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3940. $this->assertHtml($expected, $result);
  3941. $result = $this->Form->label('Person.first_name', '');
  3942. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3943. $this->assertHtml($expected, $result);
  3944. $result = $this->Form->label('Person.2.name', '');
  3945. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3946. $this->assertHtml($expected, $result);
  3947. }
  3948. /**
  3949. * testLabelContainControl method
  3950. *
  3951. * Test that label() can accept an input with the correct template vars.
  3952. *
  3953. * @return void
  3954. */
  3955. public function testLabelContainControl()
  3956. {
  3957. $this->Form->templates([
  3958. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3959. ]);
  3960. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3961. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3962. ]);
  3963. $expected = [
  3964. 'label' => ['for' => 'person-accept-terms'],
  3965. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3966. 'Accept',
  3967. '/label',
  3968. ];
  3969. $this->assertHtml($expected, $result);
  3970. }
  3971. /**
  3972. * testTextbox method
  3973. *
  3974. * Test textbox element generation.
  3975. *
  3976. * @return void
  3977. */
  3978. public function testTextbox()
  3979. {
  3980. $result = $this->Form->text('Model.field');
  3981. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3982. $this->assertHtml($expected, $result);
  3983. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3984. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3985. $this->assertHtml($expected, $result);
  3986. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3987. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3988. $this->assertHtml($expected, $result);
  3989. }
  3990. /**
  3991. * testTextBoxDataAndError method
  3992. *
  3993. * Test that text() hooks up with request data and error fields.
  3994. *
  3995. * @return void
  3996. */
  3997. public function testTextBoxDataAndError()
  3998. {
  3999. $this->article['errors'] = [
  4000. 'Contact' => ['text' => 'wrong']
  4001. ];
  4002. $this->Form->create($this->article);
  4003. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  4004. $result = $this->Form->text('Model.text');
  4005. $expected = [
  4006. 'input' => [
  4007. 'type' => 'text',
  4008. 'name' => 'Model[text]',
  4009. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  4010. ]
  4011. ];
  4012. $this->assertHtml($expected, $result);
  4013. $this->Form->request->data['Contact']['text'] = 'test';
  4014. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  4015. $expected = [
  4016. 'input' => [
  4017. 'type' => 'text',
  4018. 'name' => 'Contact[text]',
  4019. 'value' => 'test',
  4020. 'id' => 'theID',
  4021. 'class' => 'form-error'
  4022. ]
  4023. ];
  4024. $this->assertHtml($expected, $result);
  4025. }
  4026. /**
  4027. * testDefaultValue method
  4028. *
  4029. * Test default value setting.
  4030. *
  4031. * @return void
  4032. */
  4033. public function testTextDefaultValue()
  4034. {
  4035. $this->Form->request->data['Model']['field'] = 'test';
  4036. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4037. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  4038. $this->assertHtml($expected, $result);
  4039. unset($this->Form->request->data['Model']['field']);
  4040. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4041. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4042. $this->assertHtml($expected, $result);
  4043. $Articles = TableRegistry::get('Articles');
  4044. $title = $Articles->schema()->column('title');
  4045. $Articles->schema()->addColumn(
  4046. 'title',
  4047. ['default' => 'default title'] + $title
  4048. );
  4049. $entity = $Articles->newEntity();
  4050. $this->Form->create($entity);
  4051. // Get default value from schema
  4052. $result = $this->Form->text('title');
  4053. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4054. $this->assertHtml($expected, $result);
  4055. // Don't get value from schema
  4056. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4057. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4058. $this->assertHtml($expected, $result);
  4059. // Custom default value overrides default value from schema
  4060. $result = $this->Form->text('title', ['default' => 'override default']);
  4061. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4062. $this->assertHtml($expected, $result);
  4063. // Default value from schema is used only for new entities.
  4064. $entity->isNew(false);
  4065. $result = $this->Form->text('title');
  4066. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4067. $this->assertHtml($expected, $result);
  4068. }
  4069. /**
  4070. * testError method
  4071. *
  4072. * Test field error generation.
  4073. *
  4074. * @return void
  4075. */
  4076. public function testError()
  4077. {
  4078. $this->article['errors'] = [
  4079. 'Article' => ['field' => 'email']
  4080. ];
  4081. $this->Form->create($this->article);
  4082. $result = $this->Form->error('Article.field');
  4083. $expected = [
  4084. ['div' => ['class' => 'error-message']],
  4085. 'email',
  4086. '/div',
  4087. ];
  4088. $this->assertHtml($expected, $result);
  4089. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4090. $expected = [
  4091. ['div' => ['class' => 'error-message']],
  4092. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4093. '/div',
  4094. ];
  4095. $this->assertHtml($expected, $result);
  4096. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4097. $expected = [
  4098. ['div' => ['class' => 'error-message']],
  4099. '<strong', 'Badness!', '/strong',
  4100. '/div',
  4101. ];
  4102. $this->assertHtml($expected, $result);
  4103. }
  4104. /**
  4105. * testErrorRuleName method
  4106. *
  4107. * Test error translation can use rule names for translating.
  4108. *
  4109. * @return void
  4110. */
  4111. public function testErrorRuleName()
  4112. {
  4113. $this->article['errors'] = [
  4114. 'Article' => [
  4115. 'field' => ['email' => 'Your email was not good']
  4116. ]
  4117. ];
  4118. $this->Form->create($this->article);
  4119. $result = $this->Form->error('Article.field');
  4120. $expected = [
  4121. ['div' => ['class' => 'error-message']],
  4122. 'Your email was not good',
  4123. '/div',
  4124. ];
  4125. $this->assertHtml($expected, $result);
  4126. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4127. $expected = [
  4128. ['div' => ['class' => 'error-message']],
  4129. 'Email in use',
  4130. '/div',
  4131. ];
  4132. $this->assertHtml($expected, $result);
  4133. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4134. $expected = [
  4135. ['div' => ['class' => 'error-message']],
  4136. 'Email in use',
  4137. '/div',
  4138. ];
  4139. $this->assertHtml($expected, $result);
  4140. $result = $this->Form->error('Article.field', [
  4141. 'email' => 'Key is preferred',
  4142. 'Your email was not good' => 'Email in use'
  4143. ]);
  4144. $expected = [
  4145. ['div' => ['class' => 'error-message']],
  4146. 'Key is preferred',
  4147. '/div',
  4148. ];
  4149. $this->assertHtml($expected, $result);
  4150. }
  4151. /**
  4152. * testErrorMessages method
  4153. *
  4154. * Test error with nested lists.
  4155. *
  4156. * @return void
  4157. */
  4158. public function testErrorMessages()
  4159. {
  4160. $this->article['errors'] = [
  4161. 'Article' => ['field' => 'email']
  4162. ];
  4163. $this->Form->create($this->article);
  4164. $result = $this->Form->error('Article.field', [
  4165. 'email' => 'No good!'
  4166. ]);
  4167. $expected = [
  4168. 'div' => ['class' => 'error-message'],
  4169. 'No good!',
  4170. '/div'
  4171. ];
  4172. $this->assertHtml($expected, $result);
  4173. }
  4174. /**
  4175. * testErrorMultipleMessages method
  4176. *
  4177. * Test error() with multiple messages.
  4178. *
  4179. * @return void
  4180. */
  4181. public function testErrorMultipleMessages()
  4182. {
  4183. $this->article['errors'] = [
  4184. 'field' => ['notBlank', 'email', 'Something else']
  4185. ];
  4186. $this->Form->create($this->article);
  4187. $result = $this->Form->error('field', [
  4188. 'notBlank' => 'Cannot be empty',
  4189. 'email' => 'No good!'
  4190. ]);
  4191. $expected = [
  4192. 'div' => ['class' => 'error-message'],
  4193. 'ul' => [],
  4194. '<li', 'Cannot be empty', '/li',
  4195. '<li', 'No good!', '/li',
  4196. '<li', 'Something else', '/li',
  4197. '/ul',
  4198. '/div'
  4199. ];
  4200. $this->assertHtml($expected, $result);
  4201. }
  4202. /**
  4203. * testPassword method
  4204. *
  4205. * Test password element generation.
  4206. *
  4207. * @return void
  4208. */
  4209. public function testPassword()
  4210. {
  4211. $this->article['errors'] = [
  4212. 'Contact' => [
  4213. 'passwd' => 1
  4214. ]
  4215. ];
  4216. $this->Form->create($this->article);
  4217. $result = $this->Form->password('Contact.field');
  4218. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4219. $this->assertHtml($expected, $result);
  4220. $this->Form->request->data['Contact']['passwd'] = 'test';
  4221. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4222. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4223. $this->assertHtml($expected, $result);
  4224. }
  4225. /**
  4226. * testRadio method
  4227. *
  4228. * Test radio element set generation.
  4229. *
  4230. * @return void
  4231. */
  4232. public function testRadio()
  4233. {
  4234. $result = $this->Form->radio('Model.field', ['option A']);
  4235. $expected = [
  4236. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4237. 'label' => ['for' => 'model-field-0'],
  4238. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4239. 'option A',
  4240. '/label'
  4241. ];
  4242. $this->assertHtml($expected, $result);
  4243. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4244. $this->assertHtml($expected, $result);
  4245. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4246. $expected = [
  4247. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4248. ['label' => ['for' => 'model-field-0']],
  4249. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4250. 'option A',
  4251. '/label',
  4252. ['label' => ['for' => 'model-field-1']],
  4253. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4254. 'option B',
  4255. '/label',
  4256. ];
  4257. $this->assertHtml($expected, $result);
  4258. $result = $this->Form->radio(
  4259. 'Employee.gender',
  4260. ['male' => 'Male', 'female' => 'Female'],
  4261. ['form' => 'my-form']
  4262. );
  4263. $expected = [
  4264. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4265. ['label' => ['for' => 'employee-gender-male']],
  4266. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4267. 'Male',
  4268. '/label',
  4269. ['label' => ['for' => 'employee-gender-female']],
  4270. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4271. 'Female',
  4272. '/label',
  4273. ];
  4274. $this->assertHtml($expected, $result);
  4275. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4276. $expected = [
  4277. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4278. ['label' => ['for' => 'model-custom-0']],
  4279. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4280. 'option A',
  4281. '/label',
  4282. ['label' => ['for' => 'model-custom-1']],
  4283. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4284. 'option B',
  4285. '/label',
  4286. ];
  4287. $this->assertHtml($expected, $result);
  4288. $result = $this->Form->radio(
  4289. 'Employee.gender',
  4290. [
  4291. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4292. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4293. ]
  4294. );
  4295. $expected = [
  4296. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4297. ['label' => ['for' => 'employee-gender-male']],
  4298. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4299. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4300. 'Male',
  4301. '/label',
  4302. ['label' => ['for' => 'employee-gender-female']],
  4303. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4304. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4305. 'Female',
  4306. '/label',
  4307. ];
  4308. $this->assertHtml($expected, $result);
  4309. }
  4310. /**
  4311. * Test radio with complex options and empty disabled data.
  4312. *
  4313. * @return void
  4314. */
  4315. public function testRadioComplexDisabled()
  4316. {
  4317. $options = [
  4318. ['value' => 'r', 'text' => 'red'],
  4319. ['value' => 'b', 'text' => 'blue'],
  4320. ];
  4321. $attrs = ['disabled' => []];
  4322. $result = $this->Form->radio('Model.field', $options, $attrs);
  4323. $expected = [
  4324. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4325. ['label' => ['for' => 'model-field-r']],
  4326. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4327. 'red',
  4328. '/label',
  4329. ['label' => ['for' => 'model-field-b']],
  4330. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4331. 'blue',
  4332. '/label',
  4333. ];
  4334. $this->assertHtml($expected, $result);
  4335. $attrs = ['disabled' => ['r']];
  4336. $result = $this->Form->radio('Model.field', $options, $attrs);
  4337. $this->assertContains('disabled="disabled"', $result);
  4338. }
  4339. /**
  4340. * testRadioDefaultValue method
  4341. *
  4342. * Test default value setting on radio() method.
  4343. *
  4344. * @return void
  4345. */
  4346. public function testRadioDefaultValue()
  4347. {
  4348. $Articles = TableRegistry::get('Articles');
  4349. $title = $Articles->schema()->column('title');
  4350. $Articles->schema()->addColumn(
  4351. 'title',
  4352. ['default' => '1'] + $title
  4353. );
  4354. $this->Form->create($Articles->newEntity());
  4355. $result = $this->Form->radio('title', ['option A', 'option B']);
  4356. $expected = [
  4357. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4358. ['label' => ['for' => 'title-0']],
  4359. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4360. 'option A',
  4361. '/label',
  4362. ['label' => ['for' => 'title-1']],
  4363. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4364. 'option B',
  4365. '/label',
  4366. ];
  4367. $this->assertHtml($expected, $result);
  4368. }
  4369. /**
  4370. * Test setting a hiddenField value on radio buttons.
  4371. *
  4372. * @return void
  4373. */
  4374. public function testRadioHiddenFieldValue()
  4375. {
  4376. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4377. $expected = [
  4378. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4379. 'label' => ['for' => 'title-0'],
  4380. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4381. 'option A',
  4382. '/label',
  4383. ];
  4384. $this->assertHtml($expected, $result);
  4385. }
  4386. /**
  4387. * testControlRadio method
  4388. *
  4389. * Test that input works with radio types.
  4390. *
  4391. * @return void
  4392. */
  4393. public function testControlRadio()
  4394. {
  4395. $result = $this->Form->control('test', [
  4396. 'type' => 'radio',
  4397. 'options' => ['A', 'B'],
  4398. ]);
  4399. $expected = [
  4400. ['div' => ['class' => 'input radio']],
  4401. '<label',
  4402. 'Test',
  4403. '/label',
  4404. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4405. ['label' => ['for' => 'test-0']],
  4406. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4407. 'A',
  4408. '/label',
  4409. ['label' => ['for' => 'test-1']],
  4410. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4411. 'B',
  4412. '/label',
  4413. '/div',
  4414. ];
  4415. $this->assertHtml($expected, $result);
  4416. $result = $this->Form->control('test', [
  4417. 'type' => 'radio',
  4418. 'options' => ['A', 'B'],
  4419. 'value' => '0'
  4420. ]);
  4421. $expected = [
  4422. ['div' => ['class' => 'input radio']],
  4423. '<label',
  4424. 'Test',
  4425. '/label',
  4426. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4427. ['label' => ['for' => 'test-0']],
  4428. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4429. 'A',
  4430. '/label',
  4431. ['label' => ['for' => 'test-1']],
  4432. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4433. 'B',
  4434. '/label',
  4435. '/div',
  4436. ];
  4437. $this->assertHtml($expected, $result);
  4438. $result = $this->Form->control('test', [
  4439. 'type' => 'radio',
  4440. 'options' => ['A', 'B'],
  4441. 'label' => false
  4442. ]);
  4443. $expected = [
  4444. ['div' => ['class' => 'input radio']],
  4445. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4446. ['label' => ['for' => 'test-0']],
  4447. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4448. 'A',
  4449. '/label',
  4450. ['label' => ['for' => 'test-1']],
  4451. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4452. 'B',
  4453. '/label',
  4454. '/div',
  4455. ];
  4456. $this->assertHtml($expected, $result);
  4457. }
  4458. /**
  4459. * testRadioNoLabel method
  4460. *
  4461. * Test that radio() works with label = false.
  4462. *
  4463. * @return void
  4464. */
  4465. public function testRadioNoLabel()
  4466. {
  4467. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4468. $expected = [
  4469. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4470. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4471. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4472. ];
  4473. $this->assertHtml($expected, $result);
  4474. }
  4475. /**
  4476. * testRadioControlInsideLabel method
  4477. *
  4478. * Test generating radio input inside label ala twitter bootstrap.
  4479. *
  4480. * @return void
  4481. */
  4482. public function testRadioControlInsideLabel()
  4483. {
  4484. $this->Form->templates([
  4485. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4486. 'radioWrapper' => '{{label}}'
  4487. ]);
  4488. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4489. //@codingStandardsIgnoreStart
  4490. $expected = [
  4491. ['input' => [
  4492. 'type' => 'hidden',
  4493. 'name' => 'Model[field]',
  4494. 'value' => ''
  4495. ]],
  4496. ['label' => ['for' => 'model-field-0']],
  4497. ['input' => [
  4498. 'type' => 'radio',
  4499. 'name' => 'Model[field]',
  4500. 'value' => '0',
  4501. 'id' => 'model-field-0'
  4502. ]],
  4503. 'option A',
  4504. '/label',
  4505. ['label' => ['for' => 'model-field-1']],
  4506. ['input' => [
  4507. 'type' => 'radio',
  4508. 'name' => 'Model[field]',
  4509. 'value' => '1',
  4510. 'id' => 'model-field-1'
  4511. ]],
  4512. 'option B',
  4513. '/label',
  4514. ];
  4515. //@codingStandardsIgnoreEnd
  4516. $this->assertHtml($expected, $result);
  4517. }
  4518. /**
  4519. * testRadioHiddenControlDisabling method
  4520. *
  4521. * Test disabling the hidden input for radio buttons.
  4522. *
  4523. * @return void
  4524. */
  4525. public function testRadioHiddenControlDisabling()
  4526. {
  4527. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4528. $expected = [
  4529. 'label' => ['for' => 'model-1-field-0'],
  4530. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4531. 'option A',
  4532. '/label'
  4533. ];
  4534. $this->assertHtml($expected, $result);
  4535. }
  4536. /**
  4537. * testRadioOutOfRange method
  4538. *
  4539. * Test radio element set generation.
  4540. *
  4541. * @return void
  4542. */
  4543. public function testRadioOutOfRange()
  4544. {
  4545. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4546. $expected = [
  4547. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4548. 'label' => ['for' => 'model-field-v'],
  4549. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4550. 'value',
  4551. '/label'
  4552. ];
  4553. $this->assertHtml($expected, $result);
  4554. }
  4555. /**
  4556. * testSelect method
  4557. *
  4558. * Test select element generation.
  4559. *
  4560. * @return void
  4561. */
  4562. public function testSelect()
  4563. {
  4564. $result = $this->Form->select('Model.field', []);
  4565. $expected = [
  4566. 'select' => ['name' => 'Model[field]'],
  4567. '/select'
  4568. ];
  4569. $this->assertHtml($expected, $result);
  4570. $this->Form->request->data = ['Model' => ['field' => 'value']];
  4571. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4572. $expected = [
  4573. 'select' => ['name' => 'Model[field]'],
  4574. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4575. 'good',
  4576. '/option',
  4577. ['option' => ['value' => 'other']],
  4578. 'bad',
  4579. '/option',
  4580. '/select'
  4581. ];
  4582. $this->assertHtml($expected, $result);
  4583. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4584. $this->assertHtml($expected, $result);
  4585. $this->Form->request->data = [];
  4586. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4587. $expected = [
  4588. 'select' => ['name' => 'Model[field]'],
  4589. ['option' => ['value' => 'value']],
  4590. 'good',
  4591. '/option',
  4592. ['option' => ['value' => 'other']],
  4593. 'bad',
  4594. '/option',
  4595. '/select'
  4596. ];
  4597. $this->assertHtml($expected, $result);
  4598. $options = [
  4599. ['value' => 'first', 'text' => 'First'],
  4600. ['value' => 'first', 'text' => 'Another First'],
  4601. ];
  4602. $result = $this->Form->select(
  4603. 'Model.field',
  4604. $options,
  4605. ['escape' => false, 'empty' => false]
  4606. );
  4607. $expected = [
  4608. 'select' => ['name' => 'Model[field]'],
  4609. ['option' => ['value' => 'first']],
  4610. 'First',
  4611. '/option',
  4612. ['option' => ['value' => 'first']],
  4613. 'Another First',
  4614. '/option',
  4615. '/select'
  4616. ];
  4617. $this->assertHtml($expected, $result);
  4618. $this->Form->request->data = ['Model' => ['contact_id' => 228]];
  4619. $result = $this->Form->select(
  4620. 'Model.contact_id',
  4621. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4622. ['escape' => false, 'empty' => 'pick something']
  4623. );
  4624. $expected = [
  4625. 'select' => ['name' => 'Model[contact_id]'],
  4626. ['option' => ['value' => '']], 'pick something', '/option',
  4627. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4628. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4629. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4630. '/select'
  4631. ];
  4632. $this->assertHtml($expected, $result);
  4633. $this->Form->request->data['Model']['field'] = 0;
  4634. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4635. $expected = [
  4636. 'select' => ['name' => 'Model[field]'],
  4637. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4638. ['option' => ['value' => '1']], 'Yes', '/option',
  4639. '/select'
  4640. ];
  4641. $this->assertHtml($expected, $result);
  4642. }
  4643. /**
  4644. * testSelectEscapeHtml method
  4645. *
  4646. * Test that select() escapes HTML.
  4647. *
  4648. * @return void
  4649. */
  4650. public function testSelectEscapeHtml()
  4651. {
  4652. $result = $this->Form->select(
  4653. 'Model.field',
  4654. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4655. ['empty' => false]
  4656. );
  4657. $expected = [
  4658. 'select' => ['name' => 'Model[field]'],
  4659. ['option' => ['value' => 'first']],
  4660. 'first &quot;html&quot; &lt;chars&gt;',
  4661. '/option',
  4662. ['option' => ['value' => 'second']],
  4663. 'value',
  4664. '/option',
  4665. '/select'
  4666. ];
  4667. $this->assertHtml($expected, $result);
  4668. $result = $this->Form->select(
  4669. 'Model.field',
  4670. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4671. ['escape' => false, 'empty' => false]
  4672. );
  4673. $expected = [
  4674. 'select' => ['name' => 'Model[field]'],
  4675. ['option' => ['value' => 'first']],
  4676. 'first "html" <chars>',
  4677. '/option',
  4678. ['option' => ['value' => 'second']],
  4679. 'value',
  4680. '/option',
  4681. '/select'
  4682. ];
  4683. $this->assertHtml($expected, $result);
  4684. }
  4685. /**
  4686. * testSelectRequired method
  4687. *
  4688. * Test select() with required and disabled attributes.
  4689. *
  4690. * @return void
  4691. */
  4692. public function testSelectRequired()
  4693. {
  4694. $this->article['required'] = [
  4695. 'user_id' => true
  4696. ];
  4697. $this->Form->create($this->article);
  4698. $result = $this->Form->select('user_id', ['option A']);
  4699. $expected = [
  4700. 'select' => [
  4701. 'name' => 'user_id',
  4702. 'required' => 'required'
  4703. ],
  4704. ['option' => ['value' => '0']], 'option A', '/option',
  4705. '/select'
  4706. ];
  4707. $this->assertHtml($expected, $result);
  4708. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4709. $expected = [
  4710. 'select' => [
  4711. 'name' => 'user_id',
  4712. 'disabled' => 'disabled'
  4713. ],
  4714. ['option' => ['value' => '0']], 'option A', '/option',
  4715. '/select'
  4716. ];
  4717. $this->assertHtml($expected, $result);
  4718. }
  4719. /**
  4720. * testNestedSelect method
  4721. *
  4722. * Test select element generation with optgroups.
  4723. *
  4724. * @return void
  4725. */
  4726. public function testNestedSelect()
  4727. {
  4728. $result = $this->Form->select(
  4729. 'Model.field',
  4730. [1 => 'One', 2 => 'Two', 'Three' => [
  4731. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4732. ]],
  4733. ['empty' => false]
  4734. );
  4735. $expected = [
  4736. 'select' => ['name' => 'Model[field]'],
  4737. ['option' => ['value' => 1]],
  4738. 'One',
  4739. '/option',
  4740. ['option' => ['value' => 2]],
  4741. 'Two',
  4742. '/option',
  4743. ['optgroup' => ['label' => 'Three']],
  4744. ['option' => ['value' => 3]],
  4745. 'Three',
  4746. '/option',
  4747. ['option' => ['value' => 4]],
  4748. 'Four',
  4749. '/option',
  4750. ['option' => ['value' => 5]],
  4751. 'Five',
  4752. '/option',
  4753. '/optgroup',
  4754. '/select'
  4755. ];
  4756. $this->assertHtml($expected, $result);
  4757. }
  4758. /**
  4759. * testSelectMultiple method
  4760. *
  4761. * Test generation of multiple select elements.
  4762. *
  4763. * @return void
  4764. */
  4765. public function testSelectMultiple()
  4766. {
  4767. $options = ['first', 'second', 'third'];
  4768. $result = $this->Form->select(
  4769. 'Model.multi_field',
  4770. $options,
  4771. ['form' => 'my-form', 'multiple' => true]
  4772. );
  4773. $expected = [
  4774. 'input' => [
  4775. 'type' => 'hidden',
  4776. 'name' => 'Model[multi_field]',
  4777. 'value' => '',
  4778. 'form' => 'my-form',
  4779. ],
  4780. 'select' => [
  4781. 'name' => 'Model[multi_field][]',
  4782. 'multiple' => 'multiple',
  4783. 'form' => 'my-form',
  4784. ],
  4785. ['option' => ['value' => '0']],
  4786. 'first',
  4787. '/option',
  4788. ['option' => ['value' => '1']],
  4789. 'second',
  4790. '/option',
  4791. ['option' => ['value' => '2']],
  4792. 'third',
  4793. '/option',
  4794. '/select'
  4795. ];
  4796. $this->assertHtml($expected, $result);
  4797. $result = $this->Form->select(
  4798. 'Model.multi_field',
  4799. $options,
  4800. ['multiple' => 'multiple', 'form' => 'my-form']
  4801. );
  4802. $this->assertHtml($expected, $result);
  4803. $result = $this->Form->select(
  4804. 'Model.multi_field',
  4805. $options,
  4806. ['form' => 'my-form', 'multiple' => false]
  4807. );
  4808. $this->assertNotContains('multiple', $result);
  4809. }
  4810. /**
  4811. * testCheckboxZeroValue method
  4812. *
  4813. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4814. *
  4815. * @return void
  4816. */
  4817. public function testCheckboxZeroValue()
  4818. {
  4819. $result = $this->Form->control('User.get_spam', [
  4820. 'type' => 'checkbox',
  4821. 'value' => '0',
  4822. 'hiddenField' => '1',
  4823. ]);
  4824. $expected = [
  4825. 'div' => ['class' => 'input checkbox'],
  4826. 'label' => ['for' => 'user-get-spam'],
  4827. ['input' => [
  4828. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4829. 'value' => '1'
  4830. ]],
  4831. ['input' => [
  4832. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4833. 'value' => '0', 'id' => 'user-get-spam'
  4834. ]],
  4835. 'Get Spam',
  4836. '/label',
  4837. '/div'
  4838. ];
  4839. $this->assertHtml($expected, $result);
  4840. }
  4841. /**
  4842. * testHabtmSelectBox method
  4843. *
  4844. * Test generation of habtm select boxes.
  4845. *
  4846. * @return void
  4847. */
  4848. public function testHabtmSelectBox()
  4849. {
  4850. $this->loadFixtures('Articles');
  4851. $options = [
  4852. 1 => 'blue',
  4853. 2 => 'red',
  4854. 3 => 'green'
  4855. ];
  4856. $tags = [
  4857. new Entity(['id' => 1, 'name' => 'blue']),
  4858. new Entity(['id' => 3, 'name' => 'green'])
  4859. ];
  4860. $article = new Article(['tags' => $tags]);
  4861. $this->Form->create($article);
  4862. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4863. $expected = [
  4864. 'div' => ['class' => 'input select'],
  4865. 'label' => ['for' => 'tags-ids'],
  4866. 'Tags',
  4867. '/label',
  4868. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4869. 'select' => [
  4870. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4871. 'multiple' => 'multiple'
  4872. ],
  4873. ['option' => ['value' => '1', 'selected' => 'selected']],
  4874. 'blue',
  4875. '/option',
  4876. ['option' => ['value' => '2']],
  4877. 'red',
  4878. '/option',
  4879. ['option' => ['value' => '3', 'selected' => 'selected']],
  4880. 'green',
  4881. '/option',
  4882. '/select',
  4883. '/div'
  4884. ];
  4885. $this->assertHtml($expected, $result);
  4886. // make sure only 50 is selected, and not 50f5c0cf
  4887. $options = [
  4888. '1' => 'blue',
  4889. '50f5c0cf' => 'red',
  4890. '50' => 'green'
  4891. ];
  4892. $tags = [
  4893. new Entity(['id' => 1, 'name' => 'blue']),
  4894. new Entity(['id' => 50, 'name' => 'green'])
  4895. ];
  4896. $article = new Article(['tags' => $tags]);
  4897. $this->Form->create($article);
  4898. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4899. $expected = [
  4900. 'div' => ['class' => 'input select'],
  4901. 'label' => ['for' => 'tags-ids'],
  4902. 'Tags',
  4903. '/label',
  4904. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4905. 'select' => [
  4906. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4907. 'multiple' => 'multiple'
  4908. ],
  4909. ['option' => ['value' => '1', 'selected' => 'selected']],
  4910. 'blue',
  4911. '/option',
  4912. ['option' => ['value' => '50f5c0cf']],
  4913. 'red',
  4914. '/option',
  4915. ['option' => ['value' => '50', 'selected' => 'selected']],
  4916. 'green',
  4917. '/option',
  4918. '/select',
  4919. '/div'
  4920. ];
  4921. $this->assertHtml($expected, $result);
  4922. $spacecraft = [
  4923. 1 => 'Orion',
  4924. 2 => 'Helios'
  4925. ];
  4926. $this->View->viewVars['spacecraft'] = $spacecraft;
  4927. $this->Form->create();
  4928. $result = $this->Form->control('spacecraft._ids');
  4929. $expected = [
  4930. 'div' => ['class' => 'input select'],
  4931. 'label' => ['for' => 'spacecraft-ids'],
  4932. 'Spacecraft',
  4933. '/label',
  4934. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4935. 'select' => [
  4936. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4937. 'multiple' => 'multiple'
  4938. ],
  4939. ['option' => ['value' => '1']],
  4940. 'Orion',
  4941. '/option',
  4942. ['option' => ['value' => '2']],
  4943. 'Helios',
  4944. '/option',
  4945. '/select',
  4946. '/div'
  4947. ];
  4948. $this->assertHtml($expected, $result);
  4949. }
  4950. /**
  4951. * testErrorsForBelongsToManySelect method
  4952. *
  4953. * Tests that errors for belongsToMany select fields are being
  4954. * picked up properly.
  4955. *
  4956. * @return void
  4957. */
  4958. public function testErrorsForBelongsToManySelect()
  4959. {
  4960. $spacecraft = [
  4961. 1 => 'Orion',
  4962. 2 => 'Helios'
  4963. ];
  4964. $this->View->viewVars['spacecraft'] = $spacecraft;
  4965. $article = new Article();
  4966. $article->errors('spacecraft', ['Invalid']);
  4967. $this->Form->create($article);
  4968. $result = $this->Form->control('spacecraft._ids');
  4969. $expected = [
  4970. ['div' => ['class' => 'input select error']],
  4971. 'label' => ['for' => 'spacecraft-ids'],
  4972. 'Spacecraft',
  4973. '/label',
  4974. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4975. 'select' => [
  4976. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4977. 'multiple' => 'multiple'
  4978. ],
  4979. ['option' => ['value' => '1']],
  4980. 'Orion',
  4981. '/option',
  4982. ['option' => ['value' => '2']],
  4983. 'Helios',
  4984. '/option',
  4985. '/select',
  4986. ['div' => ['class' => 'error-message']],
  4987. 'Invalid',
  4988. '/div',
  4989. '/div'
  4990. ];
  4991. $this->assertHtml($expected, $result);
  4992. }
  4993. /**
  4994. * testSelectMultipleCheckboxes method
  4995. *
  4996. * Test generation of multi select elements in checkbox format.
  4997. *
  4998. * @return void
  4999. */
  5000. public function testSelectMultipleCheckboxes()
  5001. {
  5002. $result = $this->Form->select(
  5003. 'Model.multi_field',
  5004. ['first', 'second', 'third'],
  5005. ['multiple' => 'checkbox']
  5006. );
  5007. $expected = [
  5008. 'input' => [
  5009. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5010. ],
  5011. ['div' => ['class' => 'checkbox']],
  5012. ['label' => ['for' => 'model-multi-field-0']],
  5013. ['input' => [
  5014. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5015. 'value' => '0', 'id' => 'model-multi-field-0'
  5016. ]],
  5017. 'first',
  5018. '/label',
  5019. '/div',
  5020. ['div' => ['class' => 'checkbox']],
  5021. ['label' => ['for' => 'model-multi-field-1']],
  5022. ['input' => [
  5023. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5024. 'value' => '1', 'id' => 'model-multi-field-1'
  5025. ]],
  5026. 'second',
  5027. '/label',
  5028. '/div',
  5029. ['div' => ['class' => 'checkbox']],
  5030. ['label' => ['for' => 'model-multi-field-2']],
  5031. ['input' => [
  5032. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5033. 'value' => '2', 'id' => 'model-multi-field-2'
  5034. ]],
  5035. 'third',
  5036. '/label',
  5037. '/div'
  5038. ];
  5039. $this->assertHtml($expected, $result);
  5040. $result = $this->Form->select(
  5041. 'Model.multi_field',
  5042. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5043. ['multiple' => 'checkbox']
  5044. );
  5045. $expected = [
  5046. 'input' => [
  5047. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5048. ],
  5049. ['div' => ['class' => 'checkbox']],
  5050. ['label' => ['for' => 'model-multi-field-a+']],
  5051. ['input' => [
  5052. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5053. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  5054. ]],
  5055. 'first',
  5056. '/label',
  5057. '/div',
  5058. ['div' => ['class' => 'checkbox']],
  5059. ['label' => ['for' => 'model-multi-field-a++']],
  5060. ['input' => [
  5061. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5062. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  5063. ]],
  5064. 'second',
  5065. '/label',
  5066. '/div',
  5067. ['div' => ['class' => 'checkbox']],
  5068. ['label' => ['for' => 'model-multi-field-a+++']],
  5069. ['input' => [
  5070. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5071. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  5072. ]],
  5073. 'third',
  5074. '/label',
  5075. '/div'
  5076. ];
  5077. $this->assertHtml($expected, $result);
  5078. $result = $this->Form->select(
  5079. 'Model.multi_field',
  5080. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5081. ['multiple' => 'checkbox']
  5082. );
  5083. $expected = [
  5084. 'input' => [
  5085. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5086. ],
  5087. ['div' => ['class' => 'checkbox']],
  5088. ['label' => ['for' => 'model-multi-field-a-b']],
  5089. ['input' => [
  5090. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5091. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  5092. ]],
  5093. 'first',
  5094. '/label',
  5095. '/div',
  5096. ['div' => ['class' => 'checkbox']],
  5097. ['label' => ['for' => 'model-multi-field-a-b1']],
  5098. ['input' => [
  5099. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5100. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  5101. ]],
  5102. 'second',
  5103. '/label',
  5104. '/div',
  5105. ['div' => ['class' => 'checkbox']],
  5106. ['label' => ['for' => 'model-multi-field-a-b2']],
  5107. ['input' => [
  5108. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5109. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  5110. ]],
  5111. 'third',
  5112. '/label',
  5113. '/div'
  5114. ];
  5115. $this->assertHtml($expected, $result);
  5116. }
  5117. /**
  5118. * testSelectMultipleCheckboxRequestData method
  5119. *
  5120. * Ensure that multiCheckbox reads from the request data.
  5121. *
  5122. * @return void
  5123. */
  5124. public function testSelectMultipleCheckboxRequestData()
  5125. {
  5126. $this->Form->request->data = ['Model' => ['tags' => [1]]];
  5127. $result = $this->Form->select(
  5128. 'Model.tags',
  5129. ['1' => 'first', 'Array' => 'Array'],
  5130. ['multiple' => 'checkbox']
  5131. );
  5132. $expected = [
  5133. 'input' => [
  5134. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  5135. ],
  5136. ['div' => ['class' => 'checkbox']],
  5137. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5138. ['input' => [
  5139. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5140. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  5141. ]],
  5142. 'first',
  5143. '/label',
  5144. '/div',
  5145. ['div' => ['class' => 'checkbox']],
  5146. ['label' => ['for' => 'model-tags-array']],
  5147. ['input' => [
  5148. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5149. 'value' => 'Array', 'id' => 'model-tags-array'
  5150. ]],
  5151. 'Array',
  5152. '/label',
  5153. '/div'
  5154. ];
  5155. $this->assertHtml($expected, $result);
  5156. }
  5157. /**
  5158. * testSelectMultipleCheckboxSecurity method
  5159. *
  5160. * Checks the security hash array generated for multiple-input checkbox elements.
  5161. *
  5162. * @return void
  5163. */
  5164. public function testSelectMultipleCheckboxSecurity()
  5165. {
  5166. $this->Form->request->params['_Token'] = 'testKey';
  5167. $this->assertEquals([], $this->Form->fields);
  5168. $this->Form->select(
  5169. 'Model.multi_field',
  5170. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5171. ['multiple' => 'checkbox']
  5172. );
  5173. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5174. $result = $this->Form->secure($this->Form->fields);
  5175. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  5176. $this->assertRegExp('/"' . $key . '"/', $result);
  5177. }
  5178. /**
  5179. * testSelectMultipleSecureWithNoOptions method
  5180. *
  5181. * Multiple select elements should always be secured as they always participate
  5182. * in the POST data.
  5183. *
  5184. * @return void
  5185. */
  5186. public function testSelectMultipleSecureWithNoOptions()
  5187. {
  5188. $this->assertEquals([], $this->Form->fields);
  5189. $this->Form->select(
  5190. 'Model.select',
  5191. [],
  5192. ['multiple' => true]
  5193. );
  5194. $this->assertEquals(['Model.select'], $this->Form->fields);
  5195. }
  5196. /**
  5197. * testSelectNoSecureWithNoOptions method
  5198. *
  5199. * When a select box has no options it should not be added to the fields list
  5200. * as it always fail post validation.
  5201. *
  5202. * @return void
  5203. */
  5204. public function testSelectNoSecureWithNoOptions()
  5205. {
  5206. $this->Form->request->params['_Token'] = 'testkey';
  5207. $this->assertEquals([], $this->Form->fields);
  5208. $this->Form->select(
  5209. 'Model.select',
  5210. []
  5211. );
  5212. $this->assertEquals([], $this->Form->fields);
  5213. $this->Form->select(
  5214. 'Model.user_id',
  5215. [],
  5216. ['empty' => true]
  5217. );
  5218. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5219. }
  5220. /**
  5221. * testControlMultipleCheckboxes method
  5222. *
  5223. * Test control() resulting in multi select elements being generated.
  5224. *
  5225. * @return void
  5226. */
  5227. public function testControlMultipleCheckboxes()
  5228. {
  5229. $result = $this->Form->control('Model.multi_field', [
  5230. 'options' => ['first', 'second', 'third'],
  5231. 'multiple' => 'checkbox'
  5232. ]);
  5233. $expected = [
  5234. ['div' => ['class' => 'input select']],
  5235. ['label' => ['for' => 'model-multi-field']],
  5236. 'Multi Field',
  5237. '/label',
  5238. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5239. ['div' => ['class' => 'checkbox']],
  5240. ['label' => ['for' => 'model-multi-field-0']],
  5241. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5242. 'first',
  5243. '/label',
  5244. '/div',
  5245. ['div' => ['class' => 'checkbox']],
  5246. ['label' => ['for' => 'model-multi-field-1']],
  5247. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5248. 'second',
  5249. '/label',
  5250. '/div',
  5251. ['div' => ['class' => 'checkbox']],
  5252. ['label' => ['for' => 'model-multi-field-2']],
  5253. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5254. 'third',
  5255. '/label',
  5256. '/div',
  5257. '/div'
  5258. ];
  5259. $this->assertHtml($expected, $result);
  5260. $result = $this->Form->control('Model.multi_field', [
  5261. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5262. 'multiple' => 'checkbox'
  5263. ]);
  5264. $expected = [
  5265. ['div' => ['class' => 'input select']],
  5266. ['label' => ['for' => 'model-multi-field']],
  5267. 'Multi Field',
  5268. '/label',
  5269. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5270. ['div' => ['class' => 'checkbox']],
  5271. ['label' => ['for' => 'model-multi-field-a']],
  5272. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5273. 'first',
  5274. '/label',
  5275. '/div',
  5276. ['div' => ['class' => 'checkbox']],
  5277. ['label' => ['for' => 'model-multi-field-b']],
  5278. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5279. 'second',
  5280. '/label',
  5281. '/div',
  5282. ['div' => ['class' => 'checkbox']],
  5283. ['label' => ['for' => 'model-multi-field-c']],
  5284. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5285. 'third',
  5286. '/label',
  5287. '/div',
  5288. '/div'
  5289. ];
  5290. $this->assertHtml($expected, $result);
  5291. }
  5292. /**
  5293. * testSelectHiddenFieldOmission method
  5294. *
  5295. * Test that select() with 'hiddenField' => false omits the hidden field.
  5296. *
  5297. * @return void
  5298. */
  5299. public function testSelectHiddenFieldOmission()
  5300. {
  5301. $result = $this->Form->select(
  5302. 'Model.multi_field',
  5303. ['first', 'second'],
  5304. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5305. );
  5306. $this->assertNotContains('type="hidden"', $result);
  5307. }
  5308. /**
  5309. * testSelectCheckboxMultipleOverrideName method
  5310. *
  5311. * Test that select() with multiple = checkbox works with overriding name attribute.
  5312. *
  5313. * @return void
  5314. */
  5315. public function testSelectCheckboxMultipleOverrideName()
  5316. {
  5317. $result = $this->Form->select('category', ['1', '2'], [
  5318. 'multiple' => 'checkbox',
  5319. 'name' => 'fish',
  5320. ]);
  5321. $expected = [
  5322. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5323. ['div' => ['class' => 'checkbox']],
  5324. ['label' => ['for' => 'fish-0']],
  5325. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5326. '1',
  5327. '/label',
  5328. '/div',
  5329. ['div' => ['class' => 'checkbox']],
  5330. ['label' => ['for' => 'fish-1']],
  5331. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5332. '2',
  5333. '/label',
  5334. '/div'
  5335. ];
  5336. $this->assertHtml($expected, $result);
  5337. $result = $this->Form->multiCheckbox(
  5338. 'category',
  5339. new Collection(['1', '2']),
  5340. ['name' => 'fish']
  5341. );
  5342. $this->assertHtml($expected, $result);
  5343. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5344. 'name' => 'fish',
  5345. ]);
  5346. $this->assertHtml($expected, $result);
  5347. }
  5348. /**
  5349. * testControlMultiCheckbox method
  5350. *
  5351. * Test that control() works with multicheckbox.
  5352. *
  5353. * @return void
  5354. */
  5355. public function testControlMultiCheckbox()
  5356. {
  5357. $result = $this->Form->control('category', [
  5358. 'type' => 'multicheckbox',
  5359. 'options' => ['1', '2'],
  5360. ]);
  5361. $expected = [
  5362. ['div' => ['class' => 'input multicheckbox']],
  5363. '<label',
  5364. 'Category',
  5365. '/label',
  5366. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5367. ['div' => ['class' => 'checkbox']],
  5368. ['label' => ['for' => 'category-0']],
  5369. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5370. '1',
  5371. '/label',
  5372. '/div',
  5373. ['div' => ['class' => 'checkbox']],
  5374. ['label' => ['for' => 'category-1']],
  5375. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5376. '2',
  5377. '/label',
  5378. '/div',
  5379. '/div',
  5380. ];
  5381. $this->assertHtml($expected, $result);
  5382. }
  5383. /**
  5384. * testCheckbox method
  5385. *
  5386. * Test generation of checkboxes.
  5387. *
  5388. * @return void
  5389. */
  5390. public function testCheckbox()
  5391. {
  5392. $result = $this->Form->checkbox('Model.field');
  5393. $expected = [
  5394. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5395. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5396. ];
  5397. $this->assertHtml($expected, $result);
  5398. $result = $this->Form->checkbox('Model.field', [
  5399. 'id' => 'theID',
  5400. 'value' => 'myvalue',
  5401. 'form' => 'my-form',
  5402. ]);
  5403. $expected = [
  5404. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5405. ['input' => [
  5406. 'type' => 'checkbox', 'name' => 'Model[field]',
  5407. 'value' => 'myvalue', 'id' => 'theID',
  5408. 'form' => 'my-form',
  5409. ]]
  5410. ];
  5411. $this->assertHtml($expected, $result);
  5412. }
  5413. /**
  5414. * testCheckboxDefaultValue method
  5415. *
  5416. * Test default value setting on checkbox() method.
  5417. *
  5418. * @return void
  5419. */
  5420. public function testCheckboxDefaultValue()
  5421. {
  5422. $this->Form->request->data['Model']['field'] = false;
  5423. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5424. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5425. $this->assertHtml($expected, $result);
  5426. unset($this->Form->request->data['Model']['field']);
  5427. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5428. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5429. $this->assertHtml($expected, $result);
  5430. $this->Form->request->data['Model']['field'] = true;
  5431. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5432. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5433. $this->assertHtml($expected, $result);
  5434. unset($this->Form->request->data['Model']['field']);
  5435. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5436. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5437. $this->assertHtml($expected, $result);
  5438. $Articles = TableRegistry::get('Articles');
  5439. $Articles->schema()->addColumn(
  5440. 'published',
  5441. ['type' => 'boolean', 'null' => false, 'default' => true]
  5442. );
  5443. $this->Form->create($Articles->newEntity());
  5444. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5445. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5446. $this->assertHtml($expected, $result);
  5447. }
  5448. /**
  5449. * testCheckboxCheckedAndError method
  5450. *
  5451. * Test checkbox being checked or having errors.
  5452. *
  5453. * @return void
  5454. */
  5455. public function testCheckboxCheckedAndError()
  5456. {
  5457. $this->article['errors'] = [
  5458. 'published' => true
  5459. ];
  5460. $this->Form->request->data['published'] = 'myvalue';
  5461. $this->Form->create($this->article);
  5462. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5463. $expected = [
  5464. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5465. ['input' => [
  5466. 'type' => 'checkbox',
  5467. 'name' => 'published',
  5468. 'value' => 'myvalue',
  5469. 'id' => 'theID',
  5470. 'checked' => 'checked',
  5471. 'class' => 'form-error'
  5472. ]]
  5473. ];
  5474. $this->assertHtml($expected, $result);
  5475. $this->Form->request->data['published'] = '';
  5476. $result = $this->Form->checkbox('published');
  5477. $expected = [
  5478. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5479. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5480. ];
  5481. $this->assertHtml($expected, $result);
  5482. }
  5483. /**
  5484. * testCheckboxCustomNameAttribute method
  5485. *
  5486. * Test checkbox() with a custom name attribute.
  5487. *
  5488. * @return void
  5489. */
  5490. public function testCheckboxCustomNameAttribute()
  5491. {
  5492. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5493. $expected = [
  5494. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5495. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5496. ];
  5497. $this->assertHtml($expected, $result);
  5498. }
  5499. /**
  5500. * testCheckboxHiddenField method
  5501. *
  5502. * Test that the hidden input for checkboxes can be omitted or set to a
  5503. * specific value.
  5504. *
  5505. * @return void
  5506. */
  5507. public function testCheckboxHiddenField()
  5508. {
  5509. $result = $this->Form->checkbox('UserForm.something', [
  5510. 'hiddenField' => false
  5511. ]);
  5512. $expected = [
  5513. 'input' => [
  5514. 'type' => 'checkbox',
  5515. 'name' => 'UserForm[something]',
  5516. 'value' => '1'
  5517. ],
  5518. ];
  5519. $this->assertHtml($expected, $result);
  5520. $result = $this->Form->checkbox('UserForm.something', [
  5521. 'value' => 'Y',
  5522. 'hiddenField' => 'N',
  5523. ]);
  5524. $expected = [
  5525. ['input' => [
  5526. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5527. 'value' => 'N'
  5528. ]],
  5529. ['input' => [
  5530. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5531. 'value' => 'Y'
  5532. ]],
  5533. ];
  5534. $this->assertHtml($expected, $result);
  5535. }
  5536. /**
  5537. * testTime method
  5538. *
  5539. * Test the time type.
  5540. *
  5541. * @return void
  5542. */
  5543. public function testTime()
  5544. {
  5545. $result = $this->Form->time('start_time', [
  5546. 'timeFormat' => 12,
  5547. 'interval' => 5,
  5548. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5549. ]);
  5550. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5551. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5552. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5553. $this->assertNotContains('year', $result);
  5554. $this->assertNotContains('month', $result);
  5555. $this->assertNotContains('day', $result);
  5556. $result = $this->Form->time('start_time', [
  5557. 'timeFormat' => 12,
  5558. 'interval' => 5,
  5559. 'value' => '2014-03-08 16:30:00'
  5560. ]);
  5561. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5562. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5563. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5564. $this->assertNotContains('year', $result);
  5565. $this->assertNotContains('month', $result);
  5566. $this->assertNotContains('day', $result);
  5567. }
  5568. /**
  5569. * testTimeFormat24NoMeridian method
  5570. *
  5571. * Ensure that timeFormat=24 has no merdian.
  5572. *
  5573. * @return void.
  5574. */
  5575. public function testTimeFormat24NoMeridian()
  5576. {
  5577. $result = $this->Form->time('start_time', [
  5578. 'timeFormat' => 24,
  5579. 'interval' => 5,
  5580. 'value' => '2014-03-08 16:30:00'
  5581. ]);
  5582. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5583. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5584. $this->assertNotContains('meridian', $result);
  5585. $this->assertNotContains('pm', $result);
  5586. $this->assertNotContains('year', $result);
  5587. $this->assertNotContains('month', $result);
  5588. $this->assertNotContains('day', $result);
  5589. }
  5590. /**
  5591. * testDate method
  5592. *
  5593. * Test the date type.
  5594. *
  5595. * @return void
  5596. */
  5597. public function testDate()
  5598. {
  5599. $result = $this->Form->date('start_day', [
  5600. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5601. ]);
  5602. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5603. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5604. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5605. $this->assertNotContains('hour', $result);
  5606. $this->assertNotContains('minute', $result);
  5607. $this->assertNotContains('second', $result);
  5608. $this->assertNotContains('meridian', $result);
  5609. }
  5610. /**
  5611. * testDateTime method
  5612. *
  5613. * Test generation of date/time select elements.
  5614. *
  5615. * @return void
  5616. */
  5617. public function testDateTime()
  5618. {
  5619. extract($this->dateRegex);
  5620. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5621. $now = strtotime('now');
  5622. $expected = [
  5623. ['select' => ['name' => 'Contact[date][year]']],
  5624. ['option' => ['value' => '']],
  5625. '/option',
  5626. $yearsRegex,
  5627. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5628. date('Y', $now),
  5629. '/option',
  5630. '*/select',
  5631. ['select' => ['name' => 'Contact[date][month]']],
  5632. ['option' => ['value' => '']],
  5633. '/option',
  5634. $monthsRegex,
  5635. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5636. date('F', $now),
  5637. '/option',
  5638. '*/select',
  5639. ['select' => ['name' => 'Contact[date][day]']],
  5640. ['option' => ['value' => '']],
  5641. '/option',
  5642. $daysRegex,
  5643. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5644. date('j', $now),
  5645. '/option',
  5646. '*/select',
  5647. ['select' => ['name' => 'Contact[date][hour]']],
  5648. ['option' => ['value' => '']],
  5649. '/option',
  5650. $hoursRegex,
  5651. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5652. date('G', $now),
  5653. '/option',
  5654. '*/select',
  5655. ['select' => ['name' => 'Contact[date][minute]']],
  5656. ['option' => ['value' => '']],
  5657. '/option',
  5658. $minutesRegex,
  5659. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5660. date('i', $now),
  5661. '/option',
  5662. '*/select',
  5663. ];
  5664. $this->assertHtml($expected, $result);
  5665. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5666. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5667. $now = strtotime('now');
  5668. $expected = [
  5669. ['select' => ['name' => 'Contact[date][year]']],
  5670. $yearsRegex,
  5671. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5672. date('Y', $now),
  5673. '/option',
  5674. '*/select',
  5675. ['select' => ['name' => 'Contact[date][month]']],
  5676. $monthsRegex,
  5677. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5678. date('F', $now),
  5679. '/option',
  5680. '*/select',
  5681. ['select' => ['name' => 'Contact[date][day]']],
  5682. $daysRegex,
  5683. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5684. date('j', $now),
  5685. '/option',
  5686. '*/select',
  5687. ['select' => ['name' => 'Contact[date][hour]']],
  5688. $hoursRegex,
  5689. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5690. date('G', $now),
  5691. '/option',
  5692. '*/select',
  5693. ['select' => ['name' => 'Contact[date][minute]']],
  5694. $minutesRegex,
  5695. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5696. date('i', $now),
  5697. '/option',
  5698. '*/select',
  5699. ];
  5700. $this->assertHtml($expected, $result);
  5701. }
  5702. /**
  5703. * testDateTimeSecured method
  5704. *
  5705. * Test that datetime fields are added to protected fields list.
  5706. *
  5707. * @return void
  5708. */
  5709. public function testDateTimeSecured()
  5710. {
  5711. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5712. $this->Form->dateTime('Contact.date');
  5713. $expected = [
  5714. 'Contact.date.year',
  5715. 'Contact.date.month',
  5716. 'Contact.date.day',
  5717. 'Contact.date.hour',
  5718. 'Contact.date.minute',
  5719. ];
  5720. $this->assertEquals($expected, $this->Form->fields);
  5721. $this->Form->fields = [];
  5722. $this->Form->date('Contact.published');
  5723. $expected = [
  5724. 'Contact.published.year',
  5725. 'Contact.published.month',
  5726. 'Contact.published.day',
  5727. ];
  5728. $this->assertEquals($expected, $this->Form->fields);
  5729. }
  5730. /**
  5731. * testDateTimeSecuredDisabled method
  5732. *
  5733. * Test that datetime fields are added to protected fields list.
  5734. *
  5735. * @return void
  5736. */
  5737. public function testDateTimeSecuredDisabled()
  5738. {
  5739. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5740. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5741. $expected = [];
  5742. $this->assertEquals($expected, $this->Form->fields);
  5743. $this->Form->fields = [];
  5744. $this->Form->date('Contact.published', ['secure' => false]);
  5745. $expected = [];
  5746. $this->assertEquals($expected, $this->Form->fields);
  5747. }
  5748. /**
  5749. * testDatetimeEmpty method
  5750. *
  5751. * Test empty defaulting to true for datetime.
  5752. *
  5753. * @return void
  5754. */
  5755. public function testDatetimeEmpty()
  5756. {
  5757. extract($this->dateRegex);
  5758. $result = $this->Form->dateTime('Contact.date', [
  5759. 'timeFormat' => 12,
  5760. 'empty' => true,
  5761. 'default' => true
  5762. ]);
  5763. $expected = [
  5764. ['select' => ['name' => 'Contact[date][year]']],
  5765. $yearsRegex,
  5766. ['option' => ['value' => '']],
  5767. '/option',
  5768. '*/select',
  5769. ['select' => ['name' => 'Contact[date][month]']],
  5770. $monthsRegex,
  5771. ['option' => ['value' => '']],
  5772. '/option',
  5773. '*/select',
  5774. ['select' => ['name' => 'Contact[date][day]']],
  5775. $daysRegex,
  5776. ['option' => ['value' => '']],
  5777. '/option',
  5778. '*/select',
  5779. ['select' => ['name' => 'Contact[date][hour]']],
  5780. $hoursRegex,
  5781. ['option' => ['value' => '']],
  5782. '/option',
  5783. '*/select',
  5784. ['select' => ['name' => 'Contact[date][minute]']],
  5785. $minutesRegex,
  5786. ['option' => ['value' => '']],
  5787. '/option',
  5788. '*/select',
  5789. ['select' => ['name' => 'Contact[date][meridian]']],
  5790. $meridianRegex,
  5791. ['option' => ['value' => '']],
  5792. '/option',
  5793. '*/select'
  5794. ];
  5795. $this->assertHtml($expected, $result);
  5796. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5797. }
  5798. /**
  5799. * testDatetimeMinuteInterval method
  5800. *
  5801. * Test datetime with interval option.
  5802. *
  5803. * @return void
  5804. */
  5805. public function testDatetimeMinuteInterval()
  5806. {
  5807. extract($this->dateRegex);
  5808. $result = $this->Form->dateTime('Contact.date', [
  5809. 'interval' => 5,
  5810. 'value' => ''
  5811. ]);
  5812. $expected = [
  5813. ['select' => ['name' => 'Contact[date][year]']],
  5814. $yearsRegex,
  5815. ['option' => ['selected' => 'selected', 'value' => '']],
  5816. '/option',
  5817. '*/select',
  5818. ['select' => ['name' => 'Contact[date][month]']],
  5819. $monthsRegex,
  5820. ['option' => ['selected' => 'selected', 'value' => '']],
  5821. '/option',
  5822. '*/select',
  5823. ['select' => ['name' => 'Contact[date][day]']],
  5824. $daysRegex,
  5825. ['option' => ['selected' => 'selected', 'value' => '']],
  5826. '/option',
  5827. '*/select',
  5828. ['select' => ['name' => 'Contact[date][hour]']],
  5829. $hoursRegex,
  5830. ['option' => ['selected' => 'selected', 'value' => '']],
  5831. '/option',
  5832. '*/select',
  5833. ['select' => ['name' => 'Contact[date][minute]']],
  5834. $minutesRegex,
  5835. ['option' => ['selected' => 'selected', 'value' => '']],
  5836. '/option',
  5837. ['option' => ['value' => '00']],
  5838. '00',
  5839. '/option',
  5840. ['option' => ['value' => '05']],
  5841. '05',
  5842. '/option',
  5843. ['option' => ['value' => '10']],
  5844. '10',
  5845. '/option',
  5846. '*/select',
  5847. ];
  5848. $this->assertHtml($expected, $result);
  5849. }
  5850. /**
  5851. * testDateTimeRounding method
  5852. *
  5853. * Test dateTime with rounding.
  5854. *
  5855. * @return void
  5856. */
  5857. public function testDateTimeRounding()
  5858. {
  5859. $this->Form->request->data['Contact'] = [
  5860. 'date' => [
  5861. 'day' => '13',
  5862. 'month' => '12',
  5863. 'year' => '2010',
  5864. 'hour' => '04',
  5865. 'minute' => '19',
  5866. 'meridian' => 'AM'
  5867. ]
  5868. ];
  5869. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5870. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5871. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5872. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5873. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5874. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5875. }
  5876. /**
  5877. * testDatetimeWithDefault method
  5878. *
  5879. * Test that datetime() and default values work.
  5880. *
  5881. * @return void
  5882. */
  5883. public function testDatetimeWithDefault()
  5884. {
  5885. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5886. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5887. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5888. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5889. $result = $this->Form->dateTime('Contact.updated', [
  5890. 'default' => '2009-06-01 11:15:30'
  5891. ]);
  5892. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5893. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5894. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5895. }
  5896. /**
  5897. * testDateTimeAllZeros method
  5898. *
  5899. * Test datetime() with all zeros.
  5900. *
  5901. * @return void
  5902. */
  5903. public function testDateTimeAllZeros()
  5904. {
  5905. $result = $this->Form->dateTime('Contact.date', [
  5906. 'timeFormat' => false,
  5907. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5908. 'value' => '0000-00-00'
  5909. ]);
  5910. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5911. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5912. }
  5913. /**
  5914. * testDateTimeEmptyAsArray method
  5915. *
  5916. * @return void
  5917. */
  5918. public function testDateTimeEmptyAsArray()
  5919. {
  5920. $result = $this->Form->dateTime('Contact.date', [
  5921. 'empty' => [
  5922. 'day' => 'DAY',
  5923. 'month' => 'MONTH',
  5924. 'year' => 'YEAR',
  5925. 'hour' => 'HOUR',
  5926. 'minute' => 'MINUTE',
  5927. 'meridian' => false
  5928. ],
  5929. 'default' => true
  5930. ]);
  5931. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5932. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5933. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5934. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5935. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5936. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5937. $result = $this->Form->dateTime('Contact.date', [
  5938. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5939. 'default' => true
  5940. ]);
  5941. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5942. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5943. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5944. }
  5945. /**
  5946. * testFormDateTimeMulti method
  5947. *
  5948. * Test multiple datetime element generation.
  5949. *
  5950. * @return void
  5951. */
  5952. public function testFormDateTimeMulti()
  5953. {
  5954. extract($this->dateRegex);
  5955. $result = $this->Form->dateTime('Contact.1.updated');
  5956. $this->assertContains('Contact[1][updated][month]', $result);
  5957. $this->assertContains('Contact[1][updated][day]', $result);
  5958. $this->assertContains('Contact[1][updated][year]', $result);
  5959. $this->assertContains('Contact[1][updated][hour]', $result);
  5960. $this->assertContains('Contact[1][updated][minute]', $result);
  5961. }
  5962. /**
  5963. * testDateTimeLabelIdMatchesFirstControl method
  5964. *
  5965. * When changing the date format, the label should always focus the first select box when
  5966. * clicked.
  5967. *
  5968. * @return void
  5969. */
  5970. public function testDateTimeLabelIdMatchesFirstControl()
  5971. {
  5972. $result = $this->Form->control('Model.date', ['type' => 'date']);
  5973. $this->assertContains('<label>Date</label>', $result);
  5974. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  5975. $this->assertContains('<label>Date</label>', $result);
  5976. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  5977. $this->assertContains('<label>Date</label>', $result);
  5978. }
  5979. /**
  5980. * testDateTimeSecondOptions method
  5981. *
  5982. * Test datetime second=true.
  5983. *
  5984. * @return void
  5985. */
  5986. public function testDateTimeSecondOptions()
  5987. {
  5988. $result = $this->Form->dateTime('updated', ['second' => true]);
  5989. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5990. $result = $this->Form->dateTime('updated', ['second' => []]);
  5991. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5992. $result = $this->Form->dateTime('updated', ['second' => null]);
  5993. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5994. $result = $this->Form->dateTime('updated', ['second' => false]);
  5995. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5996. }
  5997. /**
  5998. * testMonth method
  5999. *
  6000. * Test generation of a month input.
  6001. *
  6002. * @return void
  6003. */
  6004. public function testMonth()
  6005. {
  6006. $result = $this->Form->month('Model.field', ['value' => '']);
  6007. $expected = [
  6008. ['select' => ['name' => 'Model[field][month]']],
  6009. ['option' => ['value' => '', 'selected' => 'selected']],
  6010. '/option',
  6011. ['option' => ['value' => '01']],
  6012. date('F', strtotime('2008-01-01 00:00:00')),
  6013. '/option',
  6014. ['option' => ['value' => '02']],
  6015. date('F', strtotime('2008-02-01 00:00:00')),
  6016. '/option',
  6017. '*/select',
  6018. ];
  6019. $this->assertHtml($expected, $result);
  6020. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  6021. $expected = [
  6022. ['select' => ['name' => 'Model[field][month]']],
  6023. ['option' => ['selected' => 'selected', 'value' => '']],
  6024. '/option',
  6025. ['option' => ['value' => '01']],
  6026. date('F', strtotime('2008-01-01 00:00:00')),
  6027. '/option',
  6028. ['option' => ['value' => '02']],
  6029. date('F', strtotime('2008-02-01 00:00:00')),
  6030. '/option',
  6031. '*/select',
  6032. ];
  6033. $this->assertHtml($expected, $result);
  6034. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  6035. $expected = [
  6036. ['select' => ['name' => 'Model[field][month]']],
  6037. ['option' => ['selected' => 'selected', 'value' => '']],
  6038. '/option',
  6039. ['option' => ['value' => '01']],
  6040. '1',
  6041. '/option',
  6042. ['option' => ['value' => '02']],
  6043. '2',
  6044. '/option',
  6045. '*/select',
  6046. ];
  6047. $this->assertHtml($expected, $result);
  6048. $monthNames = [
  6049. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6050. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  6051. ];
  6052. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  6053. $expected = [
  6054. ['select' => ['name' => 'Model[field][month]']],
  6055. ['option' => ['value' => '']],
  6056. '/option',
  6057. ['option' => ['value' => '01', 'selected' => 'selected']],
  6058. 'Jan',
  6059. '/option',
  6060. ['option' => ['value' => '02']],
  6061. 'Feb',
  6062. '/option',
  6063. '*/select',
  6064. ];
  6065. $this->assertHtml($expected, $result);
  6066. $this->Form->request->data['Project']['release'] = '2050-02-10';
  6067. $result = $this->Form->month('Project.release');
  6068. $expected = [
  6069. ['select' => ['name' => 'Project[release][month]']],
  6070. ['option' => ['value' => '']],
  6071. '/option',
  6072. ['option' => ['value' => '01']],
  6073. 'January',
  6074. '/option',
  6075. ['option' => ['value' => '02', 'selected' => 'selected']],
  6076. 'February',
  6077. '/option',
  6078. '*/select',
  6079. ];
  6080. $this->assertHtml($expected, $result);
  6081. $result = $this->Form->month('Contact.published', [
  6082. 'empty' => 'Published on',
  6083. ]);
  6084. $this->assertContains('Published on', $result);
  6085. }
  6086. /**
  6087. * testDay method
  6088. *
  6089. * Test generation of a day input.
  6090. *
  6091. * @return void
  6092. */
  6093. public function testDay()
  6094. {
  6095. extract($this->dateRegex);
  6096. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  6097. $expected = [
  6098. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  6099. ['option' => ['selected' => 'selected', 'value' => '']],
  6100. '/option',
  6101. ['option' => ['value' => '01']],
  6102. '1',
  6103. '/option',
  6104. ['option' => ['value' => '02']],
  6105. '2',
  6106. '/option',
  6107. $daysRegex,
  6108. '/select',
  6109. ];
  6110. $this->assertHtml($expected, $result);
  6111. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6112. $result = $this->Form->day('Model.field');
  6113. $expected = [
  6114. ['select' => ['name' => 'Model[field][day]']],
  6115. ['option' => ['value' => '']],
  6116. '/option',
  6117. ['option' => ['value' => '01']],
  6118. '1',
  6119. '/option',
  6120. ['option' => ['value' => '02']],
  6121. '2',
  6122. '/option',
  6123. $daysRegex,
  6124. ['option' => ['value' => '10', 'selected' => 'selected']],
  6125. '10',
  6126. '/option',
  6127. $daysRegex,
  6128. '/select',
  6129. ];
  6130. $this->assertHtml($expected, $result);
  6131. $this->Form->request->data['Model']['field'] = '';
  6132. $result = $this->Form->day('Model.field', ['value' => '10']);
  6133. $expected = [
  6134. ['select' => ['name' => 'Model[field][day]']],
  6135. ['option' => ['value' => '']],
  6136. '/option',
  6137. ['option' => ['value' => '01']],
  6138. '1',
  6139. '/option',
  6140. ['option' => ['value' => '02']],
  6141. '2',
  6142. '/option',
  6143. $daysRegex,
  6144. ['option' => ['value' => '10', 'selected' => 'selected']],
  6145. '10',
  6146. '/option',
  6147. $daysRegex,
  6148. '/select',
  6149. ];
  6150. $this->assertHtml($expected, $result);
  6151. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6152. $result = $this->Form->day('Project.release');
  6153. $expected = [
  6154. ['select' => ['name' => 'Project[release][day]']],
  6155. ['option' => ['value' => '']],
  6156. '/option',
  6157. ['option' => ['value' => '01']],
  6158. '1',
  6159. '/option',
  6160. ['option' => ['value' => '02']],
  6161. '2',
  6162. '/option',
  6163. $daysRegex,
  6164. ['option' => ['value' => '10', 'selected' => 'selected']],
  6165. '10',
  6166. '/option',
  6167. $daysRegex,
  6168. '/select',
  6169. ];
  6170. $this->assertHtml($expected, $result);
  6171. $result = $this->Form->day('Contact.published', [
  6172. 'empty' => 'Published on',
  6173. ]);
  6174. $this->assertContains('Published on', $result);
  6175. }
  6176. /**
  6177. * testMinute method
  6178. *
  6179. * Test generation of a minute input.
  6180. *
  6181. * @return void
  6182. */
  6183. public function testMinute()
  6184. {
  6185. extract($this->dateRegex);
  6186. $result = $this->Form->minute('Model.field', ['value' => '']);
  6187. $expected = [
  6188. ['select' => ['name' => 'Model[field][minute]']],
  6189. ['option' => ['selected' => 'selected', 'value' => '']],
  6190. '/option',
  6191. ['option' => ['value' => '00']],
  6192. '00',
  6193. '/option',
  6194. ['option' => ['value' => '01']],
  6195. '01',
  6196. '/option',
  6197. ['option' => ['value' => '02']],
  6198. '02',
  6199. '/option',
  6200. $minutesRegex,
  6201. '/select',
  6202. ];
  6203. $this->assertHtml($expected, $result);
  6204. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6205. $result = $this->Form->minute('Model.field');
  6206. $expected = [
  6207. ['select' => ['name' => 'Model[field][minute]']],
  6208. ['option' => ['value' => '']],
  6209. '/option',
  6210. ['option' => ['value' => '00']],
  6211. '00',
  6212. '/option',
  6213. ['option' => ['value' => '01']],
  6214. '01',
  6215. '/option',
  6216. ['option' => ['value' => '02']],
  6217. '02',
  6218. '/option',
  6219. $minutesRegex,
  6220. ['option' => ['value' => '12', 'selected' => 'selected']],
  6221. '12',
  6222. '/option',
  6223. $minutesRegex,
  6224. '/select',
  6225. ];
  6226. $this->assertHtml($expected, $result);
  6227. $this->Form->request->data['Model']['field'] = '';
  6228. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6229. $expected = [
  6230. ['select' => ['name' => 'Model[field][minute]']],
  6231. ['option' => ['selected' => 'selected', 'value' => '']],
  6232. '/option',
  6233. ['option' => ['value' => '00']],
  6234. '00',
  6235. '/option',
  6236. ['option' => ['value' => '05']],
  6237. '05',
  6238. '/option',
  6239. ['option' => ['value' => '10']],
  6240. '10',
  6241. '/option',
  6242. $minutesRegex,
  6243. '/select',
  6244. ];
  6245. $this->assertHtml($expected, $result);
  6246. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6247. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6248. $expected = [
  6249. ['select' => ['name' => 'Model[field][minute]']],
  6250. ['option' => ['value' => '']],
  6251. '/option',
  6252. ['option' => ['value' => '00']],
  6253. '00',
  6254. '/option',
  6255. ['option' => ['value' => '05']],
  6256. '05',
  6257. '/option',
  6258. ['option' => ['value' => '10', 'selected' => 'selected']],
  6259. '10',
  6260. '/option',
  6261. $minutesRegex,
  6262. '/select',
  6263. ];
  6264. $this->assertHtml($expected, $result);
  6265. }
  6266. /**
  6267. * testMeridian method
  6268. *
  6269. * Test generating an input for the meridian.
  6270. *
  6271. * @return void
  6272. */
  6273. public function testMeridian()
  6274. {
  6275. extract($this->dateRegex);
  6276. $now = new \DateTime();
  6277. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6278. $expected = [
  6279. ['select' => ['name' => 'Model[field][meridian]']],
  6280. ['option' => ['value' => '']],
  6281. '/option',
  6282. $meridianRegex,
  6283. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6284. $now->format('a'),
  6285. '/option',
  6286. '*/select'
  6287. ];
  6288. $this->assertHtml($expected, $result);
  6289. }
  6290. /**
  6291. * testHour method
  6292. *
  6293. * Test generation of an hour input.
  6294. *
  6295. * @return void
  6296. */
  6297. public function testHour()
  6298. {
  6299. extract($this->dateRegex);
  6300. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6301. $expected = [
  6302. ['select' => ['name' => 'Model[field][hour]']],
  6303. ['option' => ['selected' => 'selected', 'value' => '']],
  6304. '/option',
  6305. ['option' => ['value' => '01']],
  6306. '1',
  6307. '/option',
  6308. ['option' => ['value' => '02']],
  6309. '2',
  6310. '/option',
  6311. $hoursRegex,
  6312. '/select',
  6313. ];
  6314. $this->assertHtml($expected, $result);
  6315. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6316. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6317. $expected = [
  6318. ['select' => ['name' => 'Model[field][hour]']],
  6319. ['option' => ['value' => '']],
  6320. '/option',
  6321. ['option' => ['value' => '01']],
  6322. '1',
  6323. '/option',
  6324. ['option' => ['value' => '02']],
  6325. '2',
  6326. '/option',
  6327. $hoursRegex,
  6328. ['option' => ['value' => '12', 'selected' => 'selected']],
  6329. '12',
  6330. '/option',
  6331. '/select',
  6332. ];
  6333. $this->assertHtml($expected, $result);
  6334. $this->Form->request->data['Model']['field'] = '';
  6335. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6336. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6337. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6338. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6339. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6340. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6341. $expected = [
  6342. ['select' => ['name' => 'Model[field][hour]']],
  6343. ['option' => ['value' => '']],
  6344. '/option',
  6345. ['option' => ['value' => '00', 'selected' => 'selected']],
  6346. '0',
  6347. '/option',
  6348. ['option' => ['value' => '01']],
  6349. '1',
  6350. '/option',
  6351. ['option' => ['value' => '02']],
  6352. '2',
  6353. '/option',
  6354. $hoursRegex,
  6355. '/select',
  6356. ];
  6357. $this->assertHtml($expected, $result);
  6358. unset($this->Form->request->data['Model']['field']);
  6359. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6360. $thisHour = date('H');
  6361. $optValue = date('G');
  6362. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6363. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6364. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6365. $expected = [
  6366. ['select' => ['name' => 'Model[field][hour]']],
  6367. ['option' => ['value' => '']],
  6368. '/option',
  6369. ['option' => ['value' => '00']],
  6370. '0',
  6371. '/option',
  6372. ['option' => ['value' => '01', 'selected' => 'selected']],
  6373. '1',
  6374. '/option',
  6375. ['option' => ['value' => '02']],
  6376. '2',
  6377. '/option',
  6378. $hoursRegex,
  6379. '/select',
  6380. ];
  6381. $this->assertHtml($expected, $result);
  6382. }
  6383. /**
  6384. * testYear method
  6385. *
  6386. * Test generation of a year input.
  6387. *
  6388. * @return void
  6389. */
  6390. public function testYear()
  6391. {
  6392. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6393. $expected = [
  6394. ['select' => ['name' => 'Model[field][year]']],
  6395. ['option' => ['selected' => 'selected', 'value' => '']],
  6396. '/option',
  6397. ['option' => ['value' => '2007']],
  6398. '2007',
  6399. '/option',
  6400. ['option' => ['value' => '2006']],
  6401. '2006',
  6402. '/option',
  6403. '/select',
  6404. ];
  6405. $this->assertHtml($expected, $result);
  6406. $result = $this->Form->year('Model.field', [
  6407. 'value' => '',
  6408. 'minYear' => 2006,
  6409. 'maxYear' => 2007,
  6410. 'orderYear' => 'asc'
  6411. ]);
  6412. $expected = [
  6413. ['select' => ['name' => 'Model[field][year]']],
  6414. ['option' => ['selected' => 'selected', 'value' => '']],
  6415. '/option',
  6416. ['option' => ['value' => '2006']],
  6417. '2006',
  6418. '/option',
  6419. ['option' => ['value' => '2007']],
  6420. '2007',
  6421. '/option',
  6422. '/select',
  6423. ];
  6424. $this->assertHtml($expected, $result);
  6425. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6426. $result = $this->Form->year('Contact.published', [
  6427. 'empty' => false,
  6428. 'minYear' => 2006,
  6429. 'maxYear' => 2007,
  6430. ]);
  6431. $expected = [
  6432. ['select' => ['name' => 'Contact[published][year]']],
  6433. ['option' => ['value' => '2007']],
  6434. '2007',
  6435. '/option',
  6436. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6437. '2006',
  6438. '/option',
  6439. '/select',
  6440. ];
  6441. $this->assertHtml($expected, $result);
  6442. $result = $this->Form->year('Contact.published', [
  6443. 'empty' => 'Published on',
  6444. ]);
  6445. $this->assertContains('Published on', $result);
  6446. }
  6447. /**
  6448. * testControlDatetimePreEpoch method
  6449. *
  6450. * Test minYear being prior to the unix epoch.
  6451. *
  6452. * @return void
  6453. */
  6454. public function testControlDatetimePreEpoch()
  6455. {
  6456. $start = date('Y') - 80;
  6457. $end = date('Y') - 18;
  6458. $result = $this->Form->control('birth_year', [
  6459. 'type' => 'date',
  6460. 'label' => 'Birth Year',
  6461. 'minYear' => $start,
  6462. 'maxYear' => $end,
  6463. 'month' => false,
  6464. 'day' => false,
  6465. ]);
  6466. $this->assertContains('value="' . $start . '">' . $start, $result);
  6467. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6468. $this->assertNotContains('value="00">00', $result);
  6469. }
  6470. /**
  6471. * test control() datetime & required attributes
  6472. *
  6473. * @return void
  6474. */
  6475. public function testControlDatetimeRequired()
  6476. {
  6477. $result = $this->Form->control('birthday', [
  6478. 'type' => 'date',
  6479. 'required' => true
  6480. ]);
  6481. $this->assertContains(
  6482. '<select name="birthday[year]" required="required"',
  6483. $result
  6484. );
  6485. }
  6486. /**
  6487. * testYearAutoExpandRange method
  6488. *
  6489. * @return void
  6490. */
  6491. public function testYearAutoExpandRange()
  6492. {
  6493. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6494. $result = $this->Form->year('User.birthday');
  6495. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6496. $result = $matches[1];
  6497. $expected = range(date('Y') + 5, 1930);
  6498. $this->assertEquals($expected, $result);
  6499. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6500. $result = $this->Form->year('Project.release');
  6501. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6502. $result = $matches[1];
  6503. $expected = range(2050, date('Y') - 5);
  6504. $this->assertEquals($expected, $result);
  6505. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6506. $result = $this->Form->year('Project.release', [
  6507. 'minYear' => 1890,
  6508. 'maxYear' => 1900
  6509. ]);
  6510. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6511. $result = $matches[1];
  6512. $expected = range(1900, 1881);
  6513. $this->assertEquals($expected, $result);
  6514. }
  6515. /**
  6516. * testControlDate method
  6517. *
  6518. * Test that control() accepts the type of date and passes options in.
  6519. *
  6520. * @return void
  6521. */
  6522. public function testControlDate()
  6523. {
  6524. $this->Form->request->data = [
  6525. 'month_year' => ['month' => date('m')],
  6526. ];
  6527. $this->Form->create($this->article);
  6528. $result = $this->Form->control('month_year', [
  6529. 'label' => false,
  6530. 'type' => 'date',
  6531. 'minYear' => 2006,
  6532. 'maxYear' => 2008
  6533. ]);
  6534. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6535. $this->assertNotContains('value="2008" selected="selected"', $result);
  6536. }
  6537. /**
  6538. * testControlLabelFalse method
  6539. *
  6540. * Test the label option being set to false.
  6541. *
  6542. * @return void
  6543. */
  6544. public function testControlLabelFalse()
  6545. {
  6546. $this->Form->create($this->article);
  6547. $result = $this->Form->control('title', ['label' => false]);
  6548. $expected = [
  6549. 'div' => ['class' => 'input text required'],
  6550. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6551. '/div'
  6552. ];
  6553. $this->assertHtml($expected, $result);
  6554. }
  6555. /**
  6556. * testControlDateMaxYear method
  6557. *
  6558. * Let's say we want to only allow users born from 2006 to 2008 to register
  6559. * This being the first signup page, we still don't have any data.
  6560. *
  6561. * @return void
  6562. */
  6563. public function testControlDateMaxYear()
  6564. {
  6565. $this->Form->request->data = [];
  6566. $this->Form->create($this->article);
  6567. $result = $this->Form->control('birthday', [
  6568. 'label' => false,
  6569. 'type' => 'date',
  6570. 'minYear' => 2006,
  6571. 'maxYear' => 2008,
  6572. 'default' => true
  6573. ]);
  6574. $this->assertContains('value="2008" selected="selected"', $result);
  6575. $this->assertContains('value="2006"', $result);
  6576. $this->assertNotContains('value="2005"', $result);
  6577. $this->assertNotContains('value="2009"', $result);
  6578. }
  6579. /**
  6580. * testTextArea method
  6581. *
  6582. * Test generation of a textarea input.
  6583. *
  6584. * @return void
  6585. */
  6586. public function testTextArea()
  6587. {
  6588. $this->Form->request->data = ['field' => 'some test data'];
  6589. $result = $this->Form->textarea('field');
  6590. $expected = [
  6591. 'textarea' => ['name' => 'field', 'rows' => 5],
  6592. 'some test data',
  6593. '/textarea',
  6594. ];
  6595. $this->assertHtml($expected, $result);
  6596. $result = $this->Form->textarea('user.bio');
  6597. $expected = [
  6598. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6599. '/textarea',
  6600. ];
  6601. $this->assertHtml($expected, $result);
  6602. $this->Form->request->data = ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'];
  6603. $result = $this->Form->textarea('field');
  6604. $expected = [
  6605. 'textarea' => ['name' => 'field', 'rows' => 5],
  6606. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6607. '/textarea',
  6608. ];
  6609. $this->assertHtml($expected, $result);
  6610. $this->Form->request->data = [
  6611. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  6612. ];
  6613. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6614. $expected = [
  6615. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6616. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6617. '/textarea',
  6618. ];
  6619. $this->assertHtml($expected, $result);
  6620. $result = $this->Form->textarea('0.OtherModel.field');
  6621. $expected = [
  6622. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6623. '/textarea'
  6624. ];
  6625. $this->assertHtml($expected, $result);
  6626. }
  6627. /**
  6628. * testTextAreaWithStupidCharacters method
  6629. *
  6630. * Test text area with non-ascii characters.
  6631. *
  6632. * @return void
  6633. */
  6634. public function testTextAreaWithStupidCharacters()
  6635. {
  6636. $result = $this->Form->textarea('Post.content', [
  6637. 'value' => 'GREAT®',
  6638. 'rows' => '15',
  6639. 'cols' => '75'
  6640. ]);
  6641. $expected = [
  6642. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6643. 'GREAT®',
  6644. '/textarea',
  6645. ];
  6646. $this->assertHtml($expected, $result);
  6647. }
  6648. /**
  6649. * testTextAreaMaxLength method
  6650. *
  6651. * Test textareas maxlength read from schema.
  6652. *
  6653. * @return void
  6654. */
  6655. public function testTextAreaMaxLength()
  6656. {
  6657. $this->Form->create([
  6658. 'schema' => [
  6659. 'stuff' => ['type' => 'string', 'length' => 10],
  6660. ]
  6661. ]);
  6662. $result = $this->Form->control('other', ['type' => 'textarea']);
  6663. $expected = [
  6664. 'div' => ['class' => 'input textarea'],
  6665. 'label' => ['for' => 'other'],
  6666. 'Other',
  6667. '/label',
  6668. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6669. '/textarea',
  6670. '/div'
  6671. ];
  6672. $this->assertHtml($expected, $result);
  6673. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6674. $expected = [
  6675. 'div' => ['class' => 'input textarea'],
  6676. 'label' => ['for' => 'stuff'],
  6677. 'Stuff',
  6678. '/label',
  6679. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6680. '/textarea',
  6681. '/div'
  6682. ];
  6683. $this->assertHtml($expected, $result);
  6684. }
  6685. /**
  6686. * testHiddenField method
  6687. *
  6688. * Test generation of a hidden input.
  6689. *
  6690. * @return void
  6691. */
  6692. public function testHiddenField()
  6693. {
  6694. $this->article['errors'] = [
  6695. 'field' => true
  6696. ];
  6697. $this->Form->request->data['field'] = 'test';
  6698. $this->Form->create($this->article);
  6699. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6700. $expected = [
  6701. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6702. $this->assertHtml($expected, $result);
  6703. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6704. $expected = [
  6705. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6706. ];
  6707. $this->assertHtml($expected, $result);
  6708. }
  6709. /**
  6710. * testFileUploadField method
  6711. *
  6712. * Test generation of a file upload input.
  6713. *
  6714. * @return void
  6715. */
  6716. public function testFileUploadField()
  6717. {
  6718. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6719. $result = $this->Form->file('Model.upload');
  6720. $this->assertHtml($expected, $result);
  6721. $this->Form->request->data['Model']['upload'] = [
  6722. 'name' => '', 'type' => '', 'tmp_name' => '',
  6723. 'error' => 4, 'size' => 0
  6724. ];
  6725. $result = $this->Form->file('Model.upload');
  6726. $this->assertHtml($expected, $result);
  6727. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6728. $result = $this->Form->file('Model.upload');
  6729. $this->assertHtml($expected, $result);
  6730. }
  6731. /**
  6732. * testFileUploadOnOtherModel method
  6733. *
  6734. * Test File upload input on a model not used in create().
  6735. *
  6736. * @return void
  6737. */
  6738. public function testFileUploadOnOtherModel()
  6739. {
  6740. $this->Form->create($this->article, ['type' => 'file']);
  6741. $result = $this->Form->file('ValidateProfile.city');
  6742. $expected = [
  6743. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6744. ];
  6745. $this->assertHtml($expected, $result);
  6746. }
  6747. /**
  6748. * testButton method
  6749. *
  6750. * Test generation of a form button.
  6751. *
  6752. * @return void
  6753. */
  6754. public function testButton()
  6755. {
  6756. $result = $this->Form->button('Hi');
  6757. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6758. $this->assertHtml($expected, $result);
  6759. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6760. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6761. $this->assertHtml($expected, $result);
  6762. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6763. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6764. $this->assertHtml($expected, $result);
  6765. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6766. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6767. $this->assertHtml($expected, $result);
  6768. $result = $this->Form->button('No type', ['type' => false]);
  6769. $expected = ['button' => [], 'No type', '/button'];
  6770. $this->assertHtml($expected, $result);
  6771. $result = $this->Form->button('Upload Text', [
  6772. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6773. 'escape' => false
  6774. ]);
  6775. $this->assertNotRegExp('/\&039/', $result);
  6776. }
  6777. /**
  6778. * testButtonUnlockedByDefault method
  6779. *
  6780. * Test that button() makes unlocked fields by default.
  6781. *
  6782. * @return void
  6783. */
  6784. public function testButtonUnlockedByDefault()
  6785. {
  6786. $this->Form->request->params['_csrfToken'] = 'secured';
  6787. $this->Form->button('Save', ['name' => 'save']);
  6788. $this->Form->button('Clear');
  6789. $result = $this->Form->unlockField();
  6790. $this->assertEquals(['save'], $result);
  6791. }
  6792. /**
  6793. * Test generation of a form button with confirm message.
  6794. *
  6795. * @return void
  6796. */
  6797. public function testButtonWithConfirm()
  6798. {
  6799. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6800. $expected = ['button' => ['type' => 'submit', 'onclick' => 'if (confirm(&quot;Confirm me!&quot;)) { return true; } return false;'], 'Hi', '/button'];
  6801. $this->assertHtml($expected, $result);
  6802. }
  6803. /**
  6804. * testPostButton method
  6805. *
  6806. * @return void
  6807. */
  6808. public function testPostButton()
  6809. {
  6810. $result = $this->Form->postButton('Hi', '/controller/action');
  6811. $expected = [
  6812. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6813. 'div' => ['style' => 'display:none;'],
  6814. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6815. '/div',
  6816. 'button' => ['type' => 'submit'],
  6817. 'Hi',
  6818. '/button',
  6819. '/form'
  6820. ];
  6821. $this->assertHtml($expected, $result);
  6822. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6823. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  6824. }
  6825. /**
  6826. * testPostButtonMethodType method
  6827. *
  6828. * @return void
  6829. */
  6830. public function testPostButtonMethodType()
  6831. {
  6832. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6833. $expected = [
  6834. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6835. 'div' => ['style' => 'display:none;'],
  6836. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6837. '/div',
  6838. 'button' => ['type' => 'submit'],
  6839. 'Hi',
  6840. '/button',
  6841. '/form'
  6842. ];
  6843. $this->assertHtml($expected, $result);
  6844. }
  6845. /**
  6846. * testPostButtonFormOptions method
  6847. *
  6848. * @return void
  6849. */
  6850. public function testPostButtonFormOptions()
  6851. {
  6852. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6853. $expected = [
  6854. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6855. 'div' => ['style' => 'display:none;'],
  6856. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6857. '/div',
  6858. 'button' => ['type' => 'submit'],
  6859. 'Hi',
  6860. '/button',
  6861. '/form'
  6862. ];
  6863. $this->assertHtml($expected, $result);
  6864. }
  6865. /**
  6866. * testPostButtonNestedData method
  6867. *
  6868. * Test using postButton with N dimensional data.
  6869. *
  6870. * @return void
  6871. */
  6872. public function testPostButtonNestedData()
  6873. {
  6874. $data = [
  6875. 'one' => [
  6876. 'two' => [
  6877. 3, 4, 5
  6878. ]
  6879. ]
  6880. ];
  6881. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6882. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6883. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6884. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6885. }
  6886. /**
  6887. * testSecurePostButton method
  6888. *
  6889. * Test that postButton adds _Token fields.
  6890. *
  6891. * @return void
  6892. */
  6893. public function testSecurePostButton()
  6894. {
  6895. $this->Form->request->params['_csrfToken'] = 'testkey';
  6896. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  6897. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6898. $tokenDebug = urlencode(json_encode([
  6899. '/posts/delete/1',
  6900. [],
  6901. []
  6902. ]));
  6903. $expected = [
  6904. 'form' => [
  6905. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6906. ],
  6907. ['div' => ['style' => 'display:none;']],
  6908. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6909. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6910. '/div',
  6911. 'button' => ['type' => 'submit'],
  6912. 'Delete',
  6913. '/button',
  6914. ['div' => ['style' => 'display:none;']],
  6915. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6916. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6917. ['input' => [
  6918. 'type' => 'hidden',
  6919. 'name' => '_Token[debug]',
  6920. 'value' => $tokenDebug,
  6921. 'autocomplete' => 'off',
  6922. ]],
  6923. '/div',
  6924. '/form',
  6925. ];
  6926. $this->assertHtml($expected, $result);
  6927. }
  6928. /**
  6929. * testPostLink method
  6930. *
  6931. * @return void
  6932. */
  6933. public function testPostLink()
  6934. {
  6935. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6936. $expected = [
  6937. 'form' => [
  6938. 'method' => 'post', 'action' => '/posts/delete/1',
  6939. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6940. ],
  6941. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6942. '/form',
  6943. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6944. 'Delete',
  6945. '/a'
  6946. ];
  6947. $this->assertHtml($expected, $result);
  6948. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6949. $expected = [
  6950. 'form' => [
  6951. 'method' => 'post', 'action' => '/posts/delete/1',
  6952. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6953. ],
  6954. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6955. '/form',
  6956. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6957. 'Delete',
  6958. '/a'
  6959. ];
  6960. $this->assertHtml($expected, $result);
  6961. $result = $this->Form->postLink(
  6962. 'Delete',
  6963. '/posts/delete/1',
  6964. ['target' => '_blank', 'class' => 'btn btn-danger']
  6965. );
  6966. $expected = [
  6967. 'form' => [
  6968. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6969. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6970. ],
  6971. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6972. '/form',
  6973. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6974. 'Delete',
  6975. '/a'
  6976. ];
  6977. $this->assertHtml($expected, $result);
  6978. }
  6979. /**
  6980. * testPostLinkWithConfirm method
  6981. *
  6982. * Test the confirm option for postLink().
  6983. *
  6984. * @return void
  6985. */
  6986. public function testPostLinkWithConfirm()
  6987. {
  6988. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6989. $expected = [
  6990. 'form' => [
  6991. 'method' => 'post', 'action' => '/posts/delete/1',
  6992. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6993. ],
  6994. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6995. '/form',
  6996. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  6997. 'Delete',
  6998. '/a'
  6999. ];
  7000. $this->assertHtml($expected, $result);
  7001. $result = $this->Form->postLink(
  7002. 'Delete',
  7003. '/posts/delete/1',
  7004. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  7005. );
  7006. $expected = [
  7007. 'form' => [
  7008. 'method' => 'post', 'action' => '/posts/delete/1',
  7009. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7010. ],
  7011. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7012. '/form',
  7013. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  7014. 'Delete',
  7015. '/a'
  7016. ];
  7017. $this->assertHtml($expected, $result);
  7018. }
  7019. /**
  7020. * testPostLinkWithQuery method
  7021. *
  7022. * Test postLink() with query string args.
  7023. *
  7024. * @return void
  7025. */
  7026. public function testPostLinkWithQuery()
  7027. {
  7028. $result = $this->Form->postLink(
  7029. 'Delete',
  7030. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  7031. );
  7032. $expected = [
  7033. 'form' => [
  7034. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  7035. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7036. ],
  7037. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7038. '/form',
  7039. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7040. 'Delete',
  7041. '/a'
  7042. ];
  7043. $this->assertHtml($expected, $result);
  7044. }
  7045. /**
  7046. * testPostLinkWithData method
  7047. *
  7048. * Test postLink with additional data.
  7049. *
  7050. * @return void
  7051. */
  7052. public function testPostLinkWithData()
  7053. {
  7054. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  7055. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  7056. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  7057. $this->Form->create($entity);
  7058. $this->Form->end();
  7059. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  7060. $this->assertContains(
  7061. '<input type="hidden" name="name" value="show"',
  7062. $result,
  7063. 'should not contain entity data.'
  7064. );
  7065. }
  7066. /**
  7067. * testPostLinkSecurityHash method
  7068. *
  7069. * Test that security hashes for postLink include the url.
  7070. *
  7071. * @return void
  7072. */
  7073. public function testPostLinkSecurityHash()
  7074. {
  7075. $hash = Security::hash(
  7076. '/posts/delete/1' .
  7077. serialize(['id' => '1']) .
  7078. '' .
  7079. Security::salt()
  7080. );
  7081. $hash .= '%3Aid';
  7082. $this->Form->request->params['_Token']['key'] = 'test';
  7083. $result = $this->Form->postLink(
  7084. 'Delete',
  7085. '/posts/delete/1',
  7086. ['data' => ['id' => 1]]
  7087. );
  7088. $tokenDebug = urlencode(json_encode([
  7089. '/posts/delete/1',
  7090. [
  7091. 'id' => 1
  7092. ],
  7093. []
  7094. ]));
  7095. $expected = [
  7096. 'form' => [
  7097. 'method' => 'post', 'action' => '/posts/delete/1',
  7098. 'name', 'style' => 'display:none;'
  7099. ],
  7100. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7101. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7102. 'div' => ['style' => 'display:none;'],
  7103. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7104. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7105. ['input' => [
  7106. 'type' => 'hidden',
  7107. 'name' => '_Token[debug]',
  7108. 'value' => $tokenDebug,
  7109. 'autocomplete' => 'off',
  7110. ]],
  7111. '/div',
  7112. '/form',
  7113. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7114. 'Delete',
  7115. '/a'
  7116. ];
  7117. $this->assertHtml($expected, $result);
  7118. }
  7119. /**
  7120. * testPostLinkSecurityHashBlockMode method
  7121. *
  7122. * Test that postLink doesn't modify the fields in the containing form.
  7123. *
  7124. * postLink() calls inside open forms should not modify the field list
  7125. * for the form.
  7126. *
  7127. * @return void
  7128. */
  7129. public function testPostLinkSecurityHashBlockMode()
  7130. {
  7131. $hash = Security::hash(
  7132. '/posts/delete/1' .
  7133. serialize([]) .
  7134. '' .
  7135. Security::salt()
  7136. );
  7137. $hash .= '%3A';
  7138. $this->Form->request->params['_Token']['key'] = 'test';
  7139. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  7140. $this->Form->control('title');
  7141. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7142. $result = $this->View->fetch('postLink');
  7143. $this->assertEquals(['title'], $this->Form->fields);
  7144. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  7145. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  7146. }
  7147. /**
  7148. * testPostLinkSecurityHashNoDebugMode method
  7149. *
  7150. * Test that security does not include debug token if debug is false.
  7151. *
  7152. * @return void
  7153. */
  7154. public function testPostLinkSecurityHashNoDebugMode()
  7155. {
  7156. Configure::write('debug', false);
  7157. $hash = Security::hash(
  7158. '/posts/delete/1' .
  7159. serialize(['id' => '1']) .
  7160. '' .
  7161. Security::salt()
  7162. );
  7163. $hash .= '%3Aid';
  7164. $this->Form->request->params['_Token']['key'] = 'test';
  7165. $result = $this->Form->postLink(
  7166. 'Delete',
  7167. '/posts/delete/1',
  7168. ['data' => ['id' => 1]]
  7169. );
  7170. $expected = [
  7171. 'form' => [
  7172. 'method' => 'post', 'action' => '/posts/delete/1',
  7173. 'name', 'style' => 'display:none;'
  7174. ],
  7175. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7176. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7177. 'div' => ['style' => 'display:none;'],
  7178. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7179. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7180. '/div',
  7181. '/form',
  7182. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7183. 'Delete',
  7184. '/a'
  7185. ];
  7186. $this->assertHtml($expected, $result);
  7187. }
  7188. /**
  7189. * testPostLinkNestedData method
  7190. *
  7191. * Test using postLink with N dimensional data.
  7192. *
  7193. * @return void
  7194. */
  7195. public function testPostLinkNestedData()
  7196. {
  7197. $data = [
  7198. 'one' => [
  7199. 'two' => [
  7200. 3, 4, 5
  7201. ]
  7202. ]
  7203. ];
  7204. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7205. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7206. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7207. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7208. }
  7209. /**
  7210. * testPostLinkAfterGetForm method
  7211. *
  7212. * Test creating postLinks after a GET form.
  7213. *
  7214. * @return void
  7215. */
  7216. public function testPostLinkAfterGetForm()
  7217. {
  7218. $this->Form->request->params['_csrfToken'] = 'testkey';
  7219. $this->Form->request->params['_Token'] = 'val';
  7220. $this->Form->create($this->article, ['type' => 'get']);
  7221. $this->Form->end();
  7222. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7223. $tokenDebug = urlencode(json_encode([
  7224. '/posts/delete/1',
  7225. [],
  7226. []
  7227. ]));
  7228. $expected = [
  7229. 'form' => [
  7230. 'method' => 'post', 'action' => '/posts/delete/1',
  7231. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7232. ],
  7233. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7234. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7235. 'div' => ['style' => 'display:none;'],
  7236. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7237. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7238. ['input' => [
  7239. 'type' => 'hidden', 'name' => '_Token[debug]',
  7240. 'value' => $tokenDebug,
  7241. 'autocomplete' => 'off'
  7242. ]],
  7243. '/div',
  7244. '/form',
  7245. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7246. 'Delete',
  7247. '/a'
  7248. ];
  7249. $this->assertHtml($expected, $result);
  7250. }
  7251. /**
  7252. * testPostLinkFormBuffer method
  7253. *
  7254. * Test that postLink adds form tags to view block.
  7255. *
  7256. * @return void
  7257. */
  7258. public function testPostLinkFormBuffer()
  7259. {
  7260. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7261. $expected = [
  7262. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7263. 'Delete',
  7264. '/a'
  7265. ];
  7266. $this->assertHtml($expected, $result);
  7267. $result = $this->View->fetch('postLink');
  7268. $expected = [
  7269. 'form' => [
  7270. 'method' => 'post', 'action' => '/posts/delete/1',
  7271. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7272. ],
  7273. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7274. '/form'
  7275. ];
  7276. $this->assertHtml($expected, $result);
  7277. $result = $this->Form->postLink(
  7278. 'Delete',
  7279. '/posts/delete/2',
  7280. ['block' => true, 'method' => 'DELETE']
  7281. );
  7282. $expected = [
  7283. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7284. 'Delete',
  7285. '/a'
  7286. ];
  7287. $this->assertHtml($expected, $result);
  7288. $result = $this->View->fetch('postLink');
  7289. $expected = [
  7290. 'form' => [
  7291. 'method' => 'post', 'action' => '/posts/delete/1',
  7292. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7293. ],
  7294. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7295. '/form',
  7296. [
  7297. 'form' => [
  7298. 'method' => 'post', 'action' => '/posts/delete/2',
  7299. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7300. ],
  7301. ],
  7302. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7303. '/form'
  7304. ];
  7305. $this->assertHtml($expected, $result);
  7306. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7307. $expected = [
  7308. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7309. 'Delete',
  7310. '/a'
  7311. ];
  7312. $this->assertHtml($expected, $result);
  7313. $result = $this->View->fetch('foobar');
  7314. $expected = [
  7315. 'form' => [
  7316. 'method' => 'post', 'action' => '/posts/delete/1',
  7317. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7318. ],
  7319. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7320. '/form'
  7321. ];
  7322. $this->assertHtml($expected, $result);
  7323. }
  7324. /**
  7325. * testSubmitButton method
  7326. *
  7327. * @return void
  7328. */
  7329. public function testSubmitButton()
  7330. {
  7331. $result = $this->Form->submit('');
  7332. $expected = [
  7333. 'div' => ['class' => 'submit'],
  7334. 'input' => ['type' => 'submit', 'value' => ''],
  7335. '/div'
  7336. ];
  7337. $this->assertHtml($expected, $result);
  7338. $result = $this->Form->submit('Test Submit');
  7339. $expected = [
  7340. 'div' => ['class' => 'submit'],
  7341. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7342. '/div'
  7343. ];
  7344. $this->assertHtml($expected, $result);
  7345. $result = $this->Form->submit('Next >');
  7346. $expected = [
  7347. 'div' => ['class' => 'submit'],
  7348. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7349. '/div'
  7350. ];
  7351. $this->assertHtml($expected, $result);
  7352. $result = $this->Form->submit('Next >', ['escape' => false]);
  7353. $expected = [
  7354. 'div' => ['class' => 'submit'],
  7355. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7356. '/div'
  7357. ];
  7358. $this->assertHtml($expected, $result);
  7359. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7360. $expected = [
  7361. 'div' => ['class' => 'submit'],
  7362. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7363. '/div'
  7364. ];
  7365. $this->assertHtml($expected, $result);
  7366. }
  7367. /**
  7368. * testSubmitImage method
  7369. *
  7370. * Test image submit types.
  7371. *
  7372. * @return void
  7373. */
  7374. public function testSubmitImage()
  7375. {
  7376. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7377. $expected = [
  7378. 'div' => ['class' => 'submit'],
  7379. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7380. '/div'
  7381. ];
  7382. $this->assertHtml($expected, $result);
  7383. $result = $this->Form->submit('/relative/cake.power.gif');
  7384. $expected = [
  7385. 'div' => ['class' => 'submit'],
  7386. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7387. '/div'
  7388. ];
  7389. $this->assertHtml($expected, $result);
  7390. $result = $this->Form->submit('cake.power.gif');
  7391. $expected = [
  7392. 'div' => ['class' => 'submit'],
  7393. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7394. '/div'
  7395. ];
  7396. $this->assertHtml($expected, $result);
  7397. $result = $this->Form->submit('Not.an.image');
  7398. $expected = [
  7399. 'div' => ['class' => 'submit'],
  7400. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7401. '/div'
  7402. ];
  7403. $this->assertHtml($expected, $result);
  7404. }
  7405. /**
  7406. * testSubmitUnlockedByDefault method
  7407. *
  7408. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7409. * be submitted at a time.
  7410. *
  7411. * @return void
  7412. */
  7413. public function testSubmitUnlockedByDefault()
  7414. {
  7415. $this->Form->request->params['_Token'] = 'secured';
  7416. $this->Form->submit('Go go');
  7417. $this->Form->submit('Save', ['name' => 'save']);
  7418. $result = $this->Form->unlockField();
  7419. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7420. }
  7421. /**
  7422. * testSubmitImageTimestamp method
  7423. *
  7424. * Test submit image with timestamps.
  7425. *
  7426. * @return void
  7427. */
  7428. public function testSubmitImageTimestamp()
  7429. {
  7430. Configure::write('Asset.timestamp', 'force');
  7431. $result = $this->Form->submit('cake.power.gif');
  7432. $expected = [
  7433. 'div' => ['class' => 'submit'],
  7434. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7435. '/div'
  7436. ];
  7437. $this->assertHtml($expected, $result);
  7438. }
  7439. /**
  7440. * testDateTimeWithGetForms method
  7441. *
  7442. * Test that datetime() works with GET style forms.
  7443. *
  7444. * @return void
  7445. */
  7446. public function testDateTimeWithGetForms()
  7447. {
  7448. extract($this->dateRegex);
  7449. $this->Form->create($this->article, ['type' => 'get']);
  7450. $result = $this->Form->datetime('created');
  7451. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7452. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7453. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7454. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7455. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7456. }
  7457. /**
  7458. * testForMagicControlNonExistingNorValidated method
  7459. *
  7460. * @return void
  7461. */
  7462. public function testForMagicControlNonExistingNorValidated()
  7463. {
  7464. $this->Form->create($this->article);
  7465. $this->Form->templates(['inputContainer' => '{{content}}']);
  7466. $result = $this->Form->control('non_existing_nor_validated');
  7467. $expected = [
  7468. 'label' => ['for' => 'non-existing-nor-validated'],
  7469. 'Non Existing Nor Validated',
  7470. '/label',
  7471. 'input' => [
  7472. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7473. 'id' => 'non-existing-nor-validated'
  7474. ]
  7475. ];
  7476. $this->assertHtml($expected, $result);
  7477. $result = $this->Form->control('non_existing_nor_validated', [
  7478. 'val' => 'my value'
  7479. ]);
  7480. $expected = [
  7481. 'label' => ['for' => 'non-existing-nor-validated'],
  7482. 'Non Existing Nor Validated',
  7483. '/label',
  7484. 'input' => [
  7485. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7486. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7487. ]
  7488. ];
  7489. $this->assertHtml($expected, $result);
  7490. $this->Form->request->data = ['non_existing_nor_validated' => 'CakePHP magic'];
  7491. $result = $this->Form->control('non_existing_nor_validated');
  7492. $expected = [
  7493. 'label' => ['for' => 'non-existing-nor-validated'],
  7494. 'Non Existing Nor Validated',
  7495. '/label',
  7496. 'input' => [
  7497. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7498. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7499. ]
  7500. ];
  7501. $this->assertHtml($expected, $result);
  7502. }
  7503. /**
  7504. * testFormMagicControlLabel method
  7505. *
  7506. * @return void
  7507. */
  7508. public function testFormMagicControlLabel()
  7509. {
  7510. TableRegistry::get('Contacts', [
  7511. 'className' => __NAMESPACE__ . '\ContactsTable'
  7512. ]);
  7513. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7514. $this->Form->templates(['inputContainer' => '{{content}}']);
  7515. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7516. $expected = [
  7517. 'label' => ['for' => 'contacts-name'],
  7518. 'My label',
  7519. '/label',
  7520. 'input' => [
  7521. 'type' => 'text', 'name' => 'Contacts[name]',
  7522. 'id' => 'contacts-name', 'maxlength' => '255'
  7523. ]
  7524. ];
  7525. $this->assertHtml($expected, $result);
  7526. $result = $this->Form->control('name', [
  7527. 'label' => ['class' => 'mandatory']
  7528. ]);
  7529. $expected = [
  7530. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7531. 'Name',
  7532. '/label',
  7533. 'input' => [
  7534. 'type' => 'text', 'name' => 'name',
  7535. 'id' => 'name', 'maxlength' => '255'
  7536. ]
  7537. ];
  7538. $this->assertHtml($expected, $result);
  7539. $result = $this->Form->control('name', [
  7540. 'div' => false,
  7541. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7542. ]);
  7543. $expected = [
  7544. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7545. 'My label',
  7546. '/label',
  7547. 'input' => [
  7548. 'type' => 'text', 'name' => 'name',
  7549. 'id' => 'name', 'maxlength' => '255'
  7550. ]
  7551. ];
  7552. $this->assertHtml($expected, $result);
  7553. $result = $this->Form->control('Contact.name', [
  7554. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7555. ]);
  7556. $expected = [
  7557. 'label' => ['for' => 'my_id'],
  7558. 'Name',
  7559. '/label',
  7560. 'input' => [
  7561. 'type' => 'text', 'name' => 'Contact[name]',
  7562. 'id' => 'my_id', 'maxlength' => '255'
  7563. ]
  7564. ];
  7565. $this->assertHtml($expected, $result);
  7566. $result = $this->Form->control('1.id');
  7567. $expected = ['input' => [
  7568. 'type' => 'hidden', 'name' => '1[id]',
  7569. 'id' => '1-id'
  7570. ]];
  7571. $this->assertHtml($expected, $result);
  7572. $result = $this->Form->control('1.name');
  7573. $expected = [
  7574. 'label' => ['for' => '1-name'],
  7575. 'Name',
  7576. '/label',
  7577. 'input' => [
  7578. 'type' => 'text', 'name' => '1[name]',
  7579. 'id' => '1-name', 'maxlength' => '255'
  7580. ]
  7581. ];
  7582. $this->assertHtml($expected, $result);
  7583. }
  7584. /**
  7585. * testFormEnd method
  7586. *
  7587. * @return void
  7588. */
  7589. public function testFormEnd()
  7590. {
  7591. $this->assertEquals('</form>', $this->Form->end());
  7592. }
  7593. /**
  7594. * testMultiRecordForm method
  7595. *
  7596. * Test the generation of fields for a multi record form.
  7597. *
  7598. * @return void
  7599. */
  7600. public function testMultiRecordForm()
  7601. {
  7602. $this->loadFixtures('Articles', 'Comments');
  7603. $articles = TableRegistry::get('Articles');
  7604. $articles->hasMany('Comments');
  7605. $comment = new Entity(['comment' => 'Value']);
  7606. $article = new Article(['comments' => [$comment]]);
  7607. $this->Form->create([$article]);
  7608. $result = $this->Form->control('0.comments.1.comment');
  7609. //@codingStandardsIgnoreStart
  7610. $expected = [
  7611. 'div' => ['class' => 'input textarea'],
  7612. 'label' => ['for' => '0-comments-1-comment'],
  7613. 'Comment',
  7614. '/label',
  7615. 'textarea' => [
  7616. 'name',
  7617. 'id' => '0-comments-1-comment',
  7618. 'rows' => 5
  7619. ],
  7620. '/textarea',
  7621. '/div'
  7622. ];
  7623. //@codingStandardsIgnoreEnd
  7624. $this->assertHtml($expected, $result);
  7625. $result = $this->Form->control('0.comments.0.comment');
  7626. //@codingStandardsIgnoreStart
  7627. $expected = [
  7628. 'div' => ['class' => 'input textarea'],
  7629. 'label' => ['for' => '0-comments-0-comment'],
  7630. 'Comment',
  7631. '/label',
  7632. 'textarea' => [
  7633. 'name',
  7634. 'id' => '0-comments-0-comment',
  7635. 'rows' => 5
  7636. ],
  7637. 'Value',
  7638. '/textarea',
  7639. '/div'
  7640. ];
  7641. //@codingStandardsIgnoreEnd
  7642. $this->assertHtml($expected, $result);
  7643. $comment->errors('comment', ['Not valid']);
  7644. $result = $this->Form->control('0.comments.0.comment');
  7645. //@codingStandardsIgnoreStart
  7646. $expected = [
  7647. 'div' => ['class' => 'input textarea error'],
  7648. 'label' => ['for' => '0-comments-0-comment'],
  7649. 'Comment',
  7650. '/label',
  7651. 'textarea' => [
  7652. 'name',
  7653. 'class' => 'form-error',
  7654. 'id' => '0-comments-0-comment',
  7655. 'rows' => 5
  7656. ],
  7657. 'Value',
  7658. '/textarea',
  7659. ['div' => ['class' => 'error-message']],
  7660. 'Not valid',
  7661. '/div',
  7662. '/div'
  7663. ];
  7664. //@codingStandardsIgnoreEnd
  7665. $this->assertHtml($expected, $result);
  7666. TableRegistry::get('Comments')
  7667. ->validator('default')
  7668. ->allowEmpty('comment', false);
  7669. $result = $this->Form->control('0.comments.1.comment');
  7670. //@codingStandardsIgnoreStart
  7671. $expected = [
  7672. 'div' => ['class' => 'input textarea required'],
  7673. 'label' => ['for' => '0-comments-1-comment'],
  7674. 'Comment',
  7675. '/label',
  7676. 'textarea' => [
  7677. 'name',
  7678. 'required' => 'required',
  7679. 'id' => '0-comments-1-comment',
  7680. 'rows' => 5
  7681. ],
  7682. '/textarea',
  7683. '/div'
  7684. ];
  7685. //@codingStandardsIgnoreEnd
  7686. $this->assertHtml($expected, $result);
  7687. }
  7688. /**
  7689. * testHtml5Controls method
  7690. *
  7691. * Test that some html5 inputs + FormHelper::__call() work.
  7692. *
  7693. * @return void
  7694. */
  7695. public function testHtml5Controls()
  7696. {
  7697. $result = $this->Form->email('User.email');
  7698. $expected = [
  7699. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7700. ];
  7701. $this->assertHtml($expected, $result);
  7702. $result = $this->Form->search('User.query');
  7703. $expected = [
  7704. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7705. ];
  7706. $this->assertHtml($expected, $result);
  7707. $result = $this->Form->search('User.query', ['value' => 'test']);
  7708. $expected = [
  7709. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7710. ];
  7711. $this->assertHtml($expected, $result);
  7712. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7713. $expected = [
  7714. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7715. ];
  7716. $this->assertHtml($expected, $result);
  7717. }
  7718. /**
  7719. * testHtml5ControlWithControl method
  7720. *
  7721. * Test accessing html5 inputs through control().
  7722. *
  7723. * @return void
  7724. */
  7725. public function testHtml5ControlWithControl()
  7726. {
  7727. $this->Form->create();
  7728. $this->Form->templates(['inputContainer' => '{{content}}']);
  7729. $result = $this->Form->control('website', [
  7730. 'type' => 'url',
  7731. 'val' => 'http://domain.tld',
  7732. 'label' => false
  7733. ]);
  7734. $expected = [
  7735. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7736. ];
  7737. $this->assertHtml($expected, $result);
  7738. }
  7739. /**
  7740. * testHtml5ControlException method
  7741. *
  7742. * Test errors when field name is missing.
  7743. *
  7744. * @return void
  7745. */
  7746. public function testHtml5ControlException()
  7747. {
  7748. $this->expectException(\Cake\Core\Exception\Exception::class);
  7749. $this->Form->email();
  7750. }
  7751. /**
  7752. * testRequiredAttribute method
  7753. *
  7754. * Tests that formhelper sets required attributes.
  7755. *
  7756. * @return void
  7757. */
  7758. public function testRequiredAttribute()
  7759. {
  7760. $this->article['required'] = [
  7761. 'title' => true,
  7762. 'body' => false,
  7763. ];
  7764. $this->Form->create($this->article);
  7765. $result = $this->Form->control('title');
  7766. $expected = [
  7767. 'div' => ['class' => 'input text required'],
  7768. 'label' => ['for' => 'title'],
  7769. 'Title',
  7770. '/label',
  7771. 'input' => [
  7772. 'type' => 'text',
  7773. 'name' => 'title',
  7774. 'id' => 'title',
  7775. 'required' => 'required',
  7776. ],
  7777. '/div'
  7778. ];
  7779. $this->assertHtml($expected, $result);
  7780. $result = $this->Form->control('title', ['required' => false]);
  7781. $this->assertNotContains('required', $result);
  7782. $result = $this->Form->control('body');
  7783. $expected = [
  7784. 'div' => ['class' => 'input text'],
  7785. 'label' => ['for' => 'body'],
  7786. 'Body',
  7787. '/label',
  7788. 'input' => [
  7789. 'type' => 'text',
  7790. 'name' => 'body',
  7791. 'id' => 'body',
  7792. ],
  7793. '/div'
  7794. ];
  7795. $this->assertHtml($expected, $result);
  7796. $result = $this->Form->control('body', ['required' => true]);
  7797. $this->assertContains('required', $result);
  7798. }
  7799. /**
  7800. * testControlsNotNested method
  7801. *
  7802. * Tests that it is possible to put inputs outside of the label.
  7803. *
  7804. * @return void
  7805. */
  7806. public function testControlsNotNested()
  7807. {
  7808. $this->Form->templates([
  7809. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7810. 'formGroup' => '{{input}}{{label}}',
  7811. ]);
  7812. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7813. $expected = [
  7814. 'div' => ['class' => 'input checkbox'],
  7815. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7816. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7817. 'label' => ['for' => 'foo'],
  7818. 'Foo',
  7819. '/label',
  7820. '/div'
  7821. ];
  7822. $this->assertHtml($expected, $result);
  7823. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7824. $expected = [
  7825. 'div' => ['class' => 'input checkbox'],
  7826. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7827. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7828. '/div'
  7829. ];
  7830. $this->assertHtml($expected, $result);
  7831. $result = $this->Form->control('confirm', [
  7832. 'type' => 'radio',
  7833. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7834. ]);
  7835. $expected = [
  7836. 'div' => ['class' => 'input radio'],
  7837. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7838. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7839. ['label' => ['for' => 'confirm-y']],
  7840. 'Yes',
  7841. '/label',
  7842. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7843. ['label' => ['for' => 'confirm-n']],
  7844. 'No',
  7845. '/label',
  7846. '<label',
  7847. 'Confirm',
  7848. '/label',
  7849. '/div',
  7850. ];
  7851. $this->assertHtml($expected, $result);
  7852. $result = $this->Form->select('category', ['1', '2'], [
  7853. 'multiple' => 'checkbox',
  7854. 'name' => 'fish',
  7855. ]);
  7856. $expected = [
  7857. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7858. ['div' => ['class' => 'checkbox']],
  7859. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7860. ['label' => ['for' => 'fish-0']],
  7861. '1',
  7862. '/label',
  7863. '/div',
  7864. ['div' => ['class' => 'checkbox']],
  7865. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7866. ['label' => ['for' => 'fish-1']],
  7867. '2',
  7868. '/label',
  7869. '/div'
  7870. ];
  7871. $this->assertHtml($expected, $result);
  7872. }
  7873. /**
  7874. * testControlContainerTemplates method
  7875. *
  7876. * Test that *Container templates are used by input.
  7877. *
  7878. * @return void
  7879. */
  7880. public function testControlContainerTemplates()
  7881. {
  7882. $this->Form->templates([
  7883. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7884. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7885. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7886. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7887. ]);
  7888. $this->article['errors'] = [
  7889. 'Article' => ['published' => 'error message']
  7890. ];
  7891. $this->Form->create($this->article);
  7892. $result = $this->Form->control('accept', [
  7893. 'type' => 'checkbox'
  7894. ]);
  7895. $expected = [
  7896. 'div' => ['class' => 'check'],
  7897. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7898. 'label' => ['for' => 'accept'],
  7899. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7900. 'Accept',
  7901. '/label',
  7902. '/div'
  7903. ];
  7904. $this->assertHtml($expected, $result);
  7905. $result = $this->Form->control('accept', [
  7906. 'type' => 'radio',
  7907. 'options' => ['Y', 'N']
  7908. ]);
  7909. $this->assertContains('<div class="rad">', $result);
  7910. $result = $this->Form->control('Article.published', [
  7911. 'type' => 'radio',
  7912. 'options' => ['Y', 'N']
  7913. ]);
  7914. $this->assertContains('<div class="rad err">', $result);
  7915. $result = $this->Form->control('Article.created', [
  7916. 'type' => 'datetime',
  7917. ]);
  7918. $this->assertContains('<div class="dt">', $result);
  7919. }
  7920. /**
  7921. * testFormGroupTemplates method
  7922. *
  7923. * Test that *Container templates are used by input.
  7924. *
  7925. * @return void
  7926. */
  7927. public function testFormGroupTemplates()
  7928. {
  7929. $this->Form->templates([
  7930. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7931. ]);
  7932. $this->Form->create($this->article);
  7933. $result = $this->Form->control('accept', [
  7934. 'type' => 'radio',
  7935. 'options' => ['Y', 'N']
  7936. ]);
  7937. $this->assertContains('<div class="radio">', $result);
  7938. }
  7939. /**
  7940. * testResetTemplates method
  7941. *
  7942. * Test resetting templates.
  7943. *
  7944. * @return void
  7945. */
  7946. public function testResetTemplates()
  7947. {
  7948. $this->Form->templates(['input' => '<input/>']);
  7949. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  7950. $this->Form->resetTemplates();
  7951. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7952. }
  7953. /**
  7954. * testContext method
  7955. *
  7956. * Test the context method.
  7957. *
  7958. * @return void
  7959. */
  7960. public function testContext()
  7961. {
  7962. $result = $this->Form->context();
  7963. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7964. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7965. $this->assertSame($mock, $this->Form->context($mock));
  7966. $this->assertSame($mock, $this->Form->context());
  7967. }
  7968. /**
  7969. * testAutoDomId method
  7970. *
  7971. * @return void
  7972. */
  7973. public function testAutoDomId()
  7974. {
  7975. $result = $this->Form->text('field', ['id' => true]);
  7976. $expected = [
  7977. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7978. ];
  7979. $this->assertHtml($expected, $result);
  7980. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7981. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7982. $expected = [
  7983. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7984. ['label' => ['for' => 'field-0']],
  7985. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7986. 'option A',
  7987. '/label',
  7988. ['label' => ['for' => 'field-1']],
  7989. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7990. 'option B',
  7991. '/label',
  7992. ];
  7993. $this->assertHtml($expected, $result);
  7994. $result = $this->Form->select(
  7995. 'multi_field',
  7996. ['first', 'second'],
  7997. ['multiple' => 'checkbox', 'id' => true]
  7998. );
  7999. $expected = [
  8000. 'input' => [
  8001. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  8002. ],
  8003. ['div' => ['class' => 'checkbox']],
  8004. ['label' => ['for' => 'multi-field-0']],
  8005. ['input' => [
  8006. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8007. 'value' => '0', 'id' => 'multi-field-0'
  8008. ]],
  8009. 'first',
  8010. '/label',
  8011. '/div',
  8012. ['div' => ['class' => 'checkbox']],
  8013. ['label' => ['for' => 'multi-field-1']],
  8014. ['input' => [
  8015. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8016. 'value' => '1', 'id' => 'multi-field-1'
  8017. ]],
  8018. 'second',
  8019. '/label',
  8020. '/div',
  8021. ];
  8022. $this->assertHtml($expected, $result);
  8023. }
  8024. /**
  8025. * Test the basic setters and getters for value sources
  8026. *
  8027. * @return void
  8028. */
  8029. public function testFormValueSourcesSettersGetters()
  8030. {
  8031. $expected = ['context'];
  8032. $result = $this->Form->getValueSources();
  8033. $this->assertEquals($expected, $result);
  8034. $expected = null;
  8035. $result = $this->Form->getSourceValue('id');
  8036. $this->assertEquals($expected, $result);
  8037. $expected = ['query', 'data', 'context'];
  8038. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  8039. $result = $this->Form->getValueSources();
  8040. $this->assertEquals($expected, $result);
  8041. $this->Form->request->data['id'] = '1';
  8042. $this->Form->request->query['id'] = '2';
  8043. $this->Form->setValueSources(['context']);
  8044. $expected = '1';
  8045. $result = $this->Form->getSourceValue('id');
  8046. $this->assertEquals($expected, $result);
  8047. $this->Form->setValueSources('query');
  8048. $expected = ['query'];
  8049. $result = $this->Form->getValueSources();
  8050. $this->assertEquals($expected, $result);
  8051. $expected = '2';
  8052. $result = $this->Form->getSourceValue('id');
  8053. $this->assertEquals($expected, $result);
  8054. $this->Form->setValueSources(['data']);
  8055. $expected = '1';
  8056. $result = $this->Form->getSourceValue('id');
  8057. $this->assertEquals($expected, $result);
  8058. $this->Form->setValueSources(['query', 'data']);
  8059. $expected = '2';
  8060. $result = $this->Form->getSourceValue('id');
  8061. $this->assertEquals($expected, $result);
  8062. }
  8063. /**
  8064. * Tests the different input rendering values based on sources values switching
  8065. *
  8066. * @return void
  8067. */
  8068. public function testFormValueSourcesSingleSwitchRendering()
  8069. {
  8070. $this->loadFixtures('Articles');
  8071. $articles = TableRegistry::get('Articles');
  8072. $article = new Article();
  8073. $articles->patchEntity($article, ['id' => '3']);
  8074. $this->Form->create($article);
  8075. $this->Form->setValueSources(['context']);
  8076. $result = $this->Form->control('id');
  8077. $expected = [
  8078. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8079. ];
  8080. $this->assertHtml($expected, $result);
  8081. $this->Form->request->query['id'] = '5';
  8082. $this->Form->setValueSources(['query']);
  8083. $result = $this->Form->control('id');
  8084. $expected = [
  8085. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8086. ];
  8087. $this->assertHtml($expected, $result);
  8088. $this->Form->request->query['id'] = '5a';
  8089. $this->Form->request->data['id'] = '5b';
  8090. $this->Form->setValueSources(['context']);
  8091. $this->Form->create($article);
  8092. $result = $this->Form->control('id');
  8093. $expected = [
  8094. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8095. ];
  8096. $this->assertHtml($expected, $result);
  8097. $this->Form->setValueSources(['data']);
  8098. $this->Form->create($article);
  8099. $result = $this->Form->control('id');
  8100. $expected = [
  8101. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8102. ];
  8103. $this->assertHtml($expected, $result);
  8104. $this->Form->setValueSources(['query']);
  8105. $result = $this->Form->control('id');
  8106. $expected = [
  8107. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  8108. ];
  8109. $this->assertHtml($expected, $result);
  8110. }
  8111. /**
  8112. * Tests the different input rendering values based on sources values switching while supplying
  8113. * an entity (base context) and multiple sources (such as data, query)
  8114. *
  8115. * @return void
  8116. */
  8117. public function testFormValueSourcesListSwitchRendering()
  8118. {
  8119. $this->loadFixtures('Articles');
  8120. $articles = TableRegistry::get('Articles');
  8121. $article = new Article();
  8122. $articles->patchEntity($article, ['id' => '3']);
  8123. $this->Form->request->query['id'] = '9';
  8124. $this->Form->create($article);
  8125. $this->Form->setValueSources(['context', 'query']);
  8126. $result = $this->Form->control('id');
  8127. $expected = [
  8128. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8129. ];
  8130. $this->assertHtml($expected, $result);
  8131. $this->Form->setValueSources(['query', 'context']);
  8132. $result = $this->Form->control('id');
  8133. $expected = [
  8134. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8135. ];
  8136. $this->assertHtml($expected, $result);
  8137. $this->Form->setValueSources(['data', 'query', 'context']);
  8138. $result = $this->Form->control('id');
  8139. $expected = [
  8140. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8141. ];
  8142. $this->assertHtml($expected, $result);
  8143. $this->Form->request->data['id'] = '8';
  8144. $this->Form->request->query['id'] = '9';
  8145. $this->Form->setValueSources(['data', 'query', 'context']);
  8146. $result = $this->Form->control('id');
  8147. $expected = [
  8148. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  8149. ];
  8150. $this->assertHtml($expected, $result);
  8151. }
  8152. /**
  8153. * Test the different form input renderings based on values sources switchings through form options
  8154. *
  8155. * @return void
  8156. */
  8157. public function testFormValueSourcesSwitchViaOptionsRendering()
  8158. {
  8159. $this->loadFixtures('Articles');
  8160. $articles = TableRegistry::get('Articles');
  8161. $article = new Article();
  8162. $articles->patchEntity($article, ['id' => '3']);
  8163. $this->Form->request->data['id'] = '4';
  8164. $this->Form->request->query['id'] = '5';
  8165. $this->Form->create($article, ['valueSources' => 'query']);
  8166. $result = $this->Form->control('id');
  8167. $expected = [
  8168. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8169. ];
  8170. $this->assertHtml($expected, $result);
  8171. $result = $this->Form->getSourceValue('id');
  8172. $this->assertEquals('5', $result);
  8173. $this->Form->setValueSources(['context']);
  8174. $this->Form->create($article, ['valueSources' => 'query']);
  8175. $result = $this->Form->control('id');
  8176. $expected = [
  8177. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8178. ];
  8179. $this->assertHtml($expected, $result);
  8180. $result = $this->Form->getSourceValue('id');
  8181. $this->assertEquals('5', $result);
  8182. $this->Form->setValueSources(['query']);
  8183. $this->Form->create($article, ['valueSources' => 'data']);
  8184. $result = $this->Form->control('id');
  8185. $expected = [
  8186. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8187. ];
  8188. $this->assertHtml($expected, $result);
  8189. $result = $this->Form->getSourceValue('id');
  8190. $this->assertEquals('4', $result);
  8191. $this->Form->setValueSources(['query']);
  8192. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8193. $result = $this->Form->control('id');
  8194. $expected = [
  8195. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8196. ];
  8197. $this->assertHtml($expected, $result);
  8198. $result = $this->Form->getSourceValue('id');
  8199. $this->assertEquals('4', $result);
  8200. }
  8201. /**
  8202. * Test the different form input renderings based on values sources switchings through form options
  8203. *
  8204. * @return void
  8205. */
  8206. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8207. {
  8208. $this->loadFixtures('Articles');
  8209. $articles = TableRegistry::get('Articles');
  8210. $article = new Article();
  8211. $articles->patchEntity($article, ['id' => '3']);
  8212. $this->Form->request->data['id'] = '10';
  8213. $this->Form->request->query['id'] = '11';
  8214. $this->Form->setValueSources(['context'])
  8215. ->create($article, ['valueSources' => ['query', 'data']]);
  8216. $result = $this->Form->control('id');
  8217. $expected = [
  8218. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8219. ];
  8220. $this->assertHtml($expected, $result);
  8221. $result = $this->Form->getSourceValue('id');
  8222. $this->assertEquals('11', $result);
  8223. unset($this->Form->request->query['id']);
  8224. $this->Form->setValueSources(['context'])
  8225. ->create($article, ['valueSources' => ['query', 'data']]);
  8226. $result = $this->Form->control('id');
  8227. $expected = [
  8228. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8229. ];
  8230. $this->assertHtml($expected, $result);
  8231. $result = $this->Form->getSourceValue('id');
  8232. $this->assertEquals('10', $result);
  8233. }
  8234. /**
  8235. * Test the different form values sources resetting through From::end();
  8236. *
  8237. * @return void
  8238. */
  8239. public function testFormValueSourcesResetViaEnd()
  8240. {
  8241. $expected = ['context'];
  8242. $result = $this->Form->getValueSources();
  8243. $this->assertEquals($expected, $result);
  8244. $expected = ['query', 'context', 'data'];
  8245. $this->Form->setValueSources(['query', 'context', 'data']);
  8246. $result = $this->Form->getValueSources();
  8247. $this->assertEquals($expected, $result);
  8248. $this->Form->create();
  8249. $result = $this->Form->getValueSources();
  8250. $this->assertEquals($expected, $result);
  8251. $this->Form->end();
  8252. $result = $this->Form->getValueSources();
  8253. $this->assertEquals(['context'], $result);
  8254. }
  8255. /**
  8256. * Test sources values defaults handling
  8257. *
  8258. * @return void
  8259. */
  8260. public function testFormValueSourcesDefaults()
  8261. {
  8262. $this->Form->request->query['password'] = 'open Sesame';
  8263. $this->Form->create();
  8264. $result = $this->Form->password('password');
  8265. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8266. $this->assertHtml($expected, $result);
  8267. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8268. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8269. $this->assertHtml($expected, $result);
  8270. $this->Form->setValueSources('query');
  8271. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8272. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8273. $this->assertHtml($expected, $result);
  8274. $this->Form->setValueSources('data');
  8275. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8276. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8277. $this->assertHtml($expected, $result);
  8278. }
  8279. /**
  8280. * Test sources values schema defaults handling
  8281. *
  8282. * @return void
  8283. */
  8284. public function testSourcesValueDoesntExistPassThrough()
  8285. {
  8286. $this->Form->request->query['category'] = 'sesame-cookies';
  8287. $articles = TableRegistry::get('Articles');
  8288. $entity = $articles->newEntity();
  8289. $this->Form->create($entity);
  8290. $this->Form->setValueSources(['query', 'context']);
  8291. $result = $this->Form->getSourceValue('category');
  8292. $this->assertEquals('sesame-cookies', $result);
  8293. $this->Form->setValueSources(['context', 'query']);
  8294. $result = $this->Form->getSourceValue('category');
  8295. $this->assertEquals('sesame-cookies', $result);
  8296. }
  8297. /**
  8298. * testNestedLabelInput method
  8299. *
  8300. * Test the `nestedInput` parameter
  8301. *
  8302. * @return void
  8303. */
  8304. public function testNestedLabelInput()
  8305. {
  8306. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8307. $expected = [
  8308. 'div' => ['class' => 'input text'],
  8309. 'label' => ['for' => 'foo'],
  8310. ['input' => [
  8311. 'type' => 'text',
  8312. 'name' => 'foo',
  8313. 'id' => 'foo'
  8314. ]],
  8315. 'Foo',
  8316. '/label',
  8317. '/div'
  8318. ];
  8319. $this->assertHtml($expected, $result);
  8320. }
  8321. /**
  8322. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8323. *
  8324. * This test makes sure `false` excludes the label from the render
  8325. *
  8326. * @return void
  8327. */
  8328. public function testInputLabelManipulationDisableLabels()
  8329. {
  8330. $result = $this->Form->input('test', [
  8331. 'type' => 'radio',
  8332. 'options' => ['A', 'B'],
  8333. 'labelOptions' => false
  8334. ]);
  8335. $expected = [
  8336. ['div' => ['class' => 'input radio']],
  8337. '<label',
  8338. 'Test',
  8339. '/label',
  8340. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8341. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8342. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8343. '/div',
  8344. ];
  8345. $this->assertHtml($expected, $result);
  8346. $result = $this->Form->input('checkbox1', [
  8347. 'label' => 'My checkboxes',
  8348. 'multiple' => 'checkbox',
  8349. 'type' => 'select',
  8350. 'options' => [
  8351. ['text' => 'First Checkbox', 'value' => 1],
  8352. ['text' => 'Second Checkbox', 'value' => 2]
  8353. ],
  8354. 'labelOptions' => false
  8355. ]);
  8356. $expected = [
  8357. ['div' => ['class' => 'input select']],
  8358. ['label' => ['for' => 'checkbox1']],
  8359. 'My checkboxes',
  8360. '/label',
  8361. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8362. ['div' => ['class' => 'checkbox']],
  8363. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8364. '/div',
  8365. ['div' => ['class' => 'checkbox']],
  8366. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8367. '/div',
  8368. '/div'
  8369. ];
  8370. $this->assertHtml($expected, $result);
  8371. }
  8372. /**
  8373. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8374. *
  8375. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8376. * added to the class if checked.
  8377. *
  8378. * Also checks to make sure any custom attributes are rendered correctly
  8379. *
  8380. * @return void
  8381. */
  8382. public function testInputLabelManipulationRadios()
  8383. {
  8384. $result = $this->Form->input('test', [
  8385. 'type' => 'radio',
  8386. 'options' => ['A', 'B'],
  8387. 'labelOptions' => ['class' => 'custom-class']
  8388. ]);
  8389. $expected = [
  8390. ['div' => ['class' => 'input radio']],
  8391. '<label',
  8392. 'Test',
  8393. '/label',
  8394. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8395. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8396. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8397. 'A',
  8398. '/label',
  8399. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8400. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8401. 'B',
  8402. '/label',
  8403. '/div',
  8404. ];
  8405. $this->assertHtml($expected, $result);
  8406. $result = $this->Form->input('test', [
  8407. 'type' => 'radio',
  8408. 'options' => ['A', 'B'],
  8409. 'value' => 1,
  8410. 'labelOptions' => ['class' => 'custom-class']
  8411. ]);
  8412. $expected = [
  8413. ['div' => ['class' => 'input radio']],
  8414. '<label',
  8415. 'Test',
  8416. '/label',
  8417. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8418. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8419. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8420. 'A',
  8421. '/label',
  8422. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8423. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8424. 'B',
  8425. '/label',
  8426. '/div',
  8427. ];
  8428. $this->assertHtml($expected, $result);
  8429. $result = $this->Form->input('test', [
  8430. 'type' => 'radio',
  8431. 'options' => ['A', 'B'],
  8432. 'value' => 1,
  8433. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']]
  8434. ]);
  8435. $expected = [
  8436. ['div' => ['class' => 'input radio']],
  8437. '<label',
  8438. 'Test',
  8439. '/label',
  8440. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8441. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8442. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8443. 'A',
  8444. '/label',
  8445. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8446. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8447. 'B',
  8448. '/label',
  8449. '/div',
  8450. ];
  8451. $this->assertHtml($expected, $result);
  8452. $result = $this->Form->radio('test', ['A', 'B'], [
  8453. 'label' => [
  8454. 'class' => ['custom-class', 'another-class'],
  8455. 'data-name' => 'bob'
  8456. ],
  8457. 'value' => 1
  8458. ]);
  8459. $expected = [
  8460. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8461. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8462. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8463. 'A',
  8464. '/label',
  8465. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8466. ['input' => [
  8467. 'type' => 'radio',
  8468. 'name' => 'test',
  8469. 'value' => '1',
  8470. 'id' => 'test-1',
  8471. 'checked' => 'checked'
  8472. ]],
  8473. 'B',
  8474. '/label',
  8475. ];
  8476. $this->assertHtml($expected, $result);
  8477. }
  8478. /**
  8479. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8480. *
  8481. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8482. * added to the class if checked.
  8483. *
  8484. * Also checks to make sure any custom attributes are rendered correctly
  8485. *
  8486. * @return void
  8487. */
  8488. public function testInputLabelManipulationCheckboxes()
  8489. {
  8490. $result = $this->Form->input('checkbox1', [
  8491. 'label' => 'My checkboxes',
  8492. 'multiple' => 'checkbox',
  8493. 'type' => 'select',
  8494. 'options' => [
  8495. ['text' => 'First Checkbox', 'value' => 1],
  8496. ['text' => 'Second Checkbox', 'value' => 2]
  8497. ],
  8498. 'labelOptions' => ['class' => 'custom-class'],
  8499. 'value' => ['1']
  8500. ]);
  8501. $expected = [
  8502. ['div' => ['class' => 'input select']],
  8503. ['label' => ['for' => 'checkbox1']],
  8504. 'My checkboxes',
  8505. '/label',
  8506. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8507. ['div' => ['class' => 'checkbox']],
  8508. ['label' => [
  8509. 'class' => 'custom-class selected',
  8510. 'for' => 'checkbox1-1'
  8511. ]],
  8512. ['input' => [
  8513. 'type' => 'checkbox',
  8514. 'name' => 'checkbox1[]',
  8515. 'value' => '1',
  8516. 'id' => 'checkbox1-1',
  8517. 'checked' => 'checked'
  8518. ]],
  8519. 'First Checkbox',
  8520. '/label',
  8521. '/div',
  8522. ['div' => ['class' => 'checkbox']],
  8523. ['label' => [
  8524. 'class' => 'custom-class',
  8525. 'for' => 'checkbox1-2'
  8526. ]],
  8527. ['input' => [
  8528. 'type' => 'checkbox',
  8529. 'name' => 'checkbox1[]',
  8530. 'value' => '2',
  8531. 'id' => 'checkbox1-2'
  8532. ]],
  8533. 'Second Checkbox',
  8534. '/label',
  8535. '/div',
  8536. '/div'
  8537. ];
  8538. $this->assertHtml($expected, $result);
  8539. $result = $this->Form->input('checkbox1', [
  8540. 'label' => 'My checkboxes',
  8541. 'multiple' => 'checkbox',
  8542. 'type' => 'select',
  8543. 'options' => [
  8544. ['text' => 'First Checkbox', 'value' => 1],
  8545. ['text' => 'Second Checkbox', 'value' => 2]
  8546. ],
  8547. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8548. 'value' => ['1']
  8549. ]);
  8550. $expected = [
  8551. ['div' => ['class' => 'input select']],
  8552. ['label' => ['for' => 'checkbox1']],
  8553. 'My checkboxes',
  8554. '/label',
  8555. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8556. ['div' => ['class' => 'checkbox']],
  8557. ['label' => [
  8558. 'class' => 'custom-class another-class selected',
  8559. 'data-name' => 'bob',
  8560. 'for' => 'checkbox1-1'
  8561. ]],
  8562. ['input' => [
  8563. 'type' => 'checkbox',
  8564. 'name' => 'checkbox1[]',
  8565. 'value' => '1',
  8566. 'id' => 'checkbox1-1',
  8567. 'checked' => 'checked'
  8568. ]],
  8569. 'First Checkbox',
  8570. '/label',
  8571. '/div',
  8572. ['div' => ['class' => 'checkbox']],
  8573. ['label' => [
  8574. 'class' => 'custom-class another-class',
  8575. 'data-name' => 'bob',
  8576. 'for' => 'checkbox1-2'
  8577. ]],
  8578. ['input' => [
  8579. 'type' => 'checkbox',
  8580. 'name' => 'checkbox1[]',
  8581. 'value' => '2',
  8582. 'id' => 'checkbox1-2'
  8583. ]],
  8584. 'Second Checkbox',
  8585. '/label',
  8586. '/div',
  8587. '/div'
  8588. ];
  8589. $this->assertHtml($expected, $result);
  8590. }
  8591. }