FormHelperTest.php 304 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Collection\Collection;
  17. use Cake\Core\Configure;
  18. use Cake\Form\Form;
  19. use Cake\Http\ServerRequest;
  20. use Cake\ORM\Entity;
  21. use Cake\ORM\Table;
  22. use Cake\ORM\TableRegistry;
  23. use Cake\Routing\Router;
  24. use Cake\TestSuite\TestCase;
  25. use Cake\Utility\Security;
  26. use Cake\View\Helper\FormHelper;
  27. use Cake\View\View;
  28. /**
  29. * Test stub.
  30. */
  31. class Article extends Entity
  32. {
  33. }
  34. /**
  35. * Contact class
  36. */
  37. class ContactsTable extends Table
  38. {
  39. /**
  40. * Default schema
  41. *
  42. * @var array
  43. */
  44. protected $_schema = [
  45. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  46. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  47. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  48. 'phone' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  49. 'password' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  50. 'published' => ['type' => 'date', 'null' => true, 'default' => null, 'length' => null],
  51. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  52. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  53. 'age' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => null],
  54. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  55. ];
  56. /**
  57. * Initializes the schema
  58. *
  59. * @return void
  60. */
  61. public function initialize(array $config)
  62. {
  63. $this->schema($this->_schema);
  64. }
  65. }
  66. /**
  67. * ValidateUser class
  68. */
  69. class ValidateUsersTable extends Table
  70. {
  71. /**
  72. * schema method
  73. *
  74. * @var array
  75. */
  76. protected $_schema = [
  77. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  78. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  79. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  80. 'balance' => ['type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2],
  81. 'cost_decimal' => ['type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3],
  82. 'null_decimal' => ['type' => 'decimal', 'null' => false, 'length' => null, 'precision' => null],
  83. 'ratio' => ['type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6],
  84. 'population' => ['type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0],
  85. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  86. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  87. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  88. ];
  89. /**
  90. * Initializes the schema
  91. *
  92. * @return void
  93. */
  94. public function initialize(array $config)
  95. {
  96. $this->schema($this->_schema);
  97. }
  98. }
  99. /**
  100. * FormHelperTest class
  101. *
  102. * @property \Cake\View\Helper\FormHelper $Form
  103. * @property \Cake\View\View $View
  104. */
  105. class FormHelperTest extends TestCase
  106. {
  107. /**
  108. * Fixtures to be used
  109. *
  110. * @var array
  111. */
  112. public $fixtures = ['core.articles', 'core.comments'];
  113. /**
  114. * Do not load the fixtures by default
  115. *
  116. * @var bool
  117. */
  118. public $autoFixtures = false;
  119. /**
  120. * @var array
  121. */
  122. protected $article = [];
  123. /**
  124. * setUp method
  125. *
  126. * @return void
  127. */
  128. public function setUp()
  129. {
  130. parent::setUp();
  131. Configure::write('Config.language', 'eng');
  132. Configure::write('App.base', '');
  133. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  134. $this->View = new View();
  135. $this->Form = new FormHelper($this->View);
  136. $request = new ServerRequest([
  137. 'webroot' => '',
  138. 'base' => '',
  139. 'url' => '/articles/add',
  140. 'params' => [
  141. 'controller' => 'articles',
  142. 'action' => 'add',
  143. ]
  144. ]);
  145. $this->Form->Url->request = $this->Form->request = $request;
  146. $this->dateRegex = [
  147. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  148. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  149. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  150. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  151. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  152. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  153. ];
  154. $this->article = [
  155. 'schema' => [
  156. 'id' => ['type' => 'integer'],
  157. 'author_id' => ['type' => 'integer', 'null' => true],
  158. 'title' => ['type' => 'string', 'null' => true],
  159. 'body' => 'text',
  160. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  161. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  162. ],
  163. 'required' => [
  164. 'author_id' => true,
  165. 'title' => true,
  166. ]
  167. ];
  168. Security::salt('foo!');
  169. Router::connect('/:controller', ['action' => 'index']);
  170. Router::connect('/:controller/:action/*');
  171. }
  172. /**
  173. * tearDown method
  174. *
  175. * @return void
  176. */
  177. public function tearDown()
  178. {
  179. parent::tearDown();
  180. unset($this->Form, $this->Controller, $this->View);
  181. TableRegistry::clear();
  182. }
  183. /**
  184. * Test construct() with the templates option.
  185. *
  186. * @return void
  187. */
  188. public function testConstructTemplatesFile()
  189. {
  190. $helper = new FormHelper($this->View, [
  191. 'templates' => 'htmlhelper_tags'
  192. ]);
  193. $result = $helper->control('name');
  194. $this->assertContains('<input', $result);
  195. }
  196. /**
  197. * Test that when specifying custom widgets the config array for that widget
  198. * is overwritten instead of merged.
  199. *
  200. * @return void
  201. */
  202. public function testConstructWithWidgets()
  203. {
  204. $config = [
  205. 'widgets' => [
  206. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
  207. ]
  208. ];
  209. $helper = new FormHelper($this->View, $config);
  210. $registry = $helper->widgetRegistry();
  211. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('datetime'));
  212. }
  213. /**
  214. * Test that when specifying custom widgets config file and it should be
  215. * added to widgets array. WidgetRegistry will load widgets in constructor.
  216. *
  217. * @return void
  218. */
  219. public function testConstructWithWidgetsConfig()
  220. {
  221. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  222. $registry = $helper->widgetRegistry();
  223. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('text'));
  224. }
  225. /**
  226. * Test registering a new widget class and rendering it.
  227. *
  228. * @return void
  229. */
  230. public function testAddWidgetAndRenderWidget()
  231. {
  232. $data = [
  233. 'val' => 1
  234. ];
  235. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  236. $this->Form->addWidget('test', $mock);
  237. $mock->expects($this->once())
  238. ->method('render')
  239. ->with($data)
  240. ->will($this->returnValue('HTML'));
  241. $result = $this->Form->widget('test', $data);
  242. $this->assertEquals('HTML', $result);
  243. }
  244. /**
  245. * Test that secureFields() of widget is called after calling render(),
  246. * not before.
  247. *
  248. * @return void
  249. */
  250. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  251. {
  252. $data = [
  253. 'val' => 1,
  254. 'name' => 'test'
  255. ];
  256. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  257. $this->Form->addWidget('test', $mock);
  258. $mock->expects($this->at(0))
  259. ->method('render')
  260. ->with($data)
  261. ->will($this->returnValue('HTML'));
  262. $mock->expects($this->at(1))
  263. ->method('secureFields')
  264. ->with($data)
  265. ->will($this->returnValue(['test']));
  266. $result = $this->Form->widget('test', $data + ['secure' => true]);
  267. $this->assertEquals('HTML', $result);
  268. }
  269. /**
  270. * Test that empty string is not added to secure fields list when
  271. * rendering input widget without name.
  272. *
  273. * @return void
  274. */
  275. public function testRenderingWidgetWithEmptyName()
  276. {
  277. $this->assertEquals([], $this->Form->fields);
  278. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  279. $this->assertEquals('<select name=""></select>', $result);
  280. $this->assertEquals([], $this->Form->fields);
  281. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  282. $this->assertEquals('<select name="0"></select>', $result);
  283. $this->assertEquals(['0'], $this->Form->fields);
  284. }
  285. /**
  286. * Test registering an invalid widget class.
  287. *
  288. * @expectedException \RuntimeException
  289. * @return void
  290. */
  291. public function testAddWidgetInvalid()
  292. {
  293. $mock = new \StdClass();
  294. $this->Form->addWidget('test', $mock);
  295. $this->Form->widget('test');
  296. }
  297. /**
  298. * Test adding a new context class.
  299. *
  300. * @return void
  301. */
  302. public function testAddContextProvider()
  303. {
  304. $context = 'My data';
  305. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  306. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  307. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  308. $this->assertEquals($context, $data['entity']);
  309. return $stub;
  310. });
  311. $this->Form->create($context);
  312. $result = $this->Form->context();
  313. $this->assertSame($stub, $result);
  314. }
  315. /**
  316. * Test replacing a context class.
  317. *
  318. * @return void
  319. */
  320. public function testAddContextProviderReplace()
  321. {
  322. $entity = new Article();
  323. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  324. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  325. return $stub;
  326. });
  327. $this->Form->create($entity);
  328. $result = $this->Form->context();
  329. $this->assertSame($stub, $result);
  330. }
  331. /**
  332. * Test overriding a context class.
  333. *
  334. * @return void
  335. */
  336. public function testAddContextProviderAdd()
  337. {
  338. $entity = new Article();
  339. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  340. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  341. if ($data['entity'] instanceof Entity) {
  342. return $stub;
  343. }
  344. });
  345. $this->Form->create($entity);
  346. $result = $this->Form->context();
  347. $this->assertSame($stub, $result);
  348. }
  349. /**
  350. * Test adding an invalid context class.
  351. *
  352. * @expectedException \RuntimeException
  353. * @expectedExceptionMessage Context providers must return object implementing Cake\View\Form\ContextInterface. Got "stdClass" instead.
  354. * @return void
  355. */
  356. public function testAddContextProviderInvalid()
  357. {
  358. $context = 'My data';
  359. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  360. return new \StdClass();
  361. });
  362. $this->Form->create($context);
  363. }
  364. /**
  365. * Provides context options for create().
  366. *
  367. * @return array
  368. */
  369. public function contextSelectionProvider()
  370. {
  371. $entity = new Article();
  372. $collection = new Collection([$entity]);
  373. $emptyCollection = new Collection([]);
  374. $arrayObject = new \ArrayObject([]);
  375. $data = [
  376. 'schema' => [
  377. 'title' => ['type' => 'string']
  378. ]
  379. ];
  380. $form = new Form();
  381. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  382. return [
  383. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  384. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  385. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  386. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  387. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  388. 'form' => [$form, 'Cake\View\Form\FormContext'],
  389. 'none' => [null, 'Cake\View\Form\NullContext'],
  390. 'false' => [false, 'Cake\View\Form\NullContext'],
  391. 'custom' => [$custom, get_class($custom)]
  392. ];
  393. }
  394. /**
  395. * Test default context selection in create()
  396. *
  397. * @dataProvider contextSelectionProvider
  398. * @return void
  399. */
  400. public function testCreateContextSelectionBuiltIn($data, $class)
  401. {
  402. $this->loadFixtures('Articles');
  403. $this->Form->create($data);
  404. $this->assertInstanceOf($class, $this->Form->context());
  405. }
  406. /**
  407. * Data provider for type option.
  408. *
  409. * @return array
  410. */
  411. public static function requestTypeProvider()
  412. {
  413. return [
  414. // type, method, override
  415. ['post', 'post', 'POST'],
  416. ['put', 'post', 'PUT'],
  417. ['patch', 'post', 'PATCH'],
  418. ['delete', 'post', 'DELETE'],
  419. ];
  420. }
  421. /**
  422. * Test creating file forms.
  423. *
  424. * @return void
  425. */
  426. public function testCreateFile()
  427. {
  428. $encoding = strtolower(Configure::read('App.encoding'));
  429. $result = $this->Form->create(false, ['type' => 'file']);
  430. $expected = [
  431. 'form' => [
  432. 'method' => 'post', 'action' => '/articles/add',
  433. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  434. ],
  435. 'div' => ['style' => 'display:none;'],
  436. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  437. '/div'
  438. ];
  439. $this->assertHtml($expected, $result);
  440. }
  441. /**
  442. * Test creating GET forms.
  443. *
  444. * @return void
  445. */
  446. public function testCreateGet()
  447. {
  448. $encoding = strtolower(Configure::read('App.encoding'));
  449. $result = $this->Form->create(false, ['type' => 'get']);
  450. $expected = ['form' => [
  451. 'method' => 'get', 'action' => '/articles/add',
  452. 'accept-charset' => $encoding
  453. ]];
  454. $this->assertHtml($expected, $result);
  455. }
  456. /**
  457. * Test explicit method/enctype options.
  458. *
  459. * Explicit method overwrites inferred method from 'type'
  460. *
  461. * @return void
  462. */
  463. public function testCreateExplicitMethodEnctype()
  464. {
  465. $encoding = strtolower(Configure::read('App.encoding'));
  466. $result = $this->Form->create(false, [
  467. 'type' => 'get',
  468. 'method' => 'put',
  469. 'enctype' => 'multipart/form-data'
  470. ]);
  471. $expected = ['form' => [
  472. 'method' => 'put',
  473. 'action' => '/articles/add',
  474. 'enctype' => 'multipart/form-data',
  475. 'accept-charset' => $encoding
  476. ]];
  477. $this->assertHtml($expected, $result);
  478. }
  479. /**
  480. * Test create() with the templates option.
  481. *
  482. * @return void
  483. */
  484. public function testCreateTemplatesArray()
  485. {
  486. $result = $this->Form->create($this->article, [
  487. 'templates' => [
  488. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  489. ]
  490. ]);
  491. $expected = [
  492. 'form' => [
  493. 'class' => 'form-horizontal',
  494. 'method' => 'post',
  495. 'action' => '/articles/add',
  496. 'accept-charset' => 'utf-8'
  497. ]
  498. ];
  499. $this->assertHtml($expected, $result);
  500. }
  501. /**
  502. * Test create() with the templates option.
  503. *
  504. * @return void
  505. */
  506. public function testCreateTemplatesFile()
  507. {
  508. $result = $this->Form->create($this->article, [
  509. 'templates' => 'htmlhelper_tags',
  510. ]);
  511. $expected = [
  512. 'start form',
  513. 'div' => ['class' => 'hidden'],
  514. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  515. '/div'
  516. ];
  517. $this->assertHtml($expected, $result);
  518. }
  519. /**
  520. * Test that create() and end() restore templates.
  521. *
  522. * @return void
  523. */
  524. public function testCreateEndRestoreTemplates()
  525. {
  526. $this->Form->create($this->article, [
  527. 'templates' => ['input' => 'custom input element']
  528. ]);
  529. $this->Form->end();
  530. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  531. }
  532. /**
  533. * Test using template vars in various templates used by control() method.
  534. *
  535. * @return void
  536. */
  537. public function testControlTemplateVars()
  538. {
  539. $result = $this->Form->control('text', [
  540. 'templates' => [
  541. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  542. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  543. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  544. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  545. ],
  546. 'templateVars' => [
  547. 'forinput' => 'in-input',
  548. 'forlabel' => 'in-label',
  549. 'forgroup' => 'in-group',
  550. 'forcontainer' => 'in-container'
  551. ]
  552. ]);
  553. $expected = [
  554. 'div' => ['class'],
  555. 'label' => ['for'],
  556. 'Text in-label',
  557. '/label',
  558. 'in-group',
  559. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  560. 'in-container',
  561. '/div',
  562. ];
  563. $this->assertHtml($expected, $result);
  564. }
  565. /**
  566. * Test ensuring template variables work in template files loaded
  567. * during control().
  568. *
  569. * @return void
  570. */
  571. public function testControlTemplatesFromFile()
  572. {
  573. $result = $this->Form->control('title', [
  574. 'templates' => 'test_templates',
  575. 'templateVars' => [
  576. 'forcontainer' => 'container-data'
  577. ]
  578. ]);
  579. $expected = [
  580. 'div' => ['class'],
  581. 'label' => ['for'],
  582. 'Title',
  583. '/label',
  584. 'input' => ['name', 'type' => 'text', 'id'],
  585. 'container-data',
  586. '/div',
  587. ];
  588. $this->assertHtml($expected, $result);
  589. }
  590. /**
  591. * Test using template vars in inputSubmit and submitContainer template.
  592. *
  593. * @return void
  594. */
  595. public function testSubmitTemplateVars()
  596. {
  597. $this->Form->templates([
  598. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  599. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  600. ]);
  601. $result = $this->Form->submit('Submit', [
  602. 'templateVars' => [
  603. 'forinput' => 'in-input',
  604. 'forcontainer' => 'in-container'
  605. ]
  606. ]);
  607. $expected = [
  608. 'div' => ['class'],
  609. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  610. 'in-container',
  611. '/div',
  612. ];
  613. $this->assertHtml($expected, $result);
  614. }
  615. /**
  616. * test the create() method
  617. *
  618. * @dataProvider requestTypeProvider
  619. * @return void
  620. */
  621. public function testCreateTypeOptions($type, $method, $override)
  622. {
  623. $encoding = strtolower(Configure::read('App.encoding'));
  624. $result = $this->Form->create(false, ['type' => $type]);
  625. $expected = [
  626. 'form' => [
  627. 'method' => $method, 'action' => '/articles/add',
  628. 'accept-charset' => $encoding
  629. ],
  630. 'div' => ['style' => 'display:none;'],
  631. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  632. '/div'
  633. ];
  634. $this->assertHtml($expected, $result);
  635. }
  636. /**
  637. * Test using template vars in Create (formStart template)
  638. *
  639. * @return void
  640. */
  641. public function testCreateTemplateVars()
  642. {
  643. $result = $this->Form->create($this->article, [
  644. 'templates' => [
  645. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  646. ],
  647. 'templateVars' => ['header' => 'headertext']
  648. ]);
  649. $expected = [
  650. 'h4' => ['class'],
  651. 'headertext',
  652. '/h4',
  653. 'form' => [
  654. 'method' => 'post',
  655. 'action' => '/articles/add',
  656. 'accept-charset' => 'utf-8'
  657. ]
  658. ];
  659. $this->assertHtml($expected, $result);
  660. }
  661. /**
  662. * Test opening a form for an update operation.
  663. *
  664. * @return void
  665. */
  666. public function testCreateUpdateForm()
  667. {
  668. $encoding = strtolower(Configure::read('App.encoding'));
  669. $this->Form->request->here = '/articles/edit/1';
  670. $this->Form->request->params['action'] = 'edit';
  671. $this->article['defaults']['id'] = 1;
  672. $result = $this->Form->create($this->article);
  673. $expected = [
  674. 'form' => [
  675. 'method' => 'post',
  676. 'action' => '/articles/edit/1',
  677. 'accept-charset' => $encoding
  678. ],
  679. 'div' => ['style' => 'display:none;'],
  680. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  681. '/div'
  682. ];
  683. $this->assertHtml($expected, $result);
  684. }
  685. /**
  686. * test create() with automatic url generation
  687. *
  688. * @return void
  689. */
  690. public function testCreateAutoUrl()
  691. {
  692. $encoding = strtolower(Configure::read('App.encoding'));
  693. $this->Form->request->params['action'] = 'delete';
  694. $this->Form->request->here = '/articles/delete/10';
  695. $this->Form->request->base = '';
  696. $result = $this->Form->create($this->article);
  697. $expected = [
  698. 'form' => [
  699. 'method' => 'post', 'action' => '/articles/delete/10',
  700. 'accept-charset' => $encoding
  701. ],
  702. 'div' => ['style' => 'display:none;'],
  703. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  704. '/div'
  705. ];
  706. $this->assertHtml($expected, $result);
  707. $this->article['defaults'] = ['id' => 1];
  708. $this->Form->request->here = '/articles/edit/1';
  709. $this->Form->request->params['action'] = 'delete';
  710. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  711. $expected = [
  712. 'form' => [
  713. 'method' => 'post',
  714. 'action' => '/articles/edit/1',
  715. 'accept-charset' => $encoding
  716. ],
  717. 'div' => ['style' => 'display:none;'],
  718. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  719. '/div'
  720. ];
  721. $this->assertHtml($expected, $result);
  722. $this->Form->request->params['action'] = 'add';
  723. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  724. $expected = [
  725. 'form' => [
  726. 'method' => 'post',
  727. 'action' => '/articles/publish/1',
  728. 'accept-charset' => $encoding
  729. ],
  730. 'div' => ['style' => 'display:none;'],
  731. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  732. '/div'
  733. ];
  734. $this->assertHtml($expected, $result);
  735. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  736. $expected = [
  737. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  738. 'div' => ['style' => 'display:none;'],
  739. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  740. '/div'
  741. ];
  742. $this->assertHtml($expected, $result);
  743. $this->Form->request->params['controller'] = 'Pages';
  744. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  745. $expected = [
  746. 'form' => [
  747. 'method' => 'post', 'action' => '/Pages/signup/1',
  748. 'accept-charset' => $encoding
  749. ],
  750. 'div' => ['style' => 'display:none;'],
  751. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  752. '/div'
  753. ];
  754. $this->assertHtml($expected, $result);
  755. }
  756. /**
  757. * Test create() with no URL (no "action" attribute for <form> tag)
  758. *
  759. * @return void
  760. */
  761. public function testCreateNoUrl()
  762. {
  763. $result = $this->Form->create(false, ['url' => false]);
  764. $expected = [
  765. 'form' => [
  766. 'method' => 'post',
  767. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  768. ],
  769. 'div' => ['style' => 'display:none;'],
  770. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  771. '/div'
  772. ];
  773. $this->assertHtml($expected, $result);
  774. }
  775. /**
  776. * test create() with a custom route
  777. *
  778. * @return void
  779. */
  780. public function testCreateCustomRoute()
  781. {
  782. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  783. $encoding = strtolower(Configure::read('App.encoding'));
  784. $this->Form->request->params['controller'] = 'users';
  785. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  786. $expected = [
  787. 'form' => [
  788. 'method' => 'post', 'action' => '/login',
  789. 'accept-charset' => $encoding
  790. ],
  791. 'div' => ['style' => 'display:none;'],
  792. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  793. '/div'
  794. ];
  795. $this->assertHtml($expected, $result);
  796. Router::connect(
  797. '/new-article',
  798. ['controller' => 'articles', 'action' => 'myaction'],
  799. ['_name' => 'my-route']
  800. );
  801. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  802. $expected = [
  803. 'form' => [
  804. 'method' => 'post', 'action' => '/new-article',
  805. 'accept-charset' => $encoding,
  806. ],
  807. 'div' => ['style' => 'display:none;'],
  808. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  809. '/div'
  810. ];
  811. $this->assertHtml($expected, $result);
  812. }
  813. /**
  814. * test automatic accept-charset overriding
  815. *
  816. * @return void
  817. */
  818. public function testCreateWithAcceptCharset()
  819. {
  820. $result = $this->Form->create(
  821. $this->article,
  822. [
  823. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  824. ]
  825. );
  826. $expected = [
  827. 'form' => [
  828. 'method' => 'post', 'action' => '/articles',
  829. 'accept-charset' => 'iso-8859-1'
  830. ],
  831. 'div' => ['style' => 'display:none;'],
  832. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  833. '/div'
  834. ];
  835. $this->assertHtml($expected, $result);
  836. }
  837. /**
  838. * Test base form URL when url param is passed with multiple parameters (&)
  839. *
  840. */
  841. public function testCreateQueryStringRequest()
  842. {
  843. $encoding = strtolower(Configure::read('App.encoding'));
  844. $result = $this->Form->create($this->article, [
  845. 'type' => 'post',
  846. 'escape' => false,
  847. 'url' => [
  848. 'controller' => 'controller',
  849. 'action' => 'action',
  850. '?' => ['param1' => 'value1', 'param2' => 'value2']
  851. ]
  852. ]);
  853. $expected = [
  854. 'form' => [
  855. 'method' => 'post',
  856. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  857. 'accept-charset' => $encoding
  858. ],
  859. 'div' => ['style' => 'display:none;'],
  860. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  861. '/div'
  862. ];
  863. $this->assertHtml($expected, $result);
  864. $result = $this->Form->create($this->article, [
  865. 'type' => 'post',
  866. 'url' => [
  867. 'controller' => 'controller',
  868. 'action' => 'action',
  869. '?' => ['param1' => 'value1', 'param2' => 'value2']
  870. ]
  871. ]);
  872. $this->assertHtml($expected, $result);
  873. }
  874. /**
  875. * test that create() doesn't cause errors by multiple id's being in the primary key
  876. * as could happen with multiple select or checkboxes.
  877. *
  878. * @return void
  879. */
  880. public function testCreateWithMultipleIdInData()
  881. {
  882. $encoding = strtolower(Configure::read('App.encoding'));
  883. $this->Form->request->data['Article']['id'] = [1, 2];
  884. $result = $this->Form->create($this->article);
  885. $expected = [
  886. 'form' => [
  887. 'method' => 'post',
  888. 'action' => '/articles/add',
  889. 'accept-charset' => $encoding
  890. ],
  891. 'div' => ['style' => 'display:none;'],
  892. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  893. '/div'
  894. ];
  895. $this->assertHtml($expected, $result);
  896. }
  897. /**
  898. * test that create() doesn't add in extra passed params.
  899. *
  900. * @return void
  901. */
  902. public function testCreatePassedArgs()
  903. {
  904. $encoding = strtolower(Configure::read('App.encoding'));
  905. $this->Form->request->data['Article']['id'] = 1;
  906. $result = $this->Form->create($this->article, [
  907. 'type' => 'post',
  908. 'escape' => false,
  909. 'url' => [
  910. 'action' => 'edit',
  911. 'myparam'
  912. ]
  913. ]);
  914. $expected = [
  915. 'form' => [
  916. 'method' => 'post',
  917. 'action' => '/articles/edit/myparam',
  918. 'accept-charset' => $encoding
  919. ],
  920. 'div' => ['style' => 'display:none;'],
  921. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  922. '/div'
  923. ];
  924. $this->assertHtml($expected, $result);
  925. }
  926. /**
  927. * test creating a get form, and get form inputs.
  928. *
  929. * @return void
  930. */
  931. public function testGetFormCreate()
  932. {
  933. $encoding = strtolower(Configure::read('App.encoding'));
  934. $result = $this->Form->create($this->article, ['type' => 'get']);
  935. $expected = ['form' => [
  936. 'method' => 'get', 'action' => '/articles/add',
  937. 'accept-charset' => $encoding
  938. ]];
  939. $this->assertHtml($expected, $result);
  940. $result = $this->Form->text('title');
  941. $expected = ['input' => [
  942. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  943. ]];
  944. $this->assertHtml($expected, $result);
  945. $result = $this->Form->password('password');
  946. $expected = ['input' => [
  947. 'name' => 'password', 'type' => 'password'
  948. ]];
  949. $this->assertHtml($expected, $result);
  950. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  951. $result = $this->Form->text('user_form');
  952. $expected = ['input' => [
  953. 'name' => 'user_form', 'type' => 'text'
  954. ]];
  955. $this->assertHtml($expected, $result);
  956. }
  957. /**
  958. * test get form, and inputs when the model param is false
  959. *
  960. * @return void
  961. */
  962. public function testGetFormWithFalseModel()
  963. {
  964. $encoding = strtolower(Configure::read('App.encoding'));
  965. $this->Form->request->params['controller'] = 'contact_test';
  966. $result = $this->Form->create(false, [
  967. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  968. ]);
  969. $expected = ['form' => [
  970. 'method' => 'get', 'action' => '/contact_test/add',
  971. 'accept-charset' => $encoding
  972. ]];
  973. $this->assertHtml($expected, $result);
  974. $result = $this->Form->text('reason');
  975. $expected = [
  976. 'input' => ['type' => 'text', 'name' => 'reason']
  977. ];
  978. $this->assertHtml($expected, $result);
  979. }
  980. /**
  981. * testFormCreateWithSecurity method
  982. *
  983. * Test form->create() with security key.
  984. *
  985. * @return void
  986. */
  987. public function testCreateWithSecurity()
  988. {
  989. $this->Form->request->params['_csrfToken'] = 'testKey';
  990. $encoding = strtolower(Configure::read('App.encoding'));
  991. $result = $this->Form->create($this->article, [
  992. 'url' => '/articles/publish',
  993. ]);
  994. $expected = [
  995. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  996. 'div' => ['style' => 'display:none;'],
  997. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  998. ['input' => [
  999. 'type' => 'hidden',
  1000. 'name' => '_csrfToken',
  1001. 'value' => 'testKey',
  1002. 'autocomplete' => 'off',
  1003. ]],
  1004. '/div'
  1005. ];
  1006. $this->assertHtml($expected, $result);
  1007. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1008. $expected['form']['id'] = 'MyForm';
  1009. $this->assertHtml($expected, $result);
  1010. }
  1011. /**
  1012. * testFormCreateGetNoSecurity method
  1013. *
  1014. * Test form->create() with no security key as its a get form
  1015. *
  1016. * @return void
  1017. */
  1018. public function testCreateEndGetNoSecurity()
  1019. {
  1020. $this->Form->request->params['_csrfToken'] = 'testKey';
  1021. $article = new Article();
  1022. $result = $this->Form->create($article, [
  1023. 'type' => 'get',
  1024. 'url' => '/contacts/add'
  1025. ]);
  1026. $this->assertNotContains('testKey', $result);
  1027. $result = $this->Form->end();
  1028. $this->assertNotContains('testKey', $result);
  1029. }
  1030. /**
  1031. * test that create() clears the fields property so it starts fresh
  1032. *
  1033. * @return void
  1034. */
  1035. public function testCreateClearingFields()
  1036. {
  1037. $this->Form->fields = ['model_id'];
  1038. $this->Form->create($this->article);
  1039. $this->assertEquals([], $this->Form->fields);
  1040. }
  1041. /**
  1042. * Tests form hash generation with model-less data
  1043. *
  1044. * @return void
  1045. */
  1046. public function testValidateHashNoModel()
  1047. {
  1048. $this->Form->request->params['_Token'] = 'foo';
  1049. $result = $this->Form->secure(['anything']);
  1050. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  1051. }
  1052. /**
  1053. * Tests that hidden fields generated for checkboxes don't get locked
  1054. *
  1055. * @return void
  1056. */
  1057. public function testNoCheckboxLocking()
  1058. {
  1059. $this->Form->request->params['_Token'] = 'foo';
  1060. $this->assertSame([], $this->Form->fields);
  1061. $this->Form->checkbox('check', ['value' => '1']);
  1062. $this->assertSame($this->Form->fields, ['check']);
  1063. }
  1064. /**
  1065. * testFormSecurityFields method
  1066. *
  1067. * Test generation of secure form hash generation.
  1068. *
  1069. * @return void
  1070. */
  1071. public function testFormSecurityFields()
  1072. {
  1073. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1074. $this->Form->request->params['_Token'] = 'testKey';
  1075. $result = $this->Form->secure($fields);
  1076. $hash = Security::hash(serialize($fields) . Security::salt());
  1077. $hash .= ':' . 'Model.valid';
  1078. $hash = urlencode($hash);
  1079. $tokenDebug = urlencode(json_encode([
  1080. '',
  1081. $fields,
  1082. []
  1083. ]));
  1084. $expected = [
  1085. 'div' => ['style' => 'display:none;'],
  1086. ['input' => [
  1087. 'type' => 'hidden',
  1088. 'name' => '_Token[fields]',
  1089. 'value' => $hash,
  1090. 'autocomplete' => 'off',
  1091. ]],
  1092. ['input' => [
  1093. 'type' => 'hidden',
  1094. 'name' => '_Token[unlocked]',
  1095. 'value' => '',
  1096. 'autocomplete' => 'off',
  1097. ]],
  1098. ['input' => [
  1099. 'type' => 'hidden',
  1100. 'name' => '_Token[debug]',
  1101. 'value' => $tokenDebug,
  1102. 'autocomplete' => 'off',
  1103. ]],
  1104. '/div'
  1105. ];
  1106. $this->assertHtml($expected, $result);
  1107. }
  1108. /**
  1109. * testFormSecurityFields method
  1110. *
  1111. * Test debug token is not generated if debug is false
  1112. *
  1113. * @return void
  1114. */
  1115. public function testFormSecurityFieldsNoDebugMode()
  1116. {
  1117. Configure::write('debug', false);
  1118. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1119. $this->Form->request->params['_Token'] = 'testKey';
  1120. $result = $this->Form->secure($fields);
  1121. $hash = Security::hash(serialize($fields) . Security::salt());
  1122. $hash .= ':' . 'Model.valid';
  1123. $hash = urlencode($hash);
  1124. $expected = [
  1125. 'div' => ['style' => 'display:none;'],
  1126. ['input' => [
  1127. 'type' => 'hidden',
  1128. 'name' => '_Token[fields]',
  1129. 'autocomplete' => 'off',
  1130. 'value' => $hash
  1131. ]],
  1132. ['input' => [
  1133. 'type' => 'hidden',
  1134. 'name' => '_Token[unlocked]',
  1135. 'autocomplete' => 'off',
  1136. 'value' => '',
  1137. ]],
  1138. '/div'
  1139. ];
  1140. $this->assertHtml($expected, $result);
  1141. }
  1142. /**
  1143. * Tests correct generation of number fields for smallint
  1144. *
  1145. * @return void
  1146. */
  1147. public function testTextFieldGenerationForSmallint()
  1148. {
  1149. $this->article['schema'] = [
  1150. 'foo' => [
  1151. 'type' => 'smallinteger',
  1152. 'null' => false,
  1153. 'default' => null,
  1154. 'length' => 10
  1155. ]
  1156. ];
  1157. $this->Form->create($this->article);
  1158. $result = $this->Form->control('foo');
  1159. $this->assertContains('class="input number"', $result);
  1160. $this->assertContains('type="number"', $result);
  1161. }
  1162. /**
  1163. * Tests correct generation of number fields for tinyint
  1164. *
  1165. * @return void
  1166. */
  1167. public function testTextFieldGenerationForTinyint()
  1168. {
  1169. $this->article['schema'] = [
  1170. 'foo' => [
  1171. 'type' => 'tinyinteger',
  1172. 'null' => false,
  1173. 'default' => null,
  1174. 'length' => 10
  1175. ]
  1176. ];
  1177. $this->Form->create($this->article);
  1178. $result = $this->Form->control('foo');
  1179. $this->assertContains('class="input number"', $result);
  1180. $this->assertContains('type="number"', $result);
  1181. }
  1182. /**
  1183. * Tests correct generation of number fields for double and float fields
  1184. *
  1185. * @return void
  1186. */
  1187. public function testTextFieldGenerationForFloats()
  1188. {
  1189. $this->article['schema'] = [
  1190. 'foo' => [
  1191. 'type' => 'float',
  1192. 'null' => false,
  1193. 'default' => null,
  1194. 'length' => 10
  1195. ]
  1196. ];
  1197. $this->Form->create($this->article);
  1198. $result = $this->Form->control('foo');
  1199. $expected = [
  1200. 'div' => ['class' => 'input number'],
  1201. 'label' => ['for' => 'foo'],
  1202. 'Foo',
  1203. '/label',
  1204. ['input' => [
  1205. 'type' => 'number',
  1206. 'name' => 'foo',
  1207. 'id' => 'foo',
  1208. 'step' => 'any'
  1209. ]],
  1210. '/div'
  1211. ];
  1212. $this->assertHtml($expected, $result);
  1213. $result = $this->Form->control('foo', ['step' => 0.5]);
  1214. $expected = [
  1215. 'div' => ['class' => 'input number'],
  1216. 'label' => ['for' => 'foo'],
  1217. 'Foo',
  1218. '/label',
  1219. ['input' => [
  1220. 'type' => 'number',
  1221. 'name' => 'foo',
  1222. 'id' => 'foo',
  1223. 'step' => '0.5'
  1224. ]],
  1225. '/div'
  1226. ];
  1227. $this->assertHtml($expected, $result);
  1228. }
  1229. /**
  1230. * Tests correct generation of number fields for integer fields
  1231. *
  1232. * @return void
  1233. */
  1234. public function testTextFieldTypeNumberGenerationForIntegers()
  1235. {
  1236. TableRegistry::get('Contacts', [
  1237. 'className' => __NAMESPACE__ . '\ContactsTable'
  1238. ]);
  1239. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1240. $result = $this->Form->control('age');
  1241. $expected = [
  1242. 'div' => ['class' => 'input number'],
  1243. 'label' => ['for' => 'age'],
  1244. 'Age',
  1245. '/label',
  1246. ['input' => [
  1247. 'type' => 'number', 'name' => 'age',
  1248. 'id' => 'age'
  1249. ]],
  1250. '/div'
  1251. ];
  1252. $this->assertHtml($expected, $result);
  1253. }
  1254. /**
  1255. * Tests correct generation of file upload fields for binary fields
  1256. *
  1257. * @return void
  1258. */
  1259. public function testFileUploadFieldTypeGenerationForBinaries()
  1260. {
  1261. $table = TableRegistry::get('Contacts', [
  1262. 'className' => __NAMESPACE__ . '\ContactsTable'
  1263. ]);
  1264. $table->schema(['foo' => [
  1265. 'type' => 'binary',
  1266. 'null' => false,
  1267. 'default' => null,
  1268. 'length' => 1024
  1269. ]]);
  1270. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1271. $result = $this->Form->control('foo');
  1272. $expected = [
  1273. 'div' => ['class' => 'input file'],
  1274. 'label' => ['for' => 'foo'],
  1275. 'Foo',
  1276. '/label',
  1277. ['input' => [
  1278. 'type' => 'file', 'name' => 'foo',
  1279. 'id' => 'foo'
  1280. ]],
  1281. '/div'
  1282. ];
  1283. $this->assertHtml($expected, $result);
  1284. }
  1285. /**
  1286. * testFormSecurityMultipleFields method
  1287. *
  1288. * Test secure() with multiple row form. Ensure hash is correct.
  1289. *
  1290. * @return void
  1291. */
  1292. public function testFormSecurityMultipleFields()
  1293. {
  1294. $this->Form->request->params['_Token'] = 'foo';
  1295. $fields = [
  1296. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1297. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1298. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1299. ];
  1300. $result = $this->Form->secure($fields);
  1301. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  1302. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  1303. $tokenDebug = urlencode(json_encode([
  1304. '',
  1305. $fields,
  1306. []
  1307. ]));
  1308. $expected = [
  1309. 'div' => ['style' => 'display:none;'],
  1310. ['input' => [
  1311. 'type' => 'hidden',
  1312. 'name' => '_Token[fields]',
  1313. 'value' => $hash,
  1314. 'autocomplete' => 'off',
  1315. ]],
  1316. ['input' => [
  1317. 'type' => 'hidden',
  1318. 'name' => '_Token[unlocked]',
  1319. 'autocomplete' => 'off',
  1320. 'value' => ''
  1321. ]],
  1322. ['input' => [
  1323. 'type' => 'hidden',
  1324. 'name' => '_Token[debug]',
  1325. 'value' => $tokenDebug,
  1326. 'autocomplete' => 'off',
  1327. ]],
  1328. '/div'
  1329. ];
  1330. $this->assertHtml($expected, $result);
  1331. }
  1332. /**
  1333. * testFormSecurityMultipleSubmitButtons
  1334. *
  1335. * test form submit generation and ensure that _Token is only created on end()
  1336. *
  1337. * @return void
  1338. */
  1339. public function testFormSecurityMultipleSubmitButtons()
  1340. {
  1341. $this->Form->request->params['_Token'] = 'testKey';
  1342. $this->Form->create($this->article);
  1343. $this->Form->text('Address.title');
  1344. $this->Form->text('Address.first_name');
  1345. $result = $this->Form->submit('Save', ['name' => 'save']);
  1346. $expected = [
  1347. 'div' => ['class' => 'submit'],
  1348. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1349. '/div',
  1350. ];
  1351. $this->assertHtml($expected, $result);
  1352. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1353. $expected = [
  1354. 'div' => ['class' => 'submit'],
  1355. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1356. '/div',
  1357. ];
  1358. $this->assertHtml($expected, $result);
  1359. $result = $this->Form->end();
  1360. $tokenDebug = urlencode(json_encode([
  1361. '/articles/add',
  1362. [
  1363. 'Address.title',
  1364. 'Address.first_name',
  1365. ],
  1366. ['save', 'cancel']
  1367. ]));
  1368. $expected = [
  1369. 'div' => ['style' => 'display:none;'],
  1370. ['input' => [
  1371. 'type' => 'hidden',
  1372. 'name' => '_Token[fields]',
  1373. 'autocomplete',
  1374. 'value'
  1375. ]],
  1376. ['input' => [
  1377. 'type' => 'hidden',
  1378. 'name' => '_Token[unlocked]',
  1379. 'value' => 'cancel%7Csave',
  1380. 'autocomplete' => 'off',
  1381. ]],
  1382. ['input' => [
  1383. 'type' => 'hidden',
  1384. 'name' => '_Token[debug]',
  1385. 'value' => $tokenDebug,
  1386. 'autocomplete' => 'off',
  1387. ]],
  1388. '/div'
  1389. ];
  1390. $this->assertHtml($expected, $result);
  1391. }
  1392. /**
  1393. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1394. *
  1395. * @return void
  1396. */
  1397. public function testSecurityButtonNestedNamed()
  1398. {
  1399. $key = 'testKey';
  1400. $this->Form->request->params['_csrfToken'] = $key;
  1401. $this->Form->create('Addresses');
  1402. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1403. $result = $this->Form->unlockField();
  1404. $this->assertEquals(['Address.button'], $result);
  1405. }
  1406. /**
  1407. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1408. *
  1409. * @return void
  1410. */
  1411. public function testSecuritySubmitNestedNamed()
  1412. {
  1413. $this->Form->request->params['_Token'] = 'testKey';
  1414. $this->Form->create($this->article);
  1415. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1416. $result = $this->Form->unlockField();
  1417. $this->assertEquals(['Address.button'], $result);
  1418. }
  1419. /**
  1420. * Test that the correct fields are unlocked for image submits with no names.
  1421. *
  1422. * @return void
  1423. */
  1424. public function testSecuritySubmitImageNoName()
  1425. {
  1426. $this->Form->request->params['_Token'] = 'testKey';
  1427. $this->Form->create(false);
  1428. $result = $this->Form->submit('save.png');
  1429. $expected = [
  1430. 'div' => ['class' => 'submit'],
  1431. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1432. '/div'
  1433. ];
  1434. $this->assertHtml($expected, $result);
  1435. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1436. }
  1437. /**
  1438. * Test that the correct fields are unlocked for image submits with names.
  1439. *
  1440. * @return void
  1441. */
  1442. public function testSecuritySubmitImageName()
  1443. {
  1444. $this->Form->request->params['_Token'] = 'testKey';
  1445. $this->Form->create(null);
  1446. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1447. $expected = [
  1448. 'div' => ['class' => 'submit'],
  1449. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1450. '/div'
  1451. ];
  1452. $this->assertHtml($expected, $result);
  1453. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1454. }
  1455. /**
  1456. * testFormSecurityMultipleControlFields method
  1457. *
  1458. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1459. *
  1460. * @return void
  1461. */
  1462. public function testFormSecurityMultipleControlFields()
  1463. {
  1464. $this->Form->request->params['_Token'] = 'testKey';
  1465. $this->Form->create();
  1466. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1467. $this->Form->control('Addresses.0.title');
  1468. $this->Form->control('Addresses.0.first_name');
  1469. $this->Form->control('Addresses.0.last_name');
  1470. $this->Form->control('Addresses.0.address');
  1471. $this->Form->control('Addresses.0.city');
  1472. $this->Form->control('Addresses.0.phone');
  1473. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1474. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1475. $this->Form->control('Addresses.1.title');
  1476. $this->Form->control('Addresses.1.first_name');
  1477. $this->Form->control('Addresses.1.last_name');
  1478. $this->Form->control('Addresses.1.address');
  1479. $this->Form->control('Addresses.1.city');
  1480. $this->Form->control('Addresses.1.phone');
  1481. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1482. $result = $this->Form->secure($this->Form->fields);
  1483. $hash = '8bd3911b07b507408b1a969b31ee90c47b7d387e%3AAddresses.0.id%7CAddresses.1.id';
  1484. $tokenDebug = urlencode(json_encode([
  1485. '/articles/add',
  1486. [
  1487. 'Addresses.0.id' => '123456',
  1488. 'Addresses.0.title',
  1489. 'Addresses.0.first_name',
  1490. 'Addresses.0.last_name',
  1491. 'Addresses.0.address',
  1492. 'Addresses.0.city',
  1493. 'Addresses.0.phone',
  1494. 'Addresses.0.primary',
  1495. 'Addresses.1.id' => '654321',
  1496. 'Addresses.1.title',
  1497. 'Addresses.1.first_name',
  1498. 'Addresses.1.last_name',
  1499. 'Addresses.1.address',
  1500. 'Addresses.1.city',
  1501. 'Addresses.1.phone',
  1502. 'Addresses.1.primary',
  1503. ],
  1504. []
  1505. ]));
  1506. $expected = [
  1507. 'div' => ['style' => 'display:none;'],
  1508. ['input' => [
  1509. 'type' => 'hidden',
  1510. 'name' => '_Token[fields]',
  1511. 'value' => $hash,
  1512. 'autocomplete' => 'off',
  1513. ]],
  1514. ['input' => [
  1515. 'type' => 'hidden',
  1516. 'name' => '_Token[unlocked]',
  1517. 'autocomplete' => 'off',
  1518. 'value' => ''
  1519. ]],
  1520. ['input' => [
  1521. 'type' => 'hidden',
  1522. 'name' => '_Token[debug]',
  1523. 'value' => $tokenDebug,
  1524. 'autocomplete' => 'off',
  1525. ]],
  1526. '/div'
  1527. ];
  1528. $this->assertHtml($expected, $result);
  1529. }
  1530. /**
  1531. * testFormSecurityArrayFields method
  1532. *
  1533. * Test form security with Model.field.0 style inputs.
  1534. *
  1535. * @return void
  1536. */
  1537. public function testFormSecurityArrayFields()
  1538. {
  1539. $this->Form->request->params['_Token'] = 'testKey';
  1540. $this->Form->create();
  1541. $this->Form->text('Address.primary.1');
  1542. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1543. $this->Form->text('Address.secondary.1.0');
  1544. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1545. }
  1546. /**
  1547. * testFormSecurityMultipleControlDisabledFields method
  1548. *
  1549. * Test secure form generation with multiple records and disabled fields.
  1550. *
  1551. * @return void
  1552. */
  1553. public function testFormSecurityMultipleControlDisabledFields()
  1554. {
  1555. $this->Form->request->params['_Token'] = [
  1556. 'unlockedFields' => ['first_name', 'address']
  1557. ];
  1558. $this->Form->create();
  1559. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1560. $this->Form->text('Addresses.0.title');
  1561. $this->Form->text('Addresses.0.first_name');
  1562. $this->Form->text('Addresses.0.last_name');
  1563. $this->Form->text('Addresses.0.address');
  1564. $this->Form->text('Addresses.0.city');
  1565. $this->Form->text('Addresses.0.phone');
  1566. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1567. $this->Form->text('Addresses.1.title');
  1568. $this->Form->text('Addresses.1.first_name');
  1569. $this->Form->text('Addresses.1.last_name');
  1570. $this->Form->text('Addresses.1.address');
  1571. $this->Form->text('Addresses.1.city');
  1572. $this->Form->text('Addresses.1.phone');
  1573. $result = $this->Form->secure($this->Form->fields);
  1574. $hash = '4fb10b46873df4ddd4ef5c3a19944a2f29b38991%3AAddresses.0.id%7CAddresses.1.id';
  1575. $tokenDebug = urlencode(json_encode([
  1576. '/articles/add',
  1577. [
  1578. 'Addresses.0.id' => '123456',
  1579. 'Addresses.0.title',
  1580. 'Addresses.0.last_name',
  1581. 'Addresses.0.city',
  1582. 'Addresses.0.phone',
  1583. 'Addresses.1.id' => '654321',
  1584. 'Addresses.1.title',
  1585. 'Addresses.1.last_name',
  1586. 'Addresses.1.city',
  1587. 'Addresses.1.phone'
  1588. ],
  1589. [
  1590. 'first_name',
  1591. 'address'
  1592. ]
  1593. ]));
  1594. $expected = [
  1595. 'div' => ['style' => 'display:none;'],
  1596. ['input' => [
  1597. 'type' => 'hidden',
  1598. 'name' => '_Token[fields]',
  1599. 'autocomplete' => 'off',
  1600. 'value' => $hash
  1601. ]],
  1602. ['input' => [
  1603. 'type' => 'hidden',
  1604. 'name' => '_Token[unlocked]',
  1605. 'autocomplete' => 'off',
  1606. 'value' => 'address%7Cfirst_name',
  1607. ]],
  1608. ['input' => [
  1609. 'type' => 'hidden',
  1610. 'name' => '_Token[debug]',
  1611. 'autocomplete' => 'off',
  1612. 'value' => $tokenDebug,
  1613. ]],
  1614. '/div'
  1615. ];
  1616. $this->assertHtml($expected, $result);
  1617. }
  1618. /**
  1619. * testFormSecurityControlDisabledFields method
  1620. *
  1621. * Test single record form with disabled fields.
  1622. *
  1623. * @return void
  1624. */
  1625. public function testFormSecurityControlUnlockedFields()
  1626. {
  1627. $this->Form->request->params['_Token'] = [
  1628. 'unlockedFields' => ['first_name', 'address']
  1629. ];
  1630. $this->Form->create();
  1631. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1632. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1633. $this->Form->text('Addresses.title');
  1634. $this->Form->text('Addresses.first_name');
  1635. $this->Form->text('Addresses.last_name');
  1636. $this->Form->text('Addresses.address');
  1637. $this->Form->text('Addresses.city');
  1638. $this->Form->text('Addresses.phone');
  1639. $result = $this->Form->fields;
  1640. $expected = [
  1641. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1642. 'Addresses.city', 'Addresses.phone'
  1643. ];
  1644. $this->assertEquals($expected, $result);
  1645. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1646. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1647. $tokenDebug = urlencode(json_encode([
  1648. '/articles/add',
  1649. [
  1650. 'Addresses.id' => '123456',
  1651. 'Addresses.title',
  1652. 'Addresses.last_name',
  1653. 'Addresses.city',
  1654. 'Addresses.phone'
  1655. ],
  1656. [
  1657. 'first_name',
  1658. 'address'
  1659. ]
  1660. ]));
  1661. $expected = [
  1662. 'div' => ['style' => 'display:none;'],
  1663. ['input' => [
  1664. 'type' => 'hidden',
  1665. 'name' => '_Token[fields]',
  1666. 'value' => $hash,
  1667. 'autocomplete' => 'off',
  1668. 'data-foo' => 'bar',
  1669. ]],
  1670. ['input' => [
  1671. 'type' => 'hidden',
  1672. 'name' => '_Token[unlocked]',
  1673. 'value' => 'address%7Cfirst_name',
  1674. 'autocomplete' => 'off',
  1675. 'data-foo' => 'bar',
  1676. ]],
  1677. ['input' => [
  1678. 'type' => 'hidden', 'name' => '_Token[debug]',
  1679. 'value' => $tokenDebug,
  1680. 'autocomplete' => 'off',
  1681. 'data-foo' => 'bar'
  1682. ]],
  1683. '/div'
  1684. ];
  1685. $this->assertHtml($expected, $result);
  1686. }
  1687. /**
  1688. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1689. *
  1690. * Test single record form with debugSecurity param.
  1691. *
  1692. * @return void
  1693. */
  1694. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1695. {
  1696. $this->Form->request->params['_Token'] = [
  1697. 'unlockedFields' => ['first_name', 'address']
  1698. ];
  1699. $this->Form->create();
  1700. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1701. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1702. $this->Form->text('Addresses.title');
  1703. $this->Form->text('Addresses.first_name');
  1704. $this->Form->text('Addresses.last_name');
  1705. $this->Form->text('Addresses.address');
  1706. $this->Form->text('Addresses.city');
  1707. $this->Form->text('Addresses.phone');
  1708. $result = $this->Form->fields;
  1709. $expected = [
  1710. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1711. 'Addresses.city', 'Addresses.phone'
  1712. ];
  1713. $this->assertEquals($expected, $result);
  1714. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1715. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1716. $tokenDebug = urlencode(json_encode([
  1717. '/articles/add',
  1718. [
  1719. 'Addresses.id' => '123456',
  1720. 'Addresses.title',
  1721. 'Addresses.last_name',
  1722. 'Addresses.city',
  1723. 'Addresses.phone'
  1724. ],
  1725. [
  1726. 'first_name',
  1727. 'address'
  1728. ]
  1729. ]));
  1730. $expected = [
  1731. 'div' => ['style' => 'display:none;'],
  1732. ['input' => [
  1733. 'type' => 'hidden',
  1734. 'name' => '_Token[fields]',
  1735. 'value' => $hash,
  1736. 'autocomplete' => 'off',
  1737. 'data-foo' => 'bar',
  1738. ]],
  1739. ['input' => [
  1740. 'type' => 'hidden',
  1741. 'name' => '_Token[unlocked]',
  1742. 'value' => 'address%7Cfirst_name',
  1743. 'autocomplete' => 'off',
  1744. 'data-foo' => 'bar',
  1745. ]],
  1746. ['input' => [
  1747. 'type' => 'hidden', 'name' => '_Token[debug]',
  1748. 'value' => $tokenDebug,
  1749. 'autocomplete' => 'off',
  1750. 'data-foo' => 'bar'
  1751. ]],
  1752. '/div'
  1753. ];
  1754. $this->assertHtml($expected, $result);
  1755. }
  1756. /**
  1757. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1758. *
  1759. * Debug is false, debugSecurity is true -> no debug
  1760. *
  1761. * @return void
  1762. */
  1763. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1764. {
  1765. $this->Form->request->params['_Token'] = [
  1766. 'unlockedFields' => ['first_name', 'address']
  1767. ];
  1768. $this->Form->create();
  1769. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1770. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1771. $this->Form->text('Addresses.title');
  1772. $this->Form->text('Addresses.first_name');
  1773. $this->Form->text('Addresses.last_name');
  1774. $this->Form->text('Addresses.address');
  1775. $this->Form->text('Addresses.city');
  1776. $this->Form->text('Addresses.phone');
  1777. $result = $this->Form->fields;
  1778. $expected = [
  1779. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1780. 'Addresses.city', 'Addresses.phone'
  1781. ];
  1782. $this->assertEquals($expected, $result);
  1783. Configure::write('debug', false);
  1784. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1785. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1786. $expected = [
  1787. 'div' => ['style' => 'display:none;'],
  1788. ['input' => [
  1789. 'type' => 'hidden',
  1790. 'name' => '_Token[fields]',
  1791. 'value' => $hash,
  1792. 'autocomplete' => 'off',
  1793. 'data-foo' => 'bar',
  1794. ]],
  1795. ['input' => [
  1796. 'type' => 'hidden',
  1797. 'name' => '_Token[unlocked]',
  1798. 'value' => 'address%7Cfirst_name',
  1799. 'autocomplete' => 'off',
  1800. 'data-foo' => 'bar',
  1801. ]],
  1802. '/div'
  1803. ];
  1804. $this->assertHtml($expected, $result);
  1805. }
  1806. /**
  1807. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1808. *
  1809. * Test single record form with debugSecurity param.
  1810. *
  1811. * @return void
  1812. */
  1813. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1814. {
  1815. $this->Form->request->params['_Token'] = [
  1816. 'unlockedFields' => ['first_name', 'address']
  1817. ];
  1818. $this->Form->create();
  1819. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1820. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1821. $this->Form->text('Addresses.title');
  1822. $this->Form->text('Addresses.first_name');
  1823. $this->Form->text('Addresses.last_name');
  1824. $this->Form->text('Addresses.address');
  1825. $this->Form->text('Addresses.city');
  1826. $this->Form->text('Addresses.phone');
  1827. $result = $this->Form->fields;
  1828. $expected = [
  1829. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1830. 'Addresses.city', 'Addresses.phone'
  1831. ];
  1832. $this->assertEquals($expected, $result);
  1833. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1834. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1835. $expected = [
  1836. 'div' => ['style' => 'display:none;'],
  1837. ['input' => [
  1838. 'type' => 'hidden',
  1839. 'name' => '_Token[fields]',
  1840. 'value' => $hash,
  1841. 'autocomplete' => 'off',
  1842. 'data-foo' => 'bar',
  1843. ]],
  1844. ['input' => [
  1845. 'type' => 'hidden',
  1846. 'name' => '_Token[unlocked]',
  1847. 'value' => 'address%7Cfirst_name',
  1848. 'autocomplete' => 'off',
  1849. 'data-foo' => 'bar',
  1850. ]],
  1851. '/div'
  1852. ];
  1853. $this->assertHtml($expected, $result);
  1854. }
  1855. /**
  1856. * testFormSecureWithCustomNameAttribute method
  1857. *
  1858. * Test securing inputs with custom name attributes.
  1859. *
  1860. * @return void
  1861. */
  1862. public function testFormSecureWithCustomNameAttribute()
  1863. {
  1864. $this->Form->request->params['_Token'] = 'testKey';
  1865. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1866. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1867. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1868. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1869. }
  1870. /**
  1871. * testFormSecuredControl method
  1872. *
  1873. * Test generation of entire secure form, assertions made on control() output.
  1874. *
  1875. * @return void
  1876. */
  1877. public function testFormSecuredControl()
  1878. {
  1879. $this->Form->request->params['_csrfToken'] = 'testKey';
  1880. $this->Form->request->params['_Token'] = 'stuff';
  1881. $this->article['schema'] = [
  1882. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1883. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1884. ];
  1885. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1886. $encoding = strtolower(Configure::read('App.encoding'));
  1887. $expected = [
  1888. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1889. 'div' => ['style' => 'display:none;'],
  1890. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1891. ['input' => [
  1892. 'type' => 'hidden',
  1893. 'name' => '_csrfToken',
  1894. 'value' => 'testKey',
  1895. 'autocomplete' => 'off'
  1896. ]],
  1897. '/div'
  1898. ];
  1899. $this->assertHtml($expected, $result);
  1900. $result = $this->Form->control('ratio');
  1901. $expected = [
  1902. 'div' => ['class'],
  1903. 'label' => ['for'],
  1904. 'Ratio',
  1905. '/label',
  1906. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1907. '/div',
  1908. ];
  1909. $this->assertHtml($expected, $result);
  1910. $result = $this->Form->control('population');
  1911. $expected = [
  1912. 'div' => ['class'],
  1913. 'label' => ['for'],
  1914. 'Population',
  1915. '/label',
  1916. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1917. '/div',
  1918. ];
  1919. $this->assertHtml($expected, $result);
  1920. $result = $this->Form->control('published', ['type' => 'text']);
  1921. $expected = [
  1922. 'div' => ['class' => 'input text'],
  1923. 'label' => ['for' => 'published'],
  1924. 'Published',
  1925. '/label',
  1926. ['input' => [
  1927. 'type' => 'text',
  1928. 'name' => 'published',
  1929. 'id' => 'published'
  1930. ]],
  1931. '/div'
  1932. ];
  1933. $this->assertHtml($expected, $result);
  1934. $result = $this->Form->control('other', ['type' => 'text']);
  1935. $expected = [
  1936. 'div' => ['class' => 'input text'],
  1937. 'label' => ['for' => 'other'],
  1938. 'Other',
  1939. '/label',
  1940. ['input' => [
  1941. 'type' => 'text',
  1942. 'name' => 'other',
  1943. 'id',
  1944. ]],
  1945. '/div'
  1946. ];
  1947. $this->assertHtml($expected, $result);
  1948. $result = $this->Form->hidden('stuff');
  1949. $expected = [
  1950. 'input' => [
  1951. 'type' => 'hidden',
  1952. 'name' => 'stuff'
  1953. ]
  1954. ];
  1955. $this->assertHtml($expected, $result);
  1956. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1957. $expected = ['input' => [
  1958. 'type' => 'hidden',
  1959. 'name' => 'hidden',
  1960. 'value' => '0'
  1961. ]];
  1962. $this->assertHtml($expected, $result);
  1963. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1964. $expected = [
  1965. 'div' => ['class' => 'input checkbox'],
  1966. ['input' => [
  1967. 'type' => 'hidden',
  1968. 'name' => 'something',
  1969. 'value' => '0'
  1970. ]],
  1971. 'label' => ['for' => 'something'],
  1972. ['input' => [
  1973. 'type' => 'checkbox',
  1974. 'name' => 'something',
  1975. 'value' => '1',
  1976. 'id' => 'something'
  1977. ]],
  1978. 'Something',
  1979. '/label',
  1980. '/div'
  1981. ];
  1982. $this->assertHtml($expected, $result);
  1983. $result = $this->Form->fields;
  1984. $expectedFields = [
  1985. 'ratio',
  1986. 'population',
  1987. 'published',
  1988. 'other',
  1989. 'stuff' => '',
  1990. 'hidden' => '0',
  1991. 'something'
  1992. ];
  1993. $this->assertEquals($expectedFields, $result);
  1994. $result = $this->Form->secure($this->Form->fields);
  1995. $tokenDebug = urlencode(json_encode([
  1996. '/articles/add',
  1997. $expectedFields,
  1998. []
  1999. ]));
  2000. $expected = [
  2001. 'div' => ['style' => 'display:none;'],
  2002. ['input' => [
  2003. 'type' => 'hidden',
  2004. 'name' => '_Token[fields]',
  2005. 'value',
  2006. 'autocomplete'
  2007. ]],
  2008. ['input' => [
  2009. 'type' => 'hidden',
  2010. 'name' => '_Token[unlocked]',
  2011. 'value' => '',
  2012. 'autocomplete' => 'off'
  2013. ]],
  2014. ['input' => [
  2015. 'type' => 'hidden', 'name' => '_Token[debug]',
  2016. 'value' => $tokenDebug,
  2017. 'autocomplete' => 'off'
  2018. ]],
  2019. '/div'
  2020. ];
  2021. $this->assertHtml($expected, $result);
  2022. }
  2023. /**
  2024. * testSecuredControlCustomName method
  2025. *
  2026. * Test secured inputs with custom names.
  2027. *
  2028. * @return void
  2029. */
  2030. public function testSecuredControlCustomName()
  2031. {
  2032. $this->Form->request->params['_Token'] = 'testKey';
  2033. $this->assertEquals([], $this->Form->fields);
  2034. $this->Form->text('text_input', [
  2035. 'name' => 'Option[General.default_role]',
  2036. ]);
  2037. $expected = ['Option.General.default_role'];
  2038. $this->assertEquals($expected, $this->Form->fields);
  2039. $this->Form->select('select_box', [1, 2], [
  2040. 'name' => 'Option[General.select_role]',
  2041. ]);
  2042. $expected[] = 'Option.General.select_role';
  2043. $this->assertEquals($expected, $this->Form->fields);
  2044. $this->Form->text('other.things[]');
  2045. $expected[] = 'other.things';
  2046. $this->assertEquals($expected, $this->Form->fields);
  2047. }
  2048. /**
  2049. * testSecuredControlDuplicate method
  2050. *
  2051. * Test that a hidden field followed by a visible field
  2052. * undoes the hidden field locking.
  2053. *
  2054. * @return void
  2055. */
  2056. public function testSecuredControlDuplicate()
  2057. {
  2058. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2059. $this->assertEquals([], $this->Form->fields);
  2060. $this->Form->control('text_val', [
  2061. 'type' => 'hidden',
  2062. 'value' => 'some text',
  2063. ]);
  2064. $expected = ['text_val' => 'some text'];
  2065. $this->assertEquals($expected, $this->Form->fields);
  2066. $this->Form->control('text_val', [
  2067. 'type' => 'text',
  2068. ]);
  2069. $expected = ['text_val'];
  2070. $this->assertEquals($expected, $this->Form->fields);
  2071. }
  2072. /**
  2073. * testFormSecuredFileControl method
  2074. *
  2075. * Tests that the correct keys are added to the field hash index.
  2076. *
  2077. * @return void
  2078. */
  2079. public function testFormSecuredFileControl()
  2080. {
  2081. $this->assertEquals([], $this->Form->fields);
  2082. $this->Form->file('Attachment.file');
  2083. $expected = [
  2084. 'Attachment.file.name', 'Attachment.file.type',
  2085. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2086. 'Attachment.file.size'
  2087. ];
  2088. $this->assertEquals($expected, $this->Form->fields);
  2089. }
  2090. /**
  2091. * testFormSecuredMultipleSelect method
  2092. *
  2093. * Test that multiple selects keys are added to field hash.
  2094. *
  2095. * @return void
  2096. */
  2097. public function testFormSecuredMultipleSelect()
  2098. {
  2099. $this->Form->request->params['_csrfToken'] = 'testKey';
  2100. $this->assertEquals([], $this->Form->fields);
  2101. $options = ['1' => 'one', '2' => 'two'];
  2102. $this->Form->select('Model.select', $options);
  2103. $expected = ['Model.select'];
  2104. $this->assertEquals($expected, $this->Form->fields);
  2105. $this->Form->fields = [];
  2106. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2107. $this->assertEquals($expected, $this->Form->fields);
  2108. }
  2109. /**
  2110. * testFormSecuredRadio method
  2111. *
  2112. * @return void
  2113. */
  2114. public function testFormSecuredRadio()
  2115. {
  2116. $this->Form->request->params['_Token'] = 'testKey';
  2117. $this->assertEquals([], $this->Form->fields);
  2118. $options = ['1' => 'option1', '2' => 'option2'];
  2119. $this->Form->radio('Test.test', $options);
  2120. $expected = ['Test.test'];
  2121. $this->assertEquals($expected, $this->Form->fields);
  2122. $this->Form->radio('Test.all', $options, [
  2123. 'disabled' => ['option1', 'option2']
  2124. ]);
  2125. $expected = ['Test.test', 'Test.all' => ''];
  2126. $this->assertEquals($expected, $this->Form->fields);
  2127. $this->Form->radio('Test.some', $options, [
  2128. 'disabled' => ['option1']
  2129. ]);
  2130. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2131. $this->assertEquals($expected, $this->Form->fields);
  2132. }
  2133. /**
  2134. * testFormSecuredAndDisabledNotAssoc method
  2135. *
  2136. * Test that when disabled is in a list based attribute array it works.
  2137. *
  2138. * @return void
  2139. */
  2140. public function testFormSecuredAndDisabledNotAssoc()
  2141. {
  2142. $this->Form->request->params['_Token'] = 'testKey';
  2143. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2144. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2145. $this->Form->text('Model.text', ['disabled']);
  2146. $this->Form->textarea('Model.textarea', ['disabled']);
  2147. $this->Form->password('Model.password', ['disabled']);
  2148. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2149. $expected = [
  2150. 'Model.radio' => ''
  2151. ];
  2152. $this->assertEquals($expected, $this->Form->fields);
  2153. }
  2154. /**
  2155. * testFormSecuredAndDisabled method
  2156. *
  2157. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2158. * hashing.
  2159. *
  2160. * @return void
  2161. */
  2162. public function testFormSecuredAndDisabled()
  2163. {
  2164. $this->Form->request->params['_Token'] = 'testKey';
  2165. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2166. $this->Form->text('Model.text', ['disabled' => true]);
  2167. $this->Form->password('Model.text', ['disabled' => true]);
  2168. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2169. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2170. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2171. $this->Form->year('Model.year', ['disabled' => true]);
  2172. $this->Form->month('Model.month', ['disabled' => true]);
  2173. $this->Form->day('Model.day', ['disabled' => true]);
  2174. $this->Form->hour('Model.hour', ['disabled' => true]);
  2175. $this->Form->minute('Model.minute', ['disabled' => true]);
  2176. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2177. $expected = [
  2178. 'Model.radio' => ''
  2179. ];
  2180. $this->assertEquals($expected, $this->Form->fields);
  2181. }
  2182. /**
  2183. * testDisableSecurityUsingForm method
  2184. *
  2185. * @return void
  2186. */
  2187. public function testDisableSecurityUsingForm()
  2188. {
  2189. $this->Form->request->params['_Token'] = [
  2190. 'disabledFields' => []
  2191. ];
  2192. $this->Form->create();
  2193. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2194. $this->Form->text('Addresses.title');
  2195. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2196. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2197. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2198. $result = $this->Form->fields;
  2199. $expected = [
  2200. 'Addresses.id' => '123456', 'Addresses.title',
  2201. ];
  2202. $this->assertEquals($expected, $result);
  2203. }
  2204. /**
  2205. * testUnlockFieldAddsToList method
  2206. *
  2207. * Test disableField.
  2208. *
  2209. * @return void
  2210. */
  2211. public function testUnlockFieldAddsToList()
  2212. {
  2213. $this->Form->request->params['_Token'] = [
  2214. 'unlockedFields' => []
  2215. ];
  2216. $this->Form->unlockField('Contact.name');
  2217. $this->Form->text('Contact.name');
  2218. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2219. $this->assertEquals([], $this->Form->fields);
  2220. }
  2221. /**
  2222. * testUnlockFieldRemovingFromFields method
  2223. *
  2224. * Test unlockField removing from fields array.
  2225. *
  2226. * @return void
  2227. */
  2228. public function testUnlockFieldRemovingFromFields()
  2229. {
  2230. $this->Form->request->params['_Token'] = [
  2231. 'unlockedFields' => []
  2232. ];
  2233. $this->Form->create($this->article);
  2234. $this->Form->hidden('Article.id', ['value' => 1]);
  2235. $this->Form->text('Article.title');
  2236. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2237. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  2238. $this->Form->unlockField('Article.title');
  2239. $this->Form->unlockField('Article.id');
  2240. $this->assertEquals([], $this->Form->fields);
  2241. }
  2242. /**
  2243. * testResetUnlockFields method
  2244. *
  2245. * Test reset unlockFields, when create new form.
  2246. *
  2247. * @return void
  2248. */
  2249. public function testResetUnlockFields()
  2250. {
  2251. $this->Form->request->params['_Token'] = [
  2252. 'key' => 'testKey',
  2253. 'unlockedFields' => []
  2254. ];
  2255. $this->Form->unlockField('Contact.id');
  2256. $this->Form->create('Contact');
  2257. $this->Form->hidden('Contact.id', ['value' => 1]);
  2258. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2259. $this->Form->end();
  2260. $this->Form->create('Contact');
  2261. $this->Form->hidden('Contact.id', ['value' => 1]);
  2262. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2263. }
  2264. /**
  2265. * testSecuredFormUrlIgnoresHost method
  2266. *
  2267. * Test that only the path + query elements of a form's URL show up in their hash.
  2268. *
  2269. * @return void
  2270. */
  2271. public function testSecuredFormUrlIgnoresHost()
  2272. {
  2273. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2274. $expected = '0ff0c85cd70584d8fd18fa136846d22c66c21e2d%3A';
  2275. $this->Form->create($this->article, [
  2276. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2277. ]);
  2278. $result = $this->Form->secure();
  2279. $this->assertContains($expected, $result);
  2280. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2281. $result = $this->Form->secure();
  2282. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2283. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2284. $result = $this->Form->secure();
  2285. $this->assertContains($expected, $result, 'URL path + query should work.');
  2286. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2287. $result = $this->Form->secure();
  2288. $this->assertNotContains($expected, $result, 'URL is different');
  2289. }
  2290. /**
  2291. * testSecuredFormUrlHasHtmlAndIdentifier method
  2292. *
  2293. * Test that URL, HTML and identifier show up in their hashes.
  2294. *
  2295. * @return void
  2296. */
  2297. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2298. {
  2299. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2300. $expected = 'ece0693fb1b19ca116133db1832ac29baaf41ce5%3A';
  2301. $this->Form->create($this->article, [
  2302. 'url' => [
  2303. 'controller' => 'articles',
  2304. 'action' => 'view',
  2305. '?' => [
  2306. 'page' => 1,
  2307. 'limit' => 10,
  2308. 'html' => '<>"',
  2309. ],
  2310. '#' => 'result',
  2311. ],
  2312. ]);
  2313. $result = $this->Form->secure();
  2314. $this->assertContains($expected, $result);
  2315. $this->Form->create($this->article, [
  2316. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2317. ]);
  2318. $result = $this->Form->secure();
  2319. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2320. $this->Form->create($this->article, [
  2321. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2322. ]);
  2323. $result = $this->Form->secure();
  2324. $this->assertContains($expected, $result, 'URL path + query should work.');
  2325. }
  2326. /**
  2327. * testErrorMessageDisplay method
  2328. *
  2329. * Test error message display.
  2330. *
  2331. * @return void
  2332. */
  2333. public function testErrorMessageDisplay()
  2334. {
  2335. $this->article['errors'] = [
  2336. 'Article' => [
  2337. 'title' => 'error message',
  2338. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2339. ]
  2340. ];
  2341. $this->Form->create($this->article);
  2342. $result = $this->Form->control('Article.title');
  2343. $expected = [
  2344. 'div' => ['class' => 'input text error'],
  2345. 'label' => ['for' => 'article-title'],
  2346. 'Title',
  2347. '/label',
  2348. 'input' => [
  2349. 'type' => 'text', 'name' => 'Article[title]',
  2350. 'id' => 'article-title', 'class' => 'form-error'
  2351. ],
  2352. ['div' => ['class' => 'error-message']],
  2353. 'error message',
  2354. '/div',
  2355. '/div'
  2356. ];
  2357. $this->assertHtml($expected, $result);
  2358. $result = $this->Form->control('Article.title', [
  2359. 'templates' => [
  2360. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2361. ]
  2362. ]);
  2363. $expected = [
  2364. 'div' => ['class' => 'input text error'],
  2365. 'label' => ['for' => 'article-title'],
  2366. 'Title',
  2367. '/label',
  2368. 'input' => [
  2369. 'type' => 'text', 'name' => 'Article[title]',
  2370. 'id' => 'article-title', 'class' => 'form-error'
  2371. ],
  2372. '/div'
  2373. ];
  2374. $this->assertHtml($expected, $result);
  2375. $result = $this->Form->control('Article.content');
  2376. $expected = [
  2377. 'div' => ['class' => 'input text error'],
  2378. 'label' => ['for' => 'article-content'],
  2379. 'Content',
  2380. '/label',
  2381. 'input' => [
  2382. 'type' => 'text', 'name' => 'Article[content]',
  2383. 'id' => 'article-content', 'class' => 'form-error'
  2384. ],
  2385. ['div' => ['class' => 'error-message']],
  2386. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2387. '/div',
  2388. '/div'
  2389. ];
  2390. $this->assertHtml($expected, $result);
  2391. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2392. $expected = [
  2393. 'div' => ['class' => 'input text error'],
  2394. 'label' => ['for' => 'article-content'],
  2395. 'Content',
  2396. '/label',
  2397. 'input' => [
  2398. 'type' => 'text', 'name' => 'Article[content]',
  2399. 'id' => 'article-content', 'class' => 'form-error'
  2400. ],
  2401. ['div' => ['class' => 'error-message']],
  2402. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2403. '/div',
  2404. '/div'
  2405. ];
  2406. $this->assertHtml($expected, $result);
  2407. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2408. $expected = [
  2409. 'div' => ['class' => 'input text error'],
  2410. 'label' => ['for' => 'article-content'],
  2411. 'Content',
  2412. '/label',
  2413. 'input' => [
  2414. 'type' => 'text', 'name' => 'Article[content]',
  2415. 'id' => 'article-content', 'class' => 'form-error'
  2416. ],
  2417. ['div' => ['class' => 'error-message']],
  2418. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2419. '/div',
  2420. '/div'
  2421. ];
  2422. $this->assertHtml($expected, $result);
  2423. }
  2424. /**
  2425. * testEmptyErrorValidation method
  2426. *
  2427. * Test validation errors, when validation message is an empty string.
  2428. *
  2429. * @return void
  2430. */
  2431. public function testEmptyErrorValidation()
  2432. {
  2433. $this->article['errors'] = [
  2434. 'Article' => ['title' => '']
  2435. ];
  2436. $this->Form->create($this->article);
  2437. $result = $this->Form->control('Article.title');
  2438. $expected = [
  2439. 'div' => ['class' => 'input text error'],
  2440. 'label' => ['for' => 'article-title'],
  2441. 'Title',
  2442. '/label',
  2443. 'input' => [
  2444. 'type' => 'text', 'name' => 'Article[title]',
  2445. 'id' => 'article-title', 'class' => 'form-error'
  2446. ],
  2447. ['div' => ['class' => 'error-message']],
  2448. [],
  2449. '/div',
  2450. '/div'
  2451. ];
  2452. $this->assertHtml($expected, $result);
  2453. }
  2454. /**
  2455. * testEmptyControlErrorValidation method
  2456. *
  2457. * Test validation errors, when calling control() overriding validation message by an empty string.
  2458. *
  2459. * @return void
  2460. */
  2461. public function testEmptyControlErrorValidation()
  2462. {
  2463. $this->article['errors'] = [
  2464. 'Article' => ['title' => 'error message']
  2465. ];
  2466. $this->Form->create($this->article);
  2467. $result = $this->Form->control('Article.title', ['error' => '']);
  2468. $expected = [
  2469. 'div' => ['class' => 'input text error'],
  2470. 'label' => ['for' => 'article-title'],
  2471. 'Title',
  2472. '/label',
  2473. 'input' => [
  2474. 'type' => 'text', 'name' => 'Article[title]',
  2475. 'id' => 'article-title', 'class' => 'form-error'
  2476. ],
  2477. ['div' => ['class' => 'error-message']],
  2478. [],
  2479. '/div',
  2480. '/div'
  2481. ];
  2482. $this->assertHtml($expected, $result);
  2483. }
  2484. /**
  2485. * testControlErrorMessage method
  2486. *
  2487. * Test validation errors, when calling control() overriding validation messages.
  2488. *
  2489. * @return void
  2490. */
  2491. public function testControlErrorMessage()
  2492. {
  2493. $this->article['errors'] = [
  2494. 'title' => ['error message']
  2495. ];
  2496. $this->Form->create($this->article);
  2497. $result = $this->Form->control('title', [
  2498. 'error' => 'Custom error!'
  2499. ]);
  2500. $expected = [
  2501. 'div' => ['class' => 'input text required error'],
  2502. 'label' => ['for' => 'title'],
  2503. 'Title',
  2504. '/label',
  2505. 'input' => [
  2506. 'type' => 'text', 'name' => 'title',
  2507. 'id' => 'title', 'class' => 'form-error',
  2508. 'required' => 'required',
  2509. ],
  2510. ['div' => ['class' => 'error-message']],
  2511. 'Custom error!',
  2512. '/div',
  2513. '/div'
  2514. ];
  2515. $this->assertHtml($expected, $result);
  2516. $result = $this->Form->control('title', [
  2517. 'error' => ['error message' => 'Custom error!']
  2518. ]);
  2519. $expected = [
  2520. 'div' => ['class' => 'input text required error'],
  2521. 'label' => ['for' => 'title'],
  2522. 'Title',
  2523. '/label',
  2524. 'input' => [
  2525. 'type' => 'text',
  2526. 'name' => 'title',
  2527. 'id' => 'title',
  2528. 'class' => 'form-error',
  2529. 'required' => 'required'
  2530. ],
  2531. ['div' => ['class' => 'error-message']],
  2532. 'Custom error!',
  2533. '/div',
  2534. '/div'
  2535. ];
  2536. $this->assertHtml($expected, $result);
  2537. }
  2538. /**
  2539. * testFormValidationAssociated method
  2540. *
  2541. * Tests displaying errors for nested entities.
  2542. *
  2543. * @return void
  2544. */
  2545. public function testFormValidationAssociated()
  2546. {
  2547. $nested = new Entity(['foo' => 'bar']);
  2548. $nested->errors('foo', ['not a valid bar']);
  2549. $entity = new Entity(['nested' => $nested]);
  2550. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2551. $result = $this->Form->error('nested.foo');
  2552. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2553. }
  2554. /**
  2555. * testFormValidationAssociatedSecondLevel method
  2556. *
  2557. * Test form error display with associated model.
  2558. *
  2559. * @return void
  2560. */
  2561. public function testFormValidationAssociatedSecondLevel()
  2562. {
  2563. $inner = new Entity(['bar' => 'baz']);
  2564. $nested = new Entity(['foo' => $inner]);
  2565. $entity = new Entity(['nested' => $nested]);
  2566. $inner->errors('bar', ['not a valid one']);
  2567. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2568. $result = $this->Form->error('nested.foo.bar');
  2569. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2570. }
  2571. /**
  2572. * testFormValidationMultiRecord method
  2573. *
  2574. * Test form error display with multiple records.
  2575. *
  2576. * @return void
  2577. */
  2578. public function testFormValidationMultiRecord()
  2579. {
  2580. $one = new Entity();
  2581. $two = new Entity();
  2582. TableRegistry::get('Contacts', [
  2583. 'className' => __NAMESPACE__ . '\ContactsTable'
  2584. ]);
  2585. $one->set('email', '');
  2586. $one->errors('email', ['invalid email']);
  2587. $two->set('name', '');
  2588. $two->errors('name', ['This is wrong']);
  2589. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2590. $result = $this->Form->control('0.email');
  2591. $expected = [
  2592. 'div' => ['class' => 'input email error'],
  2593. 'label' => ['for' => '0-email'],
  2594. 'Email',
  2595. '/label',
  2596. 'input' => [
  2597. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2598. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2599. ],
  2600. ['div' => ['class' => 'error-message']],
  2601. 'invalid email',
  2602. '/div',
  2603. '/div'
  2604. ];
  2605. $this->assertHtml($expected, $result);
  2606. $result = $this->Form->control('1.name');
  2607. $expected = [
  2608. 'div' => ['class' => 'input text error'],
  2609. 'label' => ['for' => '1-name'],
  2610. 'Name',
  2611. '/label',
  2612. 'input' => [
  2613. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2614. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2615. ],
  2616. ['div' => ['class' => 'error-message']],
  2617. 'This is wrong',
  2618. '/div',
  2619. '/div'
  2620. ];
  2621. $this->assertHtml($expected, $result);
  2622. }
  2623. /**
  2624. * testControl method
  2625. *
  2626. * Test various incarnations of control().
  2627. *
  2628. * @return void
  2629. */
  2630. public function testControl()
  2631. {
  2632. TableRegistry::get('ValidateUsers', [
  2633. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2634. ]);
  2635. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2636. $result = $this->Form->control('ValidateUsers.balance');
  2637. $expected = [
  2638. 'div' => ['class'],
  2639. 'label' => ['for'],
  2640. 'Balance',
  2641. '/label',
  2642. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2643. '/div',
  2644. ];
  2645. $this->assertHtml($expected, $result);
  2646. $result = $this->Form->control('ValidateUser.cost_decimal');
  2647. $expected = [
  2648. 'div' => ['class'],
  2649. 'label' => ['for'],
  2650. 'Cost Decimal',
  2651. '/label',
  2652. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2653. '/div',
  2654. ];
  2655. $this->assertHtml($expected, $result);
  2656. $result = $this->Form->control('ValidateUser.null_decimal');
  2657. $expected = [
  2658. 'div' => ['class'],
  2659. 'label' => ['for'],
  2660. 'Null Decimal',
  2661. '/label',
  2662. 'input' => ['name', 'type' => 'number', 'id'],
  2663. '/div',
  2664. ];
  2665. $this->assertHtml($expected, $result);
  2666. }
  2667. /**
  2668. * testControlCustomization method
  2669. *
  2670. * Tests the input method and passing custom options.
  2671. *
  2672. * @return void
  2673. */
  2674. public function testControlCustomization()
  2675. {
  2676. TableRegistry::get('Contacts', [
  2677. 'className' => __NAMESPACE__ . '\ContactsTable'
  2678. ]);
  2679. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2680. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2681. $expected = [
  2682. 'div' => ['class' => 'input email'],
  2683. 'label' => ['for' => 'custom'],
  2684. 'Email',
  2685. '/label',
  2686. ['input' => [
  2687. 'type' => 'email', 'name' => 'Contact[email]',
  2688. 'id' => 'custom', 'maxlength' => 255
  2689. ]],
  2690. '/div'
  2691. ];
  2692. $this->assertHtml($expected, $result);
  2693. $result = $this->Form->control('Contact.email', [
  2694. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2695. ]);
  2696. $expected = [
  2697. '<div',
  2698. 'label' => ['for' => 'contact-email'],
  2699. 'Email',
  2700. '/label',
  2701. ['input' => [
  2702. 'type' => 'email', 'name' => 'Contact[email]',
  2703. 'id' => 'contact-email', 'maxlength' => 255
  2704. ]],
  2705. '/div'
  2706. ];
  2707. $this->assertHtml($expected, $result);
  2708. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2709. $expected = [
  2710. 'div' => ['class' => 'input text'],
  2711. 'label' => ['for' => 'contact-email'],
  2712. 'Email',
  2713. '/label',
  2714. ['input' => [
  2715. 'type' => 'text', 'name' => 'Contact[email]',
  2716. 'id' => 'contact-email', 'maxlength' => '255'
  2717. ]],
  2718. '/div'
  2719. ];
  2720. $this->assertHtml($expected, $result);
  2721. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2722. $expected = [
  2723. 'div' => ['class' => 'input text'],
  2724. 'label' => ['for' => 'contact-5-email'],
  2725. 'Email',
  2726. '/label',
  2727. ['input' => [
  2728. 'type' => 'text', 'name' => 'Contact[5][email]',
  2729. 'id' => 'contact-5-email', 'maxlength' => '255'
  2730. ]],
  2731. '/div'
  2732. ];
  2733. $this->assertHtml($expected, $result);
  2734. $result = $this->Form->control('Contact.password');
  2735. $expected = [
  2736. 'div' => ['class' => 'input password'],
  2737. 'label' => ['for' => 'contact-password'],
  2738. 'Password',
  2739. '/label',
  2740. ['input' => [
  2741. 'type' => 'password', 'name' => 'Contact[password]',
  2742. 'id' => 'contact-password'
  2743. ]],
  2744. '/div'
  2745. ];
  2746. $this->assertHtml($expected, $result);
  2747. $result = $this->Form->control('Contact.email', [
  2748. 'type' => 'file', 'class' => 'textbox'
  2749. ]);
  2750. $expected = [
  2751. 'div' => ['class' => 'input file'],
  2752. 'label' => ['for' => 'contact-email'],
  2753. 'Email',
  2754. '/label',
  2755. ['input' => [
  2756. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2757. 'id' => 'contact-email'
  2758. ]],
  2759. '/div'
  2760. ];
  2761. $this->assertHtml($expected, $result);
  2762. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2763. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2764. $result = $this->Form->control('phone');
  2765. $expected = [
  2766. 'div' => ['class' => 'input tel'],
  2767. 'label' => ['for' => 'phone'],
  2768. 'Phone',
  2769. '/label',
  2770. ['input' => [
  2771. 'type' => 'tel', 'name' => 'phone',
  2772. 'value' => 'Hello &amp; World &gt; weird chars',
  2773. 'id' => 'phone', 'maxlength' => 255
  2774. ]],
  2775. '/div'
  2776. ];
  2777. $this->assertHtml($expected, $result);
  2778. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2779. $this->Form->create();
  2780. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2781. $expected = [
  2782. 'div' => ['class' => 'input text'],
  2783. 'label' => ['for' => 'myId'],
  2784. 'Field',
  2785. '/label',
  2786. 'input' => [
  2787. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2788. 'value' => 'My value', 'id' => 'myId'
  2789. ],
  2790. '/div'
  2791. ];
  2792. $this->assertHtml($expected, $result);
  2793. $this->Form->request->data = [];
  2794. $entity->errors('field', 'Badness!');
  2795. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2796. $result = $this->Form->control('field');
  2797. $expected = [
  2798. 'div' => ['class' => 'input text error'],
  2799. 'label' => ['for' => 'field'],
  2800. 'Field',
  2801. '/label',
  2802. 'input' => [
  2803. 'type' => 'text', 'name' => 'field',
  2804. 'id' => 'field', 'class' => 'form-error'
  2805. ],
  2806. ['div' => ['class' => 'error-message']],
  2807. 'Badness!',
  2808. '/div',
  2809. '/div'
  2810. ];
  2811. $this->assertHtml($expected, $result);
  2812. $result = $this->Form->control('field', [
  2813. 'templates' => [
  2814. 'inputContainerError' => '{{content}}{{error}}',
  2815. 'error' => '<span class="error-message">{{content}}</span>'
  2816. ]
  2817. ]);
  2818. $expected = [
  2819. 'label' => ['for' => 'field'],
  2820. 'Field',
  2821. '/label',
  2822. 'input' => [
  2823. 'type' => 'text', 'name' => 'field',
  2824. 'id' => 'field', 'class' => 'form-error'
  2825. ],
  2826. ['span' => ['class' => 'error-message']],
  2827. 'Badness!',
  2828. '/span'
  2829. ];
  2830. $this->assertHtml($expected, $result);
  2831. $entity->errors('field', ['minLength'], true);
  2832. $result = $this->Form->control('field', [
  2833. 'error' => [
  2834. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2835. 'maxLength' => 'login too large'
  2836. ]
  2837. ]);
  2838. $expected = [
  2839. 'div' => ['class' => 'input text error'],
  2840. 'label' => ['for' => 'field'],
  2841. 'Field',
  2842. '/label',
  2843. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2844. ['div' => ['class' => 'error-message']],
  2845. 'Le login doit contenir au moins 2 caractères',
  2846. '/div',
  2847. '/div'
  2848. ];
  2849. $this->assertHtml($expected, $result);
  2850. $entity->errors('field', ['maxLength'], true);
  2851. $result = $this->Form->control('field', [
  2852. 'error' => [
  2853. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2854. 'maxLength' => 'login too large',
  2855. ]
  2856. ]);
  2857. $expected = [
  2858. 'div' => ['class' => 'input text error'],
  2859. 'label' => ['for' => 'field'],
  2860. 'Field',
  2861. '/label',
  2862. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2863. ['div' => ['class' => 'error-message']],
  2864. 'login too large',
  2865. '/div',
  2866. '/div'
  2867. ];
  2868. $this->assertHtml($expected, $result);
  2869. }
  2870. /**
  2871. * testControlWithTemplateFile method
  2872. *
  2873. * Test that control() accepts a template file.
  2874. *
  2875. * @return void
  2876. */
  2877. public function testControlWithTemplateFile()
  2878. {
  2879. $result = $this->Form->control('field', [
  2880. 'templates' => 'htmlhelper_tags'
  2881. ]);
  2882. $expected = [
  2883. 'label' => ['for' => 'field'],
  2884. 'Field',
  2885. '/label',
  2886. 'input' => [
  2887. 'type' => 'text', 'name' => 'field',
  2888. 'id' => 'field'
  2889. ],
  2890. ];
  2891. $this->assertHtml($expected, $result);
  2892. }
  2893. /**
  2894. * testNestedControlsEndWithBrackets method
  2895. *
  2896. * Test that nested inputs end with brackets.
  2897. *
  2898. * @return void
  2899. */
  2900. public function testNestedControlsEndWithBrackets()
  2901. {
  2902. $result = $this->Form->text('nested.text[]');
  2903. $expected = [
  2904. 'input' => [
  2905. 'type' => 'text', 'name' => 'nested[text][]'
  2906. ],
  2907. ];
  2908. $this->assertHtml($expected, $result);
  2909. $result = $this->Form->file('nested.file[]');
  2910. $expected = [
  2911. 'input' => [
  2912. 'type' => 'file', 'name' => 'nested[file][]'
  2913. ],
  2914. ];
  2915. $this->assertHtml($expected, $result);
  2916. }
  2917. /**
  2918. * testCreateIdPrefix method
  2919. *
  2920. * Test id prefix.
  2921. *
  2922. * @return void
  2923. */
  2924. public function testCreateIdPrefix()
  2925. {
  2926. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2927. $result = $this->Form->control('field');
  2928. $expected = [
  2929. 'div' => ['class' => 'input text'],
  2930. 'label' => ['for' => 'prefix-field'],
  2931. 'Field',
  2932. '/label',
  2933. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2934. '/div'
  2935. ];
  2936. $this->assertHtml($expected, $result);
  2937. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2938. $expected = [
  2939. 'div' => ['class' => 'input text'],
  2940. 'label' => ['for' => 'custom-id'],
  2941. 'Field',
  2942. '/label',
  2943. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2944. '/div'
  2945. ];
  2946. $this->assertHtml($expected, $result);
  2947. $result = $this->Form->radio('Model.field', ['option A']);
  2948. $expected = [
  2949. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2950. 'label' => ['for' => 'prefix-model-field-0'],
  2951. ['input' => [
  2952. 'type' => 'radio',
  2953. 'name' => 'Model[field]',
  2954. 'value' => '0',
  2955. 'id' => 'prefix-model-field-0'
  2956. ]],
  2957. 'option A',
  2958. '/label'
  2959. ];
  2960. $this->assertHtml($expected, $result);
  2961. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2962. $expected = [
  2963. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2964. 'label' => ['for' => 'prefix-model-field-0'],
  2965. ['input' => [
  2966. 'type' => 'radio',
  2967. 'name' => 'Model[field]',
  2968. 'value' => '0',
  2969. 'id' => 'prefix-model-field-0'
  2970. ]],
  2971. 'option A',
  2972. '/label'
  2973. ];
  2974. $this->assertHtml($expected, $result);
  2975. $result = $this->Form->select(
  2976. 'Model.multi_field',
  2977. ['first'],
  2978. ['multiple' => 'checkbox']
  2979. );
  2980. $expected = [
  2981. 'input' => [
  2982. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2983. ],
  2984. ['div' => ['class' => 'checkbox']],
  2985. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2986. ['input' => [
  2987. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2988. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2989. ]],
  2990. 'first',
  2991. '/label',
  2992. '/div',
  2993. ];
  2994. $this->assertHtml($expected, $result);
  2995. $this->Form->end();
  2996. $result = $this->Form->control('field');
  2997. $expected = [
  2998. 'div' => ['class' => 'input text'],
  2999. 'label' => ['for' => 'field'],
  3000. 'Field',
  3001. '/label',
  3002. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  3003. '/div'
  3004. ];
  3005. $this->assertHtml($expected, $result);
  3006. }
  3007. /**
  3008. * testControlZero method
  3009. *
  3010. * Test that inputs with 0 can be created.
  3011. *
  3012. * @return void
  3013. */
  3014. public function testControlZero()
  3015. {
  3016. TableRegistry::get('Contacts', [
  3017. 'className' => __NAMESPACE__ . '\ContactsTable'
  3018. ]);
  3019. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  3020. $result = $this->Form->control('0');
  3021. $expected = [
  3022. 'div' => ['class' => 'input text'],
  3023. 'label' => ['for' => '0'], '/label',
  3024. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  3025. '/div'
  3026. ];
  3027. $this->assertHtml($expected, $result);
  3028. }
  3029. /**
  3030. * testControlCheckbox method
  3031. *
  3032. * Test control() with checkbox creation.
  3033. *
  3034. * @return void
  3035. */
  3036. public function testControlCheckbox()
  3037. {
  3038. $articles = TableRegistry::get('Articles');
  3039. $articles->schema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3040. $article = $articles->newEntity();
  3041. $this->Form->create($article);
  3042. $result = $this->Form->control('Articles.active');
  3043. $expected = [
  3044. 'div' => ['class' => 'input checkbox'],
  3045. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3046. 'label' => ['for' => 'articles-active'],
  3047. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3048. 'Active',
  3049. '/label',
  3050. '/div'
  3051. ];
  3052. $this->assertHtml($expected, $result);
  3053. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3054. $expected = [
  3055. 'div' => ['class' => 'input checkbox'],
  3056. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3057. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3058. '/div'
  3059. ];
  3060. $this->assertHtml($expected, $result);
  3061. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3062. $expected = [
  3063. 'div' => ['class' => 'input checkbox'],
  3064. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3065. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3066. '/div'
  3067. ];
  3068. $this->assertHtml($expected, $result);
  3069. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3070. $expected = [
  3071. 'div' => ['class' => 'input checkbox'],
  3072. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3073. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3074. '/div'
  3075. ];
  3076. $this->assertHtml($expected, $result);
  3077. $result = $this->Form->control('Articles.disabled', [
  3078. 'label' => 'Disabled',
  3079. 'type' => 'checkbox',
  3080. 'data-foo' => 'disabled'
  3081. ]);
  3082. $expected = [
  3083. 'div' => ['class' => 'input checkbox'],
  3084. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3085. 'label' => ['for' => 'articles-disabled'],
  3086. ['input' => [
  3087. 'type' => 'checkbox',
  3088. 'name' => 'Articles[disabled]',
  3089. 'value' => '1',
  3090. 'id' => 'articles-disabled',
  3091. 'data-foo' => 'disabled'
  3092. ]],
  3093. 'Disabled',
  3094. '/label',
  3095. '/div'
  3096. ];
  3097. $this->assertHtml($expected, $result);
  3098. $result = $this->Form->control('Articles.confirm', [
  3099. 'label' => 'Confirm <b>me</b>!',
  3100. 'type' => 'checkbox',
  3101. 'escape' => false
  3102. ]);
  3103. $expected = [
  3104. 'div' => ['class' => 'input checkbox'],
  3105. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3106. 'label' => ['for' => 'articles-confirm'],
  3107. ['input' => [
  3108. 'type' => 'checkbox',
  3109. 'name' => 'Articles[confirm]',
  3110. 'value' => '1',
  3111. 'id' => 'articles-confirm',
  3112. ]],
  3113. 'Confirm <b>me</b>!',
  3114. '/label',
  3115. '/div'
  3116. ];
  3117. $this->assertHtml($expected, $result);
  3118. }
  3119. /**
  3120. * testControlHidden method
  3121. *
  3122. * Test that control() does not create wrapping div and label tag for hidden fields.
  3123. *
  3124. * @return void
  3125. */
  3126. public function testControlHidden()
  3127. {
  3128. TableRegistry::get('ValidateUsers', [
  3129. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3130. ]);
  3131. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3132. $result = $this->Form->control('ValidateUser.id');
  3133. $expected = [
  3134. 'input' => ['name', 'type' => 'hidden', 'id']
  3135. ];
  3136. $this->assertHtml($expected, $result);
  3137. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3138. $expected = [
  3139. 'input' => ['name', 'type' => 'hidden', 'id']
  3140. ];
  3141. $this->assertHtml($expected, $result);
  3142. }
  3143. /**
  3144. * testControlDatetime method
  3145. *
  3146. * Test form->control() with datetime.
  3147. *
  3148. * @return void
  3149. */
  3150. public function testControlDatetime()
  3151. {
  3152. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3153. ->setMethods(['datetime'])
  3154. ->setConstructorArgs([new View()])
  3155. ->getMock();
  3156. $this->Form->expects($this->once())->method('datetime')
  3157. ->with('prueba', [
  3158. 'type' => 'datetime',
  3159. 'timeFormat' => 24,
  3160. 'minYear' => 2008,
  3161. 'maxYear' => 2011,
  3162. 'interval' => 15,
  3163. 'options' => null,
  3164. 'empty' => false,
  3165. 'id' => 'prueba',
  3166. 'required' => false,
  3167. 'templateVars' => []
  3168. ])
  3169. ->will($this->returnValue('This is it!'));
  3170. $result = $this->Form->control('prueba', [
  3171. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3172. 'maxYear' => 2011, 'interval' => 15
  3173. ]);
  3174. $expected = [
  3175. 'div' => ['class' => 'input datetime'],
  3176. '<label',
  3177. 'Prueba',
  3178. '/label',
  3179. 'This is it!',
  3180. '/div'
  3181. ];
  3182. $this->assertHtml($expected, $result);
  3183. }
  3184. /**
  3185. * testControlDatetimeIdPrefix method
  3186. *
  3187. * Test form->control() with datetime with id prefix.
  3188. *
  3189. * @return void
  3190. */
  3191. public function testControlDatetimeIdPrefix()
  3192. {
  3193. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3194. ->setMethods(['datetime'])
  3195. ->setConstructorArgs([new View()])
  3196. ->getMock();
  3197. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3198. $this->Form->expects($this->once())->method('datetime')
  3199. ->with('prueba', [
  3200. 'type' => 'datetime',
  3201. 'timeFormat' => 24,
  3202. 'minYear' => 2008,
  3203. 'maxYear' => 2011,
  3204. 'interval' => 15,
  3205. 'options' => null,
  3206. 'empty' => false,
  3207. 'id' => 'prefix-prueba',
  3208. 'required' => false,
  3209. 'templateVars' => []
  3210. ])
  3211. ->will($this->returnValue('This is it!'));
  3212. $result = $this->Form->control('prueba', [
  3213. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3214. 'maxYear' => 2011, 'interval' => 15
  3215. ]);
  3216. $expected = [
  3217. 'div' => ['class' => 'input datetime'],
  3218. '<label',
  3219. 'Prueba',
  3220. '/label',
  3221. 'This is it!',
  3222. '/div'
  3223. ];
  3224. $this->assertHtml($expected, $result);
  3225. }
  3226. /**
  3227. * testControlCheckboxWithDisabledElements method
  3228. *
  3229. * Test generating checkboxes with disabled elements.
  3230. *
  3231. * @return void
  3232. */
  3233. public function testControlCheckboxWithDisabledElements()
  3234. {
  3235. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3236. $result = $this->Form->control('Contact.multiple', [
  3237. 'multiple' => 'checkbox',
  3238. 'disabled' => 'disabled',
  3239. 'options' => $options
  3240. ]);
  3241. $expected = [
  3242. ['div' => ['class' => 'input select']],
  3243. ['label' => ['for' => 'contact-multiple']],
  3244. 'Multiple',
  3245. '/label',
  3246. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3247. ['div' => ['class' => 'checkbox']],
  3248. ['label' => ['for' => 'contact-multiple-1']],
  3249. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3250. 'One',
  3251. '/label',
  3252. '/div',
  3253. ['div' => ['class' => 'checkbox']],
  3254. ['label' => ['for' => 'contact-multiple-2']],
  3255. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3256. 'Two',
  3257. '/label',
  3258. '/div',
  3259. ['div' => ['class' => 'checkbox']],
  3260. ['label' => ['for' => 'contact-multiple-3']],
  3261. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3262. 'Three',
  3263. '/label',
  3264. '/div',
  3265. '/div'
  3266. ];
  3267. $this->assertHtml($expected, $result);
  3268. // make sure 50 does only disable 50, and not 50f5c0cf
  3269. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3270. $disabled = [50];
  3271. $expected = [
  3272. ['div' => ['class' => 'input select']],
  3273. ['label' => ['for' => 'contact-multiple']],
  3274. 'Multiple',
  3275. '/label',
  3276. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3277. ['div' => ['class' => 'checkbox']],
  3278. ['label' => ['for' => 'contact-multiple-50']],
  3279. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3280. 'Fifty',
  3281. '/label',
  3282. '/div',
  3283. ['div' => ['class' => 'checkbox']],
  3284. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3285. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3286. 'Stringy',
  3287. '/label',
  3288. '/div',
  3289. '/div'
  3290. ];
  3291. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3292. $this->assertHtml($expected, $result);
  3293. }
  3294. /**
  3295. * testControlWithLeadingInteger method
  3296. *
  3297. * Test input name with leading integer, ensure attributes are generated correctly.
  3298. *
  3299. * @return void
  3300. */
  3301. public function testControlWithLeadingInteger()
  3302. {
  3303. $result = $this->Form->text('0.Node.title');
  3304. $expected = [
  3305. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3306. ];
  3307. $this->assertHtml($expected, $result);
  3308. }
  3309. /**
  3310. * testControlSelectType method
  3311. *
  3312. * Test form->control() with select type inputs.
  3313. *
  3314. * @return void
  3315. */
  3316. public function testControlSelectType()
  3317. {
  3318. $result = $this->Form->control(
  3319. 'email',
  3320. [
  3321. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3322. );
  3323. $expected = [
  3324. 'div' => ['class' => 'input select'],
  3325. 'label' => ['for' => 'email'],
  3326. 'Email',
  3327. '/label',
  3328. ['select' => ['name' => 'email', 'id' => 'email']],
  3329. ['option' => ['value' => '']],
  3330. '/option',
  3331. ['option' => ['value' => 'è']],
  3332. 'Firést',
  3333. '/option',
  3334. ['option' => ['value' => 'é']],
  3335. 'Secoènd',
  3336. '/option',
  3337. '/select',
  3338. '/div'
  3339. ];
  3340. $this->assertHtml($expected, $result);
  3341. $result = $this->Form->control(
  3342. 'email',
  3343. [
  3344. 'options' => ['First', 'Second'], 'empty' => true]
  3345. );
  3346. $expected = [
  3347. 'div' => ['class' => 'input select'],
  3348. 'label' => ['for' => 'email'],
  3349. 'Email',
  3350. '/label',
  3351. ['select' => ['name' => 'email', 'id' => 'email']],
  3352. ['option' => ['value' => '']],
  3353. '/option',
  3354. ['option' => ['value' => '0']],
  3355. 'First',
  3356. '/option',
  3357. ['option' => ['value' => '1']],
  3358. 'Second',
  3359. '/option',
  3360. '/select',
  3361. '/div'
  3362. ];
  3363. $this->assertHtml($expected, $result);
  3364. $result = $this->Form->control('email', [
  3365. 'type' => 'select',
  3366. 'options' => new \ArrayObject(['First', 'Second']),
  3367. 'empty' => true
  3368. ]);
  3369. $this->assertHtml($expected, $result);
  3370. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3371. $this->Form->request->data = ['Model' => ['user_id' => 'value']];
  3372. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3373. $expected = [
  3374. 'div' => ['class' => 'input select'],
  3375. 'label' => ['for' => 'model-user-id'],
  3376. 'User',
  3377. '/label',
  3378. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3379. ['option' => ['value' => '']],
  3380. '/option',
  3381. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3382. 'good',
  3383. '/option',
  3384. ['option' => ['value' => 'other']],
  3385. 'bad',
  3386. '/option',
  3387. '/select',
  3388. '/div'
  3389. ];
  3390. $this->assertHtml($expected, $result);
  3391. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3392. $this->Form->request->data = ['Thing' => ['user_id' => null]];
  3393. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3394. $expected = [
  3395. 'div' => ['class' => 'input select'],
  3396. 'label' => ['for' => 'thing-user-id'],
  3397. 'User',
  3398. '/label',
  3399. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3400. ['option' => ['value' => '']],
  3401. 'Some Empty',
  3402. '/option',
  3403. ['option' => ['value' => 'value']],
  3404. 'good',
  3405. '/option',
  3406. ['option' => ['value' => 'other']],
  3407. 'bad',
  3408. '/option',
  3409. '/select',
  3410. '/div'
  3411. ];
  3412. $this->assertHtml($expected, $result);
  3413. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3414. $this->Form->request->data = ['Thing' => ['user_id' => 'value']];
  3415. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3416. $expected = [
  3417. 'div' => ['class' => 'input select'],
  3418. 'label' => ['for' => 'thing-user-id'],
  3419. 'User',
  3420. '/label',
  3421. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3422. ['option' => ['value' => '']],
  3423. 'Some Empty',
  3424. '/option',
  3425. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3426. 'good',
  3427. '/option',
  3428. ['option' => ['value' => 'other']],
  3429. 'bad',
  3430. '/option',
  3431. '/select',
  3432. '/div'
  3433. ];
  3434. $this->assertHtml($expected, $result);
  3435. $this->Form->data = [];
  3436. $result = $this->Form->control('Publisher.id', [
  3437. 'label' => 'Publisher',
  3438. 'type' => 'select',
  3439. 'multiple' => 'checkbox',
  3440. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3441. ]);
  3442. $expected = [
  3443. ['div' => ['class' => 'input select']],
  3444. ['label' => ['for' => 'publisher-id']],
  3445. 'Publisher',
  3446. '/label',
  3447. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3448. ['div' => ['class' => 'checkbox']],
  3449. ['label' => ['for' => 'publisher-id-value-1']],
  3450. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3451. 'Label 1',
  3452. '/label',
  3453. '/div',
  3454. ['div' => ['class' => 'checkbox']],
  3455. ['label' => ['for' => 'publisher-id-value-2']],
  3456. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3457. 'Label 2',
  3458. '/label',
  3459. '/div',
  3460. '/div'
  3461. ];
  3462. $this->assertHtml($expected, $result);
  3463. }
  3464. /**
  3465. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3466. *
  3467. * Test that control() and a non standard primary key makes a hidden input by default.
  3468. *
  3469. * @return void
  3470. */
  3471. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3472. {
  3473. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3474. $this->Form->create($this->article);
  3475. $result = $this->Form->control('title');
  3476. $expected = [
  3477. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3478. ];
  3479. $this->assertHtml($expected, $result);
  3480. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3481. $this->Form->create($this->article);
  3482. $result = $this->Form->control('title');
  3483. $expected = [
  3484. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3485. ];
  3486. $this->assertHtml($expected, $result);
  3487. $result = $this->Form->control('body');
  3488. $expected = [
  3489. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3490. ];
  3491. $this->assertHtml($expected, $result);
  3492. }
  3493. /**
  3494. * testControlOverridingMagicSelectType method
  3495. *
  3496. * Test that overriding the magic select type widget is possible.
  3497. *
  3498. * @return void
  3499. */
  3500. public function testControlOverridingMagicSelectType()
  3501. {
  3502. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3503. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3504. $expected = [
  3505. 'div' => ['class' => 'input text'],
  3506. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3507. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3508. '/div'
  3509. ];
  3510. $this->assertHtml($expected, $result);
  3511. //Check that magic types still work for plural/singular vars
  3512. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3513. $result = $this->Form->control('Model.type');
  3514. $expected = [
  3515. 'div' => ['class' => 'input select'],
  3516. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3517. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3518. ['option' => ['value' => 'value']], 'good', '/option',
  3519. ['option' => ['value' => 'other']], 'bad', '/option',
  3520. '/select',
  3521. '/div'
  3522. ];
  3523. $this->assertHtml($expected, $result);
  3524. }
  3525. /**
  3526. * testControlMagicTypeDoesNotOverride method
  3527. *
  3528. * Test that inferred types do not override developer input.
  3529. *
  3530. * @return void
  3531. */
  3532. public function testControlMagicTypeDoesNotOverride()
  3533. {
  3534. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3535. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3536. $expected = [
  3537. 'div' => ['class' => 'input checkbox'],
  3538. ['input' => [
  3539. 'type' => 'hidden',
  3540. 'name' => 'Model[user]',
  3541. 'value' => 0,
  3542. ]],
  3543. 'label' => ['for' => 'model-user'],
  3544. ['input' => [
  3545. 'name' => 'Model[user]',
  3546. 'type' => 'checkbox',
  3547. 'id' => 'model-user',
  3548. 'value' => 1
  3549. ]],
  3550. 'User',
  3551. '/label',
  3552. '/div'
  3553. ];
  3554. $this->assertHtml($expected, $result);
  3555. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3556. $options = [
  3557. 1 => 'blue',
  3558. 2 => 'red'
  3559. ];
  3560. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3561. $expected = [
  3562. 'div' => ['class' => 'input select'],
  3563. 'label' => ['for' => 'tags-ids'],
  3564. 'Tags',
  3565. '/label',
  3566. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3567. ['div' => ['class' => 'checkbox']],
  3568. ['label' => ['for' => 'tags-ids-1']],
  3569. ['input' => [
  3570. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3571. 'value' => '1', 'name' => 'tags[_ids][]'
  3572. ]],
  3573. 'blue',
  3574. '/label',
  3575. '/div',
  3576. ['div' => ['class' => 'checkbox']],
  3577. ['label' => ['for' => 'tags-ids-2']],
  3578. ['input' => [
  3579. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3580. 'value' => '2', 'name' => 'tags[_ids][]'
  3581. ]],
  3582. 'red',
  3583. '/label',
  3584. '/div',
  3585. '/div'
  3586. ];
  3587. $this->assertHtml($expected, $result);
  3588. }
  3589. /**
  3590. * testControlMagicSelectForTypeNumber method
  3591. *
  3592. * Test that magic control() selects are created for type=number.
  3593. *
  3594. * @return void
  3595. */
  3596. public function testControlMagicSelectForTypeNumber()
  3597. {
  3598. TableRegistry::get('ValidateUsers', [
  3599. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3600. ]);
  3601. $entity = new Entity(['balance' => 1]);
  3602. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3603. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3604. $result = $this->Form->control('balance');
  3605. $expected = [
  3606. 'div' => ['class' => 'input select'],
  3607. 'label' => ['for' => 'balance'],
  3608. 'Balance',
  3609. '/label',
  3610. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3611. ['option' => ['value' => '0']],
  3612. 'nothing',
  3613. '/option',
  3614. ['option' => ['value' => '1', 'selected' => 'selected']],
  3615. 'some',
  3616. '/option',
  3617. ['option' => ['value' => '100']],
  3618. 'a lot',
  3619. '/option',
  3620. '/select',
  3621. '/div'
  3622. ];
  3623. $this->assertHtml($expected, $result);
  3624. }
  3625. /**
  3626. * testInvalidControlTypeOption method
  3627. *
  3628. * Test invalid 'input' type option to control() function.
  3629. *
  3630. * @expectedException \RuntimeException
  3631. * @expectedExceptionMessage Invalid type 'input' used for field 'text'
  3632. * @return void
  3633. */
  3634. public function testInvalidControlTypeOption()
  3635. {
  3636. $this->Form->control('text', ['type' => 'input']);
  3637. }
  3638. /**
  3639. * testControlMagicSelectChangeToRadio method
  3640. *
  3641. * Test that magic control() selects can easily be converted into radio types without error.
  3642. *
  3643. * @return void
  3644. */
  3645. public function testControlMagicSelectChangeToRadio()
  3646. {
  3647. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3648. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3649. $this->assertContains('input type="radio"', $result);
  3650. }
  3651. /**
  3652. * testFormControlSubmit method
  3653. *
  3654. * Test correct results for form::control() and type submit.
  3655. *
  3656. * @return void
  3657. */
  3658. public function testFormControlSubmit()
  3659. {
  3660. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3661. $expected = [
  3662. 'div' => ['class' => 'submit'],
  3663. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3664. '/div'
  3665. ];
  3666. $this->assertHtml($expected, $result);
  3667. }
  3668. /**
  3669. * testFormControls method
  3670. *
  3671. * Test correct results from Form::controls().
  3672. *
  3673. * @return void
  3674. */
  3675. public function testFormControlsLegendFieldset()
  3676. {
  3677. $this->Form->create($this->article);
  3678. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3679. $expected = [
  3680. '<fieldset',
  3681. '<legend',
  3682. 'The Legend',
  3683. '/legend',
  3684. '*/fieldset',
  3685. ];
  3686. $this->assertHtml($expected, $result);
  3687. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3688. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3689. $this->assertContains('<fieldset>', $result);
  3690. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3691. $this->assertNotContains('<legend>', $result);
  3692. $this->assertNotContains('<fieldset>', $result);
  3693. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3694. $this->assertNotContains('<legend>', $result);
  3695. $this->assertNotContains('<fieldset>', $result);
  3696. $this->Form->create($this->article);
  3697. $this->Form->request->params['prefix'] = 'admin';
  3698. $this->Form->request->params['action'] = 'admin_edit';
  3699. $this->Form->request->params['controller'] = 'articles';
  3700. $result = $this->Form->allControls();
  3701. $expected = [
  3702. '<fieldset',
  3703. '<legend',
  3704. 'New Article',
  3705. '/legend',
  3706. '*/fieldset',
  3707. ];
  3708. $this->assertHtml($expected, $result);
  3709. $this->Form->create($this->article);
  3710. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3711. $expected = [
  3712. '<fieldset',
  3713. '<legend',
  3714. 'The Legend',
  3715. '/legend',
  3716. '*/fieldset',
  3717. ];
  3718. $this->assertHtml($expected, $result);
  3719. $this->Form->create($this->article);
  3720. $result = $this->Form->allControls([], [
  3721. 'fieldset' => [
  3722. 'class' => 'some-class some-other-class',
  3723. 'disabled' => true,
  3724. 'data-param' => 'a-param'
  3725. ],
  3726. 'legend' => 'The Legend'
  3727. ]);
  3728. $expected = [
  3729. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3730. '<legend',
  3731. 'The Legend',
  3732. '/legend',
  3733. '*/fieldset',
  3734. ];
  3735. $this->assertHtml($expected, $result);
  3736. }
  3737. /**
  3738. * testFormControls method
  3739. *
  3740. * Test the controls() method.
  3741. *
  3742. * @return void
  3743. */
  3744. public function testFormControls()
  3745. {
  3746. $this->Form->create($this->article);
  3747. $result = $this->Form->allControls();
  3748. $expected = [
  3749. '<fieldset',
  3750. '<legend', 'New Article', '/legend',
  3751. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3752. ['div' => ['class' => 'input select required']],
  3753. '*/div',
  3754. ['div' => ['class' => 'input text required']],
  3755. '*/div',
  3756. ['div' => ['class' => 'input text']],
  3757. '*/div',
  3758. ['div' => ['class' => 'input text']],
  3759. '*/div',
  3760. '/fieldset',
  3761. ];
  3762. $this->assertHtml($expected, $result);
  3763. $result = $this->Form->allControls([
  3764. 'published' => ['type' => 'boolean']
  3765. ]);
  3766. $expected = [
  3767. '<fieldset',
  3768. '<legend', 'New Article', '/legend',
  3769. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3770. ['div' => ['class' => 'input select required']],
  3771. '*/div',
  3772. ['div' => ['class' => 'input text required']],
  3773. '*/div',
  3774. ['div' => ['class' => 'input text']],
  3775. '*/div',
  3776. ['div' => ['class' => 'input boolean']],
  3777. '*/div',
  3778. '/fieldset',
  3779. ];
  3780. $this->assertHtml($expected, $result);
  3781. $this->Form->create($this->article);
  3782. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3783. $expected = [
  3784. 'fieldset' => [],
  3785. 'legend' => [],
  3786. 'Hello',
  3787. '/legend',
  3788. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3789. ['div' => ['class' => 'input select required']],
  3790. '*/div',
  3791. ['div' => ['class' => 'input text required']],
  3792. '*/div',
  3793. ['div' => ['class' => 'input text']],
  3794. '*/div',
  3795. ['div' => ['class' => 'input text']],
  3796. '*/div',
  3797. '/fieldset'
  3798. ];
  3799. $this->assertHtml($expected, $result);
  3800. $this->Form->create(false);
  3801. $expected = [
  3802. 'fieldset' => [],
  3803. ['div' => ['class' => 'input text']],
  3804. 'label' => ['for' => 'foo'],
  3805. 'Foo',
  3806. '/label',
  3807. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3808. '*/div',
  3809. '/fieldset'
  3810. ];
  3811. $result = $this->Form->allControls(
  3812. ['foo' => ['type' => 'text']],
  3813. ['legend' => false]
  3814. );
  3815. $this->assertHtml($expected, $result);
  3816. }
  3817. /**
  3818. * testFormControlsBlacklist method
  3819. *
  3820. * @return void
  3821. */
  3822. public function testFormControlsBlacklist()
  3823. {
  3824. $this->Form->create($this->article);
  3825. $result = $this->Form->allControls([
  3826. 'id' => false
  3827. ]);
  3828. $expected = [
  3829. '<fieldset',
  3830. '<legend', 'New Article', '/legend',
  3831. ['div' => ['class' => 'input select required']],
  3832. '*/div',
  3833. ['div' => ['class' => 'input text required']],
  3834. '*/div',
  3835. ['div' => ['class' => 'input text']],
  3836. '*/div',
  3837. ['div' => ['class' => 'input text']],
  3838. '*/div',
  3839. '/fieldset',
  3840. ];
  3841. $this->assertHtml($expected, $result);
  3842. $this->Form->create($this->article);
  3843. $result = $this->Form->allControls([
  3844. 'id' => []
  3845. ]);
  3846. $expected = [
  3847. '<fieldset',
  3848. '<legend', 'New Article', '/legend',
  3849. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3850. ['div' => ['class' => 'input select required']],
  3851. '*/div',
  3852. ['div' => ['class' => 'input text required']],
  3853. '*/div',
  3854. ['div' => ['class' => 'input text']],
  3855. '*/div',
  3856. ['div' => ['class' => 'input text']],
  3857. '*/div',
  3858. '/fieldset',
  3859. ];
  3860. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3861. }
  3862. /**
  3863. * testSelectAsCheckbox method
  3864. *
  3865. * Test multi-select widget with checkbox formatting.
  3866. *
  3867. * @return void
  3868. */
  3869. public function testSelectAsCheckbox()
  3870. {
  3871. $result = $this->Form->select(
  3872. 'Model.multi_field',
  3873. ['first', 'second', 'third'],
  3874. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3875. );
  3876. $expected = [
  3877. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3878. ['div' => ['class' => 'checkbox']],
  3879. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3880. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3881. 'first',
  3882. '/label',
  3883. '/div',
  3884. ['div' => ['class' => 'checkbox']],
  3885. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3886. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3887. 'second',
  3888. '/label',
  3889. '/div',
  3890. ['div' => ['class' => 'checkbox']],
  3891. ['label' => ['for' => 'model-multi-field-2']],
  3892. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3893. 'third',
  3894. '/label',
  3895. '/div',
  3896. ];
  3897. $this->assertHtml($expected, $result);
  3898. $result = $this->Form->select(
  3899. 'Model.multi_field',
  3900. ['1/2' => 'half'],
  3901. ['multiple' => 'checkbox']
  3902. );
  3903. $expected = [
  3904. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3905. ['div' => ['class' => 'checkbox']],
  3906. ['label' => ['for' => 'model-multi-field-1-2']],
  3907. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3908. 'half',
  3909. '/label',
  3910. '/div',
  3911. ];
  3912. $this->assertHtml($expected, $result);
  3913. }
  3914. /**
  3915. * testLabel method
  3916. *
  3917. * Test label generation.
  3918. *
  3919. * @return void
  3920. */
  3921. public function testLabel()
  3922. {
  3923. $result = $this->Form->label('Person.name');
  3924. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3925. $this->assertHtml($expected, $result);
  3926. $result = $this->Form->label('Person.name');
  3927. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3928. $this->assertHtml($expected, $result);
  3929. $result = $this->Form->label('Person.first_name');
  3930. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3931. $this->assertHtml($expected, $result);
  3932. $result = $this->Form->label('Person.first_name', 'Your first name');
  3933. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3934. $this->assertHtml($expected, $result);
  3935. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3936. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3937. $this->assertHtml($expected, $result);
  3938. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3939. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3940. $this->assertHtml($expected, $result);
  3941. $result = $this->Form->label('Person.first_name', '');
  3942. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3943. $this->assertHtml($expected, $result);
  3944. $result = $this->Form->label('Person.2.name', '');
  3945. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3946. $this->assertHtml($expected, $result);
  3947. }
  3948. /**
  3949. * testLabelContainControl method
  3950. *
  3951. * Test that label() can accept an input with the correct template vars.
  3952. *
  3953. * @return void
  3954. */
  3955. public function testLabelContainControl()
  3956. {
  3957. $this->Form->templates([
  3958. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3959. ]);
  3960. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3961. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3962. ]);
  3963. $expected = [
  3964. 'label' => ['for' => 'person-accept-terms'],
  3965. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3966. 'Accept',
  3967. '/label',
  3968. ];
  3969. $this->assertHtml($expected, $result);
  3970. }
  3971. /**
  3972. * testTextbox method
  3973. *
  3974. * Test textbox element generation.
  3975. *
  3976. * @return void
  3977. */
  3978. public function testTextbox()
  3979. {
  3980. $result = $this->Form->text('Model.field');
  3981. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3982. $this->assertHtml($expected, $result);
  3983. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3984. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3985. $this->assertHtml($expected, $result);
  3986. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3987. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3988. $this->assertHtml($expected, $result);
  3989. }
  3990. /**
  3991. * testTextBoxDataAndError method
  3992. *
  3993. * Test that text() hooks up with request data and error fields.
  3994. *
  3995. * @return void
  3996. */
  3997. public function testTextBoxDataAndError()
  3998. {
  3999. $this->article['errors'] = [
  4000. 'Contact' => ['text' => 'wrong']
  4001. ];
  4002. $this->Form->create($this->article);
  4003. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  4004. $result = $this->Form->text('Model.text');
  4005. $expected = [
  4006. 'input' => [
  4007. 'type' => 'text',
  4008. 'name' => 'Model[text]',
  4009. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  4010. ]
  4011. ];
  4012. $this->assertHtml($expected, $result);
  4013. $this->Form->request->data['Contact']['text'] = 'test';
  4014. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  4015. $expected = [
  4016. 'input' => [
  4017. 'type' => 'text',
  4018. 'name' => 'Contact[text]',
  4019. 'value' => 'test',
  4020. 'id' => 'theID',
  4021. 'class' => 'form-error'
  4022. ]
  4023. ];
  4024. $this->assertHtml($expected, $result);
  4025. }
  4026. /**
  4027. * testDefaultValue method
  4028. *
  4029. * Test default value setting.
  4030. *
  4031. * @return void
  4032. */
  4033. public function testTextDefaultValue()
  4034. {
  4035. $this->Form->request->data['Model']['field'] = 'test';
  4036. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4037. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  4038. $this->assertHtml($expected, $result);
  4039. unset($this->Form->request->data['Model']['field']);
  4040. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4041. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4042. $this->assertHtml($expected, $result);
  4043. $Articles = TableRegistry::get('Articles');
  4044. $title = $Articles->schema()->column('title');
  4045. $Articles->schema()->addColumn(
  4046. 'title',
  4047. ['default' => 'default title'] + $title
  4048. );
  4049. $entity = $Articles->newEntity();
  4050. $this->Form->create($entity);
  4051. // Get default value from schema
  4052. $result = $this->Form->text('title');
  4053. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4054. $this->assertHtml($expected, $result);
  4055. // Don't get value from schema
  4056. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4057. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4058. $this->assertHtml($expected, $result);
  4059. // Custom default value overrides default value from schema
  4060. $result = $this->Form->text('title', ['default' => 'override default']);
  4061. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4062. $this->assertHtml($expected, $result);
  4063. // Default value from schema is used only for new entities.
  4064. $entity->isNew(false);
  4065. $result = $this->Form->text('title');
  4066. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4067. $this->assertHtml($expected, $result);
  4068. }
  4069. /**
  4070. * testError method
  4071. *
  4072. * Test field error generation.
  4073. *
  4074. * @return void
  4075. */
  4076. public function testError()
  4077. {
  4078. $this->article['errors'] = [
  4079. 'Article' => ['field' => 'email']
  4080. ];
  4081. $this->Form->create($this->article);
  4082. $result = $this->Form->error('Article.field');
  4083. $expected = [
  4084. ['div' => ['class' => 'error-message']],
  4085. 'email',
  4086. '/div',
  4087. ];
  4088. $this->assertHtml($expected, $result);
  4089. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4090. $expected = [
  4091. ['div' => ['class' => 'error-message']],
  4092. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4093. '/div',
  4094. ];
  4095. $this->assertHtml($expected, $result);
  4096. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4097. $expected = [
  4098. ['div' => ['class' => 'error-message']],
  4099. '<strong', 'Badness!', '/strong',
  4100. '/div',
  4101. ];
  4102. $this->assertHtml($expected, $result);
  4103. }
  4104. /**
  4105. * testErrorRuleName method
  4106. *
  4107. * Test error translation can use rule names for translating.
  4108. *
  4109. * @return void
  4110. */
  4111. public function testErrorRuleName()
  4112. {
  4113. $this->article['errors'] = [
  4114. 'Article' => [
  4115. 'field' => ['email' => 'Your email was not good']
  4116. ]
  4117. ];
  4118. $this->Form->create($this->article);
  4119. $result = $this->Form->error('Article.field');
  4120. $expected = [
  4121. ['div' => ['class' => 'error-message']],
  4122. 'Your email was not good',
  4123. '/div',
  4124. ];
  4125. $this->assertHtml($expected, $result);
  4126. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4127. $expected = [
  4128. ['div' => ['class' => 'error-message']],
  4129. 'Email in use',
  4130. '/div',
  4131. ];
  4132. $this->assertHtml($expected, $result);
  4133. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4134. $expected = [
  4135. ['div' => ['class' => 'error-message']],
  4136. 'Email in use',
  4137. '/div',
  4138. ];
  4139. $this->assertHtml($expected, $result);
  4140. $result = $this->Form->error('Article.field', [
  4141. 'email' => 'Key is preferred',
  4142. 'Your email was not good' => 'Email in use'
  4143. ]);
  4144. $expected = [
  4145. ['div' => ['class' => 'error-message']],
  4146. 'Key is preferred',
  4147. '/div',
  4148. ];
  4149. $this->assertHtml($expected, $result);
  4150. }
  4151. /**
  4152. * testErrorMessages method
  4153. *
  4154. * Test error with nested lists.
  4155. *
  4156. * @return void
  4157. */
  4158. public function testErrorMessages()
  4159. {
  4160. $this->article['errors'] = [
  4161. 'Article' => ['field' => 'email']
  4162. ];
  4163. $this->Form->create($this->article);
  4164. $result = $this->Form->error('Article.field', [
  4165. 'email' => 'No good!'
  4166. ]);
  4167. $expected = [
  4168. 'div' => ['class' => 'error-message'],
  4169. 'No good!',
  4170. '/div'
  4171. ];
  4172. $this->assertHtml($expected, $result);
  4173. }
  4174. /**
  4175. * testErrorMultipleMessages method
  4176. *
  4177. * Test error() with multiple messages.
  4178. *
  4179. * @return void
  4180. */
  4181. public function testErrorMultipleMessages()
  4182. {
  4183. $this->article['errors'] = [
  4184. 'field' => ['notBlank', 'email', 'Something else']
  4185. ];
  4186. $this->Form->create($this->article);
  4187. $result = $this->Form->error('field', [
  4188. 'notBlank' => 'Cannot be empty',
  4189. 'email' => 'No good!'
  4190. ]);
  4191. $expected = [
  4192. 'div' => ['class' => 'error-message'],
  4193. 'ul' => [],
  4194. '<li', 'Cannot be empty', '/li',
  4195. '<li', 'No good!', '/li',
  4196. '<li', 'Something else', '/li',
  4197. '/ul',
  4198. '/div'
  4199. ];
  4200. $this->assertHtml($expected, $result);
  4201. }
  4202. /**
  4203. * testPassword method
  4204. *
  4205. * Test password element generation.
  4206. *
  4207. * @return void
  4208. */
  4209. public function testPassword()
  4210. {
  4211. $this->article['errors'] = [
  4212. 'Contact' => [
  4213. 'passwd' => 1
  4214. ]
  4215. ];
  4216. $this->Form->create($this->article);
  4217. $result = $this->Form->password('Contact.field');
  4218. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4219. $this->assertHtml($expected, $result);
  4220. $this->Form->request->data['Contact']['passwd'] = 'test';
  4221. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4222. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4223. $this->assertHtml($expected, $result);
  4224. }
  4225. /**
  4226. * testRadio method
  4227. *
  4228. * Test radio element set generation.
  4229. *
  4230. * @return void
  4231. */
  4232. public function testRadio()
  4233. {
  4234. $result = $this->Form->radio('Model.field', ['option A']);
  4235. $expected = [
  4236. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4237. 'label' => ['for' => 'model-field-0'],
  4238. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4239. 'option A',
  4240. '/label'
  4241. ];
  4242. $this->assertHtml($expected, $result);
  4243. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4244. $this->assertHtml($expected, $result);
  4245. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4246. $expected = [
  4247. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4248. ['label' => ['for' => 'model-field-0']],
  4249. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4250. 'option A',
  4251. '/label',
  4252. ['label' => ['for' => 'model-field-1']],
  4253. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4254. 'option B',
  4255. '/label',
  4256. ];
  4257. $this->assertHtml($expected, $result);
  4258. $result = $this->Form->radio(
  4259. 'Employee.gender',
  4260. ['male' => 'Male', 'female' => 'Female'],
  4261. ['form' => 'my-form']
  4262. );
  4263. $expected = [
  4264. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4265. ['label' => ['for' => 'employee-gender-male']],
  4266. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4267. 'Male',
  4268. '/label',
  4269. ['label' => ['for' => 'employee-gender-female']],
  4270. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4271. 'Female',
  4272. '/label',
  4273. ];
  4274. $this->assertHtml($expected, $result);
  4275. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4276. $expected = [
  4277. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4278. ['label' => ['for' => 'model-custom-0']],
  4279. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4280. 'option A',
  4281. '/label',
  4282. ['label' => ['for' => 'model-custom-1']],
  4283. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4284. 'option B',
  4285. '/label',
  4286. ];
  4287. $this->assertHtml($expected, $result);
  4288. $result = $this->Form->radio(
  4289. 'Employee.gender',
  4290. [
  4291. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4292. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4293. ]
  4294. );
  4295. $expected = [
  4296. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4297. ['label' => ['for' => 'employee-gender-male']],
  4298. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4299. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4300. 'Male',
  4301. '/label',
  4302. ['label' => ['for' => 'employee-gender-female']],
  4303. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4304. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4305. 'Female',
  4306. '/label',
  4307. ];
  4308. $this->assertHtml($expected, $result);
  4309. }
  4310. /**
  4311. * Test radio with complex options and empty disabled data.
  4312. *
  4313. * @return void
  4314. */
  4315. public function testRadioComplexDisabled()
  4316. {
  4317. $options = [
  4318. ['value' => 'r', 'text' => 'red'],
  4319. ['value' => 'b', 'text' => 'blue'],
  4320. ];
  4321. $attrs = ['disabled' => []];
  4322. $result = $this->Form->radio('Model.field', $options, $attrs);
  4323. $expected = [
  4324. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4325. ['label' => ['for' => 'model-field-r']],
  4326. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4327. 'red',
  4328. '/label',
  4329. ['label' => ['for' => 'model-field-b']],
  4330. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4331. 'blue',
  4332. '/label',
  4333. ];
  4334. $this->assertHtml($expected, $result);
  4335. $attrs = ['disabled' => ['r']];
  4336. $result = $this->Form->radio('Model.field', $options, $attrs);
  4337. $this->assertContains('disabled="disabled"', $result);
  4338. }
  4339. /**
  4340. * testRadioDefaultValue method
  4341. *
  4342. * Test default value setting on radio() method.
  4343. *
  4344. * @return void
  4345. */
  4346. public function testRadioDefaultValue()
  4347. {
  4348. $Articles = TableRegistry::get('Articles');
  4349. $title = $Articles->schema()->column('title');
  4350. $Articles->schema()->addColumn(
  4351. 'title',
  4352. ['default' => '1'] + $title
  4353. );
  4354. $this->Form->create($Articles->newEntity());
  4355. $result = $this->Form->radio('title', ['option A', 'option B']);
  4356. $expected = [
  4357. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4358. ['label' => ['for' => 'title-0']],
  4359. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4360. 'option A',
  4361. '/label',
  4362. ['label' => ['for' => 'title-1']],
  4363. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4364. 'option B',
  4365. '/label',
  4366. ];
  4367. $this->assertHtml($expected, $result);
  4368. }
  4369. /**
  4370. * 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. }
  4804. /**
  4805. * testCheckboxZeroValue method
  4806. *
  4807. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4808. *
  4809. * @return void
  4810. */
  4811. public function testCheckboxZeroValue()
  4812. {
  4813. $result = $this->Form->control('User.get_spam', [
  4814. 'type' => 'checkbox',
  4815. 'value' => '0',
  4816. 'hiddenField' => '1',
  4817. ]);
  4818. $expected = [
  4819. 'div' => ['class' => 'input checkbox'],
  4820. 'label' => ['for' => 'user-get-spam'],
  4821. ['input' => [
  4822. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4823. 'value' => '1'
  4824. ]],
  4825. ['input' => [
  4826. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4827. 'value' => '0', 'id' => 'user-get-spam'
  4828. ]],
  4829. 'Get Spam',
  4830. '/label',
  4831. '/div'
  4832. ];
  4833. $this->assertHtml($expected, $result);
  4834. }
  4835. /**
  4836. * testHabtmSelectBox method
  4837. *
  4838. * Test generation of habtm select boxes.
  4839. *
  4840. * @return void
  4841. */
  4842. public function testHabtmSelectBox()
  4843. {
  4844. $this->loadFixtures('Articles');
  4845. $options = [
  4846. 1 => 'blue',
  4847. 2 => 'red',
  4848. 3 => 'green'
  4849. ];
  4850. $tags = [
  4851. new Entity(['id' => 1, 'name' => 'blue']),
  4852. new Entity(['id' => 3, 'name' => 'green'])
  4853. ];
  4854. $article = new Article(['tags' => $tags]);
  4855. $this->Form->create($article);
  4856. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4857. $expected = [
  4858. 'div' => ['class' => 'input select'],
  4859. 'label' => ['for' => 'tags-ids'],
  4860. 'Tags',
  4861. '/label',
  4862. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4863. 'select' => [
  4864. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4865. 'multiple' => 'multiple'
  4866. ],
  4867. ['option' => ['value' => '1', 'selected' => 'selected']],
  4868. 'blue',
  4869. '/option',
  4870. ['option' => ['value' => '2']],
  4871. 'red',
  4872. '/option',
  4873. ['option' => ['value' => '3', 'selected' => 'selected']],
  4874. 'green',
  4875. '/option',
  4876. '/select',
  4877. '/div'
  4878. ];
  4879. $this->assertHtml($expected, $result);
  4880. // make sure only 50 is selected, and not 50f5c0cf
  4881. $options = [
  4882. '1' => 'blue',
  4883. '50f5c0cf' => 'red',
  4884. '50' => 'green'
  4885. ];
  4886. $tags = [
  4887. new Entity(['id' => 1, 'name' => 'blue']),
  4888. new Entity(['id' => 50, 'name' => 'green'])
  4889. ];
  4890. $article = new Article(['tags' => $tags]);
  4891. $this->Form->create($article);
  4892. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4893. $expected = [
  4894. 'div' => ['class' => 'input select'],
  4895. 'label' => ['for' => 'tags-ids'],
  4896. 'Tags',
  4897. '/label',
  4898. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4899. 'select' => [
  4900. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4901. 'multiple' => 'multiple'
  4902. ],
  4903. ['option' => ['value' => '1', 'selected' => 'selected']],
  4904. 'blue',
  4905. '/option',
  4906. ['option' => ['value' => '50f5c0cf']],
  4907. 'red',
  4908. '/option',
  4909. ['option' => ['value' => '50', 'selected' => 'selected']],
  4910. 'green',
  4911. '/option',
  4912. '/select',
  4913. '/div'
  4914. ];
  4915. $this->assertHtml($expected, $result);
  4916. $spacecraft = [
  4917. 1 => 'Orion',
  4918. 2 => 'Helios'
  4919. ];
  4920. $this->View->viewVars['spacecraft'] = $spacecraft;
  4921. $this->Form->create();
  4922. $result = $this->Form->control('spacecraft._ids');
  4923. $expected = [
  4924. 'div' => ['class' => 'input select'],
  4925. 'label' => ['for' => 'spacecraft-ids'],
  4926. 'Spacecraft',
  4927. '/label',
  4928. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4929. 'select' => [
  4930. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4931. 'multiple' => 'multiple'
  4932. ],
  4933. ['option' => ['value' => '1']],
  4934. 'Orion',
  4935. '/option',
  4936. ['option' => ['value' => '2']],
  4937. 'Helios',
  4938. '/option',
  4939. '/select',
  4940. '/div'
  4941. ];
  4942. $this->assertHtml($expected, $result);
  4943. }
  4944. /**
  4945. * testErrorsForBelongsToManySelect method
  4946. *
  4947. * Tests that errors for belongsToMany select fields are being
  4948. * picked up properly.
  4949. *
  4950. * @return void
  4951. */
  4952. public function testErrorsForBelongsToManySelect()
  4953. {
  4954. $spacecraft = [
  4955. 1 => 'Orion',
  4956. 2 => 'Helios'
  4957. ];
  4958. $this->View->viewVars['spacecraft'] = $spacecraft;
  4959. $article = new Article();
  4960. $article->errors('spacecraft', ['Invalid']);
  4961. $this->Form->create($article);
  4962. $result = $this->Form->control('spacecraft._ids');
  4963. $expected = [
  4964. ['div' => ['class' => 'input select error']],
  4965. 'label' => ['for' => 'spacecraft-ids'],
  4966. 'Spacecraft',
  4967. '/label',
  4968. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4969. 'select' => [
  4970. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4971. 'multiple' => 'multiple'
  4972. ],
  4973. ['option' => ['value' => '1']],
  4974. 'Orion',
  4975. '/option',
  4976. ['option' => ['value' => '2']],
  4977. 'Helios',
  4978. '/option',
  4979. '/select',
  4980. ['div' => ['class' => 'error-message']],
  4981. 'Invalid',
  4982. '/div',
  4983. '/div'
  4984. ];
  4985. $this->assertHtml($expected, $result);
  4986. }
  4987. /**
  4988. * testSelectMultipleCheckboxes method
  4989. *
  4990. * Test generation of multi select elements in checkbox format.
  4991. *
  4992. * @return void
  4993. */
  4994. public function testSelectMultipleCheckboxes()
  4995. {
  4996. $result = $this->Form->select(
  4997. 'Model.multi_field',
  4998. ['first', 'second', 'third'],
  4999. ['multiple' => 'checkbox']
  5000. );
  5001. $expected = [
  5002. 'input' => [
  5003. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5004. ],
  5005. ['div' => ['class' => 'checkbox']],
  5006. ['label' => ['for' => 'model-multi-field-0']],
  5007. ['input' => [
  5008. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5009. 'value' => '0', 'id' => 'model-multi-field-0'
  5010. ]],
  5011. 'first',
  5012. '/label',
  5013. '/div',
  5014. ['div' => ['class' => 'checkbox']],
  5015. ['label' => ['for' => 'model-multi-field-1']],
  5016. ['input' => [
  5017. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5018. 'value' => '1', 'id' => 'model-multi-field-1'
  5019. ]],
  5020. 'second',
  5021. '/label',
  5022. '/div',
  5023. ['div' => ['class' => 'checkbox']],
  5024. ['label' => ['for' => 'model-multi-field-2']],
  5025. ['input' => [
  5026. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5027. 'value' => '2', 'id' => 'model-multi-field-2'
  5028. ]],
  5029. 'third',
  5030. '/label',
  5031. '/div'
  5032. ];
  5033. $this->assertHtml($expected, $result);
  5034. $result = $this->Form->select(
  5035. 'Model.multi_field',
  5036. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5037. ['multiple' => 'checkbox']
  5038. );
  5039. $expected = [
  5040. 'input' => [
  5041. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5042. ],
  5043. ['div' => ['class' => 'checkbox']],
  5044. ['label' => ['for' => 'model-multi-field-a+']],
  5045. ['input' => [
  5046. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5047. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  5048. ]],
  5049. 'first',
  5050. '/label',
  5051. '/div',
  5052. ['div' => ['class' => 'checkbox']],
  5053. ['label' => ['for' => 'model-multi-field-a++']],
  5054. ['input' => [
  5055. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5056. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  5057. ]],
  5058. 'second',
  5059. '/label',
  5060. '/div',
  5061. ['div' => ['class' => 'checkbox']],
  5062. ['label' => ['for' => 'model-multi-field-a+++']],
  5063. ['input' => [
  5064. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5065. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  5066. ]],
  5067. 'third',
  5068. '/label',
  5069. '/div'
  5070. ];
  5071. $this->assertHtml($expected, $result);
  5072. $result = $this->Form->select(
  5073. 'Model.multi_field',
  5074. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5075. ['multiple' => 'checkbox']
  5076. );
  5077. $expected = [
  5078. 'input' => [
  5079. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5080. ],
  5081. ['div' => ['class' => 'checkbox']],
  5082. ['label' => ['for' => 'model-multi-field-a-b']],
  5083. ['input' => [
  5084. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5085. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  5086. ]],
  5087. 'first',
  5088. '/label',
  5089. '/div',
  5090. ['div' => ['class' => 'checkbox']],
  5091. ['label' => ['for' => 'model-multi-field-a-b1']],
  5092. ['input' => [
  5093. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5094. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  5095. ]],
  5096. 'second',
  5097. '/label',
  5098. '/div',
  5099. ['div' => ['class' => 'checkbox']],
  5100. ['label' => ['for' => 'model-multi-field-a-b2']],
  5101. ['input' => [
  5102. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5103. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  5104. ]],
  5105. 'third',
  5106. '/label',
  5107. '/div'
  5108. ];
  5109. $this->assertHtml($expected, $result);
  5110. }
  5111. /**
  5112. * testSelectMultipleCheckboxRequestData method
  5113. *
  5114. * Ensure that multiCheckbox reads from the request data.
  5115. *
  5116. * @return void
  5117. */
  5118. public function testSelectMultipleCheckboxRequestData()
  5119. {
  5120. $this->Form->request->data = ['Model' => ['tags' => [1]]];
  5121. $result = $this->Form->select(
  5122. 'Model.tags',
  5123. ['1' => 'first', 'Array' => 'Array'],
  5124. ['multiple' => 'checkbox']
  5125. );
  5126. $expected = [
  5127. 'input' => [
  5128. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  5129. ],
  5130. ['div' => ['class' => 'checkbox']],
  5131. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5132. ['input' => [
  5133. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5134. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  5135. ]],
  5136. 'first',
  5137. '/label',
  5138. '/div',
  5139. ['div' => ['class' => 'checkbox']],
  5140. ['label' => ['for' => 'model-tags-array']],
  5141. ['input' => [
  5142. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5143. 'value' => 'Array', 'id' => 'model-tags-array'
  5144. ]],
  5145. 'Array',
  5146. '/label',
  5147. '/div'
  5148. ];
  5149. $this->assertHtml($expected, $result);
  5150. }
  5151. /**
  5152. * testSelectMultipleCheckboxSecurity method
  5153. *
  5154. * Checks the security hash array generated for multiple-input checkbox elements.
  5155. *
  5156. * @return void
  5157. */
  5158. public function testSelectMultipleCheckboxSecurity()
  5159. {
  5160. $this->Form->request->params['_Token'] = 'testKey';
  5161. $this->assertEquals([], $this->Form->fields);
  5162. $this->Form->select(
  5163. 'Model.multi_field',
  5164. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5165. ['multiple' => 'checkbox']
  5166. );
  5167. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5168. $result = $this->Form->secure($this->Form->fields);
  5169. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  5170. $this->assertRegExp('/"' . $key . '"/', $result);
  5171. }
  5172. /**
  5173. * testSelectMultipleSecureWithNoOptions method
  5174. *
  5175. * Multiple select elements should always be secured as they always participate
  5176. * in the POST data.
  5177. *
  5178. * @return void
  5179. */
  5180. public function testSelectMultipleSecureWithNoOptions()
  5181. {
  5182. $this->assertEquals([], $this->Form->fields);
  5183. $this->Form->select(
  5184. 'Model.select',
  5185. [],
  5186. ['multiple' => true]
  5187. );
  5188. $this->assertEquals(['Model.select'], $this->Form->fields);
  5189. }
  5190. /**
  5191. * testSelectNoSecureWithNoOptions method
  5192. *
  5193. * When a select box has no options it should not be added to the fields list
  5194. * as it always fail post validation.
  5195. *
  5196. * @return void
  5197. */
  5198. public function testSelectNoSecureWithNoOptions()
  5199. {
  5200. $this->Form->request->params['_Token'] = 'testkey';
  5201. $this->assertEquals([], $this->Form->fields);
  5202. $this->Form->select(
  5203. 'Model.select',
  5204. []
  5205. );
  5206. $this->assertEquals([], $this->Form->fields);
  5207. $this->Form->select(
  5208. 'Model.user_id',
  5209. [],
  5210. ['empty' => true]
  5211. );
  5212. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5213. }
  5214. /**
  5215. * testControlMultipleCheckboxes method
  5216. *
  5217. * Test control() resulting in multi select elements being generated.
  5218. *
  5219. * @return void
  5220. */
  5221. public function testControlMultipleCheckboxes()
  5222. {
  5223. $result = $this->Form->control('Model.multi_field', [
  5224. 'options' => ['first', 'second', 'third'],
  5225. 'multiple' => 'checkbox'
  5226. ]);
  5227. $expected = [
  5228. ['div' => ['class' => 'input select']],
  5229. ['label' => ['for' => 'model-multi-field']],
  5230. 'Multi Field',
  5231. '/label',
  5232. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5233. ['div' => ['class' => 'checkbox']],
  5234. ['label' => ['for' => 'model-multi-field-0']],
  5235. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5236. 'first',
  5237. '/label',
  5238. '/div',
  5239. ['div' => ['class' => 'checkbox']],
  5240. ['label' => ['for' => 'model-multi-field-1']],
  5241. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5242. 'second',
  5243. '/label',
  5244. '/div',
  5245. ['div' => ['class' => 'checkbox']],
  5246. ['label' => ['for' => 'model-multi-field-2']],
  5247. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5248. 'third',
  5249. '/label',
  5250. '/div',
  5251. '/div'
  5252. ];
  5253. $this->assertHtml($expected, $result);
  5254. $result = $this->Form->control('Model.multi_field', [
  5255. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5256. 'multiple' => 'checkbox'
  5257. ]);
  5258. $expected = [
  5259. ['div' => ['class' => 'input select']],
  5260. ['label' => ['for' => 'model-multi-field']],
  5261. 'Multi Field',
  5262. '/label',
  5263. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5264. ['div' => ['class' => 'checkbox']],
  5265. ['label' => ['for' => 'model-multi-field-a']],
  5266. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5267. 'first',
  5268. '/label',
  5269. '/div',
  5270. ['div' => ['class' => 'checkbox']],
  5271. ['label' => ['for' => 'model-multi-field-b']],
  5272. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5273. 'second',
  5274. '/label',
  5275. '/div',
  5276. ['div' => ['class' => 'checkbox']],
  5277. ['label' => ['for' => 'model-multi-field-c']],
  5278. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5279. 'third',
  5280. '/label',
  5281. '/div',
  5282. '/div'
  5283. ];
  5284. $this->assertHtml($expected, $result);
  5285. }
  5286. /**
  5287. * testSelectHiddenFieldOmission method
  5288. *
  5289. * Test that select() with 'hiddenField' => false omits the hidden field.
  5290. *
  5291. * @return void
  5292. */
  5293. public function testSelectHiddenFieldOmission()
  5294. {
  5295. $result = $this->Form->select(
  5296. 'Model.multi_field',
  5297. ['first', 'second'],
  5298. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5299. );
  5300. $this->assertNotContains('type="hidden"', $result);
  5301. }
  5302. /**
  5303. * testSelectCheckboxMultipleOverrideName method
  5304. *
  5305. * Test that select() with multiple = checkbox works with overriding name attribute.
  5306. *
  5307. * @return void
  5308. */
  5309. public function testSelectCheckboxMultipleOverrideName()
  5310. {
  5311. $result = $this->Form->select('category', ['1', '2'], [
  5312. 'multiple' => 'checkbox',
  5313. 'name' => 'fish',
  5314. ]);
  5315. $expected = [
  5316. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5317. ['div' => ['class' => 'checkbox']],
  5318. ['label' => ['for' => 'fish-0']],
  5319. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5320. '1',
  5321. '/label',
  5322. '/div',
  5323. ['div' => ['class' => 'checkbox']],
  5324. ['label' => ['for' => 'fish-1']],
  5325. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5326. '2',
  5327. '/label',
  5328. '/div'
  5329. ];
  5330. $this->assertHtml($expected, $result);
  5331. $result = $this->Form->multiCheckbox(
  5332. 'category',
  5333. new Collection(['1', '2']),
  5334. ['name' => 'fish']
  5335. );
  5336. $this->assertHtml($expected, $result);
  5337. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5338. 'name' => 'fish',
  5339. ]);
  5340. $this->assertHtml($expected, $result);
  5341. }
  5342. /**
  5343. * testControlMultiCheckbox method
  5344. *
  5345. * Test that control() works with multicheckbox.
  5346. *
  5347. * @return void
  5348. */
  5349. public function testControlMultiCheckbox()
  5350. {
  5351. $result = $this->Form->control('category', [
  5352. 'type' => 'multicheckbox',
  5353. 'options' => ['1', '2'],
  5354. ]);
  5355. $expected = [
  5356. ['div' => ['class' => 'input multicheckbox']],
  5357. '<label',
  5358. 'Category',
  5359. '/label',
  5360. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5361. ['div' => ['class' => 'checkbox']],
  5362. ['label' => ['for' => 'category-0']],
  5363. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5364. '1',
  5365. '/label',
  5366. '/div',
  5367. ['div' => ['class' => 'checkbox']],
  5368. ['label' => ['for' => 'category-1']],
  5369. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5370. '2',
  5371. '/label',
  5372. '/div',
  5373. '/div',
  5374. ];
  5375. $this->assertHtml($expected, $result);
  5376. }
  5377. /**
  5378. * testCheckbox method
  5379. *
  5380. * Test generation of checkboxes.
  5381. *
  5382. * @return void
  5383. */
  5384. public function testCheckbox()
  5385. {
  5386. $result = $this->Form->checkbox('Model.field');
  5387. $expected = [
  5388. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5389. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5390. ];
  5391. $this->assertHtml($expected, $result);
  5392. $result = $this->Form->checkbox('Model.field', [
  5393. 'id' => 'theID',
  5394. 'value' => 'myvalue',
  5395. 'form' => 'my-form',
  5396. ]);
  5397. $expected = [
  5398. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5399. ['input' => [
  5400. 'type' => 'checkbox', 'name' => 'Model[field]',
  5401. 'value' => 'myvalue', 'id' => 'theID',
  5402. 'form' => 'my-form',
  5403. ]]
  5404. ];
  5405. $this->assertHtml($expected, $result);
  5406. }
  5407. /**
  5408. * testCheckboxDefaultValue method
  5409. *
  5410. * Test default value setting on checkbox() method.
  5411. *
  5412. * @return void
  5413. */
  5414. public function testCheckboxDefaultValue()
  5415. {
  5416. $this->Form->request->data['Model']['field'] = false;
  5417. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5418. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5419. $this->assertHtml($expected, $result);
  5420. unset($this->Form->request->data['Model']['field']);
  5421. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5422. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5423. $this->assertHtml($expected, $result);
  5424. $this->Form->request->data['Model']['field'] = true;
  5425. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5426. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5427. $this->assertHtml($expected, $result);
  5428. unset($this->Form->request->data['Model']['field']);
  5429. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5430. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5431. $this->assertHtml($expected, $result);
  5432. $Articles = TableRegistry::get('Articles');
  5433. $Articles->schema()->addColumn(
  5434. 'published',
  5435. ['type' => 'boolean', 'null' => false, 'default' => true]
  5436. );
  5437. $this->Form->create($Articles->newEntity());
  5438. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5439. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5440. $this->assertHtml($expected, $result);
  5441. }
  5442. /**
  5443. * testCheckboxCheckedAndError method
  5444. *
  5445. * Test checkbox being checked or having errors.
  5446. *
  5447. * @return void
  5448. */
  5449. public function testCheckboxCheckedAndError()
  5450. {
  5451. $this->article['errors'] = [
  5452. 'published' => true
  5453. ];
  5454. $this->Form->request->data['published'] = 'myvalue';
  5455. $this->Form->create($this->article);
  5456. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5457. $expected = [
  5458. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5459. ['input' => [
  5460. 'type' => 'checkbox',
  5461. 'name' => 'published',
  5462. 'value' => 'myvalue',
  5463. 'id' => 'theID',
  5464. 'checked' => 'checked',
  5465. 'class' => 'form-error'
  5466. ]]
  5467. ];
  5468. $this->assertHtml($expected, $result);
  5469. $this->Form->request->data['published'] = '';
  5470. $result = $this->Form->checkbox('published');
  5471. $expected = [
  5472. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5473. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5474. ];
  5475. $this->assertHtml($expected, $result);
  5476. }
  5477. /**
  5478. * testCheckboxCustomNameAttribute method
  5479. *
  5480. * Test checkbox() with a custom name attribute.
  5481. *
  5482. * @return void
  5483. */
  5484. public function testCheckboxCustomNameAttribute()
  5485. {
  5486. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5487. $expected = [
  5488. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5489. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5490. ];
  5491. $this->assertHtml($expected, $result);
  5492. }
  5493. /**
  5494. * testCheckboxHiddenField method
  5495. *
  5496. * Test that the hidden input for checkboxes can be omitted or set to a
  5497. * specific value.
  5498. *
  5499. * @return void
  5500. */
  5501. public function testCheckboxHiddenField()
  5502. {
  5503. $result = $this->Form->checkbox('UserForm.something', [
  5504. 'hiddenField' => false
  5505. ]);
  5506. $expected = [
  5507. 'input' => [
  5508. 'type' => 'checkbox',
  5509. 'name' => 'UserForm[something]',
  5510. 'value' => '1'
  5511. ],
  5512. ];
  5513. $this->assertHtml($expected, $result);
  5514. $result = $this->Form->checkbox('UserForm.something', [
  5515. 'value' => 'Y',
  5516. 'hiddenField' => 'N',
  5517. ]);
  5518. $expected = [
  5519. ['input' => [
  5520. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5521. 'value' => 'N'
  5522. ]],
  5523. ['input' => [
  5524. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5525. 'value' => 'Y'
  5526. ]],
  5527. ];
  5528. $this->assertHtml($expected, $result);
  5529. }
  5530. /**
  5531. * testTime method
  5532. *
  5533. * Test the time type.
  5534. *
  5535. * @return void
  5536. */
  5537. public function testTime()
  5538. {
  5539. $result = $this->Form->time('start_time', [
  5540. 'timeFormat' => 12,
  5541. 'interval' => 5,
  5542. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5543. ]);
  5544. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5545. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5546. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5547. $this->assertNotContains('year', $result);
  5548. $this->assertNotContains('month', $result);
  5549. $this->assertNotContains('day', $result);
  5550. $result = $this->Form->time('start_time', [
  5551. 'timeFormat' => 12,
  5552. 'interval' => 5,
  5553. 'value' => '2014-03-08 16:30:00'
  5554. ]);
  5555. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5556. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5557. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5558. $this->assertNotContains('year', $result);
  5559. $this->assertNotContains('month', $result);
  5560. $this->assertNotContains('day', $result);
  5561. }
  5562. /**
  5563. * testTimeFormat24NoMeridian method
  5564. *
  5565. * Ensure that timeFormat=24 has no merdian.
  5566. *
  5567. * @return void.
  5568. */
  5569. public function testTimeFormat24NoMeridian()
  5570. {
  5571. $result = $this->Form->time('start_time', [
  5572. 'timeFormat' => 24,
  5573. 'interval' => 5,
  5574. 'value' => '2014-03-08 16:30:00'
  5575. ]);
  5576. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5577. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5578. $this->assertNotContains('meridian', $result);
  5579. $this->assertNotContains('pm', $result);
  5580. $this->assertNotContains('year', $result);
  5581. $this->assertNotContains('month', $result);
  5582. $this->assertNotContains('day', $result);
  5583. }
  5584. /**
  5585. * testDate method
  5586. *
  5587. * Test the date type.
  5588. *
  5589. * @return void
  5590. */
  5591. public function testDate()
  5592. {
  5593. $result = $this->Form->date('start_day', [
  5594. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5595. ]);
  5596. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5597. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5598. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5599. $this->assertNotContains('hour', $result);
  5600. $this->assertNotContains('minute', $result);
  5601. $this->assertNotContains('second', $result);
  5602. $this->assertNotContains('meridian', $result);
  5603. }
  5604. /**
  5605. * testDateTime method
  5606. *
  5607. * Test generation of date/time select elements.
  5608. *
  5609. * @return void
  5610. */
  5611. public function testDateTime()
  5612. {
  5613. extract($this->dateRegex);
  5614. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5615. $now = strtotime('now');
  5616. $expected = [
  5617. ['select' => ['name' => 'Contact[date][year]']],
  5618. ['option' => ['value' => '']],
  5619. '/option',
  5620. $yearsRegex,
  5621. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5622. date('Y', $now),
  5623. '/option',
  5624. '*/select',
  5625. ['select' => ['name' => 'Contact[date][month]']],
  5626. ['option' => ['value' => '']],
  5627. '/option',
  5628. $monthsRegex,
  5629. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5630. date('F', $now),
  5631. '/option',
  5632. '*/select',
  5633. ['select' => ['name' => 'Contact[date][day]']],
  5634. ['option' => ['value' => '']],
  5635. '/option',
  5636. $daysRegex,
  5637. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5638. date('j', $now),
  5639. '/option',
  5640. '*/select',
  5641. ['select' => ['name' => 'Contact[date][hour]']],
  5642. ['option' => ['value' => '']],
  5643. '/option',
  5644. $hoursRegex,
  5645. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5646. date('G', $now),
  5647. '/option',
  5648. '*/select',
  5649. ['select' => ['name' => 'Contact[date][minute]']],
  5650. ['option' => ['value' => '']],
  5651. '/option',
  5652. $minutesRegex,
  5653. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5654. date('i', $now),
  5655. '/option',
  5656. '*/select',
  5657. ];
  5658. $this->assertHtml($expected, $result);
  5659. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5660. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5661. $now = strtotime('now');
  5662. $expected = [
  5663. ['select' => ['name' => 'Contact[date][year]']],
  5664. $yearsRegex,
  5665. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5666. date('Y', $now),
  5667. '/option',
  5668. '*/select',
  5669. ['select' => ['name' => 'Contact[date][month]']],
  5670. $monthsRegex,
  5671. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5672. date('F', $now),
  5673. '/option',
  5674. '*/select',
  5675. ['select' => ['name' => 'Contact[date][day]']],
  5676. $daysRegex,
  5677. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5678. date('j', $now),
  5679. '/option',
  5680. '*/select',
  5681. ['select' => ['name' => 'Contact[date][hour]']],
  5682. $hoursRegex,
  5683. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5684. date('G', $now),
  5685. '/option',
  5686. '*/select',
  5687. ['select' => ['name' => 'Contact[date][minute]']],
  5688. $minutesRegex,
  5689. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5690. date('i', $now),
  5691. '/option',
  5692. '*/select',
  5693. ];
  5694. $this->assertHtml($expected, $result);
  5695. }
  5696. /**
  5697. * testDateTimeSecured method
  5698. *
  5699. * Test that datetime fields are added to protected fields list.
  5700. *
  5701. * @return void
  5702. */
  5703. public function testDateTimeSecured()
  5704. {
  5705. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5706. $this->Form->dateTime('Contact.date');
  5707. $expected = [
  5708. 'Contact.date.year',
  5709. 'Contact.date.month',
  5710. 'Contact.date.day',
  5711. 'Contact.date.hour',
  5712. 'Contact.date.minute',
  5713. ];
  5714. $this->assertEquals($expected, $this->Form->fields);
  5715. $this->Form->fields = [];
  5716. $this->Form->date('Contact.published');
  5717. $expected = [
  5718. 'Contact.published.year',
  5719. 'Contact.published.month',
  5720. 'Contact.published.day',
  5721. ];
  5722. $this->assertEquals($expected, $this->Form->fields);
  5723. }
  5724. /**
  5725. * testDateTimeSecuredDisabled method
  5726. *
  5727. * Test that datetime fields are added to protected fields list.
  5728. *
  5729. * @return void
  5730. */
  5731. public function testDateTimeSecuredDisabled()
  5732. {
  5733. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5734. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5735. $expected = [];
  5736. $this->assertEquals($expected, $this->Form->fields);
  5737. $this->Form->fields = [];
  5738. $this->Form->date('Contact.published', ['secure' => false]);
  5739. $expected = [];
  5740. $this->assertEquals($expected, $this->Form->fields);
  5741. }
  5742. /**
  5743. * testDatetimeEmpty method
  5744. *
  5745. * Test empty defaulting to true for datetime.
  5746. *
  5747. * @return void
  5748. */
  5749. public function testDatetimeEmpty()
  5750. {
  5751. extract($this->dateRegex);
  5752. $result = $this->Form->dateTime('Contact.date', [
  5753. 'timeFormat' => 12,
  5754. 'empty' => true,
  5755. 'default' => true
  5756. ]);
  5757. $expected = [
  5758. ['select' => ['name' => 'Contact[date][year]']],
  5759. $yearsRegex,
  5760. ['option' => ['value' => '']],
  5761. '/option',
  5762. '*/select',
  5763. ['select' => ['name' => 'Contact[date][month]']],
  5764. $monthsRegex,
  5765. ['option' => ['value' => '']],
  5766. '/option',
  5767. '*/select',
  5768. ['select' => ['name' => 'Contact[date][day]']],
  5769. $daysRegex,
  5770. ['option' => ['value' => '']],
  5771. '/option',
  5772. '*/select',
  5773. ['select' => ['name' => 'Contact[date][hour]']],
  5774. $hoursRegex,
  5775. ['option' => ['value' => '']],
  5776. '/option',
  5777. '*/select',
  5778. ['select' => ['name' => 'Contact[date][minute]']],
  5779. $minutesRegex,
  5780. ['option' => ['value' => '']],
  5781. '/option',
  5782. '*/select',
  5783. ['select' => ['name' => 'Contact[date][meridian]']],
  5784. $meridianRegex,
  5785. ['option' => ['value' => '']],
  5786. '/option',
  5787. '*/select'
  5788. ];
  5789. $this->assertHtml($expected, $result);
  5790. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5791. }
  5792. /**
  5793. * testDatetimeMinuteInterval method
  5794. *
  5795. * Test datetime with interval option.
  5796. *
  5797. * @return void
  5798. */
  5799. public function testDatetimeMinuteInterval()
  5800. {
  5801. extract($this->dateRegex);
  5802. $result = $this->Form->dateTime('Contact.date', [
  5803. 'interval' => 5,
  5804. 'value' => ''
  5805. ]);
  5806. $expected = [
  5807. ['select' => ['name' => 'Contact[date][year]']],
  5808. $yearsRegex,
  5809. ['option' => ['selected' => 'selected', 'value' => '']],
  5810. '/option',
  5811. '*/select',
  5812. ['select' => ['name' => 'Contact[date][month]']],
  5813. $monthsRegex,
  5814. ['option' => ['selected' => 'selected', 'value' => '']],
  5815. '/option',
  5816. '*/select',
  5817. ['select' => ['name' => 'Contact[date][day]']],
  5818. $daysRegex,
  5819. ['option' => ['selected' => 'selected', 'value' => '']],
  5820. '/option',
  5821. '*/select',
  5822. ['select' => ['name' => 'Contact[date][hour]']],
  5823. $hoursRegex,
  5824. ['option' => ['selected' => 'selected', 'value' => '']],
  5825. '/option',
  5826. '*/select',
  5827. ['select' => ['name' => 'Contact[date][minute]']],
  5828. $minutesRegex,
  5829. ['option' => ['selected' => 'selected', 'value' => '']],
  5830. '/option',
  5831. ['option' => ['value' => '00']],
  5832. '00',
  5833. '/option',
  5834. ['option' => ['value' => '05']],
  5835. '05',
  5836. '/option',
  5837. ['option' => ['value' => '10']],
  5838. '10',
  5839. '/option',
  5840. '*/select',
  5841. ];
  5842. $this->assertHtml($expected, $result);
  5843. }
  5844. /**
  5845. * testDateTimeRounding method
  5846. *
  5847. * Test dateTime with rounding.
  5848. *
  5849. * @return void
  5850. */
  5851. public function testDateTimeRounding()
  5852. {
  5853. $this->Form->request->data['Contact'] = [
  5854. 'date' => [
  5855. 'day' => '13',
  5856. 'month' => '12',
  5857. 'year' => '2010',
  5858. 'hour' => '04',
  5859. 'minute' => '19',
  5860. 'meridian' => 'AM'
  5861. ]
  5862. ];
  5863. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5864. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5865. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5866. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5867. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5868. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5869. }
  5870. /**
  5871. * testDatetimeWithDefault method
  5872. *
  5873. * Test that datetime() and default values work.
  5874. *
  5875. * @return void
  5876. */
  5877. public function testDatetimeWithDefault()
  5878. {
  5879. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5880. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5881. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5882. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5883. $result = $this->Form->dateTime('Contact.updated', [
  5884. 'default' => '2009-06-01 11:15:30'
  5885. ]);
  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. }
  5890. /**
  5891. * testDateTimeAllZeros method
  5892. *
  5893. * Test datetime() with all zeros.
  5894. *
  5895. * @return void
  5896. */
  5897. public function testDateTimeAllZeros()
  5898. {
  5899. $result = $this->Form->dateTime('Contact.date', [
  5900. 'timeFormat' => false,
  5901. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5902. 'value' => '0000-00-00'
  5903. ]);
  5904. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5905. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5906. }
  5907. /**
  5908. * testDateTimeEmptyAsArray method
  5909. *
  5910. * @return void
  5911. */
  5912. public function testDateTimeEmptyAsArray()
  5913. {
  5914. $result = $this->Form->dateTime('Contact.date', [
  5915. 'empty' => [
  5916. 'day' => 'DAY',
  5917. 'month' => 'MONTH',
  5918. 'year' => 'YEAR',
  5919. 'hour' => 'HOUR',
  5920. 'minute' => 'MINUTE',
  5921. 'meridian' => false
  5922. ],
  5923. 'default' => true
  5924. ]);
  5925. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5926. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5927. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5928. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5929. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5930. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5931. $result = $this->Form->dateTime('Contact.date', [
  5932. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5933. 'default' => true
  5934. ]);
  5935. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5936. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5937. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5938. }
  5939. /**
  5940. * testFormDateTimeMulti method
  5941. *
  5942. * Test multiple datetime element generation.
  5943. *
  5944. * @return void
  5945. */
  5946. public function testFormDateTimeMulti()
  5947. {
  5948. extract($this->dateRegex);
  5949. $result = $this->Form->dateTime('Contact.1.updated');
  5950. $this->assertContains('Contact[1][updated][month]', $result);
  5951. $this->assertContains('Contact[1][updated][day]', $result);
  5952. $this->assertContains('Contact[1][updated][year]', $result);
  5953. $this->assertContains('Contact[1][updated][hour]', $result);
  5954. $this->assertContains('Contact[1][updated][minute]', $result);
  5955. }
  5956. /**
  5957. * testDateTimeLabelIdMatchesFirstControl method
  5958. *
  5959. * When changing the date format, the label should always focus the first select box when
  5960. * clicked.
  5961. *
  5962. * @return void
  5963. */
  5964. public function testDateTimeLabelIdMatchesFirstControl()
  5965. {
  5966. $result = $this->Form->control('Model.date', ['type' => 'date']);
  5967. $this->assertContains('<label>Date</label>', $result);
  5968. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  5969. $this->assertContains('<label>Date</label>', $result);
  5970. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  5971. $this->assertContains('<label>Date</label>', $result);
  5972. }
  5973. /**
  5974. * testDateTimeSecondOptions method
  5975. *
  5976. * Test datetime second=true.
  5977. *
  5978. * @return void
  5979. */
  5980. public function testDateTimeSecondOptions()
  5981. {
  5982. $result = $this->Form->dateTime('updated', ['second' => true]);
  5983. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5984. $result = $this->Form->dateTime('updated', ['second' => []]);
  5985. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5986. $result = $this->Form->dateTime('updated', ['second' => null]);
  5987. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5988. $result = $this->Form->dateTime('updated', ['second' => false]);
  5989. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5990. }
  5991. /**
  5992. * testMonth method
  5993. *
  5994. * Test generation of a month input.
  5995. *
  5996. * @return void
  5997. */
  5998. public function testMonth()
  5999. {
  6000. $result = $this->Form->month('Model.field', ['value' => '']);
  6001. $expected = [
  6002. ['select' => ['name' => 'Model[field][month]']],
  6003. ['option' => ['value' => '', 'selected' => 'selected']],
  6004. '/option',
  6005. ['option' => ['value' => '01']],
  6006. date('F', strtotime('2008-01-01 00:00:00')),
  6007. '/option',
  6008. ['option' => ['value' => '02']],
  6009. date('F', strtotime('2008-02-01 00:00:00')),
  6010. '/option',
  6011. '*/select',
  6012. ];
  6013. $this->assertHtml($expected, $result);
  6014. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  6015. $expected = [
  6016. ['select' => ['name' => 'Model[field][month]']],
  6017. ['option' => ['selected' => 'selected', 'value' => '']],
  6018. '/option',
  6019. ['option' => ['value' => '01']],
  6020. date('F', strtotime('2008-01-01 00:00:00')),
  6021. '/option',
  6022. ['option' => ['value' => '02']],
  6023. date('F', strtotime('2008-02-01 00:00:00')),
  6024. '/option',
  6025. '*/select',
  6026. ];
  6027. $this->assertHtml($expected, $result);
  6028. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  6029. $expected = [
  6030. ['select' => ['name' => 'Model[field][month]']],
  6031. ['option' => ['selected' => 'selected', 'value' => '']],
  6032. '/option',
  6033. ['option' => ['value' => '01']],
  6034. '1',
  6035. '/option',
  6036. ['option' => ['value' => '02']],
  6037. '2',
  6038. '/option',
  6039. '*/select',
  6040. ];
  6041. $this->assertHtml($expected, $result);
  6042. $monthNames = [
  6043. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6044. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  6045. ];
  6046. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  6047. $expected = [
  6048. ['select' => ['name' => 'Model[field][month]']],
  6049. ['option' => ['value' => '']],
  6050. '/option',
  6051. ['option' => ['value' => '01', 'selected' => 'selected']],
  6052. 'Jan',
  6053. '/option',
  6054. ['option' => ['value' => '02']],
  6055. 'Feb',
  6056. '/option',
  6057. '*/select',
  6058. ];
  6059. $this->assertHtml($expected, $result);
  6060. $this->Form->request->data['Project']['release'] = '2050-02-10';
  6061. $result = $this->Form->month('Project.release');
  6062. $expected = [
  6063. ['select' => ['name' => 'Project[release][month]']],
  6064. ['option' => ['value' => '']],
  6065. '/option',
  6066. ['option' => ['value' => '01']],
  6067. 'January',
  6068. '/option',
  6069. ['option' => ['value' => '02', 'selected' => 'selected']],
  6070. 'February',
  6071. '/option',
  6072. '*/select',
  6073. ];
  6074. $this->assertHtml($expected, $result);
  6075. $result = $this->Form->month('Contact.published', [
  6076. 'empty' => 'Published on',
  6077. ]);
  6078. $this->assertContains('Published on', $result);
  6079. }
  6080. /**
  6081. * testDay method
  6082. *
  6083. * Test generation of a day input.
  6084. *
  6085. * @return void
  6086. */
  6087. public function testDay()
  6088. {
  6089. extract($this->dateRegex);
  6090. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  6091. $expected = [
  6092. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  6093. ['option' => ['selected' => 'selected', 'value' => '']],
  6094. '/option',
  6095. ['option' => ['value' => '01']],
  6096. '1',
  6097. '/option',
  6098. ['option' => ['value' => '02']],
  6099. '2',
  6100. '/option',
  6101. $daysRegex,
  6102. '/select',
  6103. ];
  6104. $this->assertHtml($expected, $result);
  6105. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6106. $result = $this->Form->day('Model.field');
  6107. $expected = [
  6108. ['select' => ['name' => 'Model[field][day]']],
  6109. ['option' => ['value' => '']],
  6110. '/option',
  6111. ['option' => ['value' => '01']],
  6112. '1',
  6113. '/option',
  6114. ['option' => ['value' => '02']],
  6115. '2',
  6116. '/option',
  6117. $daysRegex,
  6118. ['option' => ['value' => '10', 'selected' => 'selected']],
  6119. '10',
  6120. '/option',
  6121. $daysRegex,
  6122. '/select',
  6123. ];
  6124. $this->assertHtml($expected, $result);
  6125. $this->Form->request->data['Model']['field'] = '';
  6126. $result = $this->Form->day('Model.field', ['value' => '10']);
  6127. $expected = [
  6128. ['select' => ['name' => 'Model[field][day]']],
  6129. ['option' => ['value' => '']],
  6130. '/option',
  6131. ['option' => ['value' => '01']],
  6132. '1',
  6133. '/option',
  6134. ['option' => ['value' => '02']],
  6135. '2',
  6136. '/option',
  6137. $daysRegex,
  6138. ['option' => ['value' => '10', 'selected' => 'selected']],
  6139. '10',
  6140. '/option',
  6141. $daysRegex,
  6142. '/select',
  6143. ];
  6144. $this->assertHtml($expected, $result);
  6145. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6146. $result = $this->Form->day('Project.release');
  6147. $expected = [
  6148. ['select' => ['name' => 'Project[release][day]']],
  6149. ['option' => ['value' => '']],
  6150. '/option',
  6151. ['option' => ['value' => '01']],
  6152. '1',
  6153. '/option',
  6154. ['option' => ['value' => '02']],
  6155. '2',
  6156. '/option',
  6157. $daysRegex,
  6158. ['option' => ['value' => '10', 'selected' => 'selected']],
  6159. '10',
  6160. '/option',
  6161. $daysRegex,
  6162. '/select',
  6163. ];
  6164. $this->assertHtml($expected, $result);
  6165. $result = $this->Form->day('Contact.published', [
  6166. 'empty' => 'Published on',
  6167. ]);
  6168. $this->assertContains('Published on', $result);
  6169. }
  6170. /**
  6171. * testMinute method
  6172. *
  6173. * Test generation of a minute input.
  6174. *
  6175. * @return void
  6176. */
  6177. public function testMinute()
  6178. {
  6179. extract($this->dateRegex);
  6180. $result = $this->Form->minute('Model.field', ['value' => '']);
  6181. $expected = [
  6182. ['select' => ['name' => 'Model[field][minute]']],
  6183. ['option' => ['selected' => 'selected', 'value' => '']],
  6184. '/option',
  6185. ['option' => ['value' => '00']],
  6186. '00',
  6187. '/option',
  6188. ['option' => ['value' => '01']],
  6189. '01',
  6190. '/option',
  6191. ['option' => ['value' => '02']],
  6192. '02',
  6193. '/option',
  6194. $minutesRegex,
  6195. '/select',
  6196. ];
  6197. $this->assertHtml($expected, $result);
  6198. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6199. $result = $this->Form->minute('Model.field');
  6200. $expected = [
  6201. ['select' => ['name' => 'Model[field][minute]']],
  6202. ['option' => ['value' => '']],
  6203. '/option',
  6204. ['option' => ['value' => '00']],
  6205. '00',
  6206. '/option',
  6207. ['option' => ['value' => '01']],
  6208. '01',
  6209. '/option',
  6210. ['option' => ['value' => '02']],
  6211. '02',
  6212. '/option',
  6213. $minutesRegex,
  6214. ['option' => ['value' => '12', 'selected' => 'selected']],
  6215. '12',
  6216. '/option',
  6217. $minutesRegex,
  6218. '/select',
  6219. ];
  6220. $this->assertHtml($expected, $result);
  6221. $this->Form->request->data['Model']['field'] = '';
  6222. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6223. $expected = [
  6224. ['select' => ['name' => 'Model[field][minute]']],
  6225. ['option' => ['selected' => 'selected', 'value' => '']],
  6226. '/option',
  6227. ['option' => ['value' => '00']],
  6228. '00',
  6229. '/option',
  6230. ['option' => ['value' => '05']],
  6231. '05',
  6232. '/option',
  6233. ['option' => ['value' => '10']],
  6234. '10',
  6235. '/option',
  6236. $minutesRegex,
  6237. '/select',
  6238. ];
  6239. $this->assertHtml($expected, $result);
  6240. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6241. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6242. $expected = [
  6243. ['select' => ['name' => 'Model[field][minute]']],
  6244. ['option' => ['value' => '']],
  6245. '/option',
  6246. ['option' => ['value' => '00']],
  6247. '00',
  6248. '/option',
  6249. ['option' => ['value' => '05']],
  6250. '05',
  6251. '/option',
  6252. ['option' => ['value' => '10', 'selected' => 'selected']],
  6253. '10',
  6254. '/option',
  6255. $minutesRegex,
  6256. '/select',
  6257. ];
  6258. $this->assertHtml($expected, $result);
  6259. }
  6260. /**
  6261. * testMeridian method
  6262. *
  6263. * Test generating an input for the meridian.
  6264. *
  6265. * @return void
  6266. */
  6267. public function testMeridian()
  6268. {
  6269. extract($this->dateRegex);
  6270. $now = new \DateTime();
  6271. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6272. $expected = [
  6273. ['select' => ['name' => 'Model[field][meridian]']],
  6274. ['option' => ['value' => '']],
  6275. '/option',
  6276. $meridianRegex,
  6277. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6278. $now->format('a'),
  6279. '/option',
  6280. '*/select'
  6281. ];
  6282. $this->assertHtml($expected, $result);
  6283. }
  6284. /**
  6285. * testHour method
  6286. *
  6287. * Test generation of an hour input.
  6288. *
  6289. * @return void
  6290. */
  6291. public function testHour()
  6292. {
  6293. extract($this->dateRegex);
  6294. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6295. $expected = [
  6296. ['select' => ['name' => 'Model[field][hour]']],
  6297. ['option' => ['selected' => 'selected', 'value' => '']],
  6298. '/option',
  6299. ['option' => ['value' => '01']],
  6300. '1',
  6301. '/option',
  6302. ['option' => ['value' => '02']],
  6303. '2',
  6304. '/option',
  6305. $hoursRegex,
  6306. '/select',
  6307. ];
  6308. $this->assertHtml($expected, $result);
  6309. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6310. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6311. $expected = [
  6312. ['select' => ['name' => 'Model[field][hour]']],
  6313. ['option' => ['value' => '']],
  6314. '/option',
  6315. ['option' => ['value' => '01']],
  6316. '1',
  6317. '/option',
  6318. ['option' => ['value' => '02']],
  6319. '2',
  6320. '/option',
  6321. $hoursRegex,
  6322. ['option' => ['value' => '12', 'selected' => 'selected']],
  6323. '12',
  6324. '/option',
  6325. '/select',
  6326. ];
  6327. $this->assertHtml($expected, $result);
  6328. $this->Form->request->data['Model']['field'] = '';
  6329. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6330. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6331. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6332. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6333. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6334. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6335. $expected = [
  6336. ['select' => ['name' => 'Model[field][hour]']],
  6337. ['option' => ['value' => '']],
  6338. '/option',
  6339. ['option' => ['value' => '00', 'selected' => 'selected']],
  6340. '0',
  6341. '/option',
  6342. ['option' => ['value' => '01']],
  6343. '1',
  6344. '/option',
  6345. ['option' => ['value' => '02']],
  6346. '2',
  6347. '/option',
  6348. $hoursRegex,
  6349. '/select',
  6350. ];
  6351. $this->assertHtml($expected, $result);
  6352. unset($this->Form->request->data['Model']['field']);
  6353. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6354. $thisHour = date('H');
  6355. $optValue = date('G');
  6356. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6357. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6358. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6359. $expected = [
  6360. ['select' => ['name' => 'Model[field][hour]']],
  6361. ['option' => ['value' => '']],
  6362. '/option',
  6363. ['option' => ['value' => '00']],
  6364. '0',
  6365. '/option',
  6366. ['option' => ['value' => '01', 'selected' => 'selected']],
  6367. '1',
  6368. '/option',
  6369. ['option' => ['value' => '02']],
  6370. '2',
  6371. '/option',
  6372. $hoursRegex,
  6373. '/select',
  6374. ];
  6375. $this->assertHtml($expected, $result);
  6376. }
  6377. /**
  6378. * testYear method
  6379. *
  6380. * Test generation of a year input.
  6381. *
  6382. * @return void
  6383. */
  6384. public function testYear()
  6385. {
  6386. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6387. $expected = [
  6388. ['select' => ['name' => 'Model[field][year]']],
  6389. ['option' => ['selected' => 'selected', 'value' => '']],
  6390. '/option',
  6391. ['option' => ['value' => '2007']],
  6392. '2007',
  6393. '/option',
  6394. ['option' => ['value' => '2006']],
  6395. '2006',
  6396. '/option',
  6397. '/select',
  6398. ];
  6399. $this->assertHtml($expected, $result);
  6400. $result = $this->Form->year('Model.field', [
  6401. 'value' => '',
  6402. 'minYear' => 2006,
  6403. 'maxYear' => 2007,
  6404. 'orderYear' => 'asc'
  6405. ]);
  6406. $expected = [
  6407. ['select' => ['name' => 'Model[field][year]']],
  6408. ['option' => ['selected' => 'selected', 'value' => '']],
  6409. '/option',
  6410. ['option' => ['value' => '2006']],
  6411. '2006',
  6412. '/option',
  6413. ['option' => ['value' => '2007']],
  6414. '2007',
  6415. '/option',
  6416. '/select',
  6417. ];
  6418. $this->assertHtml($expected, $result);
  6419. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6420. $result = $this->Form->year('Contact.published', [
  6421. 'empty' => false,
  6422. 'minYear' => 2006,
  6423. 'maxYear' => 2007,
  6424. ]);
  6425. $expected = [
  6426. ['select' => ['name' => 'Contact[published][year]']],
  6427. ['option' => ['value' => '2007']],
  6428. '2007',
  6429. '/option',
  6430. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6431. '2006',
  6432. '/option',
  6433. '/select',
  6434. ];
  6435. $this->assertHtml($expected, $result);
  6436. $result = $this->Form->year('Contact.published', [
  6437. 'empty' => 'Published on',
  6438. ]);
  6439. $this->assertContains('Published on', $result);
  6440. }
  6441. /**
  6442. * testControlDatetimePreEpoch method
  6443. *
  6444. * Test minYear being prior to the unix epoch.
  6445. *
  6446. * @return void
  6447. */
  6448. public function testControlDatetimePreEpoch()
  6449. {
  6450. $start = date('Y') - 80;
  6451. $end = date('Y') - 18;
  6452. $result = $this->Form->control('birth_year', [
  6453. 'type' => 'date',
  6454. 'label' => 'Birth Year',
  6455. 'minYear' => $start,
  6456. 'maxYear' => $end,
  6457. 'month' => false,
  6458. 'day' => false,
  6459. ]);
  6460. $this->assertContains('value="' . $start . '">' . $start, $result);
  6461. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6462. $this->assertNotContains('value="00">00', $result);
  6463. }
  6464. /**
  6465. * testYearAutoExpandRange method
  6466. *
  6467. * @return void
  6468. */
  6469. public function testYearAutoExpandRange()
  6470. {
  6471. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6472. $result = $this->Form->year('User.birthday');
  6473. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6474. $result = $matches[1];
  6475. $expected = range(date('Y') + 5, 1930);
  6476. $this->assertEquals($expected, $result);
  6477. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6478. $result = $this->Form->year('Project.release');
  6479. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6480. $result = $matches[1];
  6481. $expected = range(2050, date('Y') - 5);
  6482. $this->assertEquals($expected, $result);
  6483. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6484. $result = $this->Form->year('Project.release', [
  6485. 'minYear' => 1890,
  6486. 'maxYear' => 1900
  6487. ]);
  6488. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6489. $result = $matches[1];
  6490. $expected = range(1900, 1881);
  6491. $this->assertEquals($expected, $result);
  6492. }
  6493. /**
  6494. * testControlDate method
  6495. *
  6496. * Test that control() accepts the type of date and passes options in.
  6497. *
  6498. * @return void
  6499. */
  6500. public function testControlDate()
  6501. {
  6502. $this->Form->request->data = [
  6503. 'month_year' => ['month' => date('m')],
  6504. ];
  6505. $this->Form->create($this->article);
  6506. $result = $this->Form->control('month_year', [
  6507. 'label' => false,
  6508. 'type' => 'date',
  6509. 'minYear' => 2006,
  6510. 'maxYear' => 2008
  6511. ]);
  6512. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6513. $this->assertNotContains('value="2008" selected="selected"', $result);
  6514. }
  6515. /**
  6516. * testControlLabelFalse method
  6517. *
  6518. * Test the label option being set to false.
  6519. *
  6520. * @return void
  6521. */
  6522. public function testControlLabelFalse()
  6523. {
  6524. $this->Form->create($this->article);
  6525. $result = $this->Form->control('title', ['label' => false]);
  6526. $expected = [
  6527. 'div' => ['class' => 'input text required'],
  6528. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6529. '/div'
  6530. ];
  6531. $this->assertHtml($expected, $result);
  6532. }
  6533. /**
  6534. * testControlDateMaxYear method
  6535. *
  6536. * Let's say we want to only allow users born from 2006 to 2008 to register
  6537. * This being the first signup page, we still don't have any data.
  6538. *
  6539. * @return void
  6540. */
  6541. public function testControlDateMaxYear()
  6542. {
  6543. $this->Form->request->data = [];
  6544. $this->Form->create($this->article);
  6545. $result = $this->Form->control('birthday', [
  6546. 'label' => false,
  6547. 'type' => 'date',
  6548. 'minYear' => 2006,
  6549. 'maxYear' => 2008,
  6550. 'default' => true
  6551. ]);
  6552. $this->assertContains('value="2008" selected="selected"', $result);
  6553. $this->assertContains('value="2006"', $result);
  6554. $this->assertNotContains('value="2005"', $result);
  6555. $this->assertNotContains('value="2009"', $result);
  6556. }
  6557. /**
  6558. * testTextArea method
  6559. *
  6560. * Test generation of a textarea input.
  6561. *
  6562. * @return void
  6563. */
  6564. public function testTextArea()
  6565. {
  6566. $this->Form->request->data = ['field' => 'some test data'];
  6567. $result = $this->Form->textarea('field');
  6568. $expected = [
  6569. 'textarea' => ['name' => 'field', 'rows' => 5],
  6570. 'some test data',
  6571. '/textarea',
  6572. ];
  6573. $this->assertHtml($expected, $result);
  6574. $result = $this->Form->textarea('user.bio');
  6575. $expected = [
  6576. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6577. '/textarea',
  6578. ];
  6579. $this->assertHtml($expected, $result);
  6580. $this->Form->request->data = ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'];
  6581. $result = $this->Form->textarea('field');
  6582. $expected = [
  6583. 'textarea' => ['name' => 'field', 'rows' => 5],
  6584. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6585. '/textarea',
  6586. ];
  6587. $this->assertHtml($expected, $result);
  6588. $this->Form->request->data = [
  6589. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  6590. ];
  6591. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6592. $expected = [
  6593. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6594. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6595. '/textarea',
  6596. ];
  6597. $this->assertHtml($expected, $result);
  6598. $result = $this->Form->textarea('0.OtherModel.field');
  6599. $expected = [
  6600. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6601. '/textarea'
  6602. ];
  6603. $this->assertHtml($expected, $result);
  6604. }
  6605. /**
  6606. * testTextAreaWithStupidCharacters method
  6607. *
  6608. * Test text area with non-ascii characters.
  6609. *
  6610. * @return void
  6611. */
  6612. public function testTextAreaWithStupidCharacters()
  6613. {
  6614. $result = $this->Form->textarea('Post.content', [
  6615. 'value' => 'GREAT®',
  6616. 'rows' => '15',
  6617. 'cols' => '75'
  6618. ]);
  6619. $expected = [
  6620. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6621. 'GREAT®',
  6622. '/textarea',
  6623. ];
  6624. $this->assertHtml($expected, $result);
  6625. }
  6626. /**
  6627. * testTextAreaMaxLength method
  6628. *
  6629. * Test textareas maxlength read from schema.
  6630. *
  6631. * @return void
  6632. */
  6633. public function testTextAreaMaxLength()
  6634. {
  6635. $this->Form->create([
  6636. 'schema' => [
  6637. 'stuff' => ['type' => 'string', 'length' => 10],
  6638. ]
  6639. ]);
  6640. $result = $this->Form->control('other', ['type' => 'textarea']);
  6641. $expected = [
  6642. 'div' => ['class' => 'input textarea'],
  6643. 'label' => ['for' => 'other'],
  6644. 'Other',
  6645. '/label',
  6646. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6647. '/textarea',
  6648. '/div'
  6649. ];
  6650. $this->assertHtml($expected, $result);
  6651. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6652. $expected = [
  6653. 'div' => ['class' => 'input textarea'],
  6654. 'label' => ['for' => 'stuff'],
  6655. 'Stuff',
  6656. '/label',
  6657. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6658. '/textarea',
  6659. '/div'
  6660. ];
  6661. $this->assertHtml($expected, $result);
  6662. }
  6663. /**
  6664. * testHiddenField method
  6665. *
  6666. * Test generation of a hidden input.
  6667. *
  6668. * @return void
  6669. */
  6670. public function testHiddenField()
  6671. {
  6672. $this->article['errors'] = [
  6673. 'field' => true
  6674. ];
  6675. $this->Form->request->data['field'] = 'test';
  6676. $this->Form->create($this->article);
  6677. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6678. $expected = [
  6679. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6680. $this->assertHtml($expected, $result);
  6681. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6682. $expected = [
  6683. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6684. ];
  6685. $this->assertHtml($expected, $result);
  6686. }
  6687. /**
  6688. * testFileUploadField method
  6689. *
  6690. * Test generation of a file upload input.
  6691. *
  6692. * @return void
  6693. */
  6694. public function testFileUploadField()
  6695. {
  6696. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6697. $result = $this->Form->file('Model.upload');
  6698. $this->assertHtml($expected, $result);
  6699. $this->Form->request->data['Model']['upload'] = [
  6700. 'name' => '', 'type' => '', 'tmp_name' => '',
  6701. 'error' => 4, 'size' => 0
  6702. ];
  6703. $result = $this->Form->file('Model.upload');
  6704. $this->assertHtml($expected, $result);
  6705. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6706. $result = $this->Form->file('Model.upload');
  6707. $this->assertHtml($expected, $result);
  6708. }
  6709. /**
  6710. * testFileUploadOnOtherModel method
  6711. *
  6712. * Test File upload input on a model not used in create().
  6713. *
  6714. * @return void
  6715. */
  6716. public function testFileUploadOnOtherModel()
  6717. {
  6718. $this->Form->create($this->article, ['type' => 'file']);
  6719. $result = $this->Form->file('ValidateProfile.city');
  6720. $expected = [
  6721. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6722. ];
  6723. $this->assertHtml($expected, $result);
  6724. }
  6725. /**
  6726. * testButton method
  6727. *
  6728. * Test generation of a form button.
  6729. *
  6730. * @return void
  6731. */
  6732. public function testButton()
  6733. {
  6734. $result = $this->Form->button('Hi');
  6735. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6736. $this->assertHtml($expected, $result);
  6737. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6738. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6739. $this->assertHtml($expected, $result);
  6740. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6741. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6742. $this->assertHtml($expected, $result);
  6743. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6744. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6745. $this->assertHtml($expected, $result);
  6746. $result = $this->Form->button('No type', ['type' => false]);
  6747. $expected = ['button' => [], 'No type', '/button'];
  6748. $this->assertHtml($expected, $result);
  6749. $result = $this->Form->button('Upload Text', [
  6750. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6751. 'escape' => false
  6752. ]);
  6753. $this->assertNotRegExp('/\&039/', $result);
  6754. }
  6755. /**
  6756. * testButtonUnlockedByDefault method
  6757. *
  6758. * Test that button() makes unlocked fields by default.
  6759. *
  6760. * @return void
  6761. */
  6762. public function testButtonUnlockedByDefault()
  6763. {
  6764. $this->Form->request->params['_csrfToken'] = 'secured';
  6765. $this->Form->button('Save', ['name' => 'save']);
  6766. $this->Form->button('Clear');
  6767. $result = $this->Form->unlockField();
  6768. $this->assertEquals(['save'], $result);
  6769. }
  6770. /**
  6771. * testPostButton method
  6772. *
  6773. * @return void
  6774. */
  6775. public function testPostButton()
  6776. {
  6777. $result = $this->Form->postButton('Hi', '/controller/action');
  6778. $expected = [
  6779. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6780. 'div' => ['style' => 'display:none;'],
  6781. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6782. '/div',
  6783. 'button' => ['type' => 'submit'],
  6784. 'Hi',
  6785. '/button',
  6786. '/form'
  6787. ];
  6788. $this->assertHtml($expected, $result);
  6789. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6790. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  6791. }
  6792. /**
  6793. * testPostButtonMethodType method
  6794. *
  6795. * @return void
  6796. */
  6797. public function testPostButtonMethodType()
  6798. {
  6799. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6800. $expected = [
  6801. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6802. 'div' => ['style' => 'display:none;'],
  6803. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6804. '/div',
  6805. 'button' => ['type' => 'submit'],
  6806. 'Hi',
  6807. '/button',
  6808. '/form'
  6809. ];
  6810. $this->assertHtml($expected, $result);
  6811. }
  6812. /**
  6813. * testPostButtonFormOptions method
  6814. *
  6815. * @return void
  6816. */
  6817. public function testPostButtonFormOptions()
  6818. {
  6819. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6820. $expected = [
  6821. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6822. 'div' => ['style' => 'display:none;'],
  6823. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6824. '/div',
  6825. 'button' => ['type' => 'submit'],
  6826. 'Hi',
  6827. '/button',
  6828. '/form'
  6829. ];
  6830. $this->assertHtml($expected, $result);
  6831. }
  6832. /**
  6833. * testPostButtonNestedData method
  6834. *
  6835. * Test using postButton with N dimensional data.
  6836. *
  6837. * @return void
  6838. */
  6839. public function testPostButtonNestedData()
  6840. {
  6841. $data = [
  6842. 'one' => [
  6843. 'two' => [
  6844. 3, 4, 5
  6845. ]
  6846. ]
  6847. ];
  6848. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6849. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6850. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6851. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6852. }
  6853. /**
  6854. * testSecurePostButton method
  6855. *
  6856. * Test that postButton adds _Token fields.
  6857. *
  6858. * @return void
  6859. */
  6860. public function testSecurePostButton()
  6861. {
  6862. $this->Form->request->params['_csrfToken'] = 'testkey';
  6863. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  6864. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6865. $tokenDebug = urlencode(json_encode([
  6866. '/posts/delete/1',
  6867. [],
  6868. []
  6869. ]));
  6870. $expected = [
  6871. 'form' => [
  6872. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6873. ],
  6874. ['div' => ['style' => 'display:none;']],
  6875. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6876. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6877. '/div',
  6878. 'button' => ['type' => 'submit'],
  6879. 'Delete',
  6880. '/button',
  6881. ['div' => ['style' => 'display:none;']],
  6882. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6883. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6884. ['input' => [
  6885. 'type' => 'hidden',
  6886. 'name' => '_Token[debug]',
  6887. 'value' => $tokenDebug,
  6888. 'autocomplete' => 'off',
  6889. ]],
  6890. '/div',
  6891. '/form',
  6892. ];
  6893. $this->assertHtml($expected, $result);
  6894. }
  6895. /**
  6896. * testPostLink method
  6897. *
  6898. * @return void
  6899. */
  6900. public function testPostLink()
  6901. {
  6902. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6903. $expected = [
  6904. 'form' => [
  6905. 'method' => 'post', 'action' => '/posts/delete/1',
  6906. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6907. ],
  6908. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6909. '/form',
  6910. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6911. 'Delete',
  6912. '/a'
  6913. ];
  6914. $this->assertHtml($expected, $result);
  6915. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6916. $expected = [
  6917. 'form' => [
  6918. 'method' => 'post', 'action' => '/posts/delete/1',
  6919. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6920. ],
  6921. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6922. '/form',
  6923. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6924. 'Delete',
  6925. '/a'
  6926. ];
  6927. $this->assertHtml($expected, $result);
  6928. $result = $this->Form->postLink(
  6929. 'Delete',
  6930. '/posts/delete/1',
  6931. ['target' => '_blank', 'class' => 'btn btn-danger']
  6932. );
  6933. $expected = [
  6934. 'form' => [
  6935. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6936. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6937. ],
  6938. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6939. '/form',
  6940. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6941. 'Delete',
  6942. '/a'
  6943. ];
  6944. $this->assertHtml($expected, $result);
  6945. }
  6946. /**
  6947. * testPostLinkWithConfirm method
  6948. *
  6949. * Test the confirm option for postLink().
  6950. *
  6951. * @return void
  6952. */
  6953. public function testPostLinkWithConfirm()
  6954. {
  6955. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6956. $expected = [
  6957. 'form' => [
  6958. 'method' => 'post', 'action' => '/posts/delete/1',
  6959. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6960. ],
  6961. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6962. '/form',
  6963. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  6964. 'Delete',
  6965. '/a'
  6966. ];
  6967. $this->assertHtml($expected, $result);
  6968. $result = $this->Form->postLink(
  6969. 'Delete',
  6970. '/posts/delete/1',
  6971. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  6972. );
  6973. $expected = [
  6974. 'form' => [
  6975. 'method' => 'post', 'action' => '/posts/delete/1',
  6976. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6977. ],
  6978. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6979. '/form',
  6980. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  6981. 'Delete',
  6982. '/a'
  6983. ];
  6984. $this->assertHtml($expected, $result);
  6985. }
  6986. /**
  6987. * testPostLinkWithQuery method
  6988. *
  6989. * Test postLink() with query string args.
  6990. *
  6991. * @return void
  6992. */
  6993. public function testPostLinkWithQuery()
  6994. {
  6995. $result = $this->Form->postLink(
  6996. 'Delete',
  6997. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6998. );
  6999. $expected = [
  7000. 'form' => [
  7001. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  7002. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7003. ],
  7004. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7005. '/form',
  7006. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7007. 'Delete',
  7008. '/a'
  7009. ];
  7010. $this->assertHtml($expected, $result);
  7011. }
  7012. /**
  7013. * testPostLinkWithData method
  7014. *
  7015. * Test postLink with additional data.
  7016. *
  7017. * @return void
  7018. */
  7019. public function testPostLinkWithData()
  7020. {
  7021. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  7022. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  7023. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  7024. $this->Form->create($entity);
  7025. $this->Form->end();
  7026. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  7027. $this->assertContains(
  7028. '<input type="hidden" name="name" value="show"',
  7029. $result,
  7030. 'should not contain entity data.'
  7031. );
  7032. }
  7033. /**
  7034. * testPostLinkSecurityHash method
  7035. *
  7036. * Test that security hashes for postLink include the url.
  7037. *
  7038. * @return void
  7039. */
  7040. public function testPostLinkSecurityHash()
  7041. {
  7042. $hash = Security::hash(
  7043. '/posts/delete/1' .
  7044. serialize(['id' => '1']) .
  7045. '' .
  7046. Security::salt()
  7047. );
  7048. $hash .= '%3Aid';
  7049. $this->Form->request->params['_Token']['key'] = 'test';
  7050. $result = $this->Form->postLink(
  7051. 'Delete',
  7052. '/posts/delete/1',
  7053. ['data' => ['id' => 1]]
  7054. );
  7055. $tokenDebug = urlencode(json_encode([
  7056. '/posts/delete/1',
  7057. [
  7058. 'id' => 1
  7059. ],
  7060. []
  7061. ]));
  7062. $expected = [
  7063. 'form' => [
  7064. 'method' => 'post', 'action' => '/posts/delete/1',
  7065. 'name', 'style' => 'display:none;'
  7066. ],
  7067. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7068. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7069. 'div' => ['style' => 'display:none;'],
  7070. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7071. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7072. ['input' => [
  7073. 'type' => 'hidden',
  7074. 'name' => '_Token[debug]',
  7075. 'value' => $tokenDebug,
  7076. 'autocomplete' => 'off',
  7077. ]],
  7078. '/div',
  7079. '/form',
  7080. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7081. 'Delete',
  7082. '/a'
  7083. ];
  7084. $this->assertHtml($expected, $result);
  7085. }
  7086. /**
  7087. * testPostLinkSecurityHashBlockMode method
  7088. *
  7089. * Test that postLink doesn't modify the fields in the containing form.
  7090. *
  7091. * postLink() calls inside open forms should not modify the field list
  7092. * for the form.
  7093. *
  7094. * @return void
  7095. */
  7096. public function testPostLinkSecurityHashBlockMode()
  7097. {
  7098. $hash = Security::hash(
  7099. '/posts/delete/1' .
  7100. serialize([]) .
  7101. '' .
  7102. Security::salt()
  7103. );
  7104. $hash .= '%3A';
  7105. $this->Form->request->params['_Token']['key'] = 'test';
  7106. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  7107. $this->Form->control('title');
  7108. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7109. $result = $this->View->fetch('postLink');
  7110. $this->assertEquals(['title'], $this->Form->fields);
  7111. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  7112. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  7113. }
  7114. /**
  7115. * testPostLinkSecurityHashNoDebugMode method
  7116. *
  7117. * Test that security does not include debug token if debug is false.
  7118. *
  7119. * @return void
  7120. */
  7121. public function testPostLinkSecurityHashNoDebugMode()
  7122. {
  7123. Configure::write('debug', false);
  7124. $hash = Security::hash(
  7125. '/posts/delete/1' .
  7126. serialize(['id' => '1']) .
  7127. '' .
  7128. Security::salt()
  7129. );
  7130. $hash .= '%3Aid';
  7131. $this->Form->request->params['_Token']['key'] = 'test';
  7132. $result = $this->Form->postLink(
  7133. 'Delete',
  7134. '/posts/delete/1',
  7135. ['data' => ['id' => 1]]
  7136. );
  7137. $expected = [
  7138. 'form' => [
  7139. 'method' => 'post', 'action' => '/posts/delete/1',
  7140. 'name', 'style' => 'display:none;'
  7141. ],
  7142. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7143. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7144. 'div' => ['style' => 'display:none;'],
  7145. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7146. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7147. '/div',
  7148. '/form',
  7149. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7150. 'Delete',
  7151. '/a'
  7152. ];
  7153. $this->assertHtml($expected, $result);
  7154. }
  7155. /**
  7156. * testPostLinkNestedData method
  7157. *
  7158. * Test using postLink with N dimensional data.
  7159. *
  7160. * @return void
  7161. */
  7162. public function testPostLinkNestedData()
  7163. {
  7164. $data = [
  7165. 'one' => [
  7166. 'two' => [
  7167. 3, 4, 5
  7168. ]
  7169. ]
  7170. ];
  7171. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7172. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7173. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7174. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7175. }
  7176. /**
  7177. * testPostLinkAfterGetForm method
  7178. *
  7179. * Test creating postLinks after a GET form.
  7180. *
  7181. * @return void
  7182. */
  7183. public function testPostLinkAfterGetForm()
  7184. {
  7185. $this->Form->request->params['_csrfToken'] = 'testkey';
  7186. $this->Form->request->params['_Token'] = 'val';
  7187. $this->Form->create($this->article, ['type' => 'get']);
  7188. $this->Form->end();
  7189. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7190. $tokenDebug = urlencode(json_encode([
  7191. '/posts/delete/1',
  7192. [],
  7193. []
  7194. ]));
  7195. $expected = [
  7196. 'form' => [
  7197. 'method' => 'post', 'action' => '/posts/delete/1',
  7198. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7199. ],
  7200. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7201. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7202. 'div' => ['style' => 'display:none;'],
  7203. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7204. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7205. ['input' => [
  7206. 'type' => 'hidden', 'name' => '_Token[debug]',
  7207. 'value' => $tokenDebug,
  7208. 'autocomplete' => 'off'
  7209. ]],
  7210. '/div',
  7211. '/form',
  7212. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7213. 'Delete',
  7214. '/a'
  7215. ];
  7216. $this->assertHtml($expected, $result);
  7217. }
  7218. /**
  7219. * testPostLinkFormBuffer method
  7220. *
  7221. * Test that postLink adds form tags to view block.
  7222. *
  7223. * @return void
  7224. */
  7225. public function testPostLinkFormBuffer()
  7226. {
  7227. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7228. $expected = [
  7229. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7230. 'Delete',
  7231. '/a'
  7232. ];
  7233. $this->assertHtml($expected, $result);
  7234. $result = $this->View->fetch('postLink');
  7235. $expected = [
  7236. 'form' => [
  7237. 'method' => 'post', 'action' => '/posts/delete/1',
  7238. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7239. ],
  7240. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7241. '/form'
  7242. ];
  7243. $this->assertHtml($expected, $result);
  7244. $result = $this->Form->postLink(
  7245. 'Delete',
  7246. '/posts/delete/2',
  7247. ['block' => true, 'method' => 'DELETE']
  7248. );
  7249. $expected = [
  7250. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7251. 'Delete',
  7252. '/a'
  7253. ];
  7254. $this->assertHtml($expected, $result);
  7255. $result = $this->View->fetch('postLink');
  7256. $expected = [
  7257. 'form' => [
  7258. 'method' => 'post', 'action' => '/posts/delete/1',
  7259. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7260. ],
  7261. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7262. '/form',
  7263. [
  7264. 'form' => [
  7265. 'method' => 'post', 'action' => '/posts/delete/2',
  7266. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7267. ],
  7268. ],
  7269. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7270. '/form'
  7271. ];
  7272. $this->assertHtml($expected, $result);
  7273. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7274. $expected = [
  7275. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7276. 'Delete',
  7277. '/a'
  7278. ];
  7279. $this->assertHtml($expected, $result);
  7280. $result = $this->View->fetch('foobar');
  7281. $expected = [
  7282. 'form' => [
  7283. 'method' => 'post', 'action' => '/posts/delete/1',
  7284. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7285. ],
  7286. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7287. '/form'
  7288. ];
  7289. $this->assertHtml($expected, $result);
  7290. }
  7291. /**
  7292. * testSubmitButton method
  7293. *
  7294. * @return void
  7295. */
  7296. public function testSubmitButton()
  7297. {
  7298. $result = $this->Form->submit('');
  7299. $expected = [
  7300. 'div' => ['class' => 'submit'],
  7301. 'input' => ['type' => 'submit', 'value' => ''],
  7302. '/div'
  7303. ];
  7304. $this->assertHtml($expected, $result);
  7305. $result = $this->Form->submit('Test Submit');
  7306. $expected = [
  7307. 'div' => ['class' => 'submit'],
  7308. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7309. '/div'
  7310. ];
  7311. $this->assertHtml($expected, $result);
  7312. $result = $this->Form->submit('Next >');
  7313. $expected = [
  7314. 'div' => ['class' => 'submit'],
  7315. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7316. '/div'
  7317. ];
  7318. $this->assertHtml($expected, $result);
  7319. $result = $this->Form->submit('Next >', ['escape' => false]);
  7320. $expected = [
  7321. 'div' => ['class' => 'submit'],
  7322. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7323. '/div'
  7324. ];
  7325. $this->assertHtml($expected, $result);
  7326. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7327. $expected = [
  7328. 'div' => ['class' => 'submit'],
  7329. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7330. '/div'
  7331. ];
  7332. $this->assertHtml($expected, $result);
  7333. }
  7334. /**
  7335. * testSubmitImage method
  7336. *
  7337. * Test image submit types.
  7338. *
  7339. * @return void
  7340. */
  7341. public function testSubmitImage()
  7342. {
  7343. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7344. $expected = [
  7345. 'div' => ['class' => 'submit'],
  7346. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7347. '/div'
  7348. ];
  7349. $this->assertHtml($expected, $result);
  7350. $result = $this->Form->submit('/relative/cake.power.gif');
  7351. $expected = [
  7352. 'div' => ['class' => 'submit'],
  7353. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7354. '/div'
  7355. ];
  7356. $this->assertHtml($expected, $result);
  7357. $result = $this->Form->submit('cake.power.gif');
  7358. $expected = [
  7359. 'div' => ['class' => 'submit'],
  7360. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7361. '/div'
  7362. ];
  7363. $this->assertHtml($expected, $result);
  7364. $result = $this->Form->submit('Not.an.image');
  7365. $expected = [
  7366. 'div' => ['class' => 'submit'],
  7367. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7368. '/div'
  7369. ];
  7370. $this->assertHtml($expected, $result);
  7371. }
  7372. /**
  7373. * testSubmitUnlockedByDefault method
  7374. *
  7375. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7376. * be submitted at a time.
  7377. *
  7378. * @return void
  7379. */
  7380. public function testSubmitUnlockedByDefault()
  7381. {
  7382. $this->Form->request->params['_Token'] = 'secured';
  7383. $this->Form->submit('Go go');
  7384. $this->Form->submit('Save', ['name' => 'save']);
  7385. $result = $this->Form->unlockField();
  7386. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7387. }
  7388. /**
  7389. * testSubmitImageTimestamp method
  7390. *
  7391. * Test submit image with timestamps.
  7392. *
  7393. * @return void
  7394. */
  7395. public function testSubmitImageTimestamp()
  7396. {
  7397. Configure::write('Asset.timestamp', 'force');
  7398. $result = $this->Form->submit('cake.power.gif');
  7399. $expected = [
  7400. 'div' => ['class' => 'submit'],
  7401. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7402. '/div'
  7403. ];
  7404. $this->assertHtml($expected, $result);
  7405. }
  7406. /**
  7407. * testDateTimeWithGetForms method
  7408. *
  7409. * Test that datetime() works with GET style forms.
  7410. *
  7411. * @return void
  7412. */
  7413. public function testDateTimeWithGetForms()
  7414. {
  7415. extract($this->dateRegex);
  7416. $this->Form->create($this->article, ['type' => 'get']);
  7417. $result = $this->Form->datetime('created');
  7418. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7419. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7420. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7421. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7422. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7423. }
  7424. /**
  7425. * testForMagicControlNonExistingNorValidated method
  7426. *
  7427. * @return void
  7428. */
  7429. public function testForMagicControlNonExistingNorValidated()
  7430. {
  7431. $this->Form->create($this->article);
  7432. $this->Form->templates(['inputContainer' => '{{content}}']);
  7433. $result = $this->Form->control('non_existing_nor_validated');
  7434. $expected = [
  7435. 'label' => ['for' => 'non-existing-nor-validated'],
  7436. 'Non Existing Nor Validated',
  7437. '/label',
  7438. 'input' => [
  7439. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7440. 'id' => 'non-existing-nor-validated'
  7441. ]
  7442. ];
  7443. $this->assertHtml($expected, $result);
  7444. $result = $this->Form->control('non_existing_nor_validated', [
  7445. 'val' => 'my value'
  7446. ]);
  7447. $expected = [
  7448. 'label' => ['for' => 'non-existing-nor-validated'],
  7449. 'Non Existing Nor Validated',
  7450. '/label',
  7451. 'input' => [
  7452. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7453. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7454. ]
  7455. ];
  7456. $this->assertHtml($expected, $result);
  7457. $this->Form->request->data = ['non_existing_nor_validated' => 'CakePHP magic'];
  7458. $result = $this->Form->control('non_existing_nor_validated');
  7459. $expected = [
  7460. 'label' => ['for' => 'non-existing-nor-validated'],
  7461. 'Non Existing Nor Validated',
  7462. '/label',
  7463. 'input' => [
  7464. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7465. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7466. ]
  7467. ];
  7468. $this->assertHtml($expected, $result);
  7469. }
  7470. /**
  7471. * testFormMagicControlLabel method
  7472. *
  7473. * @return void
  7474. */
  7475. public function testFormMagicControlLabel()
  7476. {
  7477. TableRegistry::get('Contacts', [
  7478. 'className' => __NAMESPACE__ . '\ContactsTable'
  7479. ]);
  7480. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7481. $this->Form->templates(['inputContainer' => '{{content}}']);
  7482. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7483. $expected = [
  7484. 'label' => ['for' => 'contacts-name'],
  7485. 'My label',
  7486. '/label',
  7487. 'input' => [
  7488. 'type' => 'text', 'name' => 'Contacts[name]',
  7489. 'id' => 'contacts-name', 'maxlength' => '255'
  7490. ]
  7491. ];
  7492. $this->assertHtml($expected, $result);
  7493. $result = $this->Form->control('name', [
  7494. 'label' => ['class' => 'mandatory']
  7495. ]);
  7496. $expected = [
  7497. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7498. 'Name',
  7499. '/label',
  7500. 'input' => [
  7501. 'type' => 'text', 'name' => 'name',
  7502. 'id' => 'name', 'maxlength' => '255'
  7503. ]
  7504. ];
  7505. $this->assertHtml($expected, $result);
  7506. $result = $this->Form->control('name', [
  7507. 'div' => false,
  7508. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7509. ]);
  7510. $expected = [
  7511. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7512. 'My label',
  7513. '/label',
  7514. 'input' => [
  7515. 'type' => 'text', 'name' => 'name',
  7516. 'id' => 'name', 'maxlength' => '255'
  7517. ]
  7518. ];
  7519. $this->assertHtml($expected, $result);
  7520. $result = $this->Form->control('Contact.name', [
  7521. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7522. ]);
  7523. $expected = [
  7524. 'label' => ['for' => 'my_id'],
  7525. 'Name',
  7526. '/label',
  7527. 'input' => [
  7528. 'type' => 'text', 'name' => 'Contact[name]',
  7529. 'id' => 'my_id', 'maxlength' => '255'
  7530. ]
  7531. ];
  7532. $this->assertHtml($expected, $result);
  7533. $result = $this->Form->control('1.id');
  7534. $expected = ['input' => [
  7535. 'type' => 'hidden', 'name' => '1[id]',
  7536. 'id' => '1-id'
  7537. ]];
  7538. $this->assertHtml($expected, $result);
  7539. $result = $this->Form->control('1.name');
  7540. $expected = [
  7541. 'label' => ['for' => '1-name'],
  7542. 'Name',
  7543. '/label',
  7544. 'input' => [
  7545. 'type' => 'text', 'name' => '1[name]',
  7546. 'id' => '1-name', 'maxlength' => '255'
  7547. ]
  7548. ];
  7549. $this->assertHtml($expected, $result);
  7550. }
  7551. /**
  7552. * testFormEnd method
  7553. *
  7554. * @return void
  7555. */
  7556. public function testFormEnd()
  7557. {
  7558. $this->assertEquals('</form>', $this->Form->end());
  7559. }
  7560. /**
  7561. * testMultiRecordForm method
  7562. *
  7563. * Test the generation of fields for a multi record form.
  7564. *
  7565. * @return void
  7566. */
  7567. public function testMultiRecordForm()
  7568. {
  7569. $this->loadFixtures('Articles', 'Comments');
  7570. $articles = TableRegistry::get('Articles');
  7571. $articles->hasMany('Comments');
  7572. $comment = new Entity(['comment' => 'Value']);
  7573. $article = new Article(['comments' => [$comment]]);
  7574. $this->Form->create([$article]);
  7575. $result = $this->Form->control('0.comments.1.comment');
  7576. //@codingStandardsIgnoreStart
  7577. $expected = [
  7578. 'div' => ['class' => 'input textarea'],
  7579. 'label' => ['for' => '0-comments-1-comment'],
  7580. 'Comment',
  7581. '/label',
  7582. 'textarea' => [
  7583. 'name',
  7584. 'id' => '0-comments-1-comment',
  7585. 'rows' => 5
  7586. ],
  7587. '/textarea',
  7588. '/div'
  7589. ];
  7590. //@codingStandardsIgnoreEnd
  7591. $this->assertHtml($expected, $result);
  7592. $result = $this->Form->control('0.comments.0.comment');
  7593. //@codingStandardsIgnoreStart
  7594. $expected = [
  7595. 'div' => ['class' => 'input textarea'],
  7596. 'label' => ['for' => '0-comments-0-comment'],
  7597. 'Comment',
  7598. '/label',
  7599. 'textarea' => [
  7600. 'name',
  7601. 'id' => '0-comments-0-comment',
  7602. 'rows' => 5
  7603. ],
  7604. 'Value',
  7605. '/textarea',
  7606. '/div'
  7607. ];
  7608. //@codingStandardsIgnoreEnd
  7609. $this->assertHtml($expected, $result);
  7610. $comment->errors('comment', ['Not valid']);
  7611. $result = $this->Form->control('0.comments.0.comment');
  7612. //@codingStandardsIgnoreStart
  7613. $expected = [
  7614. 'div' => ['class' => 'input textarea error'],
  7615. 'label' => ['for' => '0-comments-0-comment'],
  7616. 'Comment',
  7617. '/label',
  7618. 'textarea' => [
  7619. 'name',
  7620. 'class' => 'form-error',
  7621. 'id' => '0-comments-0-comment',
  7622. 'rows' => 5
  7623. ],
  7624. 'Value',
  7625. '/textarea',
  7626. ['div' => ['class' => 'error-message']],
  7627. 'Not valid',
  7628. '/div',
  7629. '/div'
  7630. ];
  7631. //@codingStandardsIgnoreEnd
  7632. $this->assertHtml($expected, $result);
  7633. TableRegistry::get('Comments')
  7634. ->validator('default')
  7635. ->allowEmpty('comment', false);
  7636. $result = $this->Form->control('0.comments.1.comment');
  7637. //@codingStandardsIgnoreStart
  7638. $expected = [
  7639. 'div' => ['class' => 'input textarea required'],
  7640. 'label' => ['for' => '0-comments-1-comment'],
  7641. 'Comment',
  7642. '/label',
  7643. 'textarea' => [
  7644. 'name',
  7645. 'required' => 'required',
  7646. 'id' => '0-comments-1-comment',
  7647. 'rows' => 5
  7648. ],
  7649. '/textarea',
  7650. '/div'
  7651. ];
  7652. //@codingStandardsIgnoreEnd
  7653. $this->assertHtml($expected, $result);
  7654. }
  7655. /**
  7656. * testHtml5Controls method
  7657. *
  7658. * Test that some html5 inputs + FormHelper::__call() work.
  7659. *
  7660. * @return void
  7661. */
  7662. public function testHtml5Controls()
  7663. {
  7664. $result = $this->Form->email('User.email');
  7665. $expected = [
  7666. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7667. ];
  7668. $this->assertHtml($expected, $result);
  7669. $result = $this->Form->search('User.query');
  7670. $expected = [
  7671. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7672. ];
  7673. $this->assertHtml($expected, $result);
  7674. $result = $this->Form->search('User.query', ['value' => 'test']);
  7675. $expected = [
  7676. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7677. ];
  7678. $this->assertHtml($expected, $result);
  7679. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7680. $expected = [
  7681. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7682. ];
  7683. $this->assertHtml($expected, $result);
  7684. }
  7685. /**
  7686. * testHtml5ControlWithControl method
  7687. *
  7688. * Test accessing html5 inputs through control().
  7689. *
  7690. * @return void
  7691. */
  7692. public function testHtml5ControlWithControl()
  7693. {
  7694. $this->Form->create();
  7695. $this->Form->templates(['inputContainer' => '{{content}}']);
  7696. $result = $this->Form->control('website', [
  7697. 'type' => 'url',
  7698. 'val' => 'http://domain.tld',
  7699. 'label' => false
  7700. ]);
  7701. $expected = [
  7702. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7703. ];
  7704. $this->assertHtml($expected, $result);
  7705. }
  7706. /**
  7707. * testHtml5ControlException method
  7708. *
  7709. * Test errors when field name is missing.
  7710. *
  7711. * @expectedException \Cake\Core\Exception\Exception
  7712. * @return void
  7713. */
  7714. public function testHtml5ControlException()
  7715. {
  7716. $this->Form->email();
  7717. }
  7718. /**
  7719. * testRequiredAttribute method
  7720. *
  7721. * Tests that formhelper sets required attributes.
  7722. *
  7723. * @return void
  7724. */
  7725. public function testRequiredAttribute()
  7726. {
  7727. $this->article['required'] = [
  7728. 'title' => true,
  7729. 'body' => false,
  7730. ];
  7731. $this->Form->create($this->article);
  7732. $result = $this->Form->control('title');
  7733. $expected = [
  7734. 'div' => ['class' => 'input text required'],
  7735. 'label' => ['for' => 'title'],
  7736. 'Title',
  7737. '/label',
  7738. 'input' => [
  7739. 'type' => 'text',
  7740. 'name' => 'title',
  7741. 'id' => 'title',
  7742. 'required' => 'required',
  7743. ],
  7744. '/div'
  7745. ];
  7746. $this->assertHtml($expected, $result);
  7747. $result = $this->Form->control('title', ['required' => false]);
  7748. $this->assertNotContains('required', $result);
  7749. $result = $this->Form->control('body');
  7750. $expected = [
  7751. 'div' => ['class' => 'input text'],
  7752. 'label' => ['for' => 'body'],
  7753. 'Body',
  7754. '/label',
  7755. 'input' => [
  7756. 'type' => 'text',
  7757. 'name' => 'body',
  7758. 'id' => 'body',
  7759. ],
  7760. '/div'
  7761. ];
  7762. $this->assertHtml($expected, $result);
  7763. $result = $this->Form->control('body', ['required' => true]);
  7764. $this->assertContains('required', $result);
  7765. }
  7766. /**
  7767. * testControlsNotNested method
  7768. *
  7769. * Tests that it is possible to put inputs outside of the label.
  7770. *
  7771. * @return void
  7772. */
  7773. public function testControlsNotNested()
  7774. {
  7775. $this->Form->templates([
  7776. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7777. 'formGroup' => '{{input}}{{label}}',
  7778. ]);
  7779. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7780. $expected = [
  7781. 'div' => ['class' => 'input checkbox'],
  7782. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7783. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7784. 'label' => ['for' => 'foo'],
  7785. 'Foo',
  7786. '/label',
  7787. '/div'
  7788. ];
  7789. $this->assertHtml($expected, $result);
  7790. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7791. $expected = [
  7792. 'div' => ['class' => 'input checkbox'],
  7793. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7794. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7795. '/div'
  7796. ];
  7797. $this->assertHtml($expected, $result);
  7798. $result = $this->Form->control('confirm', [
  7799. 'type' => 'radio',
  7800. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7801. ]);
  7802. $expected = [
  7803. 'div' => ['class' => 'input radio'],
  7804. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7805. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7806. ['label' => ['for' => 'confirm-y']],
  7807. 'Yes',
  7808. '/label',
  7809. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7810. ['label' => ['for' => 'confirm-n']],
  7811. 'No',
  7812. '/label',
  7813. '<label',
  7814. 'Confirm',
  7815. '/label',
  7816. '/div',
  7817. ];
  7818. $this->assertHtml($expected, $result);
  7819. $result = $this->Form->select('category', ['1', '2'], [
  7820. 'multiple' => 'checkbox',
  7821. 'name' => 'fish',
  7822. ]);
  7823. $expected = [
  7824. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7825. ['div' => ['class' => 'checkbox']],
  7826. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7827. ['label' => ['for' => 'fish-0']],
  7828. '1',
  7829. '/label',
  7830. '/div',
  7831. ['div' => ['class' => 'checkbox']],
  7832. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7833. ['label' => ['for' => 'fish-1']],
  7834. '2',
  7835. '/label',
  7836. '/div'
  7837. ];
  7838. $this->assertHtml($expected, $result);
  7839. }
  7840. /**
  7841. * testControlContainerTemplates method
  7842. *
  7843. * Test that *Container templates are used by input.
  7844. *
  7845. * @return void
  7846. */
  7847. public function testControlContainerTemplates()
  7848. {
  7849. $this->Form->templates([
  7850. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7851. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7852. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7853. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7854. ]);
  7855. $this->article['errors'] = [
  7856. 'Article' => ['published' => 'error message']
  7857. ];
  7858. $this->Form->create($this->article);
  7859. $result = $this->Form->control('accept', [
  7860. 'type' => 'checkbox'
  7861. ]);
  7862. $expected = [
  7863. 'div' => ['class' => 'check'],
  7864. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7865. 'label' => ['for' => 'accept'],
  7866. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7867. 'Accept',
  7868. '/label',
  7869. '/div'
  7870. ];
  7871. $this->assertHtml($expected, $result);
  7872. $result = $this->Form->control('accept', [
  7873. 'type' => 'radio',
  7874. 'options' => ['Y', 'N']
  7875. ]);
  7876. $this->assertContains('<div class="rad">', $result);
  7877. $result = $this->Form->control('Article.published', [
  7878. 'type' => 'radio',
  7879. 'options' => ['Y', 'N']
  7880. ]);
  7881. $this->assertContains('<div class="rad err">', $result);
  7882. $result = $this->Form->control('Article.created', [
  7883. 'type' => 'datetime',
  7884. ]);
  7885. $this->assertContains('<div class="dt">', $result);
  7886. }
  7887. /**
  7888. * testFormGroupTemplates method
  7889. *
  7890. * Test that *Container templates are used by input.
  7891. *
  7892. * @return void
  7893. */
  7894. public function testFormGroupTemplates()
  7895. {
  7896. $this->Form->templates([
  7897. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7898. ]);
  7899. $this->Form->create($this->article);
  7900. $result = $this->Form->control('accept', [
  7901. 'type' => 'radio',
  7902. 'options' => ['Y', 'N']
  7903. ]);
  7904. $this->assertContains('<div class="radio">', $result);
  7905. }
  7906. /**
  7907. * testResetTemplates method
  7908. *
  7909. * Test resetting templates.
  7910. *
  7911. * @return void
  7912. */
  7913. public function testResetTemplates()
  7914. {
  7915. $this->Form->templates(['input' => '<input/>']);
  7916. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  7917. $this->Form->resetTemplates();
  7918. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7919. }
  7920. /**
  7921. * testContext method
  7922. *
  7923. * Test the context method.
  7924. *
  7925. * @return void
  7926. */
  7927. public function testContext()
  7928. {
  7929. $result = $this->Form->context();
  7930. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7931. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7932. $this->assertSame($mock, $this->Form->context($mock));
  7933. $this->assertSame($mock, $this->Form->context());
  7934. }
  7935. /**
  7936. * testAutoDomId method
  7937. *
  7938. * @return void
  7939. */
  7940. public function testAutoDomId()
  7941. {
  7942. $result = $this->Form->text('field', ['id' => true]);
  7943. $expected = [
  7944. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7945. ];
  7946. $this->assertHtml($expected, $result);
  7947. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7948. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7949. $expected = [
  7950. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7951. ['label' => ['for' => 'field-0']],
  7952. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7953. 'option A',
  7954. '/label',
  7955. ['label' => ['for' => 'field-1']],
  7956. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7957. 'option B',
  7958. '/label',
  7959. ];
  7960. $this->assertHtml($expected, $result);
  7961. $result = $this->Form->select(
  7962. 'multi_field',
  7963. ['first', 'second'],
  7964. ['multiple' => 'checkbox', 'id' => true]
  7965. );
  7966. $expected = [
  7967. 'input' => [
  7968. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  7969. ],
  7970. ['div' => ['class' => 'checkbox']],
  7971. ['label' => ['for' => 'multi-field-0']],
  7972. ['input' => [
  7973. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7974. 'value' => '0', 'id' => 'multi-field-0'
  7975. ]],
  7976. 'first',
  7977. '/label',
  7978. '/div',
  7979. ['div' => ['class' => 'checkbox']],
  7980. ['label' => ['for' => 'multi-field-1']],
  7981. ['input' => [
  7982. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7983. 'value' => '1', 'id' => 'multi-field-1'
  7984. ]],
  7985. 'second',
  7986. '/label',
  7987. '/div',
  7988. ];
  7989. $this->assertHtml($expected, $result);
  7990. }
  7991. /**
  7992. * Test the basic setters and getters for value sources
  7993. *
  7994. * @return void
  7995. */
  7996. public function testFormValueSourcesSettersGetters()
  7997. {
  7998. $expected = ['context'];
  7999. $result = $this->Form->getValueSources();
  8000. $this->assertEquals($expected, $result);
  8001. $expected = null;
  8002. $result = $this->Form->getSourceValue('id');
  8003. $this->assertEquals($expected, $result);
  8004. $expected = ['query', 'data', 'context'];
  8005. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  8006. $result = $this->Form->getValueSources();
  8007. $this->assertEquals($expected, $result);
  8008. $this->Form->request->data['id'] = '1';
  8009. $this->Form->request->query['id'] = '2';
  8010. $this->Form->setValueSources(['context']);
  8011. $expected = '1';
  8012. $result = $this->Form->getSourceValue('id');
  8013. $this->assertEquals($expected, $result);
  8014. $this->Form->setValueSources('query');
  8015. $expected = ['query'];
  8016. $result = $this->Form->getValueSources();
  8017. $this->assertEquals($expected, $result);
  8018. $expected = '2';
  8019. $result = $this->Form->getSourceValue('id');
  8020. $this->assertEquals($expected, $result);
  8021. $this->Form->setValueSources(['data']);
  8022. $expected = '1';
  8023. $result = $this->Form->getSourceValue('id');
  8024. $this->assertEquals($expected, $result);
  8025. $this->Form->setValueSources(['query', 'data']);
  8026. $expected = '2';
  8027. $result = $this->Form->getSourceValue('id');
  8028. $this->assertEquals($expected, $result);
  8029. }
  8030. /**
  8031. * Tests the different input rendering values based on sources values switching
  8032. *
  8033. * @return void
  8034. */
  8035. public function testFormValueSourcesSingleSwitchRendering()
  8036. {
  8037. $this->loadFixtures('Articles');
  8038. $articles = TableRegistry::get('Articles');
  8039. $article = new Article();
  8040. $articles->patchEntity($article, ['id' => '3']);
  8041. $this->Form->create($article);
  8042. $this->Form->setValueSources(['context']);
  8043. $result = $this->Form->control('id');
  8044. $expected = [
  8045. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8046. ];
  8047. $this->assertHtml($expected, $result);
  8048. $this->Form->request->query['id'] = '5';
  8049. $this->Form->setValueSources(['query']);
  8050. $result = $this->Form->control('id');
  8051. $expected = [
  8052. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8053. ];
  8054. $this->assertHtml($expected, $result);
  8055. $this->Form->request->query['id'] = '5a';
  8056. $this->Form->request->data['id'] = '5b';
  8057. $this->Form->setValueSources(['context']);
  8058. $this->Form->create($article);
  8059. $result = $this->Form->control('id');
  8060. $expected = [
  8061. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8062. ];
  8063. $this->assertHtml($expected, $result);
  8064. $this->Form->setValueSources(['data']);
  8065. $this->Form->create($article);
  8066. $result = $this->Form->control('id');
  8067. $expected = [
  8068. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8069. ];
  8070. $this->assertHtml($expected, $result);
  8071. $this->Form->setValueSources(['query']);
  8072. $result = $this->Form->control('id');
  8073. $expected = [
  8074. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  8075. ];
  8076. $this->assertHtml($expected, $result);
  8077. }
  8078. /**
  8079. * Tests the different input rendering values based on sources values switching while supplying
  8080. * an entity (base context) and multiple sources (such as data, query)
  8081. *
  8082. * @return void
  8083. */
  8084. public function testFormValueSourcesListSwitchRendering()
  8085. {
  8086. $this->loadFixtures('Articles');
  8087. $articles = TableRegistry::get('Articles');
  8088. $article = new Article();
  8089. $articles->patchEntity($article, ['id' => '3']);
  8090. $this->Form->request->query['id'] = '9';
  8091. $this->Form->create($article);
  8092. $this->Form->setValueSources(['context', 'query']);
  8093. $result = $this->Form->control('id');
  8094. $expected = [
  8095. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8096. ];
  8097. $this->assertHtml($expected, $result);
  8098. $this->Form->setValueSources(['query', 'context']);
  8099. $result = $this->Form->control('id');
  8100. $expected = [
  8101. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8102. ];
  8103. $this->assertHtml($expected, $result);
  8104. $this->Form->setValueSources(['data', 'query', 'context']);
  8105. $result = $this->Form->control('id');
  8106. $expected = [
  8107. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8108. ];
  8109. $this->assertHtml($expected, $result);
  8110. $this->Form->request->data['id'] = '8';
  8111. $this->Form->request->query['id'] = '9';
  8112. $this->Form->setValueSources(['data', 'query', 'context']);
  8113. $result = $this->Form->control('id');
  8114. $expected = [
  8115. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  8116. ];
  8117. $this->assertHtml($expected, $result);
  8118. }
  8119. /**
  8120. * Test the different form input renderings based on values sources switchings through form options
  8121. *
  8122. * @return void
  8123. */
  8124. public function testFormValueSourcesSwitchViaOptionsRendering()
  8125. {
  8126. $this->loadFixtures('Articles');
  8127. $articles = TableRegistry::get('Articles');
  8128. $article = new Article();
  8129. $articles->patchEntity($article, ['id' => '3']);
  8130. $this->Form->request->data['id'] = '4';
  8131. $this->Form->request->query['id'] = '5';
  8132. $this->Form->create($article, ['valueSources' => 'query']);
  8133. $result = $this->Form->control('id');
  8134. $expected = [
  8135. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8136. ];
  8137. $this->assertHtml($expected, $result);
  8138. $result = $this->Form->getSourceValue('id');
  8139. $this->assertEquals('5', $result);
  8140. $this->Form->setValueSources(['context']);
  8141. $this->Form->create($article, ['valueSources' => 'query']);
  8142. $result = $this->Form->control('id');
  8143. $expected = [
  8144. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8145. ];
  8146. $this->assertHtml($expected, $result);
  8147. $result = $this->Form->getSourceValue('id');
  8148. $this->assertEquals('5', $result);
  8149. $this->Form->setValueSources(['query']);
  8150. $this->Form->create($article, ['valueSources' => 'data']);
  8151. $result = $this->Form->control('id');
  8152. $expected = [
  8153. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8154. ];
  8155. $this->assertHtml($expected, $result);
  8156. $result = $this->Form->getSourceValue('id');
  8157. $this->assertEquals('4', $result);
  8158. $this->Form->setValueSources(['query']);
  8159. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8160. $result = $this->Form->control('id');
  8161. $expected = [
  8162. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8163. ];
  8164. $this->assertHtml($expected, $result);
  8165. $result = $this->Form->getSourceValue('id');
  8166. $this->assertEquals('4', $result);
  8167. }
  8168. /**
  8169. * Test the different form input renderings based on values sources switchings through form options
  8170. *
  8171. * @return void
  8172. */
  8173. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8174. {
  8175. $this->loadFixtures('Articles');
  8176. $articles = TableRegistry::get('Articles');
  8177. $article = new Article();
  8178. $articles->patchEntity($article, ['id' => '3']);
  8179. $this->Form->request->data['id'] = '10';
  8180. $this->Form->request->query['id'] = '11';
  8181. $this->Form->setValueSources(['context'])
  8182. ->create($article, ['valueSources' => ['query', 'data']]);
  8183. $result = $this->Form->control('id');
  8184. $expected = [
  8185. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8186. ];
  8187. $this->assertHtml($expected, $result);
  8188. $result = $this->Form->getSourceValue('id');
  8189. $this->assertEquals('11', $result);
  8190. unset($this->Form->request->query['id']);
  8191. $this->Form->setValueSources(['context'])
  8192. ->create($article, ['valueSources' => ['query', 'data']]);
  8193. $result = $this->Form->control('id');
  8194. $expected = [
  8195. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8196. ];
  8197. $this->assertHtml($expected, $result);
  8198. $result = $this->Form->getSourceValue('id');
  8199. $this->assertEquals('10', $result);
  8200. }
  8201. /**
  8202. * Test the different form values sources resetting through From::end();
  8203. *
  8204. * @return void
  8205. */
  8206. public function testFormValueSourcesResetViaEnd()
  8207. {
  8208. $expected = ['context'];
  8209. $result = $this->Form->getValueSources();
  8210. $this->assertEquals($expected, $result);
  8211. $expected = ['query', 'context', 'data'];
  8212. $this->Form->setValueSources(['query', 'context', 'data']);
  8213. $result = $this->Form->getValueSources();
  8214. $this->assertEquals($expected, $result);
  8215. $this->Form->create();
  8216. $result = $this->Form->getValueSources();
  8217. $this->assertEquals($expected, $result);
  8218. $this->Form->end();
  8219. $result = $this->Form->getValueSources();
  8220. $this->assertEquals(['context'], $result);
  8221. }
  8222. /**
  8223. * Test sources values defaults handling
  8224. *
  8225. * @return void
  8226. */
  8227. public function testFormValueSourcesDefaults()
  8228. {
  8229. $this->Form->request->query['password'] = 'open Sesame';
  8230. $this->Form->create();
  8231. $result = $this->Form->password('password');
  8232. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8233. $this->assertHtml($expected, $result);
  8234. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8235. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8236. $this->assertHtml($expected, $result);
  8237. $this->Form->setValueSources('query');
  8238. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8239. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8240. $this->assertHtml($expected, $result);
  8241. $this->Form->setValueSources('data');
  8242. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8243. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8244. $this->assertHtml($expected, $result);
  8245. }
  8246. /**
  8247. * Test sources values schema defaults handling
  8248. *
  8249. * @return void
  8250. */
  8251. public function testSourcesValueDoesntExistPassThrough()
  8252. {
  8253. $this->Form->request->query['category'] = 'sesame-cookies';
  8254. $articles = TableRegistry::get('Articles');
  8255. $entity = $articles->newEntity();
  8256. $this->Form->create($entity);
  8257. $this->Form->setValueSources(['query', 'context']);
  8258. $result = $this->Form->getSourceValue('category');
  8259. $this->assertEquals('sesame-cookies', $result);
  8260. $this->Form->setValueSources(['context', 'query']);
  8261. $result = $this->Form->getSourceValue('category');
  8262. $this->assertEquals('sesame-cookies', $result);
  8263. }
  8264. /**
  8265. * testNestedLabelInput method
  8266. *
  8267. * Test the `nestedInput` parameter
  8268. *
  8269. * @return void
  8270. */
  8271. public function testNestedLabelInput()
  8272. {
  8273. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8274. $expected = [
  8275. 'div' => ['class' => 'input text'],
  8276. 'label' => ['for' => 'foo'],
  8277. ['input' => [
  8278. 'type' => 'text',
  8279. 'name' => 'foo',
  8280. 'id' => 'foo'
  8281. ]],
  8282. 'Foo',
  8283. '/label',
  8284. '/div'
  8285. ];
  8286. $this->assertHtml($expected, $result);
  8287. }
  8288. /**
  8289. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8290. *
  8291. * This test makes sure `false` excludes the label from the render
  8292. *
  8293. * @return void
  8294. */
  8295. public function testInputLabelManipulationDisableLabels()
  8296. {
  8297. $result = $this->Form->input('test', [
  8298. 'type' => 'radio',
  8299. 'options' => ['A', 'B'],
  8300. 'labelOptions' => false
  8301. ]);
  8302. $expected = [
  8303. ['div' => ['class' => 'input radio']],
  8304. '<label',
  8305. 'Test',
  8306. '/label',
  8307. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8308. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8309. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8310. '/div',
  8311. ];
  8312. $this->assertHtml($expected, $result);
  8313. $result = $this->Form->input('checkbox1', [
  8314. 'label' => 'My checkboxes',
  8315. 'multiple' => 'checkbox',
  8316. 'type' => 'select',
  8317. 'options' => [
  8318. ['text' => 'First Checkbox', 'value' => 1],
  8319. ['text' => 'Second Checkbox', 'value' => 2]
  8320. ],
  8321. 'labelOptions' => false
  8322. ]);
  8323. $expected = [
  8324. ['div' => ['class' => 'input select']],
  8325. ['label' => ['for' => 'checkbox1']],
  8326. 'My checkboxes',
  8327. '/label',
  8328. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8329. ['div' => ['class' => 'checkbox']],
  8330. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8331. '/div',
  8332. ['div' => ['class' => 'checkbox']],
  8333. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8334. '/div',
  8335. '/div'
  8336. ];
  8337. $this->assertHtml($expected, $result);
  8338. }
  8339. /**
  8340. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8341. *
  8342. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8343. * added to the class if checked.
  8344. *
  8345. * Also checks to make sure any custom attributes are rendered correctly
  8346. *
  8347. * @return void
  8348. */
  8349. public function testInputLabelManipulationRadios()
  8350. {
  8351. $result = $this->Form->input('test', [
  8352. 'type' => 'radio',
  8353. 'options' => ['A', 'B'],
  8354. 'labelOptions' => ['class' => 'custom-class']
  8355. ]);
  8356. $expected = [
  8357. ['div' => ['class' => 'input radio']],
  8358. '<label',
  8359. 'Test',
  8360. '/label',
  8361. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8362. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8363. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8364. 'A',
  8365. '/label',
  8366. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8367. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8368. 'B',
  8369. '/label',
  8370. '/div',
  8371. ];
  8372. $this->assertHtml($expected, $result);
  8373. $result = $this->Form->input('test', [
  8374. 'type' => 'radio',
  8375. 'options' => ['A', 'B'],
  8376. 'value' => 1,
  8377. 'labelOptions' => ['class' => 'custom-class']
  8378. ]);
  8379. $expected = [
  8380. ['div' => ['class' => 'input radio']],
  8381. '<label',
  8382. 'Test',
  8383. '/label',
  8384. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8385. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8386. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8387. 'A',
  8388. '/label',
  8389. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8390. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8391. 'B',
  8392. '/label',
  8393. '/div',
  8394. ];
  8395. $this->assertHtml($expected, $result);
  8396. $result = $this->Form->input('test', [
  8397. 'type' => 'radio',
  8398. 'options' => ['A', 'B'],
  8399. 'value' => 1,
  8400. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']]
  8401. ]);
  8402. $expected = [
  8403. ['div' => ['class' => 'input radio']],
  8404. '<label',
  8405. 'Test',
  8406. '/label',
  8407. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8408. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8409. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8410. 'A',
  8411. '/label',
  8412. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8413. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8414. 'B',
  8415. '/label',
  8416. '/div',
  8417. ];
  8418. $this->assertHtml($expected, $result);
  8419. $result = $this->Form->radio('test', ['A', 'B'], [
  8420. 'label' => [
  8421. 'class' => ['custom-class', 'another-class'],
  8422. 'data-name' => 'bob'
  8423. ],
  8424. 'value' => 1
  8425. ]);
  8426. $expected = [
  8427. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8428. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8429. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8430. 'A',
  8431. '/label',
  8432. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8433. ['input' => [
  8434. 'type' => 'radio',
  8435. 'name' => 'test',
  8436. 'value' => '1',
  8437. 'id' => 'test-1',
  8438. 'checked' => 'checked'
  8439. ]],
  8440. 'B',
  8441. '/label',
  8442. ];
  8443. $this->assertHtml($expected, $result);
  8444. }
  8445. /**
  8446. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8447. *
  8448. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8449. * added to the class if checked.
  8450. *
  8451. * Also checks to make sure any custom attributes are rendered correctly
  8452. *
  8453. * @return void
  8454. */
  8455. public function testInputLabelManipulationCheckboxes()
  8456. {
  8457. $result = $this->Form->input('checkbox1', [
  8458. 'label' => 'My checkboxes',
  8459. 'multiple' => 'checkbox',
  8460. 'type' => 'select',
  8461. 'options' => [
  8462. ['text' => 'First Checkbox', 'value' => 1],
  8463. ['text' => 'Second Checkbox', 'value' => 2]
  8464. ],
  8465. 'labelOptions' => ['class' => 'custom-class'],
  8466. 'value' => ['1']
  8467. ]);
  8468. $expected = [
  8469. ['div' => ['class' => 'input select']],
  8470. ['label' => ['for' => 'checkbox1']],
  8471. 'My checkboxes',
  8472. '/label',
  8473. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8474. ['div' => ['class' => 'checkbox']],
  8475. ['label' => [
  8476. 'class' => 'custom-class selected',
  8477. 'for' => 'checkbox1-1'
  8478. ]],
  8479. ['input' => [
  8480. 'type' => 'checkbox',
  8481. 'name' => 'checkbox1[]',
  8482. 'value' => '1',
  8483. 'id' => 'checkbox1-1',
  8484. 'checked' => 'checked'
  8485. ]],
  8486. 'First Checkbox',
  8487. '/label',
  8488. '/div',
  8489. ['div' => ['class' => 'checkbox']],
  8490. ['label' => [
  8491. 'class' => 'custom-class',
  8492. 'for' => 'checkbox1-2'
  8493. ]],
  8494. ['input' => [
  8495. 'type' => 'checkbox',
  8496. 'name' => 'checkbox1[]',
  8497. 'value' => '2',
  8498. 'id' => 'checkbox1-2'
  8499. ]],
  8500. 'Second Checkbox',
  8501. '/label',
  8502. '/div',
  8503. '/div'
  8504. ];
  8505. $this->assertHtml($expected, $result);
  8506. $result = $this->Form->input('checkbox1', [
  8507. 'label' => 'My checkboxes',
  8508. 'multiple' => 'checkbox',
  8509. 'type' => 'select',
  8510. 'options' => [
  8511. ['text' => 'First Checkbox', 'value' => 1],
  8512. ['text' => 'Second Checkbox', 'value' => 2]
  8513. ],
  8514. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8515. 'value' => ['1']
  8516. ]);
  8517. $expected = [
  8518. ['div' => ['class' => 'input select']],
  8519. ['label' => ['for' => 'checkbox1']],
  8520. 'My checkboxes',
  8521. '/label',
  8522. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8523. ['div' => ['class' => 'checkbox']],
  8524. ['label' => [
  8525. 'class' => 'custom-class another-class selected',
  8526. 'data-name' => 'bob',
  8527. 'for' => 'checkbox1-1'
  8528. ]],
  8529. ['input' => [
  8530. 'type' => 'checkbox',
  8531. 'name' => 'checkbox1[]',
  8532. 'value' => '1',
  8533. 'id' => 'checkbox1-1',
  8534. 'checked' => 'checked'
  8535. ]],
  8536. 'First Checkbox',
  8537. '/label',
  8538. '/div',
  8539. ['div' => ['class' => 'checkbox']],
  8540. ['label' => [
  8541. 'class' => 'custom-class another-class',
  8542. 'data-name' => 'bob',
  8543. 'for' => 'checkbox1-2'
  8544. ]],
  8545. ['input' => [
  8546. 'type' => 'checkbox',
  8547. 'name' => 'checkbox1[]',
  8548. 'value' => '2',
  8549. 'id' => 'checkbox1-2'
  8550. ]],
  8551. 'Second Checkbox',
  8552. '/label',
  8553. '/div',
  8554. '/div'
  8555. ];
  8556. $this->assertHtml($expected, $result);
  8557. }
  8558. }