FormHelperTest.php 281 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license http://www.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\Network\Request;
  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 FormHelper $Form
  103. */
  104. class FormHelperTest extends TestCase
  105. {
  106. /**
  107. * Fixtures to be used
  108. *
  109. * @var array
  110. */
  111. public $fixtures = ['core.articles', 'core.comments'];
  112. /**
  113. * Do not load the fixtures by default
  114. *
  115. * @var bool
  116. */
  117. public $autoFixtures = false;
  118. /**
  119. * setUp method
  120. *
  121. * @return void
  122. */
  123. public function setUp()
  124. {
  125. parent::setUp();
  126. Configure::write('Config.language', 'eng');
  127. Configure::write('App.base', '');
  128. Configure::write('App.namespace', 'Cake\Test\TestCase\View\Helper');
  129. $this->View = new View();
  130. $this->Form = new FormHelper($this->View);
  131. $request = new Request([
  132. 'webroot' => '',
  133. 'base' => '',
  134. 'url' => '/articles/add',
  135. 'params' => [
  136. 'controller' => 'articles',
  137. 'action' => 'add',
  138. ]
  139. ]);
  140. $this->Form->Url->request = $this->Form->request = $request;
  141. $this->dateRegex = [
  142. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  143. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  144. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  145. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  146. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  147. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  148. ];
  149. $this->article = [
  150. 'schema' => [
  151. 'id' => ['type' => 'integer'],
  152. 'author_id' => ['type' => 'integer', 'null' => true],
  153. 'title' => ['type' => 'string', 'null' => true],
  154. 'body' => 'text',
  155. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  156. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  157. ],
  158. 'required' => [
  159. 'author_id' => true,
  160. 'title' => true,
  161. ]
  162. ];
  163. Security::salt('foo!');
  164. Router::connect('/:controller', ['action' => 'index']);
  165. Router::connect('/:controller/:action/*');
  166. }
  167. /**
  168. * tearDown method
  169. *
  170. * @return void
  171. */
  172. public function tearDown()
  173. {
  174. parent::tearDown();
  175. unset($this->Form, $this->Controller, $this->View);
  176. TableRegistry::clear();
  177. }
  178. /**
  179. * Test construct() with the templates option.
  180. *
  181. * @return void
  182. */
  183. public function testConstructTemplatesFile()
  184. {
  185. $helper = new FormHelper($this->View, [
  186. 'templates' => 'htmlhelper_tags'
  187. ]);
  188. $result = $helper->input('name');
  189. $this->assertContains('<input', $result);
  190. }
  191. /**
  192. * Test that when specifying custom widgets the config array for that widget
  193. * is overwritten instead of merged.
  194. *
  195. * @return void
  196. */
  197. public function testConstructWithWidgets()
  198. {
  199. $config = [
  200. 'widgets' => [
  201. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
  202. ]
  203. ];
  204. $helper = new FormHelper($this->View, $config);
  205. $registry = $helper->widgetRegistry();
  206. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('datetime'));
  207. }
  208. /**
  209. * Test that when specifying custom widgets config file and it should be
  210. * added to widgets array. WidgetRegistry will load widgets in constructor.
  211. *
  212. * @return void
  213. */
  214. public function testConstructWithWidgetsConfig()
  215. {
  216. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  217. $registry = $helper->widgetRegistry();
  218. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('text'));
  219. }
  220. /**
  221. * Test registering a new widget class and rendering it.
  222. *
  223. * @return void
  224. */
  225. public function testAddWidgetAndRenderWidget()
  226. {
  227. $data = [
  228. 'val' => 1
  229. ];
  230. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  231. $this->assertNull($this->Form->addWidget('test', $mock));
  232. $mock->expects($this->once())
  233. ->method('render')
  234. ->with($data)
  235. ->will($this->returnValue('HTML'));
  236. $result = $this->Form->widget('test', $data);
  237. $this->assertEquals('HTML', $result);
  238. }
  239. /**
  240. * Test that secureFields() of widget is called after calling render(),
  241. * not before.
  242. *
  243. * @return void
  244. */
  245. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  246. {
  247. $data = [
  248. 'val' => 1,
  249. 'name' => 'test'
  250. ];
  251. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  252. $this->assertNull($this->Form->addWidget('test', $mock));
  253. $mock->expects($this->at(0))
  254. ->method('render')
  255. ->with($data)
  256. ->will($this->returnValue('HTML'));
  257. $mock->expects($this->at(1))
  258. ->method('secureFields')
  259. ->with($data)
  260. ->will($this->returnValue(['test']));
  261. $result = $this->Form->widget('test', $data + ['secure' => true]);
  262. $this->assertEquals('HTML', $result);
  263. }
  264. /**
  265. * Test that empty string is not added to secure fields list when
  266. * rendering input widget without name.
  267. *
  268. * @return void
  269. */
  270. public function testRenderingWidgetWithEmptyName()
  271. {
  272. $this->assertEquals([], $this->Form->fields);
  273. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  274. $this->assertEquals('<select name=""></select>', $result);
  275. $this->assertEquals([], $this->Form->fields);
  276. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  277. $this->assertEquals('<select name="0"></select>', $result);
  278. $this->assertEquals(['0'], $this->Form->fields);
  279. }
  280. /**
  281. * Test registering an invalid widget class.
  282. *
  283. * @expectedException \RuntimeException
  284. * @return void
  285. */
  286. public function testAddWidgetInvalid()
  287. {
  288. $mock = new \StdClass();
  289. $this->Form->addWidget('test', $mock);
  290. $this->Form->widget('test');
  291. }
  292. /**
  293. * Test adding a new context class.
  294. *
  295. * @return void
  296. */
  297. public function testAddContextProvider()
  298. {
  299. $context = 'My data';
  300. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  301. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  302. $this->assertInstanceOf('Cake\Network\Request', $request);
  303. $this->assertEquals($context, $data['entity']);
  304. return $stub;
  305. });
  306. $this->Form->create($context);
  307. $result = $this->Form->context();
  308. $this->assertSame($stub, $result);
  309. }
  310. /**
  311. * Test replacing a context class.
  312. *
  313. * @return void
  314. */
  315. public function testAddContextProviderReplace()
  316. {
  317. $entity = new Article();
  318. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  319. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  320. return $stub;
  321. });
  322. $this->Form->create($entity);
  323. $result = $this->Form->context();
  324. $this->assertSame($stub, $result);
  325. }
  326. /**
  327. * Test overriding a context class.
  328. *
  329. * @return void
  330. */
  331. public function testAddContextProviderAdd()
  332. {
  333. $entity = new Article();
  334. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  335. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  336. if ($data['entity'] instanceof Entity) {
  337. return $stub;
  338. }
  339. });
  340. $this->Form->create($entity);
  341. $result = $this->Form->context();
  342. $this->assertSame($stub, $result);
  343. }
  344. /**
  345. * Test adding an invalid context class.
  346. *
  347. * @expectedException \RuntimeException
  348. * @expectedExceptionMessage Context objects must implement Cake\View\Form\ContextInterface
  349. * @return void
  350. */
  351. public function testAddContextProviderInvalid()
  352. {
  353. $context = 'My data';
  354. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  355. return new \StdClass();
  356. });
  357. $this->Form->create($context);
  358. }
  359. /**
  360. * Provides context options for create().
  361. *
  362. * @return array
  363. */
  364. public function contextSelectionProvider()
  365. {
  366. $entity = new Article();
  367. $collection = $this->getMockBuilder('Cake\Collection\Collection')
  368. ->setMethods(['extract'])
  369. ->setConstructorArgs([[$entity]])
  370. ->getMock();
  371. $emptyCollection = new Collection([]);
  372. $emptyArray = [];
  373. $arrayObject = new \ArrayObject([]);
  374. $data = [
  375. 'schema' => [
  376. 'title' => ['type' => 'string']
  377. ]
  378. ];
  379. $form = new Form();
  380. return [
  381. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  382. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  383. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  384. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  385. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  386. 'form' => [$form, 'Cake\View\Form\FormContext'],
  387. 'none' => [null, 'Cake\View\Form\NullContext'],
  388. 'false' => [false, 'Cake\View\Form\NullContext'],
  389. ];
  390. }
  391. /**
  392. * Test default context selection in create()
  393. *
  394. * @dataProvider contextSelectionProvider
  395. * @return void
  396. */
  397. public function testCreateContextSelectionBuiltIn($data, $class)
  398. {
  399. $this->loadFixtures('Articles');
  400. $this->Form->create($data);
  401. $this->assertInstanceOf($class, $this->Form->context());
  402. }
  403. /**
  404. * Data provider for type option.
  405. *
  406. * @return array
  407. */
  408. public static function requestTypeProvider()
  409. {
  410. return [
  411. // type, method, override
  412. ['post', 'post', 'POST'],
  413. ['put', 'post', 'PUT'],
  414. ['patch', 'post', 'PATCH'],
  415. ['delete', 'post', 'DELETE'],
  416. ];
  417. }
  418. /**
  419. * Test creating file forms.
  420. *
  421. * @return void
  422. */
  423. public function testCreateFile()
  424. {
  425. $encoding = strtolower(Configure::read('App.encoding'));
  426. $result = $this->Form->create(false, ['type' => 'file']);
  427. $expected = [
  428. 'form' => [
  429. 'method' => 'post', 'action' => '/articles/add',
  430. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  431. ],
  432. 'div' => ['style' => 'display:none;'],
  433. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  434. '/div'
  435. ];
  436. $this->assertHtml($expected, $result);
  437. }
  438. /**
  439. * Test creating GET forms.
  440. *
  441. * @return void
  442. */
  443. public function testCreateGet()
  444. {
  445. $encoding = strtolower(Configure::read('App.encoding'));
  446. $result = $this->Form->create(false, ['type' => 'get']);
  447. $expected = ['form' => [
  448. 'method' => 'get', 'action' => '/articles/add',
  449. 'accept-charset' => $encoding
  450. ]];
  451. $this->assertHtml($expected, $result);
  452. }
  453. /**
  454. * Test create() with the templates option.
  455. *
  456. * @return void
  457. */
  458. public function testCreateTemplatesArray()
  459. {
  460. $result = $this->Form->create($this->article, [
  461. 'templates' => [
  462. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  463. ]
  464. ]);
  465. $expected = [
  466. 'form' => [
  467. 'class' => 'form-horizontal',
  468. 'method' => 'post',
  469. 'action' => '/articles/add',
  470. 'accept-charset' => 'utf-8'
  471. ]
  472. ];
  473. $this->assertHtml($expected, $result);
  474. }
  475. /**
  476. * Test create() with the templates option.
  477. *
  478. * @return void
  479. */
  480. public function testCreateTemplatesFile()
  481. {
  482. $result = $this->Form->create($this->article, [
  483. 'templates' => 'htmlhelper_tags',
  484. ]);
  485. $expected = [
  486. 'start form',
  487. 'div' => ['class' => 'hidden'],
  488. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  489. '/div'
  490. ];
  491. $this->assertHtml($expected, $result);
  492. }
  493. /**
  494. * Test that create() and end() restore templates.
  495. *
  496. * @return void
  497. */
  498. public function testCreateEndRestoreTemplates()
  499. {
  500. $this->Form->create($this->article, [
  501. 'templates' => ['input' => 'custom input element']
  502. ]);
  503. $this->Form->end();
  504. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  505. }
  506. /**
  507. * Test using template vars in various templates used by input() method.
  508. *
  509. * @return void
  510. */
  511. public function testInputTemplateVars()
  512. {
  513. $result = $this->Form->input('text', [
  514. 'templates' => [
  515. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  516. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  517. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  518. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  519. ],
  520. 'templateVars' => [
  521. 'forinput' => 'in-input',
  522. 'forlabel' => 'in-label',
  523. 'forgroup' => 'in-group',
  524. 'forcontainer' => 'in-container'
  525. ]
  526. ]);
  527. $expected = [
  528. 'div' => ['class'],
  529. 'label' => ['for'],
  530. 'Text in-label',
  531. '/label',
  532. 'in-group',
  533. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  534. 'in-container',
  535. '/div',
  536. ];
  537. $this->assertHtml($expected, $result);
  538. }
  539. /**
  540. * Test ensuring template variables work in template files loaded
  541. * during input().
  542. *
  543. * @return void
  544. */
  545. public function testInputTemplatesFromFile()
  546. {
  547. $result = $this->Form->input('title', [
  548. 'templates' => 'test_templates',
  549. 'templateVars' => [
  550. 'forcontainer' => 'container-data'
  551. ]
  552. ]);
  553. $expected = [
  554. 'div' => ['class'],
  555. 'label' => ['for'],
  556. 'Title',
  557. '/label',
  558. 'input' => ['name', 'type' => 'text', 'id'],
  559. 'container-data',
  560. '/div',
  561. ];
  562. $this->assertHtml($expected, $result);
  563. }
  564. /**
  565. * Test using template vars in inputSubmit and submitContainer template.
  566. *
  567. * @return void
  568. */
  569. public function testSubmitTemplateVars()
  570. {
  571. $this->Form->templates([
  572. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  573. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  574. ]);
  575. $result = $this->Form->submit('Submit', [
  576. 'templateVars' => [
  577. 'forinput' => 'in-input',
  578. 'forcontainer' => 'in-container'
  579. ]
  580. ]);
  581. $expected = [
  582. 'div' => ['class'],
  583. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  584. 'in-container',
  585. '/div',
  586. ];
  587. $this->assertHtml($expected, $result);
  588. }
  589. /**
  590. * test the create() method
  591. *
  592. * @dataProvider requestTypeProvider
  593. * @return void
  594. */
  595. public function testCreateTypeOptions($type, $method, $override)
  596. {
  597. $encoding = strtolower(Configure::read('App.encoding'));
  598. $result = $this->Form->create(false, ['type' => $type]);
  599. $expected = [
  600. 'form' => [
  601. 'method' => $method, 'action' => '/articles/add',
  602. 'accept-charset' => $encoding
  603. ],
  604. 'div' => ['style' => 'display:none;'],
  605. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  606. '/div'
  607. ];
  608. $this->assertHtml($expected, $result);
  609. }
  610. /**
  611. * Test using template vars in Create (formStart template)
  612. *
  613. * @return void
  614. */
  615. public function testCreateTemplateVars()
  616. {
  617. $result = $this->Form->create($this->article, [
  618. 'templates' => [
  619. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  620. ],
  621. 'templateVars' => ['header' => 'headertext']
  622. ]);
  623. $expected = [
  624. 'h4' => ['class'],
  625. 'headertext',
  626. '/h4',
  627. 'form' => [
  628. 'method' => 'post',
  629. 'action' => '/articles/add',
  630. 'accept-charset' => 'utf-8'
  631. ]
  632. ];
  633. $this->assertHtml($expected, $result);
  634. }
  635. /**
  636. * Test opening a form for an update operation.
  637. *
  638. * @return void
  639. */
  640. public function testCreateUpdateForm()
  641. {
  642. $encoding = strtolower(Configure::read('App.encoding'));
  643. $this->Form->request->here = '/articles/edit/1';
  644. $this->Form->request->params['action'] = 'edit';
  645. $this->article['defaults']['id'] = 1;
  646. $result = $this->Form->create($this->article);
  647. $expected = [
  648. 'form' => [
  649. 'method' => 'post',
  650. 'action' => '/articles/edit/1',
  651. 'accept-charset' => $encoding
  652. ],
  653. 'div' => ['style' => 'display:none;'],
  654. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  655. '/div'
  656. ];
  657. $this->assertHtml($expected, $result);
  658. }
  659. /**
  660. * test create() with automatic url generation
  661. *
  662. * @return void
  663. */
  664. public function testCreateAutoUrl()
  665. {
  666. $encoding = strtolower(Configure::read('App.encoding'));
  667. $this->Form->request->params['action'] = 'delete';
  668. $this->Form->request->here = '/articles/delete/10';
  669. $this->Form->request->base = '';
  670. $result = $this->Form->create($this->article);
  671. $expected = [
  672. 'form' => [
  673. 'method' => 'post', 'action' => '/articles/delete/10',
  674. 'accept-charset' => $encoding
  675. ],
  676. 'div' => ['style' => 'display:none;'],
  677. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  678. '/div'
  679. ];
  680. $this->assertHtml($expected, $result);
  681. $this->article['defaults'] = ['id' => 1];
  682. $this->Form->request->here = '/articles/edit/1';
  683. $this->Form->request->params['action'] = 'delete';
  684. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  685. $expected = [
  686. 'form' => [
  687. 'method' => 'post',
  688. 'action' => '/articles/edit/1',
  689. 'accept-charset' => $encoding
  690. ],
  691. 'div' => ['style' => 'display:none;'],
  692. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  693. '/div'
  694. ];
  695. $this->assertHtml($expected, $result);
  696. $this->Form->request->params['action'] = 'add';
  697. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  698. $expected = [
  699. 'form' => [
  700. 'method' => 'post',
  701. 'action' => '/articles/publish/1',
  702. 'accept-charset' => $encoding
  703. ],
  704. 'div' => ['style' => 'display:none;'],
  705. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  706. '/div'
  707. ];
  708. $this->assertHtml($expected, $result);
  709. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  710. $expected = [
  711. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  712. 'div' => ['style' => 'display:none;'],
  713. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  714. '/div'
  715. ];
  716. $this->assertHtml($expected, $result);
  717. $this->Form->request->params['controller'] = 'Pages';
  718. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  719. $expected = [
  720. 'form' => [
  721. 'method' => 'post', 'action' => '/Pages/signup/1',
  722. 'accept-charset' => $encoding
  723. ],
  724. 'div' => ['style' => 'display:none;'],
  725. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  726. '/div'
  727. ];
  728. $this->assertHtml($expected, $result);
  729. }
  730. /**
  731. * Test create() with no URL (no "action" attribute for <form> tag)
  732. *
  733. * @return void
  734. */
  735. public function testCreateNoUrl()
  736. {
  737. $result = $this->Form->create(false, ['url' => false]);
  738. $expected = [
  739. 'form' => [
  740. 'method' => 'post',
  741. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  742. ],
  743. 'div' => ['style' => 'display:none;'],
  744. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  745. '/div'
  746. ];
  747. $this->assertHtml($expected, $result);
  748. }
  749. /**
  750. * test create() with a custom route
  751. *
  752. * @return void
  753. */
  754. public function testCreateCustomRoute()
  755. {
  756. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  757. $encoding = strtolower(Configure::read('App.encoding'));
  758. $this->Form->request->params['controller'] = 'users';
  759. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  760. $expected = [
  761. 'form' => [
  762. 'method' => 'post', 'action' => '/login',
  763. 'accept-charset' => $encoding
  764. ],
  765. 'div' => ['style' => 'display:none;'],
  766. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  767. '/div'
  768. ];
  769. $this->assertHtml($expected, $result);
  770. Router::connect(
  771. '/new-article',
  772. ['controller' => 'articles', 'action' => 'myaction'],
  773. ['_name' => 'my-route']
  774. );
  775. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  776. $expected = [
  777. 'form' => [
  778. 'method' => 'post', 'action' => '/new-article',
  779. 'accept-charset' => $encoding,
  780. ],
  781. 'div' => ['style' => 'display:none;'],
  782. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  783. '/div'
  784. ];
  785. $this->assertHtml($expected, $result);
  786. }
  787. /**
  788. * test automatic accept-charset overriding
  789. *
  790. * @return void
  791. */
  792. public function testCreateWithAcceptCharset()
  793. {
  794. $result = $this->Form->create(
  795. $this->article,
  796. [
  797. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  798. ]
  799. );
  800. $expected = [
  801. 'form' => [
  802. 'method' => 'post', 'action' => '/articles',
  803. 'accept-charset' => 'iso-8859-1'
  804. ],
  805. 'div' => ['style' => 'display:none;'],
  806. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  807. '/div'
  808. ];
  809. $this->assertHtml($expected, $result);
  810. }
  811. /**
  812. * Test base form URL when url param is passed with multiple parameters (&)
  813. *
  814. */
  815. public function testCreateQuerystringrequest()
  816. {
  817. $encoding = strtolower(Configure::read('App.encoding'));
  818. $result = $this->Form->create($this->article, [
  819. 'type' => 'post',
  820. 'escape' => false,
  821. 'url' => [
  822. 'controller' => 'controller',
  823. 'action' => 'action',
  824. '?' => ['param1' => 'value1', 'param2' => 'value2']
  825. ]
  826. ]);
  827. $expected = [
  828. 'form' => [
  829. 'method' => 'post',
  830. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  831. 'accept-charset' => $encoding
  832. ],
  833. 'div' => ['style' => 'display:none;'],
  834. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  835. '/div'
  836. ];
  837. $this->assertHtml($expected, $result);
  838. $result = $this->Form->create($this->article, [
  839. 'type' => 'post',
  840. 'url' => [
  841. 'controller' => 'controller',
  842. 'action' => 'action',
  843. '?' => ['param1' => 'value1', 'param2' => 'value2']
  844. ]
  845. ]);
  846. $this->assertHtml($expected, $result);
  847. }
  848. /**
  849. * test that create() doesn't cause errors by multiple id's being in the primary key
  850. * as could happen with multiple select or checkboxes.
  851. *
  852. * @return void
  853. */
  854. public function testCreateWithMultipleIdInData()
  855. {
  856. $encoding = strtolower(Configure::read('App.encoding'));
  857. $this->Form->request->data['Article']['id'] = [1, 2];
  858. $result = $this->Form->create($this->article);
  859. $expected = [
  860. 'form' => [
  861. 'method' => 'post',
  862. 'action' => '/articles/add',
  863. 'accept-charset' => $encoding
  864. ],
  865. 'div' => ['style' => 'display:none;'],
  866. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  867. '/div'
  868. ];
  869. $this->assertHtml($expected, $result);
  870. }
  871. /**
  872. * test that create() doesn't add in extra passed params.
  873. *
  874. * @return void
  875. */
  876. public function testCreatePassedArgs()
  877. {
  878. $encoding = strtolower(Configure::read('App.encoding'));
  879. $this->Form->request->data['Article']['id'] = 1;
  880. $result = $this->Form->create($this->article, [
  881. 'type' => 'post',
  882. 'escape' => false,
  883. 'url' => [
  884. 'action' => 'edit',
  885. 'myparam'
  886. ]
  887. ]);
  888. $expected = [
  889. 'form' => [
  890. 'method' => 'post',
  891. 'action' => '/articles/edit/myparam',
  892. 'accept-charset' => $encoding
  893. ],
  894. 'div' => ['style' => 'display:none;'],
  895. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  896. '/div'
  897. ];
  898. $this->assertHtml($expected, $result);
  899. }
  900. /**
  901. * test creating a get form, and get form inputs.
  902. *
  903. * @return void
  904. */
  905. public function testGetFormCreate()
  906. {
  907. $encoding = strtolower(Configure::read('App.encoding'));
  908. $result = $this->Form->create($this->article, ['type' => 'get']);
  909. $expected = ['form' => [
  910. 'method' => 'get', 'action' => '/articles/add',
  911. 'accept-charset' => $encoding
  912. ]];
  913. $this->assertHtml($expected, $result);
  914. $result = $this->Form->text('title');
  915. $expected = ['input' => [
  916. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  917. ]];
  918. $this->assertHtml($expected, $result);
  919. $result = $this->Form->password('password');
  920. $expected = ['input' => [
  921. 'name' => 'password', 'type' => 'password'
  922. ]];
  923. $this->assertHtml($expected, $result);
  924. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  925. $result = $this->Form->text('user_form');
  926. $expected = ['input' => [
  927. 'name' => 'user_form', 'type' => 'text'
  928. ]];
  929. $this->assertHtml($expected, $result);
  930. }
  931. /**
  932. * test get form, and inputs when the model param is false
  933. *
  934. * @return void
  935. */
  936. public function testGetFormWithFalseModel()
  937. {
  938. $encoding = strtolower(Configure::read('App.encoding'));
  939. $this->Form->request->params['controller'] = 'contact_test';
  940. $result = $this->Form->create(false, [
  941. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  942. ]);
  943. $expected = ['form' => [
  944. 'method' => 'get', 'action' => '/contact_test/add',
  945. 'accept-charset' => $encoding
  946. ]];
  947. $this->assertHtml($expected, $result);
  948. $result = $this->Form->text('reason');
  949. $expected = [
  950. 'input' => ['type' => 'text', 'name' => 'reason']
  951. ];
  952. $this->assertHtml($expected, $result);
  953. }
  954. /**
  955. * testFormCreateWithSecurity method
  956. *
  957. * Test form->create() with security key.
  958. *
  959. * @return void
  960. */
  961. public function testCreateWithSecurity()
  962. {
  963. $this->Form->request->params['_csrfToken'] = 'testKey';
  964. $encoding = strtolower(Configure::read('App.encoding'));
  965. $result = $this->Form->create($this->article, [
  966. 'url' => '/articles/publish',
  967. ]);
  968. $expected = [
  969. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  970. 'div' => ['style' => 'display:none;'],
  971. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  972. ['input' => [
  973. 'type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testKey'
  974. ]],
  975. '/div'
  976. ];
  977. $this->assertHtml($expected, $result);
  978. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  979. $expected['form']['id'] = 'MyForm';
  980. $this->assertHtml($expected, $result);
  981. }
  982. /**
  983. * testFormCreateGetNoSecurity method
  984. *
  985. * Test form->create() with no security key as its a get form
  986. *
  987. * @return void
  988. */
  989. public function testCreateEndGetNoSecurity()
  990. {
  991. $this->Form->request->params['_csrfToken'] = 'testKey';
  992. $encoding = strtolower(Configure::read('App.encoding'));
  993. $article = new Article();
  994. $result = $this->Form->create($article, [
  995. 'type' => 'get',
  996. 'url' => '/contacts/add'
  997. ]);
  998. $this->assertNotContains('testKey', $result);
  999. $result = $this->Form->end();
  1000. $this->assertNotContains('testKey', $result);
  1001. }
  1002. /**
  1003. * test that create() clears the fields property so it starts fresh
  1004. *
  1005. * @return void
  1006. */
  1007. public function testCreateClearingFields()
  1008. {
  1009. $this->Form->fields = ['model_id'];
  1010. $this->Form->create($this->article);
  1011. $this->assertEquals([], $this->Form->fields);
  1012. }
  1013. /**
  1014. * Tests form hash generation with model-less data
  1015. *
  1016. * @return void
  1017. */
  1018. public function testValidateHashNoModel()
  1019. {
  1020. $this->Form->request->params['_Token'] = 'foo';
  1021. $result = $this->Form->secure(['anything']);
  1022. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  1023. }
  1024. /**
  1025. * Tests that hidden fields generated for checkboxes don't get locked
  1026. *
  1027. * @return void
  1028. */
  1029. public function testNoCheckboxLocking()
  1030. {
  1031. $this->Form->request->params['_Token'] = 'foo';
  1032. $this->assertSame([], $this->Form->fields);
  1033. $this->Form->checkbox('check', ['value' => '1']);
  1034. $this->assertSame($this->Form->fields, ['check']);
  1035. }
  1036. /**
  1037. * testFormSecurityFields method
  1038. *
  1039. * Test generation of secure form hash generation.
  1040. *
  1041. * @return void
  1042. */
  1043. public function testFormSecurityFields()
  1044. {
  1045. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1046. $this->Form->request->params['_Token'] = 'testKey';
  1047. $result = $this->Form->secure($fields);
  1048. $hash = Security::hash(serialize($fields) . Security::salt());
  1049. $hash .= ':' . 'Model.valid';
  1050. $hash = urlencode($hash);
  1051. $tokenDebug = urlencode(json_encode([
  1052. '',
  1053. $fields,
  1054. []
  1055. ]));
  1056. $expected = [
  1057. 'div' => ['style' => 'display:none;'],
  1058. ['input' => [
  1059. 'type' => 'hidden',
  1060. 'name' => '_Token[fields]',
  1061. 'value' => $hash
  1062. ]],
  1063. ['input' => [
  1064. 'type' => 'hidden',
  1065. 'name' => '_Token[unlocked]',
  1066. 'value' => '',
  1067. ]],
  1068. ['input' => [
  1069. 'type' => 'hidden',
  1070. 'name' => '_Token[debug]',
  1071. 'value' => $tokenDebug
  1072. ]],
  1073. '/div'
  1074. ];
  1075. $this->assertHtml($expected, $result);
  1076. }
  1077. /**
  1078. * testFormSecurityFields method
  1079. *
  1080. * Test debug token is not generated if debug is false
  1081. *
  1082. * @return void
  1083. */
  1084. public function testFormSecurityFieldsNoDebugMode()
  1085. {
  1086. Configure::write('debug', false);
  1087. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1088. $this->Form->request->params['_Token'] = 'testKey';
  1089. $result = $this->Form->secure($fields);
  1090. $hash = Security::hash(serialize($fields) . Security::salt());
  1091. $hash .= ':' . 'Model.valid';
  1092. $hash = urlencode($hash);
  1093. $expected = [
  1094. 'div' => ['style' => 'display:none;'],
  1095. ['input' => [
  1096. 'type' => 'hidden',
  1097. 'name' => '_Token[fields]',
  1098. 'value' => $hash
  1099. ]],
  1100. ['input' => [
  1101. 'type' => 'hidden',
  1102. 'name' => '_Token[unlocked]',
  1103. 'value' => '',
  1104. ]],
  1105. '/div'
  1106. ];
  1107. $this->assertHtml($expected, $result);
  1108. }
  1109. /**
  1110. * Tests correct generation of number fields for double and float fields
  1111. *
  1112. * @return void
  1113. */
  1114. public function testTextFieldGenerationForFloats()
  1115. {
  1116. $this->article['schema'] = [
  1117. 'foo' => [
  1118. 'type' => 'float',
  1119. 'null' => false,
  1120. 'default' => null,
  1121. 'length' => 10
  1122. ]
  1123. ];
  1124. $this->Form->create($this->article);
  1125. $result = $this->Form->input('foo');
  1126. $expected = [
  1127. 'div' => ['class' => 'input number'],
  1128. 'label' => ['for' => 'foo'],
  1129. 'Foo',
  1130. '/label',
  1131. ['input' => [
  1132. 'type' => 'number',
  1133. 'name' => 'foo',
  1134. 'id' => 'foo',
  1135. 'step' => 'any'
  1136. ]],
  1137. '/div'
  1138. ];
  1139. $this->assertHtml($expected, $result);
  1140. $result = $this->Form->input('foo', ['step' => 0.5]);
  1141. $expected = [
  1142. 'div' => ['class' => 'input number'],
  1143. 'label' => ['for' => 'foo'],
  1144. 'Foo',
  1145. '/label',
  1146. ['input' => [
  1147. 'type' => 'number',
  1148. 'name' => 'foo',
  1149. 'id' => 'foo',
  1150. 'step' => '0.5'
  1151. ]],
  1152. '/div'
  1153. ];
  1154. $this->assertHtml($expected, $result);
  1155. }
  1156. /**
  1157. * Tests correct generation of number fields for integer fields
  1158. *
  1159. * @return void
  1160. */
  1161. public function testTextFieldTypeNumberGenerationForIntegers()
  1162. {
  1163. TableRegistry::get('Contacts', [
  1164. 'className' => __NAMESPACE__ . '\ContactsTable'
  1165. ]);
  1166. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1167. $result = $this->Form->input('age');
  1168. $expected = [
  1169. 'div' => ['class' => 'input number'],
  1170. 'label' => ['for' => 'age'],
  1171. 'Age',
  1172. '/label',
  1173. ['input' => [
  1174. 'type' => 'number', 'name' => 'age',
  1175. 'id' => 'age'
  1176. ]],
  1177. '/div'
  1178. ];
  1179. $this->assertHtml($expected, $result);
  1180. }
  1181. /**
  1182. * Tests correct generation of file upload fields for binary fields
  1183. *
  1184. * @return void
  1185. */
  1186. public function testFileUploadFieldTypeGenerationForBinaries()
  1187. {
  1188. $table = TableRegistry::get('Contacts', [
  1189. 'className' => __NAMESPACE__ . '\ContactsTable'
  1190. ]);
  1191. $table->schema(['foo' => [
  1192. 'type' => 'binary',
  1193. 'null' => false,
  1194. 'default' => null,
  1195. 'length' => 1024
  1196. ]]);
  1197. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1198. $result = $this->Form->input('foo');
  1199. $expected = [
  1200. 'div' => ['class' => 'input file'],
  1201. 'label' => ['for' => 'foo'],
  1202. 'Foo',
  1203. '/label',
  1204. ['input' => [
  1205. 'type' => 'file', 'name' => 'foo',
  1206. 'id' => 'foo'
  1207. ]],
  1208. '/div'
  1209. ];
  1210. $this->assertHtml($expected, $result);
  1211. }
  1212. /**
  1213. * testFormSecurityMultipleFields method
  1214. *
  1215. * Test secure() with multiple row form. Ensure hash is correct.
  1216. *
  1217. * @return void
  1218. */
  1219. public function testFormSecurityMultipleFields()
  1220. {
  1221. $this->Form->request->params['_Token'] = 'foo';
  1222. $fields = [
  1223. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1224. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1225. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1226. ];
  1227. $result = $this->Form->secure($fields);
  1228. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  1229. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  1230. $tokenDebug = urlencode(json_encode([
  1231. '',
  1232. $fields,
  1233. []
  1234. ]));
  1235. $expected = [
  1236. 'div' => ['style' => 'display:none;'],
  1237. ['input' => [
  1238. 'type' => 'hidden', 'name' => '_Token[fields]',
  1239. 'value' => $hash
  1240. ]],
  1241. ['input' => [
  1242. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1243. 'value' => ''
  1244. ]],
  1245. ['input' => [
  1246. 'type' => 'hidden', 'name' => '_Token[debug]',
  1247. 'value' => $tokenDebug,
  1248. ]],
  1249. '/div'
  1250. ];
  1251. $this->assertHtml($expected, $result);
  1252. }
  1253. /**
  1254. * testFormSecurityMultipleSubmitButtons
  1255. *
  1256. * test form submit generation and ensure that _Token is only created on end()
  1257. *
  1258. * @return void
  1259. */
  1260. public function testFormSecurityMultipleSubmitButtons()
  1261. {
  1262. $this->Form->request->params['_Token'] = 'testKey';
  1263. $this->Form->create($this->article);
  1264. $this->Form->text('Address.title');
  1265. $this->Form->text('Address.first_name');
  1266. $result = $this->Form->submit('Save', ['name' => 'save']);
  1267. $expected = [
  1268. 'div' => ['class' => 'submit'],
  1269. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1270. '/div',
  1271. ];
  1272. $this->assertHtml($expected, $result);
  1273. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1274. $expected = [
  1275. 'div' => ['class' => 'submit'],
  1276. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1277. '/div',
  1278. ];
  1279. $this->assertHtml($expected, $result);
  1280. $result = $this->Form->end();
  1281. $tokenDebug = urlencode(json_encode([
  1282. '/articles/add',
  1283. [
  1284. 'Address.title',
  1285. 'Address.first_name',
  1286. ],
  1287. ['save', 'cancel']
  1288. ]));
  1289. $expected = [
  1290. 'div' => ['style' => 'display:none;'],
  1291. ['input' => [
  1292. 'type' => 'hidden',
  1293. 'name' => '_Token[fields]',
  1294. 'value'
  1295. ]],
  1296. ['input' => [
  1297. 'type' => 'hidden',
  1298. 'name' => '_Token[unlocked]',
  1299. 'value' => 'cancel%7Csave'
  1300. ]],
  1301. ['input' => [
  1302. 'type' => 'hidden', 'name' => '_Token[debug]',
  1303. 'value' => $tokenDebug,
  1304. ]],
  1305. '/div'
  1306. ];
  1307. $this->assertHtml($expected, $result);
  1308. }
  1309. /**
  1310. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1311. *
  1312. * @return void
  1313. */
  1314. public function testSecurityButtonNestedNamed()
  1315. {
  1316. $key = 'testKey';
  1317. $this->Form->request->params['_csrfToken'] = $key;
  1318. $this->Form->create('Addresses');
  1319. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1320. $result = $this->Form->unlockField();
  1321. $this->assertEquals(['Address.button'], $result);
  1322. }
  1323. /**
  1324. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1325. *
  1326. * @return void
  1327. */
  1328. public function testSecuritySubmitNestedNamed()
  1329. {
  1330. $this->Form->request->params['_Token'] = 'testKey';
  1331. $this->Form->create($this->article);
  1332. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1333. $result = $this->Form->unlockField();
  1334. $this->assertEquals(['Address.button'], $result);
  1335. }
  1336. /**
  1337. * Test that the correct fields are unlocked for image submits with no names.
  1338. *
  1339. * @return void
  1340. */
  1341. public function testSecuritySubmitImageNoName()
  1342. {
  1343. $key = 'testKey';
  1344. $this->Form->request->params['_Token'] = 'testKey';
  1345. $this->Form->create(false);
  1346. $result = $this->Form->submit('save.png');
  1347. $expected = [
  1348. 'div' => ['class' => 'submit'],
  1349. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1350. '/div'
  1351. ];
  1352. $this->assertHtml($expected, $result);
  1353. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1354. }
  1355. /**
  1356. * Test that the correct fields are unlocked for image submits with names.
  1357. *
  1358. * @return void
  1359. */
  1360. public function testSecuritySubmitImageName()
  1361. {
  1362. $this->Form->request->params['_Token'] = 'testKey';
  1363. $this->Form->create(null);
  1364. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1365. $expected = [
  1366. 'div' => ['class' => 'submit'],
  1367. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1368. '/div'
  1369. ];
  1370. $this->assertHtml($expected, $result);
  1371. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1372. }
  1373. /**
  1374. * testFormSecurityMultipleInputFields method
  1375. *
  1376. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1377. *
  1378. * @return void
  1379. */
  1380. public function testFormSecurityMultipleInputFields()
  1381. {
  1382. $this->Form->request->params['_Token'] = 'testKey';
  1383. $this->Form->create();
  1384. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1385. $this->Form->input('Addresses.0.title');
  1386. $this->Form->input('Addresses.0.first_name');
  1387. $this->Form->input('Addresses.0.last_name');
  1388. $this->Form->input('Addresses.0.address');
  1389. $this->Form->input('Addresses.0.city');
  1390. $this->Form->input('Addresses.0.phone');
  1391. $this->Form->input('Addresses.0.primary', ['type' => 'checkbox']);
  1392. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1393. $this->Form->input('Addresses.1.title');
  1394. $this->Form->input('Addresses.1.first_name');
  1395. $this->Form->input('Addresses.1.last_name');
  1396. $this->Form->input('Addresses.1.address');
  1397. $this->Form->input('Addresses.1.city');
  1398. $this->Form->input('Addresses.1.phone');
  1399. $this->Form->input('Addresses.1.primary', ['type' => 'checkbox']);
  1400. $result = $this->Form->secure($this->Form->fields);
  1401. $hash = '8bd3911b07b507408b1a969b31ee90c47b7d387e%3AAddresses.0.id%7CAddresses.1.id';
  1402. $tokenDebug = urlencode(json_encode([
  1403. '/articles/add',
  1404. [
  1405. 'Addresses.0.id' => '123456',
  1406. 'Addresses.0.title',
  1407. 'Addresses.0.first_name',
  1408. 'Addresses.0.last_name',
  1409. 'Addresses.0.address',
  1410. 'Addresses.0.city',
  1411. 'Addresses.0.phone',
  1412. 'Addresses.0.primary',
  1413. 'Addresses.1.id' => '654321',
  1414. 'Addresses.1.title',
  1415. 'Addresses.1.first_name',
  1416. 'Addresses.1.last_name',
  1417. 'Addresses.1.address',
  1418. 'Addresses.1.city',
  1419. 'Addresses.1.phone',
  1420. 'Addresses.1.primary',
  1421. ],
  1422. []
  1423. ]));
  1424. $expected = [
  1425. 'div' => ['style' => 'display:none;'],
  1426. ['input' => [
  1427. 'type' => 'hidden', 'name' => '_Token[fields]',
  1428. 'value' => $hash
  1429. ]],
  1430. ['input' => [
  1431. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1432. 'value' => ''
  1433. ]],
  1434. ['input' => [
  1435. 'type' => 'hidden', 'name' => '_Token[debug]',
  1436. 'value' => $tokenDebug,
  1437. ]],
  1438. '/div'
  1439. ];
  1440. $this->assertHtml($expected, $result);
  1441. }
  1442. /**
  1443. * Test form security with Model.field.0 style inputs
  1444. *
  1445. * @return void
  1446. */
  1447. public function testFormSecurityArrayFields()
  1448. {
  1449. $this->Form->request->params['_Token'] = 'testKey';
  1450. $this->Form->create();
  1451. $this->Form->text('Address.primary.1');
  1452. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1453. $this->Form->text('Address.secondary.1.0');
  1454. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1455. }
  1456. /**
  1457. * testFormSecurityMultipleInputDisabledFields method
  1458. *
  1459. * test secure form generation with multiple records and disabled fields.
  1460. *
  1461. * @return void
  1462. */
  1463. public function testFormSecurityMultipleInputDisabledFields()
  1464. {
  1465. $this->Form->request->params['_Token'] = [
  1466. 'unlockedFields' => ['first_name', 'address']
  1467. ];
  1468. $this->Form->create();
  1469. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1470. $this->Form->text('Addresses.0.title');
  1471. $this->Form->text('Addresses.0.first_name');
  1472. $this->Form->text('Addresses.0.last_name');
  1473. $this->Form->text('Addresses.0.address');
  1474. $this->Form->text('Addresses.0.city');
  1475. $this->Form->text('Addresses.0.phone');
  1476. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1477. $this->Form->text('Addresses.1.title');
  1478. $this->Form->text('Addresses.1.first_name');
  1479. $this->Form->text('Addresses.1.last_name');
  1480. $this->Form->text('Addresses.1.address');
  1481. $this->Form->text('Addresses.1.city');
  1482. $this->Form->text('Addresses.1.phone');
  1483. $result = $this->Form->secure($this->Form->fields);
  1484. $hash = '4fb10b46873df4ddd4ef5c3a19944a2f29b38991%3AAddresses.0.id%7CAddresses.1.id';
  1485. $tokenDebug = urlencode(json_encode([
  1486. '/articles/add',
  1487. [
  1488. 'Addresses.0.id' => '123456',
  1489. 'Addresses.0.title',
  1490. 'Addresses.0.last_name',
  1491. 'Addresses.0.city',
  1492. 'Addresses.0.phone',
  1493. 'Addresses.1.id' => '654321',
  1494. 'Addresses.1.title',
  1495. 'Addresses.1.last_name',
  1496. 'Addresses.1.city',
  1497. 'Addresses.1.phone'
  1498. ],
  1499. [
  1500. 'first_name',
  1501. 'address'
  1502. ]
  1503. ]));
  1504. $expected = [
  1505. 'div' => ['style' => 'display:none;'],
  1506. ['input' => [
  1507. 'type' => 'hidden',
  1508. 'name' => '_Token[fields]',
  1509. 'value' => $hash
  1510. ]],
  1511. ['input' => [
  1512. 'type' => 'hidden',
  1513. 'name' => '_Token[unlocked]',
  1514. 'value' => 'address%7Cfirst_name',
  1515. ]],
  1516. ['input' => [
  1517. 'type' => 'hidden', 'name' => '_Token[debug]',
  1518. 'value' => $tokenDebug,
  1519. ]],
  1520. '/div'
  1521. ];
  1522. $this->assertHtml($expected, $result);
  1523. }
  1524. /**
  1525. * testFormSecurityInputDisabledFields method
  1526. *
  1527. * Test single record form with disabled fields.
  1528. *
  1529. * @return void
  1530. */
  1531. public function testFormSecurityInputUnlockedFields()
  1532. {
  1533. $this->Form->request->params['_Token'] = [
  1534. 'unlockedFields' => ['first_name', 'address']
  1535. ];
  1536. $this->Form->create();
  1537. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1538. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1539. $this->Form->text('Addresses.title');
  1540. $this->Form->text('Addresses.first_name');
  1541. $this->Form->text('Addresses.last_name');
  1542. $this->Form->text('Addresses.address');
  1543. $this->Form->text('Addresses.city');
  1544. $this->Form->text('Addresses.phone');
  1545. $result = $this->Form->fields;
  1546. $expected = [
  1547. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1548. 'Addresses.city', 'Addresses.phone'
  1549. ];
  1550. $this->assertEquals($expected, $result);
  1551. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1552. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1553. $tokenDebug = urlencode(json_encode([
  1554. '/articles/add',
  1555. [
  1556. 'Addresses.id' => '123456',
  1557. 'Addresses.title',
  1558. 'Addresses.last_name',
  1559. 'Addresses.city',
  1560. 'Addresses.phone'
  1561. ],
  1562. [
  1563. 'first_name',
  1564. 'address'
  1565. ]
  1566. ]));
  1567. $expected = [
  1568. 'div' => ['style' => 'display:none;'],
  1569. ['input' => [
  1570. 'type' => 'hidden',
  1571. 'name' => '_Token[fields]',
  1572. 'value' => $hash,
  1573. 'data-foo' => 'bar',
  1574. ]],
  1575. ['input' => [
  1576. 'type' => 'hidden',
  1577. 'name' => '_Token[unlocked]',
  1578. 'value' => 'address%7Cfirst_name',
  1579. 'data-foo' => 'bar',
  1580. ]],
  1581. ['input' => [
  1582. 'type' => 'hidden', 'name' => '_Token[debug]',
  1583. 'value' => $tokenDebug,
  1584. 'data-foo' => 'bar'
  1585. ]],
  1586. '/div'
  1587. ];
  1588. $this->assertHtml($expected, $result);
  1589. }
  1590. /**
  1591. * testFormSecurityInputUnlockedFieldsDebugSecurityTrue method
  1592. *
  1593. * Test single record form with debugSecurity param.
  1594. *
  1595. * @return void
  1596. */
  1597. public function testFormSecurityInputUnlockedFieldsDebugSecurityTrue()
  1598. {
  1599. $this->Form->request->params['_Token'] = [
  1600. 'unlockedFields' => ['first_name', 'address']
  1601. ];
  1602. $this->Form->create();
  1603. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1604. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1605. $this->Form->text('Addresses.title');
  1606. $this->Form->text('Addresses.first_name');
  1607. $this->Form->text('Addresses.last_name');
  1608. $this->Form->text('Addresses.address');
  1609. $this->Form->text('Addresses.city');
  1610. $this->Form->text('Addresses.phone');
  1611. $result = $this->Form->fields;
  1612. $expected = [
  1613. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1614. 'Addresses.city', 'Addresses.phone'
  1615. ];
  1616. $this->assertEquals($expected, $result);
  1617. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1618. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1619. $tokenDebug = urlencode(json_encode([
  1620. '/articles/add',
  1621. [
  1622. 'Addresses.id' => '123456',
  1623. 'Addresses.title',
  1624. 'Addresses.last_name',
  1625. 'Addresses.city',
  1626. 'Addresses.phone'
  1627. ],
  1628. [
  1629. 'first_name',
  1630. 'address'
  1631. ]
  1632. ]));
  1633. $expected = [
  1634. 'div' => ['style' => 'display:none;'],
  1635. ['input' => [
  1636. 'type' => 'hidden',
  1637. 'name' => '_Token[fields]',
  1638. 'value' => $hash,
  1639. 'data-foo' => 'bar',
  1640. ]],
  1641. ['input' => [
  1642. 'type' => 'hidden',
  1643. 'name' => '_Token[unlocked]',
  1644. 'value' => 'address%7Cfirst_name',
  1645. 'data-foo' => 'bar',
  1646. ]],
  1647. ['input' => [
  1648. 'type' => 'hidden', 'name' => '_Token[debug]',
  1649. 'value' => $tokenDebug,
  1650. 'data-foo' => 'bar'
  1651. ]],
  1652. '/div'
  1653. ];
  1654. $this->assertHtml($expected, $result);
  1655. }
  1656. /**
  1657. * testFormSecurityInputUnlockedFieldsDebugSecurityFalse method
  1658. *
  1659. * Debug is false, debugSecurity is true -> no debug
  1660. *
  1661. * @return void
  1662. */
  1663. public function testFormSecurityInputUnlockedFieldsDebugSecurityDebugFalse()
  1664. {
  1665. $this->Form->request->params['_Token'] = [
  1666. 'unlockedFields' => ['first_name', 'address']
  1667. ];
  1668. $this->Form->create();
  1669. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1670. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1671. $this->Form->text('Addresses.title');
  1672. $this->Form->text('Addresses.first_name');
  1673. $this->Form->text('Addresses.last_name');
  1674. $this->Form->text('Addresses.address');
  1675. $this->Form->text('Addresses.city');
  1676. $this->Form->text('Addresses.phone');
  1677. $result = $this->Form->fields;
  1678. $expected = [
  1679. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1680. 'Addresses.city', 'Addresses.phone'
  1681. ];
  1682. $this->assertEquals($expected, $result);
  1683. Configure::write('debug', false);
  1684. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1685. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1686. $expected = [
  1687. 'div' => ['style' => 'display:none;'],
  1688. ['input' => [
  1689. 'type' => 'hidden',
  1690. 'name' => '_Token[fields]',
  1691. 'value' => $hash,
  1692. 'data-foo' => 'bar',
  1693. ]],
  1694. ['input' => [
  1695. 'type' => 'hidden',
  1696. 'name' => '_Token[unlocked]',
  1697. 'value' => 'address%7Cfirst_name',
  1698. 'data-foo' => 'bar',
  1699. ]],
  1700. '/div'
  1701. ];
  1702. $this->assertHtml($expected, $result);
  1703. }
  1704. /**
  1705. * testFormSecurityInputUnlockedFieldsDebugSecurityFalse method
  1706. *
  1707. * Test single record form with debugSecurity param.
  1708. *
  1709. * @return void
  1710. */
  1711. public function testFormSecurityInputUnlockedFieldsDebugSecurityFalse()
  1712. {
  1713. $this->Form->request->params['_Token'] = [
  1714. 'unlockedFields' => ['first_name', 'address']
  1715. ];
  1716. $this->Form->create();
  1717. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1718. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1719. $this->Form->text('Addresses.title');
  1720. $this->Form->text('Addresses.first_name');
  1721. $this->Form->text('Addresses.last_name');
  1722. $this->Form->text('Addresses.address');
  1723. $this->Form->text('Addresses.city');
  1724. $this->Form->text('Addresses.phone');
  1725. $result = $this->Form->fields;
  1726. $expected = [
  1727. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1728. 'Addresses.city', 'Addresses.phone'
  1729. ];
  1730. $this->assertEquals($expected, $result);
  1731. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1732. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1733. $expected = [
  1734. 'div' => ['style' => 'display:none;'],
  1735. ['input' => [
  1736. 'type' => 'hidden',
  1737. 'name' => '_Token[fields]',
  1738. 'value' => $hash,
  1739. 'data-foo' => 'bar',
  1740. ]],
  1741. ['input' => [
  1742. 'type' => 'hidden',
  1743. 'name' => '_Token[unlocked]',
  1744. 'value' => 'address%7Cfirst_name',
  1745. 'data-foo' => 'bar',
  1746. ]],
  1747. '/div'
  1748. ];
  1749. $this->assertHtml($expected, $result);
  1750. }
  1751. /**
  1752. * test securing inputs with custom name attributes.
  1753. *
  1754. * @return void
  1755. */
  1756. public function testFormSecureWithCustomNameAttribute()
  1757. {
  1758. $this->Form->request->params['_Token'] = 'testKey';
  1759. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1760. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1761. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1762. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1763. }
  1764. /**
  1765. * testFormSecuredInput method
  1766. *
  1767. * Test generation of entire secure form, assertions made on input() output.
  1768. *
  1769. * @return void
  1770. */
  1771. public function testFormSecuredInput()
  1772. {
  1773. $this->Form->request->params['_csrfToken'] = 'testKey';
  1774. $this->Form->request->params['_Token'] = 'stuff';
  1775. $this->article['schema'] = [
  1776. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1777. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1778. ];
  1779. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1780. $encoding = strtolower(Configure::read('App.encoding'));
  1781. $expected = [
  1782. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1783. 'div' => ['style' => 'display:none;'],
  1784. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1785. ['input' => [
  1786. 'type' => 'hidden',
  1787. 'name' => '_csrfToken',
  1788. 'value' => 'testKey'
  1789. ]],
  1790. '/div'
  1791. ];
  1792. $this->assertHtml($expected, $result);
  1793. $result = $this->Form->input('ratio');
  1794. $expected = [
  1795. 'div' => ['class'],
  1796. 'label' => ['for'],
  1797. 'Ratio',
  1798. '/label',
  1799. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1800. '/div',
  1801. ];
  1802. $this->assertHtml($expected, $result);
  1803. $result = $this->Form->input('population');
  1804. $expected = [
  1805. 'div' => ['class'],
  1806. 'label' => ['for'],
  1807. 'Population',
  1808. '/label',
  1809. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1810. '/div',
  1811. ];
  1812. $this->assertHtml($expected, $result);
  1813. $result = $this->Form->input('published', ['type' => 'text']);
  1814. $expected = [
  1815. 'div' => ['class' => 'input text'],
  1816. 'label' => ['for' => 'published'],
  1817. 'Published',
  1818. '/label',
  1819. ['input' => [
  1820. 'type' => 'text',
  1821. 'name' => 'published',
  1822. 'id' => 'published'
  1823. ]],
  1824. '/div'
  1825. ];
  1826. $this->assertHtml($expected, $result);
  1827. $result = $this->Form->input('other', ['type' => 'text']);
  1828. $expected = [
  1829. 'div' => ['class' => 'input text'],
  1830. 'label' => ['for' => 'other'],
  1831. 'Other',
  1832. '/label',
  1833. ['input' => [
  1834. 'type' => 'text',
  1835. 'name' => 'other',
  1836. 'id',
  1837. ]],
  1838. '/div'
  1839. ];
  1840. $this->assertHtml($expected, $result);
  1841. $result = $this->Form->hidden('stuff');
  1842. $expected = [
  1843. 'input' => [
  1844. 'type' => 'hidden',
  1845. 'name' => 'stuff'
  1846. ]
  1847. ];
  1848. $this->assertHtml($expected, $result);
  1849. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1850. $expected = ['input' => [
  1851. 'type' => 'hidden',
  1852. 'name' => 'hidden',
  1853. 'value' => '0'
  1854. ]];
  1855. $this->assertHtml($expected, $result);
  1856. $result = $this->Form->input('something', ['type' => 'checkbox']);
  1857. $expected = [
  1858. 'div' => ['class' => 'input checkbox'],
  1859. ['input' => [
  1860. 'type' => 'hidden',
  1861. 'name' => 'something',
  1862. 'value' => '0'
  1863. ]],
  1864. 'label' => ['for' => 'something'],
  1865. ['input' => [
  1866. 'type' => 'checkbox',
  1867. 'name' => 'something',
  1868. 'value' => '1',
  1869. 'id' => 'something'
  1870. ]],
  1871. 'Something',
  1872. '/label',
  1873. '/div'
  1874. ];
  1875. $this->assertHtml($expected, $result);
  1876. $result = $this->Form->fields;
  1877. $expectedFields = [
  1878. 'ratio',
  1879. 'population',
  1880. 'published',
  1881. 'other',
  1882. 'stuff' => '',
  1883. 'hidden' => '0',
  1884. 'something'
  1885. ];
  1886. $this->assertEquals($expectedFields, $result);
  1887. $result = $this->Form->secure($this->Form->fields);
  1888. $tokenDebug = urlencode(json_encode([
  1889. '/articles/add',
  1890. $expectedFields,
  1891. []
  1892. ]));
  1893. $expected = [
  1894. 'div' => ['style' => 'display:none;'],
  1895. ['input' => [
  1896. 'type' => 'hidden',
  1897. 'name' => '_Token[fields]',
  1898. 'value'
  1899. ]],
  1900. ['input' => [
  1901. 'type' => 'hidden',
  1902. 'name' => '_Token[unlocked]',
  1903. 'value' => ''
  1904. ]],
  1905. ['input' => [
  1906. 'type' => 'hidden', 'name' => '_Token[debug]',
  1907. 'value' => $tokenDebug,
  1908. ]],
  1909. '/div'
  1910. ];
  1911. $this->assertHtml($expected, $result);
  1912. }
  1913. /**
  1914. * Test secured inputs with custom names.
  1915. *
  1916. * @return void
  1917. */
  1918. public function testSecuredInputCustomName()
  1919. {
  1920. $this->Form->request->params['_Token'] = 'testKey';
  1921. $this->assertEquals([], $this->Form->fields);
  1922. $this->Form->text('text_input', [
  1923. 'name' => 'Option[General.default_role]',
  1924. ]);
  1925. $expected = ['Option.General.default_role'];
  1926. $this->assertEquals($expected, $this->Form->fields);
  1927. $this->Form->select('select_box', [1, 2], [
  1928. 'name' => 'Option[General.select_role]',
  1929. ]);
  1930. $expected[] = 'Option.General.select_role';
  1931. $this->assertEquals($expected, $this->Form->fields);
  1932. $this->Form->text('other.things[]');
  1933. $expected[] = 'other.things';
  1934. $this->assertEquals($expected, $this->Form->fields);
  1935. }
  1936. /**
  1937. * Test that a hidden field followed by a visible field
  1938. * undoes the hidden field locking.
  1939. *
  1940. * @return void
  1941. */
  1942. public function testSecuredInputDuplicate()
  1943. {
  1944. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  1945. $this->assertEquals([], $this->Form->fields);
  1946. $this->Form->input('text_val', [
  1947. 'type' => 'hidden',
  1948. 'value' => 'some text',
  1949. ]);
  1950. $expected = ['text_val' => 'some text'];
  1951. $this->assertEquals($expected, $this->Form->fields);
  1952. $this->Form->input('text_val', [
  1953. 'type' => 'text',
  1954. ]);
  1955. $expected = ['text_val'];
  1956. $this->assertEquals($expected, $this->Form->fields);
  1957. }
  1958. /**
  1959. * Tests that the correct keys are added to the field hash index
  1960. *
  1961. * @return void
  1962. */
  1963. public function testFormSecuredFileInput()
  1964. {
  1965. $this->assertEquals([], $this->Form->fields);
  1966. $this->Form->file('Attachment.file');
  1967. $expected = [
  1968. 'Attachment.file.name', 'Attachment.file.type',
  1969. 'Attachment.file.tmp_name', 'Attachment.file.error',
  1970. 'Attachment.file.size'
  1971. ];
  1972. $this->assertEquals($expected, $this->Form->fields);
  1973. }
  1974. /**
  1975. * test that multiple selects keys are added to field hash
  1976. *
  1977. * @return void
  1978. */
  1979. public function testFormSecuredMultipleSelect()
  1980. {
  1981. $this->Form->request->params['_csrfToken'] = 'testKey';
  1982. $this->assertEquals([], $this->Form->fields);
  1983. $options = ['1' => 'one', '2' => 'two'];
  1984. $this->Form->select('Model.select', $options);
  1985. $expected = ['Model.select'];
  1986. $this->assertEquals($expected, $this->Form->fields);
  1987. $this->Form->fields = [];
  1988. $this->Form->select('Model.select', $options, ['multiple' => true]);
  1989. $this->assertEquals($expected, $this->Form->fields);
  1990. }
  1991. /**
  1992. * testFormSecuredRadio method
  1993. *
  1994. * @return void
  1995. */
  1996. public function testFormSecuredRadio()
  1997. {
  1998. $this->Form->request->params['_Token'] = 'testKey';
  1999. $this->assertEquals([], $this->Form->fields);
  2000. $options = ['1' => 'option1', '2' => 'option2'];
  2001. $this->Form->radio('Test.test', $options);
  2002. $expected = ['Test.test'];
  2003. $this->assertEquals($expected, $this->Form->fields);
  2004. $this->Form->radio('Test.all', $options, [
  2005. 'disabled' => ['option1', 'option2']
  2006. ]);
  2007. $expected = ['Test.test', 'Test.all' => ''];
  2008. $this->assertEquals($expected, $this->Form->fields);
  2009. $this->Form->radio('Test.some', $options, [
  2010. 'disabled' => ['option1']
  2011. ]);
  2012. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2013. $this->assertEquals($expected, $this->Form->fields);
  2014. }
  2015. /**
  2016. * Test that when disabled is in a list based attribute array it works.
  2017. *
  2018. * @return void
  2019. */
  2020. public function testFormSecuredAndDisabledNotAssoc()
  2021. {
  2022. $this->Form->request->params['_Token'] = 'testKey';
  2023. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2024. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2025. $this->Form->text('Model.text', ['disabled']);
  2026. $this->Form->textarea('Model.textarea', ['disabled']);
  2027. $this->Form->password('Model.password', ['disabled']);
  2028. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2029. $expected = [
  2030. 'Model.radio' => ''
  2031. ];
  2032. $this->assertEquals($expected, $this->Form->fields);
  2033. }
  2034. /**
  2035. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  2036. * hashing.
  2037. *
  2038. * @return void
  2039. */
  2040. public function testFormSecuredAndDisabled()
  2041. {
  2042. $this->Form->request->params['_Token'] = 'testKey';
  2043. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2044. $this->Form->text('Model.text', ['disabled' => true]);
  2045. $this->Form->password('Model.text', ['disabled' => true]);
  2046. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2047. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2048. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2049. $this->Form->year('Model.year', ['disabled' => true]);
  2050. $this->Form->month('Model.month', ['disabled' => true]);
  2051. $this->Form->day('Model.day', ['disabled' => true]);
  2052. $this->Form->hour('Model.hour', ['disabled' => true]);
  2053. $this->Form->minute('Model.minute', ['disabled' => true]);
  2054. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2055. $expected = [
  2056. 'Model.radio' => ''
  2057. ];
  2058. $this->assertEquals($expected, $this->Form->fields);
  2059. }
  2060. /**
  2061. * testDisableSecurityUsingForm method
  2062. *
  2063. * @return void
  2064. */
  2065. public function testDisableSecurityUsingForm()
  2066. {
  2067. $this->Form->request->params['_Token'] = [
  2068. 'disabledFields' => []
  2069. ];
  2070. $this->Form->create();
  2071. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2072. $this->Form->text('Addresses.title');
  2073. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2074. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2075. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2076. $result = $this->Form->fields;
  2077. $expected = [
  2078. 'Addresses.id' => '123456', 'Addresses.title',
  2079. ];
  2080. $this->assertEquals($expected, $result);
  2081. }
  2082. /**
  2083. * test disableField
  2084. *
  2085. * @return void
  2086. */
  2087. public function testUnlockFieldAddsToList()
  2088. {
  2089. $this->Form->request->params['_Token'] = [
  2090. 'unlockedFields' => []
  2091. ];
  2092. $this->Form->unlockField('Contact.name');
  2093. $this->Form->text('Contact.name');
  2094. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2095. $this->assertEquals([], $this->Form->fields);
  2096. }
  2097. /**
  2098. * test unlockField removing from fields array.
  2099. *
  2100. * @return void
  2101. */
  2102. public function testUnlockFieldRemovingFromFields()
  2103. {
  2104. $this->Form->request->params['_Token'] = [
  2105. 'unlockedFields' => []
  2106. ];
  2107. $this->Form->create($this->article);
  2108. $this->Form->hidden('Article.id', ['value' => 1]);
  2109. $this->Form->text('Article.title');
  2110. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2111. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  2112. $this->Form->unlockField('Article.title');
  2113. $this->Form->unlockField('Article.id');
  2114. $this->assertEquals([], $this->Form->fields);
  2115. }
  2116. /**
  2117. * test reset unlockFields, when create new form.
  2118. *
  2119. * @return void
  2120. */
  2121. public function testResetUnlockFields()
  2122. {
  2123. $this->Form->request->params['_Token'] = [
  2124. 'key' => 'testKey',
  2125. 'unlockedFields' => []
  2126. ];
  2127. $this->Form->unlockField('Contact.id');
  2128. $this->Form->create('Contact');
  2129. $this->Form->hidden('Contact.id', ['value' => 1]);
  2130. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2131. $this->Form->end();
  2132. $this->Form->create('Contact');
  2133. $this->Form->hidden('Contact.id', ['value' => 1]);
  2134. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2135. }
  2136. /**
  2137. * Test that only the path + query elements of a form's URL show up in their hash.
  2138. *
  2139. * @return void
  2140. */
  2141. public function testSecuredFormUrlIgnoresHost()
  2142. {
  2143. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2144. $expected = '0ff0c85cd70584d8fd18fa136846d22c66c21e2d%3A';
  2145. $this->Form->create($this->article, [
  2146. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2147. ]);
  2148. $result = $this->Form->secure();
  2149. $this->assertContains($expected, $result);
  2150. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2151. $result = $this->Form->secure();
  2152. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2153. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2154. $result = $this->Form->secure();
  2155. $this->assertContains($expected, $result, 'URL path + query should work.');
  2156. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2157. $result = $this->Form->secure();
  2158. $this->assertNotContains($expected, $result, 'URL is different');
  2159. }
  2160. /**
  2161. * Test that URL, HTML and identifer show up in their hashs.
  2162. *
  2163. * @return void
  2164. */
  2165. public function testSecuredFormUrlHasHtmlAndIdentifer()
  2166. {
  2167. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2168. $expected = 'ece0693fb1b19ca116133db1832ac29baaf41ce5%3A';
  2169. $res = $this->Form->create($this->article, [
  2170. 'url' => [
  2171. 'controller' => 'articles',
  2172. 'action' => 'view',
  2173. '?' => [
  2174. 'page' => 1,
  2175. 'limit' => 10,
  2176. 'html' => '<>"',
  2177. ],
  2178. '#' => 'result',
  2179. ],
  2180. ]);
  2181. $result = $this->Form->secure();
  2182. $this->assertContains($expected, $result);
  2183. $this->Form->create($this->article, [
  2184. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2185. ]);
  2186. $result = $this->Form->secure();
  2187. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2188. $this->Form->create($this->article, [
  2189. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2190. ]);
  2191. $result = $this->Form->secure();
  2192. $this->assertContains($expected, $result, 'URL path + query should work.');
  2193. }
  2194. /**
  2195. * test error message display
  2196. *
  2197. * @return void
  2198. */
  2199. public function testErrorMessageDisplay()
  2200. {
  2201. $this->article['errors'] = [
  2202. 'Article' => [
  2203. 'title' => 'error message',
  2204. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2205. ]
  2206. ];
  2207. $this->Form->create($this->article);
  2208. $result = $this->Form->input('Article.title');
  2209. $expected = [
  2210. 'div' => ['class' => 'input text error'],
  2211. 'label' => ['for' => 'article-title'],
  2212. 'Title',
  2213. '/label',
  2214. 'input' => [
  2215. 'type' => 'text', 'name' => 'Article[title]',
  2216. 'id' => 'article-title', 'class' => 'form-error'
  2217. ],
  2218. ['div' => ['class' => 'error-message']],
  2219. 'error message',
  2220. '/div',
  2221. '/div'
  2222. ];
  2223. $this->assertHtml($expected, $result);
  2224. $result = $this->Form->input('Article.title', [
  2225. 'templates' => [
  2226. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2227. ]
  2228. ]);
  2229. $expected = [
  2230. 'div' => ['class' => 'input text error'],
  2231. 'label' => ['for' => 'article-title'],
  2232. 'Title',
  2233. '/label',
  2234. 'input' => [
  2235. 'type' => 'text', 'name' => 'Article[title]',
  2236. 'id' => 'article-title', 'class' => 'form-error'
  2237. ],
  2238. '/div'
  2239. ];
  2240. $this->assertHtml($expected, $result);
  2241. $result = $this->Form->input('Article.content');
  2242. $expected = [
  2243. 'div' => ['class' => 'input text error'],
  2244. 'label' => ['for' => 'article-content'],
  2245. 'Content',
  2246. '/label',
  2247. 'input' => [
  2248. 'type' => 'text', 'name' => 'Article[content]',
  2249. 'id' => 'article-content', 'class' => 'form-error'
  2250. ],
  2251. ['div' => ['class' => 'error-message']],
  2252. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2253. '/div',
  2254. '/div'
  2255. ];
  2256. $this->assertHtml($expected, $result);
  2257. $result = $this->Form->input('Article.content', ['error' => ['escape' => true]]);
  2258. $expected = [
  2259. 'div' => ['class' => 'input text error'],
  2260. 'label' => ['for' => 'article-content'],
  2261. 'Content',
  2262. '/label',
  2263. 'input' => [
  2264. 'type' => 'text', 'name' => 'Article[content]',
  2265. 'id' => 'article-content', 'class' => 'form-error'
  2266. ],
  2267. ['div' => ['class' => 'error-message']],
  2268. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2269. '/div',
  2270. '/div'
  2271. ];
  2272. $this->assertHtml($expected, $result);
  2273. $result = $this->Form->input('Article.content', ['error' => ['escape' => false]]);
  2274. $expected = [
  2275. 'div' => ['class' => 'input text error'],
  2276. 'label' => ['for' => 'article-content'],
  2277. 'Content',
  2278. '/label',
  2279. 'input' => [
  2280. 'type' => 'text', 'name' => 'Article[content]',
  2281. 'id' => 'article-content', 'class' => 'form-error'
  2282. ],
  2283. ['div' => ['class' => 'error-message']],
  2284. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2285. '/div',
  2286. '/div'
  2287. ];
  2288. $this->assertHtml($expected, $result);
  2289. }
  2290. /**
  2291. * Test validation errors, when validation message is an empty string.
  2292. *
  2293. * @return void
  2294. */
  2295. public function testEmptyErrorValidation()
  2296. {
  2297. $this->article['errors'] = [
  2298. 'Article' => ['title' => '']
  2299. ];
  2300. $this->Form->create($this->article);
  2301. $result = $this->Form->input('Article.title');
  2302. $expected = [
  2303. 'div' => ['class' => 'input text error'],
  2304. 'label' => ['for' => 'article-title'],
  2305. 'Title',
  2306. '/label',
  2307. 'input' => [
  2308. 'type' => 'text', 'name' => 'Article[title]',
  2309. 'id' => 'article-title', 'class' => 'form-error'
  2310. ],
  2311. ['div' => ['class' => 'error-message']],
  2312. [],
  2313. '/div',
  2314. '/div'
  2315. ];
  2316. $this->assertHtml($expected, $result);
  2317. }
  2318. /**
  2319. * Test validation errors, when calling input() overriding validation message by an empty string.
  2320. *
  2321. * @return void
  2322. */
  2323. public function testEmptyInputErrorValidation()
  2324. {
  2325. $this->article['errors'] = [
  2326. 'Article' => ['title' => 'error message']
  2327. ];
  2328. $this->Form->create($this->article);
  2329. $result = $this->Form->input('Article.title', ['error' => '']);
  2330. $expected = [
  2331. 'div' => ['class' => 'input text error'],
  2332. 'label' => ['for' => 'article-title'],
  2333. 'Title',
  2334. '/label',
  2335. 'input' => [
  2336. 'type' => 'text', 'name' => 'Article[title]',
  2337. 'id' => 'article-title', 'class' => 'form-error'
  2338. ],
  2339. ['div' => ['class' => 'error-message']],
  2340. [],
  2341. '/div',
  2342. '/div'
  2343. ];
  2344. $this->assertHtml($expected, $result);
  2345. }
  2346. /**
  2347. * Test validation errors, when calling input() overriding validation messages
  2348. *
  2349. * @return void
  2350. */
  2351. public function testInputErrorMessage()
  2352. {
  2353. $this->article['errors'] = [
  2354. 'title' => ['error message']
  2355. ];
  2356. $this->Form->create($this->article);
  2357. $result = $this->Form->input('title', [
  2358. 'error' => 'Custom error!'
  2359. ]);
  2360. $expected = [
  2361. 'div' => ['class' => 'input text required error'],
  2362. 'label' => ['for' => 'title'],
  2363. 'Title',
  2364. '/label',
  2365. 'input' => [
  2366. 'type' => 'text', 'name' => 'title',
  2367. 'id' => 'title', 'class' => 'form-error',
  2368. 'required' => 'required',
  2369. ],
  2370. ['div' => ['class' => 'error-message']],
  2371. 'Custom error!',
  2372. '/div',
  2373. '/div'
  2374. ];
  2375. $this->assertHtml($expected, $result);
  2376. $result = $this->Form->input('title', [
  2377. 'error' => ['error message' => 'Custom error!']
  2378. ]);
  2379. $expected = [
  2380. 'div' => ['class' => 'input text required error'],
  2381. 'label' => ['for' => 'title'],
  2382. 'Title',
  2383. '/label',
  2384. 'input' => [
  2385. 'type' => 'text',
  2386. 'name' => 'title',
  2387. 'id' => 'title',
  2388. 'class' => 'form-error',
  2389. 'required' => 'required'
  2390. ],
  2391. ['div' => ['class' => 'error-message']],
  2392. 'Custom error!',
  2393. '/div',
  2394. '/div'
  2395. ];
  2396. $this->assertHtml($expected, $result);
  2397. }
  2398. /**
  2399. * Tests displaying errors for nested entities
  2400. *
  2401. * @return void
  2402. */
  2403. public function testFormValidationAssociated()
  2404. {
  2405. $nested = new Entity(['foo' => 'bar']);
  2406. $nested->errors('foo', ['not a valid bar']);
  2407. $entity = new Entity(['nested' => $nested]);
  2408. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2409. $result = $this->Form->error('nested.foo');
  2410. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2411. }
  2412. /**
  2413. * testFormValidationAssociatedSecondLevel method
  2414. *
  2415. * test form error display with associated model.
  2416. *
  2417. * @return void
  2418. */
  2419. public function testFormValidationAssociatedSecondLevel()
  2420. {
  2421. $inner = new Entity(['bar' => 'baz']);
  2422. $nested = new Entity(['foo' => $inner]);
  2423. $entity = new Entity(['nested' => $nested]);
  2424. $inner->errors('bar', ['not a valid one']);
  2425. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2426. $result = $this->Form->error('nested.foo.bar');
  2427. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2428. }
  2429. /**
  2430. * testFormValidationMultiRecord method
  2431. *
  2432. * test form error display with multiple records.
  2433. *
  2434. * @return void
  2435. */
  2436. public function testFormValidationMultiRecord()
  2437. {
  2438. $one = new Entity;
  2439. $two = new Entity;
  2440. TableRegistry::get('Contacts', [
  2441. 'className' => __NAMESPACE__ . '\ContactsTable'
  2442. ]);
  2443. $one->set('email', '');
  2444. $one->errors('email', ['invalid email']);
  2445. $two->set('name', '');
  2446. $two->errors('name', ['This is wrong']);
  2447. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2448. $result = $this->Form->input('0.email');
  2449. $expected = [
  2450. 'div' => ['class' => 'input email error'],
  2451. 'label' => ['for' => '0-email'],
  2452. 'Email',
  2453. '/label',
  2454. 'input' => [
  2455. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2456. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2457. ],
  2458. ['div' => ['class' => 'error-message']],
  2459. 'invalid email',
  2460. '/div',
  2461. '/div'
  2462. ];
  2463. $this->assertHtml($expected, $result);
  2464. $result = $this->Form->input('1.name');
  2465. $expected = [
  2466. 'div' => ['class' => 'input text error'],
  2467. 'label' => ['for' => '1-name'],
  2468. 'Name',
  2469. '/label',
  2470. 'input' => [
  2471. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2472. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2473. ],
  2474. ['div' => ['class' => 'error-message']],
  2475. 'This is wrong',
  2476. '/div',
  2477. '/div'
  2478. ];
  2479. $this->assertHtml($expected, $result);
  2480. }
  2481. /**
  2482. * testInput method
  2483. *
  2484. * Test various incarnations of input().
  2485. *
  2486. * @return void
  2487. */
  2488. public function testInput()
  2489. {
  2490. TableRegistry::get('ValidateUsers', [
  2491. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2492. ]);
  2493. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2494. $result = $this->Form->input('ValidateUsers.balance');
  2495. $expected = [
  2496. 'div' => ['class'],
  2497. 'label' => ['for'],
  2498. 'Balance',
  2499. '/label',
  2500. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2501. '/div',
  2502. ];
  2503. $this->assertHtml($expected, $result);
  2504. $result = $this->Form->input('ValidateUser.cost_decimal');
  2505. $expected = [
  2506. 'div' => ['class'],
  2507. 'label' => ['for'],
  2508. 'Cost Decimal',
  2509. '/label',
  2510. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2511. '/div',
  2512. ];
  2513. $this->assertHtml($expected, $result);
  2514. $result = $this->Form->input('ValidateUser.null_decimal');
  2515. $expected = [
  2516. 'div' => ['class'],
  2517. 'label' => ['for'],
  2518. 'Null Decimal',
  2519. '/label',
  2520. 'input' => ['name', 'type' => 'number', 'id'],
  2521. '/div',
  2522. ];
  2523. $this->assertHtml($expected, $result);
  2524. }
  2525. /**
  2526. * Tests the input method and passing custom options
  2527. *
  2528. * @return void
  2529. */
  2530. public function testInputCustomization()
  2531. {
  2532. TableRegistry::get('Contacts', [
  2533. 'className' => __NAMESPACE__ . '\ContactsTable'
  2534. ]);
  2535. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2536. $result = $this->Form->input('Contact.email', ['id' => 'custom']);
  2537. $expected = [
  2538. 'div' => ['class' => 'input email'],
  2539. 'label' => ['for' => 'custom'],
  2540. 'Email',
  2541. '/label',
  2542. ['input' => [
  2543. 'type' => 'email', 'name' => 'Contact[email]',
  2544. 'id' => 'custom', 'maxlength' => 255
  2545. ]],
  2546. '/div'
  2547. ];
  2548. $this->assertHtml($expected, $result);
  2549. $result = $this->Form->input('Contact.email', [
  2550. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2551. ]);
  2552. $expected = [
  2553. '<div',
  2554. 'label' => ['for' => 'contact-email'],
  2555. 'Email',
  2556. '/label',
  2557. ['input' => [
  2558. 'type' => 'email', 'name' => 'Contact[email]',
  2559. 'id' => 'contact-email', 'maxlength' => 255
  2560. ]],
  2561. '/div'
  2562. ];
  2563. $this->assertHtml($expected, $result);
  2564. $result = $this->Form->input('Contact.email', ['type' => 'text']);
  2565. $expected = [
  2566. 'div' => ['class' => 'input text'],
  2567. 'label' => ['for' => 'contact-email'],
  2568. 'Email',
  2569. '/label',
  2570. ['input' => [
  2571. 'type' => 'text', 'name' => 'Contact[email]',
  2572. 'id' => 'contact-email', 'maxlength' => '255'
  2573. ]],
  2574. '/div'
  2575. ];
  2576. $this->assertHtml($expected, $result);
  2577. $result = $this->Form->input('Contact.5.email', ['type' => 'text']);
  2578. $expected = [
  2579. 'div' => ['class' => 'input text'],
  2580. 'label' => ['for' => 'contact-5-email'],
  2581. 'Email',
  2582. '/label',
  2583. ['input' => [
  2584. 'type' => 'text', 'name' => 'Contact[5][email]',
  2585. 'id' => 'contact-5-email', 'maxlength' => '255'
  2586. ]],
  2587. '/div'
  2588. ];
  2589. $this->assertHtml($expected, $result);
  2590. $result = $this->Form->input('Contact.password');
  2591. $expected = [
  2592. 'div' => ['class' => 'input password'],
  2593. 'label' => ['for' => 'contact-password'],
  2594. 'Password',
  2595. '/label',
  2596. ['input' => [
  2597. 'type' => 'password', 'name' => 'Contact[password]',
  2598. 'id' => 'contact-password'
  2599. ]],
  2600. '/div'
  2601. ];
  2602. $this->assertHtml($expected, $result);
  2603. $result = $this->Form->input('Contact.email', [
  2604. 'type' => 'file', 'class' => 'textbox'
  2605. ]);
  2606. $expected = [
  2607. 'div' => ['class' => 'input file'],
  2608. 'label' => ['for' => 'contact-email'],
  2609. 'Email',
  2610. '/label',
  2611. ['input' => [
  2612. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2613. 'id' => 'contact-email'
  2614. ]],
  2615. '/div'
  2616. ];
  2617. $this->assertHtml($expected, $result);
  2618. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2619. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2620. $result = $this->Form->input('phone');
  2621. $expected = [
  2622. 'div' => ['class' => 'input tel'],
  2623. 'label' => ['for' => 'phone'],
  2624. 'Phone',
  2625. '/label',
  2626. ['input' => [
  2627. 'type' => 'tel', 'name' => 'phone',
  2628. 'value' => 'Hello &amp; World &gt; weird chars',
  2629. 'id' => 'phone', 'maxlength' => 255
  2630. ]],
  2631. '/div'
  2632. ];
  2633. $this->assertHtml($expected, $result);
  2634. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2635. $this->Form->create();
  2636. $result = $this->Form->input('Model.0.OtherModel.field', ['id' => 'myId']);
  2637. $expected = [
  2638. 'div' => ['class' => 'input text'],
  2639. 'label' => ['for' => 'myId'],
  2640. 'Field',
  2641. '/label',
  2642. 'input' => [
  2643. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2644. 'value' => 'My value', 'id' => 'myId'
  2645. ],
  2646. '/div'
  2647. ];
  2648. $this->assertHtml($expected, $result);
  2649. $this->Form->request->data = [];
  2650. $entity->errors('field', 'Badness!');
  2651. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2652. $result = $this->Form->input('field');
  2653. $expected = [
  2654. 'div' => ['class' => 'input text error'],
  2655. 'label' => ['for' => 'field'],
  2656. 'Field',
  2657. '/label',
  2658. 'input' => [
  2659. 'type' => 'text', 'name' => 'field',
  2660. 'id' => 'field', 'class' => 'form-error'
  2661. ],
  2662. ['div' => ['class' => 'error-message']],
  2663. 'Badness!',
  2664. '/div',
  2665. '/div'
  2666. ];
  2667. $this->assertHtml($expected, $result);
  2668. $result = $this->Form->input('field', [
  2669. 'templates' => [
  2670. 'inputContainerError' => '{{content}}{{error}}',
  2671. 'error' => '<span class="error-message">{{content}}</span>'
  2672. ]
  2673. ]);
  2674. $expected = [
  2675. 'label' => ['for' => 'field'],
  2676. 'Field',
  2677. '/label',
  2678. 'input' => [
  2679. 'type' => 'text', 'name' => 'field',
  2680. 'id' => 'field', 'class' => 'form-error'
  2681. ],
  2682. ['span' => ['class' => 'error-message']],
  2683. 'Badness!',
  2684. '/span'
  2685. ];
  2686. $this->assertHtml($expected, $result);
  2687. $entity->errors('field', ['minLength'], true);
  2688. $result = $this->Form->input('field', [
  2689. 'error' => [
  2690. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2691. 'maxLength' => 'login too large'
  2692. ]
  2693. ]);
  2694. $expected = [
  2695. 'div' => ['class' => 'input text error'],
  2696. 'label' => ['for' => 'field'],
  2697. 'Field',
  2698. '/label',
  2699. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2700. ['div' => ['class' => 'error-message']],
  2701. 'Le login doit contenir au moins 2 caractères',
  2702. '/div',
  2703. '/div'
  2704. ];
  2705. $this->assertHtml($expected, $result);
  2706. $entity->errors('field', ['maxLength'], true);
  2707. $result = $this->Form->input('field', [
  2708. 'error' => [
  2709. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2710. 'maxLength' => 'login too large',
  2711. ]
  2712. ]);
  2713. $expected = [
  2714. 'div' => ['class' => 'input text error'],
  2715. 'label' => ['for' => 'field'],
  2716. 'Field',
  2717. '/label',
  2718. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2719. ['div' => ['class' => 'error-message']],
  2720. 'login too large',
  2721. '/div',
  2722. '/div'
  2723. ];
  2724. $this->assertHtml($expected, $result);
  2725. }
  2726. /**
  2727. * Test that input() accepts a template file.
  2728. *
  2729. * @return void
  2730. */
  2731. public function testInputWithTemplateFile()
  2732. {
  2733. $result = $this->Form->input('field', [
  2734. 'templates' => 'htmlhelper_tags'
  2735. ]);
  2736. $expected = [
  2737. 'label' => ['for' => 'field'],
  2738. 'Field',
  2739. '/label',
  2740. 'input' => [
  2741. 'type' => 'text', 'name' => 'field',
  2742. 'id' => 'field'
  2743. ],
  2744. ];
  2745. $this->assertHtml($expected, $result);
  2746. }
  2747. /**
  2748. * Test that nested inputs end with brackets
  2749. *
  2750. * @return void
  2751. */
  2752. public function testNestedInputsEndWithBrackets()
  2753. {
  2754. $result = $this->Form->text('nested.text[]');
  2755. $expected = [
  2756. 'input' => [
  2757. 'type' => 'text', 'name' => 'nested[text][]'
  2758. ],
  2759. ];
  2760. $result = $this->Form->file('nested.file[]');
  2761. $expected = [
  2762. 'input' => [
  2763. 'type' => 'file', 'name' => 'nested[file][]'
  2764. ],
  2765. ];
  2766. $this->assertHtml($expected, $result);
  2767. }
  2768. /**
  2769. * Test id prefix
  2770. *
  2771. * @return void
  2772. */
  2773. public function testCreateIdPrefix()
  2774. {
  2775. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2776. $result = $this->Form->input('field');
  2777. $expected = [
  2778. 'div' => ['class' => 'input text'],
  2779. 'label' => ['for' => 'prefix-field'],
  2780. 'Field',
  2781. '/label',
  2782. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2783. '/div'
  2784. ];
  2785. $this->assertHtml($expected, $result);
  2786. $result = $this->Form->input('field', ['id' => 'custom-id']);
  2787. $expected = [
  2788. 'div' => ['class' => 'input text'],
  2789. 'label' => ['for' => 'custom-id'],
  2790. 'Field',
  2791. '/label',
  2792. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2793. '/div'
  2794. ];
  2795. $this->assertHtml($expected, $result);
  2796. $result = $this->Form->radio('Model.field', ['option A']);
  2797. $expected = [
  2798. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2799. 'label' => ['for' => 'prefix-model-field-0'],
  2800. ['input' => [
  2801. 'type' => 'radio',
  2802. 'name' => 'Model[field]',
  2803. 'value' => '0',
  2804. 'id' => 'prefix-model-field-0'
  2805. ]],
  2806. 'option A',
  2807. '/label'
  2808. ];
  2809. $this->assertHtml($expected, $result);
  2810. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2811. $expected = [
  2812. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2813. 'label' => ['for' => 'prefix-model-field-0'],
  2814. ['input' => [
  2815. 'type' => 'radio',
  2816. 'name' => 'Model[field]',
  2817. 'value' => '0',
  2818. 'id' => 'prefix-model-field-0'
  2819. ]],
  2820. 'option A',
  2821. '/label'
  2822. ];
  2823. $this->assertHtml($expected, $result);
  2824. $result = $this->Form->select(
  2825. 'Model.multi_field',
  2826. ['first'],
  2827. ['multiple' => 'checkbox']
  2828. );
  2829. $expected = [
  2830. 'input' => [
  2831. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2832. ],
  2833. ['div' => ['class' => 'checkbox']],
  2834. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2835. ['input' => [
  2836. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2837. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2838. ]],
  2839. 'first',
  2840. '/label',
  2841. '/div',
  2842. ];
  2843. $this->assertHtml($expected, $result);
  2844. $this->Form->end();
  2845. $result = $this->Form->input('field');
  2846. $expected = [
  2847. 'div' => ['class' => 'input text'],
  2848. 'label' => ['for' => 'field'],
  2849. 'Field',
  2850. '/label',
  2851. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2852. '/div'
  2853. ];
  2854. $this->assertHtml($expected, $result);
  2855. }
  2856. /**
  2857. * Test that inputs with 0 can be created.
  2858. *
  2859. * @return void
  2860. */
  2861. public function testInputZero()
  2862. {
  2863. TableRegistry::get('Contacts', [
  2864. 'className' => __NAMESPACE__ . '\ContactsTable'
  2865. ]);
  2866. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2867. $result = $this->Form->input('0');
  2868. $expected = [
  2869. 'div' => ['class' => 'input text'],
  2870. 'label' => ['for' => '0'], '/label',
  2871. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2872. '/div'
  2873. ];
  2874. $this->assertHtml($expected, $result);
  2875. }
  2876. /**
  2877. * test input() with checkbox creation
  2878. *
  2879. * @return void
  2880. */
  2881. public function testInputCheckbox()
  2882. {
  2883. $result = $this->Form->input('User.active', ['label' => false, 'checked' => true]);
  2884. $expected = [
  2885. 'div' => ['class' => 'input checkbox'],
  2886. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2887. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2888. '/div'
  2889. ];
  2890. $this->assertHtml($expected, $result);
  2891. $result = $this->Form->input('User.active', ['label' => false, 'checked' => 1]);
  2892. $expected = [
  2893. 'div' => ['class' => 'input checkbox'],
  2894. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2895. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2896. '/div'
  2897. ];
  2898. $this->assertHtml($expected, $result);
  2899. $result = $this->Form->input('User.active', ['label' => false, 'checked' => '1']);
  2900. $expected = [
  2901. 'div' => ['class' => 'input checkbox'],
  2902. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2903. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2904. '/div'
  2905. ];
  2906. $this->assertHtml($expected, $result);
  2907. $result = $this->Form->input('User.disabled', [
  2908. 'label' => 'Disabled',
  2909. 'type' => 'checkbox',
  2910. 'data-foo' => 'disabled'
  2911. ]);
  2912. $expected = [
  2913. 'div' => ['class' => 'input checkbox'],
  2914. 'input' => ['type' => 'hidden', 'name' => 'User[disabled]', 'value' => '0'],
  2915. 'label' => ['for' => 'user-disabled'],
  2916. ['input' => [
  2917. 'type' => 'checkbox',
  2918. 'name' => 'User[disabled]',
  2919. 'value' => '1',
  2920. 'id' => 'user-disabled',
  2921. 'data-foo' => 'disabled'
  2922. ]],
  2923. 'Disabled',
  2924. '/label',
  2925. '/div'
  2926. ];
  2927. $this->assertHtml($expected, $result);
  2928. $result = $this->Form->input('User.confirm', [
  2929. 'label' => 'Confirm <b>me</b>!',
  2930. 'type' => 'checkbox',
  2931. 'escape' => false
  2932. ]);
  2933. $expected = [
  2934. 'div' => ['class' => 'input checkbox'],
  2935. 'input' => ['type' => 'hidden', 'name' => 'User[confirm]', 'value' => '0'],
  2936. 'label' => ['for' => 'user-confirm'],
  2937. ['input' => [
  2938. 'type' => 'checkbox',
  2939. 'name' => 'User[confirm]',
  2940. 'value' => '1',
  2941. 'id' => 'user-confirm',
  2942. ]],
  2943. 'Confirm <b>me</b>!',
  2944. '/label',
  2945. '/div'
  2946. ];
  2947. $this->assertHtml($expected, $result);
  2948. }
  2949. /**
  2950. * Test that input() does not create wrapping div and label tag for hidden fields
  2951. *
  2952. * @return void
  2953. */
  2954. public function testInputHidden()
  2955. {
  2956. TableRegistry::get('ValidateUsers', [
  2957. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2958. ]);
  2959. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2960. $result = $this->Form->input('ValidateUser.id');
  2961. $expected = [
  2962. 'input' => ['name', 'type' => 'hidden', 'id']
  2963. ];
  2964. $this->assertHtml($expected, $result);
  2965. $result = $this->Form->input('ValidateUser.custom', ['type' => 'hidden']);
  2966. $expected = [
  2967. 'input' => ['name', 'type' => 'hidden', 'id']
  2968. ];
  2969. $this->assertHtml($expected, $result);
  2970. }
  2971. /**
  2972. * test form->input() with datetime
  2973. *
  2974. * @return void
  2975. */
  2976. public function testInputDatetime()
  2977. {
  2978. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  2979. ->setMethods(['datetime'])
  2980. ->setConstructorArgs([new View()])
  2981. ->getMock();
  2982. $this->Form->expects($this->once())->method('datetime')
  2983. ->with('prueba', [
  2984. 'type' => 'datetime',
  2985. 'timeFormat' => 24,
  2986. 'minYear' => 2008,
  2987. 'maxYear' => 2011,
  2988. 'interval' => 15,
  2989. 'options' => null,
  2990. 'empty' => false,
  2991. 'id' => 'prueba',
  2992. 'required' => false,
  2993. 'templateVars' => []
  2994. ])
  2995. ->will($this->returnValue('This is it!'));
  2996. $result = $this->Form->input('prueba', [
  2997. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  2998. 'maxYear' => 2011, 'interval' => 15
  2999. ]);
  3000. $expected = [
  3001. 'div' => ['class' => 'input datetime'],
  3002. '<label',
  3003. 'Prueba',
  3004. '/label',
  3005. 'This is it!',
  3006. '/div'
  3007. ];
  3008. $this->assertHtml($expected, $result);
  3009. }
  3010. /**
  3011. * test form->input() with datetime with id prefix
  3012. *
  3013. * @return void
  3014. */
  3015. public function testInputDatetimeIdPrefix()
  3016. {
  3017. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3018. ->setMethods(['datetime'])
  3019. ->setConstructorArgs([new View()])
  3020. ->getMock();
  3021. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3022. $this->Form->expects($this->once())->method('datetime')
  3023. ->with('prueba', [
  3024. 'type' => 'datetime',
  3025. 'timeFormat' => 24,
  3026. 'minYear' => 2008,
  3027. 'maxYear' => 2011,
  3028. 'interval' => 15,
  3029. 'options' => null,
  3030. 'empty' => false,
  3031. 'id' => 'prefix-prueba',
  3032. 'required' => false,
  3033. 'templateVars' => []
  3034. ])
  3035. ->will($this->returnValue('This is it!'));
  3036. $result = $this->Form->input('prueba', [
  3037. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3038. 'maxYear' => 2011, 'interval' => 15
  3039. ]);
  3040. $expected = [
  3041. 'div' => ['class' => 'input datetime'],
  3042. '<label',
  3043. 'Prueba',
  3044. '/label',
  3045. 'This is it!',
  3046. '/div'
  3047. ];
  3048. $this->assertHtml($expected, $result);
  3049. }
  3050. /**
  3051. * Test generating checkboxes with disabled elements.
  3052. *
  3053. * @return void
  3054. */
  3055. public function testInputCheckboxWithDisabledElements()
  3056. {
  3057. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3058. $result = $this->Form->input('Contact.multiple', [
  3059. 'multiple' => 'checkbox',
  3060. 'disabled' => 'disabled',
  3061. 'options' => $options
  3062. ]);
  3063. $expected = [
  3064. ['div' => ['class' => 'input select']],
  3065. ['label' => ['for' => "contact-multiple"]],
  3066. 'Multiple',
  3067. '/label',
  3068. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'disabled' => 'disabled', 'value' => '']],
  3069. ['div' => ['class' => 'checkbox']],
  3070. ['label' => ['for' => "contact-multiple-1"]],
  3071. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "contact-multiple-1"]],
  3072. 'One',
  3073. '/label',
  3074. '/div',
  3075. ['div' => ['class' => 'checkbox']],
  3076. ['label' => ['for' => "contact-multiple-2"]],
  3077. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "contact-multiple-2"]],
  3078. 'Two',
  3079. '/label',
  3080. '/div',
  3081. ['div' => ['class' => 'checkbox']],
  3082. ['label' => ['for' => "contact-multiple-3"]],
  3083. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "contact-multiple-3"]],
  3084. 'Three',
  3085. '/label',
  3086. '/div',
  3087. '/div'
  3088. ];
  3089. $this->assertHtml($expected, $result);
  3090. // make sure 50 does only disable 50, and not 50f5c0cf
  3091. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3092. $disabled = [50];
  3093. $expected = [
  3094. ['div' => ['class' => 'input select']],
  3095. ['label' => ['for' => "contact-multiple"]],
  3096. 'Multiple',
  3097. '/label',
  3098. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '']],
  3099. ['div' => ['class' => 'checkbox']],
  3100. ['label' => ['for' => "contact-multiple-50"]],
  3101. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "contact-multiple-50"]],
  3102. 'Fifty',
  3103. '/label',
  3104. '/div',
  3105. ['div' => ['class' => 'checkbox']],
  3106. ['label' => ['for' => "contact-multiple-50f5c0cf"]],
  3107. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "contact-multiple-50f5c0cf"]],
  3108. 'Stringy',
  3109. '/label',
  3110. '/div',
  3111. '/div'
  3112. ];
  3113. $result = $this->Form->input('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3114. $this->assertHtml($expected, $result);
  3115. }
  3116. /**
  3117. * test input name with leading integer, ensure attributes are generated correctly.
  3118. *
  3119. * @return void
  3120. */
  3121. public function testInputWithLeadingInteger()
  3122. {
  3123. $result = $this->Form->text('0.Node.title');
  3124. $expected = [
  3125. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3126. ];
  3127. $this->assertHtml($expected, $result);
  3128. }
  3129. /**
  3130. * test form->input() with select type inputs.
  3131. *
  3132. * @return void
  3133. */
  3134. public function testInputSelectType()
  3135. {
  3136. $result = $this->Form->input(
  3137. 'email',
  3138. [
  3139. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3140. );
  3141. $expected = [
  3142. 'div' => ['class' => 'input select'],
  3143. 'label' => ['for' => 'email'],
  3144. 'Email',
  3145. '/label',
  3146. ['select' => ['name' => 'email', 'id' => 'email']],
  3147. ['option' => ['value' => '']],
  3148. '/option',
  3149. ['option' => ['value' => 'è']],
  3150. 'Firést',
  3151. '/option',
  3152. ['option' => ['value' => 'é']],
  3153. 'Secoènd',
  3154. '/option',
  3155. '/select',
  3156. '/div'
  3157. ];
  3158. $this->assertHtml($expected, $result);
  3159. $result = $this->Form->input(
  3160. 'email',
  3161. [
  3162. 'options' => ['First', 'Second'], 'empty' => true]
  3163. );
  3164. $expected = [
  3165. 'div' => ['class' => 'input select'],
  3166. 'label' => ['for' => 'email'],
  3167. 'Email',
  3168. '/label',
  3169. ['select' => ['name' => 'email', 'id' => 'email']],
  3170. ['option' => ['value' => '']],
  3171. '/option',
  3172. ['option' => ['value' => '0']],
  3173. 'First',
  3174. '/option',
  3175. ['option' => ['value' => '1']],
  3176. 'Second',
  3177. '/option',
  3178. '/select',
  3179. '/div'
  3180. ];
  3181. $this->assertHtml($expected, $result);
  3182. $result = $this->Form->input('email', [
  3183. 'type' => 'select',
  3184. 'options' => new \ArrayObject(['First', 'Second']),
  3185. 'empty' => true
  3186. ]);
  3187. $this->assertHtml($expected, $result);
  3188. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3189. $this->Form->request->data = ['Model' => ['user_id' => 'value']];
  3190. $result = $this->Form->input('Model.user_id', ['empty' => true]);
  3191. $expected = [
  3192. 'div' => ['class' => 'input select'],
  3193. 'label' => ['for' => 'model-user-id'],
  3194. 'User',
  3195. '/label',
  3196. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3197. ['option' => ['value' => '']],
  3198. '/option',
  3199. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3200. 'good',
  3201. '/option',
  3202. ['option' => ['value' => 'other']],
  3203. 'bad',
  3204. '/option',
  3205. '/select',
  3206. '/div'
  3207. ];
  3208. $this->assertHtml($expected, $result);
  3209. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3210. $this->Form->request->data = ['Thing' => ['user_id' => null]];
  3211. $result = $this->Form->input('Thing.user_id', ['empty' => 'Some Empty']);
  3212. $expected = [
  3213. 'div' => ['class' => 'input select'],
  3214. 'label' => ['for' => 'thing-user-id'],
  3215. 'User',
  3216. '/label',
  3217. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3218. ['option' => ['value' => '']],
  3219. 'Some Empty',
  3220. '/option',
  3221. ['option' => ['value' => 'value']],
  3222. 'good',
  3223. '/option',
  3224. ['option' => ['value' => 'other']],
  3225. 'bad',
  3226. '/option',
  3227. '/select',
  3228. '/div'
  3229. ];
  3230. $this->assertHtml($expected, $result);
  3231. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3232. $this->Form->request->data = ['Thing' => ['user_id' => 'value']];
  3233. $result = $this->Form->input('Thing.user_id', ['empty' => 'Some Empty']);
  3234. $expected = [
  3235. 'div' => ['class' => 'input select'],
  3236. 'label' => ['for' => 'thing-user-id'],
  3237. 'User',
  3238. '/label',
  3239. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3240. ['option' => ['value' => '']],
  3241. 'Some Empty',
  3242. '/option',
  3243. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3244. 'good',
  3245. '/option',
  3246. ['option' => ['value' => 'other']],
  3247. 'bad',
  3248. '/option',
  3249. '/select',
  3250. '/div'
  3251. ];
  3252. $this->assertHtml($expected, $result);
  3253. $this->Form->data = [];
  3254. $result = $this->Form->input('Publisher.id', [
  3255. 'label' => 'Publisher',
  3256. 'type' => 'select',
  3257. 'multiple' => 'checkbox',
  3258. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3259. ]);
  3260. $expected = [
  3261. ['div' => ['class' => 'input select']],
  3262. ['label' => ['for' => 'publisher-id']],
  3263. 'Publisher',
  3264. '/label',
  3265. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3266. ['div' => ['class' => 'checkbox']],
  3267. ['label' => ['for' => 'publisher-id-value-1']],
  3268. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3269. 'Label 1',
  3270. '/label',
  3271. '/div',
  3272. ['div' => ['class' => 'checkbox']],
  3273. ['label' => ['for' => 'publisher-id-value-2']],
  3274. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3275. 'Label 2',
  3276. '/label',
  3277. '/div',
  3278. '/div'
  3279. ];
  3280. $this->assertHtml($expected, $result);
  3281. }
  3282. /**
  3283. * test that input() and a non standard primary key makes a hidden input by default.
  3284. *
  3285. * @return void
  3286. */
  3287. public function testInputWithNonStandardPrimaryKeyMakesHidden()
  3288. {
  3289. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3290. $this->Form->create($this->article);
  3291. $result = $this->Form->input('title');
  3292. $expected = [
  3293. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3294. ];
  3295. $this->assertHtml($expected, $result);
  3296. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3297. $this->Form->create($this->article);
  3298. $result = $this->Form->input('title');
  3299. $expected = [
  3300. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3301. ];
  3302. $this->assertHtml($expected, $result);
  3303. $result = $this->Form->input('body');
  3304. $expected = [
  3305. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3306. ];
  3307. $this->assertHtml($expected, $result);
  3308. }
  3309. /**
  3310. * test that overriding the magic select type widget is possible
  3311. *
  3312. * @return void
  3313. */
  3314. public function testInputOverridingMagicSelectType()
  3315. {
  3316. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3317. $result = $this->Form->input('Model.user_id', ['type' => 'text']);
  3318. $expected = [
  3319. 'div' => ['class' => 'input text'],
  3320. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3321. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3322. '/div'
  3323. ];
  3324. $this->assertHtml($expected, $result);
  3325. //Check that magic types still work for plural/singular vars
  3326. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3327. $result = $this->Form->input('Model.type');
  3328. $expected = [
  3329. 'div' => ['class' => 'input select'],
  3330. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3331. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3332. ['option' => ['value' => 'value']], 'good', '/option',
  3333. ['option' => ['value' => 'other']], 'bad', '/option',
  3334. '/select',
  3335. '/div'
  3336. ];
  3337. $this->assertHtml($expected, $result);
  3338. }
  3339. /**
  3340. * Test that inferred types do not override developer input
  3341. *
  3342. * @return void
  3343. */
  3344. public function testInputMagicTypeDoesNotOverride()
  3345. {
  3346. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3347. $result = $this->Form->input('Model.user', ['type' => 'checkbox']);
  3348. $expected = [
  3349. 'div' => ['class' => 'input checkbox'],
  3350. ['input' => [
  3351. 'type' => 'hidden',
  3352. 'name' => 'Model[user]',
  3353. 'value' => 0,
  3354. ]],
  3355. 'label' => ['for' => 'model-user'],
  3356. ['input' => [
  3357. 'name' => 'Model[user]',
  3358. 'type' => 'checkbox',
  3359. 'id' => 'model-user',
  3360. 'value' => 1
  3361. ]],
  3362. 'User',
  3363. '/label',
  3364. '/div'
  3365. ];
  3366. $this->assertHtml($expected, $result);
  3367. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3368. $options = [
  3369. 1 => 'blue',
  3370. 2 => 'red'
  3371. ];
  3372. $result = $this->Form->input('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3373. $expected = [
  3374. 'div' => ['class' => 'input select'],
  3375. 'label' => ['for' => 'tags-ids'],
  3376. 'Tags',
  3377. '/label',
  3378. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3379. ['div' => ['class' => 'checkbox']],
  3380. ['label' => ['for' => 'tags-ids-1']],
  3381. ['input' => [
  3382. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3383. 'value' => '1', 'name' => 'tags[_ids][]'
  3384. ]],
  3385. 'blue',
  3386. '/label',
  3387. '/div',
  3388. ['div' => ['class' => 'checkbox']],
  3389. ['label' => ['for' => 'tags-ids-2']],
  3390. ['input' => [
  3391. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3392. 'value' => '2', 'name' => 'tags[_ids][]'
  3393. ]],
  3394. 'red',
  3395. '/label',
  3396. '/div',
  3397. '/div'
  3398. ];
  3399. $this->assertHtml($expected, $result);
  3400. }
  3401. /**
  3402. * Test that magic input() selects are created for type=number
  3403. *
  3404. * @return void
  3405. */
  3406. public function testInputMagicSelectForTypeNumber()
  3407. {
  3408. TableRegistry::get('ValidateUsers', [
  3409. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3410. ]);
  3411. $entity = new Entity(['balance' => 1]);
  3412. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3413. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3414. $result = $this->Form->input('balance');
  3415. $expected = [
  3416. 'div' => ['class' => 'input select'],
  3417. 'label' => ['for' => 'balance'],
  3418. 'Balance',
  3419. '/label',
  3420. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3421. ['option' => ['value' => '0']],
  3422. 'nothing',
  3423. '/option',
  3424. ['option' => ['value' => '1', 'selected' => 'selected']],
  3425. 'some',
  3426. '/option',
  3427. ['option' => ['value' => '100']],
  3428. 'a lot',
  3429. '/option',
  3430. '/select',
  3431. '/div'
  3432. ];
  3433. $this->assertHtml($expected, $result);
  3434. }
  3435. /**
  3436. * Test invalid 'input' type option to input() function.
  3437. *
  3438. * @expectedException \RuntimeException
  3439. * @expectedExceptionMessage Invalid type 'input' used for field 'text'
  3440. * @return void
  3441. */
  3442. public function testInvalidInputTypeOption()
  3443. {
  3444. $this->Form->input('text', ['type' => 'input']);
  3445. }
  3446. /**
  3447. * Test that magic input() selects can easily be converted into radio types without error.
  3448. *
  3449. * @return void
  3450. */
  3451. public function testInputMagicSelectChangeToRadio()
  3452. {
  3453. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3454. $result = $this->Form->input('Model.user_id', ['type' => 'radio']);
  3455. $this->assertContains('input type="radio"', $result);
  3456. }
  3457. /**
  3458. * testFormInputSubmit method
  3459. *
  3460. * test correct results for form::input() and type submit.
  3461. *
  3462. * @return void
  3463. */
  3464. public function testFormInputSubmit()
  3465. {
  3466. $result = $this->Form->input('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3467. $expected = [
  3468. 'div' => ['class' => 'submit'],
  3469. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3470. '/div'
  3471. ];
  3472. $this->assertHtml($expected, $result);
  3473. }
  3474. /**
  3475. * testFormInputs method
  3476. *
  3477. * test correct results from form::inputs().
  3478. *
  3479. * @return void
  3480. */
  3481. public function testFormInputsLegendFieldset()
  3482. {
  3483. $this->Form->create($this->article);
  3484. $result = $this->Form->allInputs([], ['legend' => 'The Legend']);
  3485. $expected = [
  3486. '<fieldset',
  3487. '<legend',
  3488. 'The Legend',
  3489. '/legend',
  3490. '*/fieldset',
  3491. ];
  3492. $this->assertHtml($expected, $result);
  3493. $result = $this->Form->allInputs([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3494. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3495. $this->assertContains('<fieldset>', $result);
  3496. $result = $this->Form->allInputs([], ['fieldset' => false, 'legend' => false]);
  3497. $this->assertNotContains('<legend>', $result);
  3498. $this->assertNotContains('<fieldset>', $result);
  3499. $result = $this->Form->allInputs([], ['fieldset' => false, 'legend' => 'Hello']);
  3500. $this->assertNotContains('<legend>', $result);
  3501. $this->assertNotContains('<fieldset>', $result);
  3502. $this->Form->create($this->article);
  3503. $this->Form->request->params['prefix'] = 'admin';
  3504. $this->Form->request->params['action'] = 'admin_edit';
  3505. $this->Form->request->params['controller'] = 'articles';
  3506. $result = $this->Form->allInputs();
  3507. $expected = [
  3508. '<fieldset',
  3509. '<legend',
  3510. 'New Article',
  3511. '/legend',
  3512. '*/fieldset',
  3513. ];
  3514. $this->assertHtml($expected, $result);
  3515. $this->Form->create($this->article);
  3516. $result = $this->Form->allInputs([], ['fieldset' => [], 'legend' => 'The Legend']);
  3517. $expected = [
  3518. '<fieldset',
  3519. '<legend',
  3520. 'The Legend',
  3521. '/legend',
  3522. '*/fieldset',
  3523. ];
  3524. $this->assertHtml($expected, $result);
  3525. $this->Form->create($this->article);
  3526. $result = $this->Form->allInputs([], [
  3527. 'fieldset' => [
  3528. 'class' => 'some-class some-other-class',
  3529. 'disabled' => true,
  3530. 'data-param' => 'a-param'
  3531. ],
  3532. 'legend' => 'The Legend'
  3533. ]);
  3534. $expected = [
  3535. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3536. '<legend',
  3537. 'The Legend',
  3538. '/legend',
  3539. '*/fieldset',
  3540. ];
  3541. $this->assertHtml($expected, $result);
  3542. }
  3543. /**
  3544. * Test the inputs() method.
  3545. *
  3546. * @return void
  3547. */
  3548. public function testFormInputs()
  3549. {
  3550. $this->Form->create($this->article);
  3551. $result = $this->Form->allInputs();
  3552. $expected = [
  3553. '<fieldset',
  3554. '<legend', 'New Article', '/legend',
  3555. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3556. ['div' => ['class' => 'input select required']],
  3557. '*/div',
  3558. ['div' => ['class' => 'input text required']],
  3559. '*/div',
  3560. ['div' => ['class' => 'input text']],
  3561. '*/div',
  3562. ['div' => ['class' => 'input text']],
  3563. '*/div',
  3564. '/fieldset',
  3565. ];
  3566. $this->assertHtml($expected, $result);
  3567. $result = $this->Form->allInputs([
  3568. 'published' => ['type' => 'boolean']
  3569. ]);
  3570. $expected = [
  3571. '<fieldset',
  3572. '<legend', 'New Article', '/legend',
  3573. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3574. ['div' => ['class' => 'input select required']],
  3575. '*/div',
  3576. ['div' => ['class' => 'input text required']],
  3577. '*/div',
  3578. ['div' => ['class' => 'input text']],
  3579. '*/div',
  3580. ['div' => ['class' => 'input boolean']],
  3581. '*/div',
  3582. '/fieldset',
  3583. ];
  3584. $this->assertHtml($expected, $result);
  3585. $this->Form->create($this->article);
  3586. $result = $this->Form->allInputs([], ['legend' => 'Hello']);
  3587. $expected = [
  3588. 'fieldset' => [],
  3589. 'legend' => [],
  3590. 'Hello',
  3591. '/legend',
  3592. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3593. ['div' => ['class' => 'input select required']],
  3594. '*/div',
  3595. ['div' => ['class' => 'input text required']],
  3596. '*/div',
  3597. ['div' => ['class' => 'input text']],
  3598. '*/div',
  3599. ['div' => ['class' => 'input text']],
  3600. '*/div',
  3601. '/fieldset'
  3602. ];
  3603. $this->assertHtml($expected, $result);
  3604. $this->Form->create(false);
  3605. $expected = [
  3606. 'fieldset' => [],
  3607. ['div' => ['class' => 'input text']],
  3608. 'label' => ['for' => 'foo'],
  3609. 'Foo',
  3610. '/label',
  3611. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3612. '*/div',
  3613. '/fieldset'
  3614. ];
  3615. $result = $this->Form->allInputs(
  3616. ['foo' => ['type' => 'text']],
  3617. ['legend' => false]
  3618. );
  3619. $this->assertHtml($expected, $result);
  3620. }
  3621. /**
  3622. * testFormInputsBlacklist
  3623. *
  3624. * @return void
  3625. */
  3626. public function testFormInputsBlacklist()
  3627. {
  3628. $this->Form->create($this->article);
  3629. $result = $this->Form->allInputs([
  3630. 'id' => false
  3631. ]);
  3632. $expected = [
  3633. '<fieldset',
  3634. '<legend', 'New Article', '/legend',
  3635. ['div' => ['class' => 'input select required']],
  3636. '*/div',
  3637. ['div' => ['class' => 'input text required']],
  3638. '*/div',
  3639. ['div' => ['class' => 'input text']],
  3640. '*/div',
  3641. ['div' => ['class' => 'input text']],
  3642. '*/div',
  3643. '/fieldset',
  3644. ];
  3645. $this->assertHtml($expected, $result);
  3646. $this->Form->create($this->article);
  3647. $result = $this->Form->allInputs([
  3648. 'id' => []
  3649. ]);
  3650. $expected = [
  3651. '<fieldset',
  3652. '<legend', 'New Article', '/legend',
  3653. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3654. ['div' => ['class' => 'input select required']],
  3655. '*/div',
  3656. ['div' => ['class' => 'input text required']],
  3657. '*/div',
  3658. ['div' => ['class' => 'input text']],
  3659. '*/div',
  3660. ['div' => ['class' => 'input text']],
  3661. '*/div',
  3662. '/fieldset',
  3663. ];
  3664. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3665. }
  3666. /**
  3667. * testSelectAsCheckbox method
  3668. *
  3669. * test multi-select widget with checkbox formatting.
  3670. *
  3671. * @return void
  3672. */
  3673. public function testSelectAsCheckbox()
  3674. {
  3675. $result = $this->Form->select(
  3676. 'Model.multi_field',
  3677. ['first', 'second', 'third'],
  3678. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3679. );
  3680. $expected = [
  3681. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3682. ['div' => ['class' => 'checkbox']],
  3683. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3684. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3685. 'first',
  3686. '/label',
  3687. '/div',
  3688. ['div' => ['class' => 'checkbox']],
  3689. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3690. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3691. 'second',
  3692. '/label',
  3693. '/div',
  3694. ['div' => ['class' => 'checkbox']],
  3695. ['label' => ['for' => 'model-multi-field-2']],
  3696. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3697. 'third',
  3698. '/label',
  3699. '/div',
  3700. ];
  3701. $this->assertHtml($expected, $result);
  3702. $result = $this->Form->select(
  3703. 'Model.multi_field',
  3704. ['1/2' => 'half'],
  3705. ['multiple' => 'checkbox']
  3706. );
  3707. $expected = [
  3708. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3709. ['div' => ['class' => 'checkbox']],
  3710. ['label' => ['for' => 'model-multi-field-1-2']],
  3711. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3712. 'half',
  3713. '/label',
  3714. '/div',
  3715. ];
  3716. $this->assertHtml($expected, $result);
  3717. }
  3718. /**
  3719. * testLabel method
  3720. *
  3721. * test label generation.
  3722. *
  3723. * @return void
  3724. */
  3725. public function testLabel()
  3726. {
  3727. $result = $this->Form->label('Person.name');
  3728. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3729. $this->assertHtml($expected, $result);
  3730. $result = $this->Form->label('Person.name');
  3731. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3732. $this->assertHtml($expected, $result);
  3733. $result = $this->Form->label('Person.first_name');
  3734. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3735. $this->assertHtml($expected, $result);
  3736. $result = $this->Form->label('Person.first_name', 'Your first name');
  3737. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3738. $this->assertHtml($expected, $result);
  3739. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3740. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3741. $this->assertHtml($expected, $result);
  3742. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3743. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3744. $this->assertHtml($expected, $result);
  3745. $result = $this->Form->label('Person.first_name', '');
  3746. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3747. $this->assertHtml($expected, $result);
  3748. $result = $this->Form->label('Person.2.name', '');
  3749. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3750. $this->assertHtml($expected, $result);
  3751. }
  3752. /**
  3753. * Test that label() can accept an input with the correct template vars.
  3754. *
  3755. * @return void
  3756. */
  3757. public function testLabelContainInput()
  3758. {
  3759. $this->Form->templates([
  3760. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3761. ]);
  3762. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3763. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3764. ]);
  3765. $expected = [
  3766. 'label' => ['for' => 'person-accept-terms'],
  3767. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3768. 'Accept',
  3769. '/label',
  3770. ];
  3771. $this->assertHtml($expected, $result);
  3772. }
  3773. /**
  3774. * testTextbox method
  3775. *
  3776. * test textbox element generation
  3777. *
  3778. * @return void
  3779. */
  3780. public function testTextbox()
  3781. {
  3782. $result = $this->Form->text('Model.field');
  3783. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3784. $this->assertHtml($expected, $result);
  3785. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3786. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3787. $this->assertHtml($expected, $result);
  3788. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3789. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3790. $this->assertHtml($expected, $result);
  3791. }
  3792. /**
  3793. * Test that text() hooks up with request data and error fields.
  3794. *
  3795. * @return void
  3796. */
  3797. public function testTextBoxDataAndError()
  3798. {
  3799. $this->article['errors'] = [
  3800. 'Contact' => ['text' => 'wrong']
  3801. ];
  3802. $this->Form->create($this->article);
  3803. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3804. $result = $this->Form->text('Model.text');
  3805. $expected = [
  3806. 'input' => [
  3807. 'type' => 'text',
  3808. 'name' => 'Model[text]',
  3809. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3810. ]
  3811. ];
  3812. $this->assertHtml($expected, $result);
  3813. $this->Form->request->data['Contact']['text'] = 'test';
  3814. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3815. $expected = [
  3816. 'input' => [
  3817. 'type' => 'text',
  3818. 'name' => 'Contact[text]',
  3819. 'value' => 'test',
  3820. 'id' => 'theID',
  3821. 'class' => 'form-error'
  3822. ]
  3823. ];
  3824. $this->assertHtml($expected, $result);
  3825. }
  3826. /**
  3827. * testDefaultValue method
  3828. *
  3829. * Test default value setting
  3830. *
  3831. * @return void
  3832. */
  3833. public function testTextDefaultValue()
  3834. {
  3835. $this->Form->request->data['Model']['field'] = 'test';
  3836. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3837. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3838. $this->assertHtml($expected, $result);
  3839. unset($this->Form->request->data['Model']['field']);
  3840. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3841. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3842. $this->assertHtml($expected, $result);
  3843. $Articles = TableRegistry::get('Articles');
  3844. $title = $Articles->schema()->column('title');
  3845. $Articles->schema()->addColumn(
  3846. 'title',
  3847. ['default' => 'default title'] + $title
  3848. );
  3849. $entity = $Articles->newEntity();
  3850. $this->Form->create($entity);
  3851. // Get default value from schema
  3852. $result = $this->Form->text('title');
  3853. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  3854. $this->assertHtml($expected, $result);
  3855. // Don't get value from schema
  3856. $result = $this->Form->text('title', ['schemaDefault' => false]);
  3857. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  3858. $this->assertHtml($expected, $result);
  3859. // Custom default value overrides default value from schema
  3860. $result = $this->Form->text('title', ['default' => 'override default']);
  3861. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  3862. $this->assertHtml($expected, $result);
  3863. // Default value from schema is used only for new entities.
  3864. $entity->isNew(false);
  3865. $result = $this->Form->text('title');
  3866. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  3867. $this->assertHtml($expected, $result);
  3868. }
  3869. /**
  3870. * testError method
  3871. *
  3872. * Test field error generation
  3873. *
  3874. * @return void
  3875. */
  3876. public function testError()
  3877. {
  3878. $this->article['errors'] = [
  3879. 'Article' => ['field' => 'email']
  3880. ];
  3881. $this->Form->create($this->article);
  3882. $result = $this->Form->error('Article.field');
  3883. $expected = [
  3884. ['div' => ['class' => 'error-message']],
  3885. 'email',
  3886. '/div',
  3887. ];
  3888. $this->assertHtml($expected, $result);
  3889. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  3890. $expected = [
  3891. ['div' => ['class' => 'error-message']],
  3892. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3893. '/div',
  3894. ];
  3895. $this->assertHtml($expected, $result);
  3896. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  3897. $expected = [
  3898. ['div' => ['class' => 'error-message']],
  3899. '<strong', 'Badness!', '/strong',
  3900. '/div',
  3901. ];
  3902. $this->assertHtml($expected, $result);
  3903. }
  3904. /**
  3905. * test error translation can use rule names for translating.
  3906. *
  3907. * @return void
  3908. */
  3909. public function testErrorRuleName()
  3910. {
  3911. $this->article['errors'] = [
  3912. 'Article' => [
  3913. 'field' => ['email' => 'Your email was not good']
  3914. ]
  3915. ];
  3916. $this->Form->create($this->article);
  3917. $result = $this->Form->error('Article.field');
  3918. $expected = [
  3919. ['div' => ['class' => 'error-message']],
  3920. 'Your email was not good',
  3921. '/div',
  3922. ];
  3923. $this->assertHtml($expected, $result);
  3924. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  3925. $expected = [
  3926. ['div' => ['class' => 'error-message']],
  3927. 'Email in use',
  3928. '/div',
  3929. ];
  3930. $this->assertHtml($expected, $result);
  3931. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  3932. $expected = [
  3933. ['div' => ['class' => 'error-message']],
  3934. 'Email in use',
  3935. '/div',
  3936. ];
  3937. $this->assertHtml($expected, $result);
  3938. $result = $this->Form->error('Article.field', [
  3939. 'email' => 'Key is preferred',
  3940. 'Your email was not good' => 'Email in use'
  3941. ]);
  3942. $expected = [
  3943. ['div' => ['class' => 'error-message']],
  3944. 'Key is preferred',
  3945. '/div',
  3946. ];
  3947. $this->assertHtml($expected, $result);
  3948. }
  3949. /**
  3950. * Test error with nested lists.
  3951. *
  3952. * @return void
  3953. */
  3954. public function testErrorMessages()
  3955. {
  3956. $this->article['errors'] = [
  3957. 'Article' => ['field' => 'email']
  3958. ];
  3959. $this->Form->create($this->article);
  3960. $result = $this->Form->error('Article.field', [
  3961. 'email' => 'No good!'
  3962. ]);
  3963. $expected = [
  3964. 'div' => ['class' => 'error-message'],
  3965. 'No good!',
  3966. '/div'
  3967. ];
  3968. $this->assertHtml($expected, $result);
  3969. }
  3970. /**
  3971. * test error() with multiple messages.
  3972. *
  3973. * @return void
  3974. */
  3975. public function testErrorMultipleMessages()
  3976. {
  3977. $this->article['errors'] = [
  3978. 'field' => ['notBlank', 'email', 'Something else']
  3979. ];
  3980. $this->Form->create($this->article);
  3981. $result = $this->Form->error('field', [
  3982. 'notBlank' => 'Cannot be empty',
  3983. 'email' => 'No good!'
  3984. ]);
  3985. $expected = [
  3986. 'div' => ['class' => 'error-message'],
  3987. 'ul' => [],
  3988. '<li', 'Cannot be empty', '/li',
  3989. '<li', 'No good!', '/li',
  3990. '<li', 'Something else', '/li',
  3991. '/ul',
  3992. '/div'
  3993. ];
  3994. $this->assertHtml($expected, $result);
  3995. }
  3996. /**
  3997. * testPassword method
  3998. *
  3999. * Test password element generation
  4000. *
  4001. * @return void
  4002. */
  4003. public function testPassword()
  4004. {
  4005. $this->article['errors'] = [
  4006. 'Contact' => [
  4007. 'passwd' => 1
  4008. ]
  4009. ];
  4010. $this->Form->create($this->article);
  4011. $result = $this->Form->password('Contact.field');
  4012. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4013. $this->assertHtml($expected, $result);
  4014. $this->Form->request->data['Contact']['passwd'] = 'test';
  4015. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4016. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4017. $this->assertHtml($expected, $result);
  4018. }
  4019. /**
  4020. * testRadio method
  4021. *
  4022. * Test radio element set generation
  4023. *
  4024. * @return void
  4025. */
  4026. public function testRadio()
  4027. {
  4028. $result = $this->Form->radio('Model.field', ['option A']);
  4029. $expected = [
  4030. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4031. 'label' => ['for' => 'model-field-0'],
  4032. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4033. 'option A',
  4034. '/label'
  4035. ];
  4036. $this->assertHtml($expected, $result);
  4037. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4038. $this->assertHtml($expected, $result);
  4039. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4040. $expected = [
  4041. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4042. ['label' => ['for' => 'model-field-0']],
  4043. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4044. 'option A',
  4045. '/label',
  4046. ['label' => ['for' => 'model-field-1']],
  4047. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4048. 'option B',
  4049. '/label',
  4050. ];
  4051. $this->assertHtml($expected, $result);
  4052. $result = $this->Form->radio(
  4053. 'Employee.gender',
  4054. ['male' => 'Male', 'female' => 'Female'],
  4055. ['form' => 'my-form']
  4056. );
  4057. $expected = [
  4058. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4059. ['label' => ['for' => 'employee-gender-male']],
  4060. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4061. 'Male',
  4062. '/label',
  4063. ['label' => ['for' => 'employee-gender-female']],
  4064. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4065. 'Female',
  4066. '/label',
  4067. ];
  4068. $this->assertHtml($expected, $result);
  4069. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4070. $expected = [
  4071. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4072. ['label' => ['for' => 'model-custom-0']],
  4073. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4074. 'option A',
  4075. '/label',
  4076. ['label' => ['for' => 'model-custom-1']],
  4077. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4078. 'option B',
  4079. '/label',
  4080. ];
  4081. $this->assertHtml($expected, $result);
  4082. $result = $this->Form->radio(
  4083. 'Employee.gender',
  4084. [
  4085. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4086. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4087. ]
  4088. );
  4089. $expected = [
  4090. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4091. ['label' => ['for' => 'employee-gender-male']],
  4092. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4093. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4094. 'Male',
  4095. '/label',
  4096. ['label' => ['for' => 'employee-gender-female']],
  4097. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4098. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4099. 'Female',
  4100. '/label',
  4101. ];
  4102. $this->assertHtml($expected, $result);
  4103. }
  4104. /**
  4105. * Test default value setting on radio() method
  4106. *
  4107. * @return void
  4108. */
  4109. public function testRadioDefaultValue()
  4110. {
  4111. $Articles = TableRegistry::get('Articles');
  4112. $title = $Articles->schema()->column('title');
  4113. $Articles->schema()->addColumn(
  4114. 'title',
  4115. ['default' => '1'] + $title
  4116. );
  4117. $this->Form->create($Articles->newEntity());
  4118. $result = $this->Form->radio('title', ['option A', 'option B']);
  4119. $expected = [
  4120. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4121. ['label' => ['for' => 'title-0']],
  4122. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4123. 'option A',
  4124. '/label',
  4125. ['label' => ['for' => 'title-1']],
  4126. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4127. 'option B',
  4128. '/label',
  4129. ];
  4130. $this->assertHtml($expected, $result);
  4131. }
  4132. /**
  4133. * Test that input works with radio types
  4134. *
  4135. * @return void
  4136. */
  4137. public function testInputRadio()
  4138. {
  4139. $result = $this->Form->input('test', [
  4140. 'type' => 'radio',
  4141. 'options' => ['A', 'B'],
  4142. ]);
  4143. $expected = [
  4144. ['div' => ['class' => 'input radio']],
  4145. '<label',
  4146. 'Test',
  4147. '/label',
  4148. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4149. ['label' => ['for' => 'test-0']],
  4150. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4151. 'A',
  4152. '/label',
  4153. ['label' => ['for' => 'test-1']],
  4154. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4155. 'B',
  4156. '/label',
  4157. '/div',
  4158. ];
  4159. $this->assertHtml($expected, $result);
  4160. $result = $this->Form->input('test', [
  4161. 'type' => 'radio',
  4162. 'options' => ['A', 'B'],
  4163. 'value' => '0'
  4164. ]);
  4165. $expected = [
  4166. ['div' => ['class' => 'input radio']],
  4167. '<label',
  4168. 'Test',
  4169. '/label',
  4170. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4171. ['label' => ['for' => 'test-0']],
  4172. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4173. 'A',
  4174. '/label',
  4175. ['label' => ['for' => 'test-1']],
  4176. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4177. 'B',
  4178. '/label',
  4179. '/div',
  4180. ];
  4181. $this->assertHtml($expected, $result);
  4182. $result = $this->Form->input('test', [
  4183. 'type' => 'radio',
  4184. 'options' => ['A', 'B'],
  4185. 'label' => false
  4186. ]);
  4187. $expected = [
  4188. ['div' => ['class' => 'input radio']],
  4189. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4190. ['label' => ['for' => 'test-0']],
  4191. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4192. 'A',
  4193. '/label',
  4194. ['label' => ['for' => 'test-1']],
  4195. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4196. 'B',
  4197. '/label',
  4198. '/div',
  4199. ];
  4200. $this->assertHtml($expected, $result);
  4201. }
  4202. /**
  4203. * Test that radio() works with label = false
  4204. *
  4205. * @return void
  4206. */
  4207. public function testRadioNoLabel()
  4208. {
  4209. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4210. $expected = [
  4211. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4212. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4213. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4214. ];
  4215. $this->assertHtml($expected, $result);
  4216. }
  4217. /**
  4218. * test generating radio input inside label ala twitter bootstrap
  4219. *
  4220. * @return void
  4221. */
  4222. public function testRadioInputInsideLabel()
  4223. {
  4224. $this->Form->templates([
  4225. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4226. 'radioWrapper' => '{{label}}'
  4227. ]);
  4228. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4229. //@codingStandardsIgnoreStart
  4230. $expected = [
  4231. ['input' => [
  4232. 'type' => 'hidden',
  4233. 'name' => 'Model[field]',
  4234. 'value' => ''
  4235. ]],
  4236. ['label' => ['for' => 'model-field-0']],
  4237. ['input' => [
  4238. 'type' => 'radio',
  4239. 'name' => 'Model[field]',
  4240. 'value' => '0',
  4241. 'id' => 'model-field-0'
  4242. ]],
  4243. 'option A',
  4244. '/label',
  4245. ['label' => ['for' => 'model-field-1']],
  4246. ['input' => [
  4247. 'type' => 'radio',
  4248. 'name' => 'Model[field]',
  4249. 'value' => '1',
  4250. 'id' => 'model-field-1'
  4251. ]],
  4252. 'option B',
  4253. '/label',
  4254. ];
  4255. //@codingStandardsIgnoreEnd
  4256. $this->assertHtml($expected, $result);
  4257. }
  4258. /**
  4259. * test disabling the hidden input for radio buttons
  4260. *
  4261. * @return void
  4262. */
  4263. public function testRadioHiddenInputDisabling()
  4264. {
  4265. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4266. $expected = [
  4267. 'label' => ['for' => 'model-1-field-0'],
  4268. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4269. 'option A',
  4270. '/label'
  4271. ];
  4272. $this->assertHtml($expected, $result);
  4273. }
  4274. /**
  4275. * testRadio method
  4276. *
  4277. * Test radio element set generation
  4278. *
  4279. * @return void
  4280. */
  4281. public function testRadioOutOfRange()
  4282. {
  4283. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4284. $expected = [
  4285. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4286. 'label' => ['for' => 'model-field-v'],
  4287. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4288. 'value',
  4289. '/label'
  4290. ];
  4291. $this->assertHtml($expected, $result);
  4292. }
  4293. /**
  4294. * testSelect method
  4295. *
  4296. * Test select element generation.
  4297. *
  4298. * @return void
  4299. */
  4300. public function testSelect()
  4301. {
  4302. $result = $this->Form->select('Model.field', []);
  4303. $expected = [
  4304. 'select' => ['name' => 'Model[field]'],
  4305. '/select'
  4306. ];
  4307. $this->assertHtml($expected, $result);
  4308. $this->Form->request->data = ['Model' => ['field' => 'value']];
  4309. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4310. $expected = [
  4311. 'select' => ['name' => 'Model[field]'],
  4312. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4313. 'good',
  4314. '/option',
  4315. ['option' => ['value' => 'other']],
  4316. 'bad',
  4317. '/option',
  4318. '/select'
  4319. ];
  4320. $this->assertHtml($expected, $result);
  4321. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4322. $this->assertHtml($expected, $result);
  4323. $this->Form->request->data = [];
  4324. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4325. $expected = [
  4326. 'select' => ['name' => 'Model[field]'],
  4327. ['option' => ['value' => 'value']],
  4328. 'good',
  4329. '/option',
  4330. ['option' => ['value' => 'other']],
  4331. 'bad',
  4332. '/option',
  4333. '/select'
  4334. ];
  4335. $this->assertHtml($expected, $result);
  4336. $options = [
  4337. ['value' => 'first', 'text' => 'First'],
  4338. ['value' => 'first', 'text' => 'Another First'],
  4339. ];
  4340. $result = $this->Form->select(
  4341. 'Model.field',
  4342. $options,
  4343. ['escape' => false, 'empty' => false]
  4344. );
  4345. $expected = [
  4346. 'select' => ['name' => 'Model[field]'],
  4347. ['option' => ['value' => 'first']],
  4348. 'First',
  4349. '/option',
  4350. ['option' => ['value' => 'first']],
  4351. 'Another First',
  4352. '/option',
  4353. '/select'
  4354. ];
  4355. $this->assertHtml($expected, $result);
  4356. $this->Form->request->data = ['Model' => ['contact_id' => 228]];
  4357. $result = $this->Form->select(
  4358. 'Model.contact_id',
  4359. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4360. ['escape' => false, 'empty' => 'pick something']
  4361. );
  4362. $expected = [
  4363. 'select' => ['name' => 'Model[contact_id]'],
  4364. ['option' => ['value' => '']], 'pick something', '/option',
  4365. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4366. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4367. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4368. '/select'
  4369. ];
  4370. $this->assertHtml($expected, $result);
  4371. $this->Form->request->data['Model']['field'] = 0;
  4372. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4373. $expected = [
  4374. 'select' => ['name' => 'Model[field]'],
  4375. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4376. ['option' => ['value' => '1']], 'Yes', '/option',
  4377. '/select'
  4378. ];
  4379. $this->assertHtml($expected, $result);
  4380. }
  4381. /**
  4382. * Test that select() escapes HTML.
  4383. *
  4384. * @return void
  4385. */
  4386. public function testSelectEscapeHtml()
  4387. {
  4388. $result = $this->Form->select(
  4389. 'Model.field',
  4390. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4391. ['empty' => false]
  4392. );
  4393. $expected = [
  4394. 'select' => ['name' => 'Model[field]'],
  4395. ['option' => ['value' => 'first']],
  4396. 'first &quot;html&quot; &lt;chars&gt;',
  4397. '/option',
  4398. ['option' => ['value' => 'second']],
  4399. 'value',
  4400. '/option',
  4401. '/select'
  4402. ];
  4403. $this->assertHtml($expected, $result);
  4404. $result = $this->Form->select(
  4405. 'Model.field',
  4406. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4407. ['escape' => false, 'empty' => false]
  4408. );
  4409. $expected = [
  4410. 'select' => ['name' => 'Model[field]'],
  4411. ['option' => ['value' => 'first']],
  4412. 'first "html" <chars>',
  4413. '/option',
  4414. ['option' => ['value' => 'second']],
  4415. 'value',
  4416. '/option',
  4417. '/select'
  4418. ];
  4419. $this->assertHtml($expected, $result);
  4420. }
  4421. /**
  4422. * test select() with required and disabled attributes.
  4423. *
  4424. * @return void
  4425. */
  4426. public function testSelectRequired()
  4427. {
  4428. $this->article['required'] = [
  4429. 'user_id' => true
  4430. ];
  4431. $this->Form->create($this->article);
  4432. $result = $this->Form->select('user_id', ['option A']);
  4433. $expected = [
  4434. 'select' => [
  4435. 'name' => 'user_id',
  4436. 'required' => 'required'
  4437. ],
  4438. ['option' => ['value' => '0']], 'option A', '/option',
  4439. '/select'
  4440. ];
  4441. $this->assertHtml($expected, $result);
  4442. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4443. $expected = [
  4444. 'select' => [
  4445. 'name' => 'user_id',
  4446. 'disabled' => 'disabled'
  4447. ],
  4448. ['option' => ['value' => '0']], 'option A', '/option',
  4449. '/select'
  4450. ];
  4451. $this->assertHtml($expected, $result);
  4452. }
  4453. /**
  4454. * testNestedSelect method
  4455. *
  4456. * test select element generation with optgroups
  4457. *
  4458. * @return void
  4459. */
  4460. public function testNestedSelect()
  4461. {
  4462. $result = $this->Form->select(
  4463. 'Model.field',
  4464. [1 => 'One', 2 => 'Two', 'Three' => [
  4465. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4466. ]],
  4467. ['empty' => false]
  4468. );
  4469. $expected = [
  4470. 'select' => ['name' => 'Model[field]'],
  4471. ['option' => ['value' => 1]],
  4472. 'One',
  4473. '/option',
  4474. ['option' => ['value' => 2]],
  4475. 'Two',
  4476. '/option',
  4477. ['optgroup' => ['label' => 'Three']],
  4478. ['option' => ['value' => 3]],
  4479. 'Three',
  4480. '/option',
  4481. ['option' => ['value' => 4]],
  4482. 'Four',
  4483. '/option',
  4484. ['option' => ['value' => 5]],
  4485. 'Five',
  4486. '/option',
  4487. '/optgroup',
  4488. '/select'
  4489. ];
  4490. $this->assertHtml($expected, $result);
  4491. }
  4492. /**
  4493. * testSelectMultiple method
  4494. *
  4495. * test generation of multiple select elements
  4496. *
  4497. * @return void
  4498. */
  4499. public function testSelectMultiple()
  4500. {
  4501. $options = ['first', 'second', 'third'];
  4502. $result = $this->Form->select(
  4503. 'Model.multi_field',
  4504. $options,
  4505. ['form' => 'my-form', 'multiple' => true]
  4506. );
  4507. $expected = [
  4508. 'input' => [
  4509. 'type' => 'hidden',
  4510. 'name' => 'Model[multi_field]',
  4511. 'value' => '',
  4512. 'form' => 'my-form',
  4513. ],
  4514. 'select' => [
  4515. 'name' => 'Model[multi_field][]',
  4516. 'multiple' => 'multiple',
  4517. 'form' => 'my-form',
  4518. ],
  4519. ['option' => ['value' => '0']],
  4520. 'first',
  4521. '/option',
  4522. ['option' => ['value' => '1']],
  4523. 'second',
  4524. '/option',
  4525. ['option' => ['value' => '2']],
  4526. 'third',
  4527. '/option',
  4528. '/select'
  4529. ];
  4530. $this->assertHtml($expected, $result);
  4531. $result = $this->Form->select(
  4532. 'Model.multi_field',
  4533. $options,
  4534. ['multiple' => 'multiple', 'form' => 'my-form']
  4535. );
  4536. $this->assertHtml($expected, $result);
  4537. }
  4538. /**
  4539. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4540. *
  4541. * @return void
  4542. */
  4543. public function testCheckboxZeroValue()
  4544. {
  4545. $result = $this->Form->input('User.get_spam', [
  4546. 'type' => 'checkbox',
  4547. 'value' => '0',
  4548. 'hiddenField' => '1',
  4549. ]);
  4550. $expected = [
  4551. 'div' => ['class' => 'input checkbox'],
  4552. 'label' => ['for' => 'user-get-spam'],
  4553. ['input' => [
  4554. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4555. 'value' => '1'
  4556. ]],
  4557. ['input' => [
  4558. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4559. 'value' => '0', 'id' => 'user-get-spam'
  4560. ]],
  4561. 'Get Spam',
  4562. '/label',
  4563. '/div'
  4564. ];
  4565. $this->assertHtml($expected, $result);
  4566. }
  4567. /**
  4568. * test generation of habtm select boxes.
  4569. *
  4570. * @return void
  4571. */
  4572. public function testHabtmSelectBox()
  4573. {
  4574. $this->loadFixtures('Articles');
  4575. $options = [
  4576. 1 => 'blue',
  4577. 2 => 'red',
  4578. 3 => 'green'
  4579. ];
  4580. $tags = [
  4581. new Entity(['id' => 1, 'name' => 'blue']),
  4582. new Entity(['id' => 3, 'name' => 'green'])
  4583. ];
  4584. $article = new Article(['tags' => $tags]);
  4585. $this->Form->create($article);
  4586. $result = $this->Form->input('tags._ids', ['options' => $options]);
  4587. $expected = [
  4588. 'div' => ['class' => 'input select'],
  4589. 'label' => ['for' => 'tags-ids'],
  4590. 'Tags',
  4591. '/label',
  4592. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4593. 'select' => [
  4594. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4595. 'multiple' => 'multiple'
  4596. ],
  4597. ['option' => ['value' => '1', 'selected' => 'selected']],
  4598. 'blue',
  4599. '/option',
  4600. ['option' => ['value' => '2']],
  4601. 'red',
  4602. '/option',
  4603. ['option' => ['value' => '3', 'selected' => 'selected']],
  4604. 'green',
  4605. '/option',
  4606. '/select',
  4607. '/div'
  4608. ];
  4609. $this->assertHtml($expected, $result);
  4610. // make sure only 50 is selected, and not 50f5c0cf
  4611. $options = [
  4612. '1' => 'blue',
  4613. '50f5c0cf' => 'red',
  4614. '50' => 'green'
  4615. ];
  4616. $tags = [
  4617. new Entity(['id' => 1, 'name' => 'blue']),
  4618. new Entity(['id' => 50, 'name' => 'green'])
  4619. ];
  4620. $article = new Article(['tags' => $tags]);
  4621. $this->Form->create($article);
  4622. $result = $this->Form->input('tags._ids', ['options' => $options]);
  4623. $expected = [
  4624. 'div' => ['class' => 'input select'],
  4625. 'label' => ['for' => 'tags-ids'],
  4626. 'Tags',
  4627. '/label',
  4628. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4629. 'select' => [
  4630. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4631. 'multiple' => 'multiple'
  4632. ],
  4633. ['option' => ['value' => '1', 'selected' => 'selected']],
  4634. 'blue',
  4635. '/option',
  4636. ['option' => ['value' => '50f5c0cf']],
  4637. 'red',
  4638. '/option',
  4639. ['option' => ['value' => '50', 'selected' => 'selected']],
  4640. 'green',
  4641. '/option',
  4642. '/select',
  4643. '/div'
  4644. ];
  4645. $this->assertHtml($expected, $result);
  4646. $spacecraft = [
  4647. 1 => 'Orion',
  4648. 2 => 'Helios'
  4649. ];
  4650. $this->View->viewVars['spacecraft'] = $spacecraft;
  4651. $this->Form->create();
  4652. $result = $this->Form->input('spacecraft._ids');
  4653. $expected = [
  4654. 'div' => ['class' => 'input select'],
  4655. 'label' => ['for' => 'spacecraft-ids'],
  4656. 'Spacecraft',
  4657. '/label',
  4658. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4659. 'select' => [
  4660. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4661. 'multiple' => 'multiple'
  4662. ],
  4663. ['option' => ['value' => '1']],
  4664. 'Orion',
  4665. '/option',
  4666. ['option' => ['value' => '2']],
  4667. 'Helios',
  4668. '/option',
  4669. '/select',
  4670. '/div'
  4671. ];
  4672. $this->assertHtml($expected, $result);
  4673. }
  4674. /**
  4675. * Tests that errors for belongsToMany select fields are being
  4676. * picked up properly.
  4677. *
  4678. * @return void
  4679. */
  4680. public function testErrorsForBelongsToManySelect()
  4681. {
  4682. $spacecraft = [
  4683. 1 => 'Orion',
  4684. 2 => 'Helios'
  4685. ];
  4686. $this->View->viewVars['spacecraft'] = $spacecraft;
  4687. $article = new Article();
  4688. $article->errors('spacecraft', ['Invalid']);
  4689. $this->Form->create($article);
  4690. $result = $this->Form->input('spacecraft._ids');
  4691. $expected = [
  4692. ['div' => ['class' => 'input select error']],
  4693. 'label' => ['for' => 'spacecraft-ids'],
  4694. 'Spacecraft',
  4695. '/label',
  4696. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4697. 'select' => [
  4698. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4699. 'multiple' => 'multiple'
  4700. ],
  4701. ['option' => ['value' => '1']],
  4702. 'Orion',
  4703. '/option',
  4704. ['option' => ['value' => '2']],
  4705. 'Helios',
  4706. '/option',
  4707. '/select',
  4708. ['div' => ['class' => 'error-message']],
  4709. 'Invalid',
  4710. '/div',
  4711. '/div'
  4712. ];
  4713. $this->assertHtml($expected, $result);
  4714. }
  4715. /**
  4716. * test generation of multi select elements in checkbox format
  4717. *
  4718. * @return void
  4719. */
  4720. public function testSelectMultipleCheckboxes()
  4721. {
  4722. $result = $this->Form->select(
  4723. 'Model.multi_field',
  4724. ['first', 'second', 'third'],
  4725. ['multiple' => 'checkbox']
  4726. );
  4727. $expected = [
  4728. 'input' => [
  4729. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4730. ],
  4731. ['div' => ['class' => 'checkbox']],
  4732. ['label' => ['for' => 'model-multi-field-0']],
  4733. ['input' => [
  4734. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4735. 'value' => '0', 'id' => 'model-multi-field-0'
  4736. ]],
  4737. 'first',
  4738. '/label',
  4739. '/div',
  4740. ['div' => ['class' => 'checkbox']],
  4741. ['label' => ['for' => 'model-multi-field-1']],
  4742. ['input' => [
  4743. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4744. 'value' => '1', 'id' => 'model-multi-field-1'
  4745. ]],
  4746. 'second',
  4747. '/label',
  4748. '/div',
  4749. ['div' => ['class' => 'checkbox']],
  4750. ['label' => ['for' => 'model-multi-field-2']],
  4751. ['input' => [
  4752. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4753. 'value' => '2', 'id' => 'model-multi-field-2'
  4754. ]],
  4755. 'third',
  4756. '/label',
  4757. '/div'
  4758. ];
  4759. $this->assertHtml($expected, $result);
  4760. $result = $this->Form->select(
  4761. 'Model.multi_field',
  4762. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  4763. ['multiple' => 'checkbox']
  4764. );
  4765. $expected = [
  4766. 'input' => [
  4767. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4768. ],
  4769. ['div' => ['class' => 'checkbox']],
  4770. ['label' => ['for' => 'model-multi-field-a+']],
  4771. ['input' => [
  4772. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4773. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  4774. ]],
  4775. 'first',
  4776. '/label',
  4777. '/div',
  4778. ['div' => ['class' => 'checkbox']],
  4779. ['label' => ['for' => 'model-multi-field-a++']],
  4780. ['input' => [
  4781. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4782. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  4783. ]],
  4784. 'second',
  4785. '/label',
  4786. '/div',
  4787. ['div' => ['class' => 'checkbox']],
  4788. ['label' => ['for' => 'model-multi-field-a+++']],
  4789. ['input' => [
  4790. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4791. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  4792. ]],
  4793. 'third',
  4794. '/label',
  4795. '/div'
  4796. ];
  4797. $this->assertHtml($expected, $result);
  4798. $result = $this->Form->select(
  4799. 'Model.multi_field',
  4800. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  4801. ['multiple' => 'checkbox']
  4802. );
  4803. $expected = [
  4804. 'input' => [
  4805. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4806. ],
  4807. ['div' => ['class' => 'checkbox']],
  4808. ['label' => ['for' => 'model-multi-field-a-b']],
  4809. ['input' => [
  4810. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4811. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  4812. ]],
  4813. 'first',
  4814. '/label',
  4815. '/div',
  4816. ['div' => ['class' => 'checkbox']],
  4817. ['label' => ['for' => 'model-multi-field-a-b1']],
  4818. ['input' => [
  4819. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4820. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  4821. ]],
  4822. 'second',
  4823. '/label',
  4824. '/div',
  4825. ['div' => ['class' => 'checkbox']],
  4826. ['label' => ['for' => 'model-multi-field-a-b2']],
  4827. ['input' => [
  4828. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4829. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  4830. ]],
  4831. 'third',
  4832. '/label',
  4833. '/div'
  4834. ];
  4835. $this->assertHtml($expected, $result);
  4836. }
  4837. /**
  4838. * Ensure that multiCheckbox reads from the request data.
  4839. *
  4840. * @return void
  4841. */
  4842. public function testSelectMultipleCheckboxRequestData()
  4843. {
  4844. $this->Form->request->data = ['Model' => ['tags' => [1]]];
  4845. $result = $this->Form->select(
  4846. 'Model.tags',
  4847. ['1' => 'first', 'Array' => 'Array'],
  4848. ['multiple' => 'checkbox']
  4849. );
  4850. $expected = [
  4851. 'input' => [
  4852. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  4853. ],
  4854. ['div' => ['class' => 'checkbox']],
  4855. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  4856. ['input' => [
  4857. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4858. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  4859. ]],
  4860. 'first',
  4861. '/label',
  4862. '/div',
  4863. ['div' => ['class' => 'checkbox']],
  4864. ['label' => ['for' => 'model-tags-array']],
  4865. ['input' => [
  4866. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4867. 'value' => 'Array', 'id' => 'model-tags-array'
  4868. ]],
  4869. 'Array',
  4870. '/label',
  4871. '/div'
  4872. ];
  4873. $this->assertHtml($expected, $result);
  4874. }
  4875. /**
  4876. * Checks the security hash array generated for multiple-input checkbox elements
  4877. *
  4878. * @return void
  4879. */
  4880. public function testSelectMultipleCheckboxSecurity()
  4881. {
  4882. $this->Form->request->params['_Token'] = 'testKey';
  4883. $this->assertEquals([], $this->Form->fields);
  4884. $result = $this->Form->select(
  4885. 'Model.multi_field',
  4886. ['1' => 'first', '2' => 'second', '3' => 'third'],
  4887. ['multiple' => 'checkbox']
  4888. );
  4889. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  4890. $result = $this->Form->secure($this->Form->fields);
  4891. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4892. $this->assertRegExp('/"' . $key . '"/', $result);
  4893. }
  4894. /**
  4895. * Multiple select elements should always be secured as they always participate
  4896. * in the POST data.
  4897. *
  4898. * @return void
  4899. */
  4900. public function testSelectMultipleSecureWithNoOptions()
  4901. {
  4902. $this->assertEquals([], $this->Form->fields);
  4903. $this->Form->select(
  4904. 'Model.select',
  4905. [],
  4906. ['multiple' => true]
  4907. );
  4908. $this->assertEquals(['Model.select'], $this->Form->fields);
  4909. }
  4910. /**
  4911. * When a select box has no options it should not be added to the fields list
  4912. * as it always fail post validation.
  4913. *
  4914. * @return void
  4915. */
  4916. public function testSelectNoSecureWithNoOptions()
  4917. {
  4918. $this->Form->request->params['_Token'] = 'testkey';
  4919. $this->assertEquals([], $this->Form->fields);
  4920. $this->Form->select(
  4921. 'Model.select',
  4922. []
  4923. );
  4924. $this->assertEquals([], $this->Form->fields);
  4925. $this->Form->select(
  4926. 'Model.user_id',
  4927. [],
  4928. ['empty' => true]
  4929. );
  4930. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  4931. }
  4932. /**
  4933. * testInputMultipleCheckboxes method
  4934. *
  4935. * test input() resulting in multi select elements being generated.
  4936. *
  4937. * @return void
  4938. */
  4939. public function testInputMultipleCheckboxes()
  4940. {
  4941. $result = $this->Form->input('Model.multi_field', [
  4942. 'options' => ['first', 'second', 'third'],
  4943. 'multiple' => 'checkbox'
  4944. ]);
  4945. $expected = [
  4946. ['div' => ['class' => 'input select']],
  4947. ['label' => ['for' => 'model-multi-field']],
  4948. 'Multi Field',
  4949. '/label',
  4950. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  4951. ['div' => ['class' => 'checkbox']],
  4952. ['label' => ['for' => 'model-multi-field-0']],
  4953. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  4954. 'first',
  4955. '/label',
  4956. '/div',
  4957. ['div' => ['class' => 'checkbox']],
  4958. ['label' => ['for' => 'model-multi-field-1']],
  4959. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  4960. 'second',
  4961. '/label',
  4962. '/div',
  4963. ['div' => ['class' => 'checkbox']],
  4964. ['label' => ['for' => 'model-multi-field-2']],
  4965. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  4966. 'third',
  4967. '/label',
  4968. '/div',
  4969. '/div'
  4970. ];
  4971. $this->assertHtml($expected, $result);
  4972. $result = $this->Form->input('Model.multi_field', [
  4973. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  4974. 'multiple' => 'checkbox'
  4975. ]);
  4976. $expected = [
  4977. ['div' => ['class' => 'input select']],
  4978. ['label' => ['for' => 'model-multi-field']],
  4979. 'Multi Field',
  4980. '/label',
  4981. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  4982. ['div' => ['class' => 'checkbox']],
  4983. ['label' => ['for' => 'model-multi-field-a']],
  4984. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  4985. 'first',
  4986. '/label',
  4987. '/div',
  4988. ['div' => ['class' => 'checkbox']],
  4989. ['label' => ['for' => 'model-multi-field-b']],
  4990. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  4991. 'second',
  4992. '/label',
  4993. '/div',
  4994. ['div' => ['class' => 'checkbox']],
  4995. ['label' => ['for' => 'model-multi-field-c']],
  4996. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  4997. 'third',
  4998. '/label',
  4999. '/div',
  5000. '/div'
  5001. ];
  5002. $this->assertHtml($expected, $result);
  5003. }
  5004. /**
  5005. * testSelectHiddenFieldOmission method
  5006. *
  5007. * test that select() with 'hiddenField' => false omits the hidden field
  5008. *
  5009. * @return void
  5010. */
  5011. public function testSelectHiddenFieldOmission()
  5012. {
  5013. $result = $this->Form->select(
  5014. 'Model.multi_field',
  5015. ['first', 'second'],
  5016. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5017. );
  5018. $this->assertNotContains('type="hidden"', $result);
  5019. }
  5020. /**
  5021. * test that select() with multiple = checkbox works with overriding name attribute.
  5022. *
  5023. * @return void
  5024. */
  5025. public function testSelectCheckboxMultipleOverrideName()
  5026. {
  5027. $result = $this->Form->select('category', ['1', '2'], [
  5028. 'multiple' => 'checkbox',
  5029. 'name' => 'fish',
  5030. ]);
  5031. $expected = [
  5032. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5033. ['div' => ['class' => 'checkbox']],
  5034. ['label' => ['for' => 'fish-0']],
  5035. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5036. '1',
  5037. '/label',
  5038. '/div',
  5039. ['div' => ['class' => 'checkbox']],
  5040. ['label' => ['for' => 'fish-1']],
  5041. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5042. '2',
  5043. '/label',
  5044. '/div'
  5045. ];
  5046. $this->assertHtml($expected, $result);
  5047. $result = $this->Form->multiCheckbox(
  5048. 'category',
  5049. new Collection(['1', '2']),
  5050. ['name' => 'fish']
  5051. );
  5052. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5053. 'name' => 'fish',
  5054. ]);
  5055. $this->assertHtml($expected, $result);
  5056. }
  5057. /**
  5058. * Test that input() works with multicheckbox.
  5059. *
  5060. * @return void
  5061. */
  5062. public function testInputMultiCheckbox()
  5063. {
  5064. $result = $this->Form->input('category', [
  5065. 'type' => 'multicheckbox',
  5066. 'options' => ['1', '2'],
  5067. ]);
  5068. $expected = [
  5069. ['div' => ['class' => 'input multicheckbox']],
  5070. '<label',
  5071. 'Category',
  5072. '/label',
  5073. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5074. ['div' => ['class' => 'checkbox']],
  5075. ['label' => ['for' => 'category-0']],
  5076. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5077. '1',
  5078. '/label',
  5079. '/div',
  5080. ['div' => ['class' => 'checkbox']],
  5081. ['label' => ['for' => 'category-1']],
  5082. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5083. '2',
  5084. '/label',
  5085. '/div',
  5086. '/div',
  5087. ];
  5088. $this->assertHtml($expected, $result);
  5089. }
  5090. /**
  5091. * testCheckbox method
  5092. *
  5093. * Test generation of checkboxes
  5094. *
  5095. * @return void
  5096. */
  5097. public function testCheckbox()
  5098. {
  5099. $result = $this->Form->checkbox('Model.field');
  5100. $expected = [
  5101. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5102. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5103. ];
  5104. $this->assertHtml($expected, $result);
  5105. $result = $this->Form->checkbox('Model.field', [
  5106. 'id' => 'theID',
  5107. 'value' => 'myvalue',
  5108. 'form' => 'my-form',
  5109. ]);
  5110. $expected = [
  5111. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5112. ['input' => [
  5113. 'type' => 'checkbox', 'name' => 'Model[field]',
  5114. 'value' => 'myvalue', 'id' => 'theID',
  5115. 'form' => 'my-form',
  5116. ]]
  5117. ];
  5118. $this->assertHtml($expected, $result);
  5119. }
  5120. /**
  5121. * testCheckboxDefaultValue method
  5122. *
  5123. * Test default value setting on checkbox() method
  5124. *
  5125. * @return void
  5126. */
  5127. public function testCheckboxDefaultValue()
  5128. {
  5129. $this->Form->request->data['Model']['field'] = false;
  5130. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5131. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5132. $this->assertHtml($expected, $result);
  5133. unset($this->Form->request->data['Model']['field']);
  5134. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5135. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5136. $this->assertHtml($expected, $result);
  5137. $this->Form->request->data['Model']['field'] = true;
  5138. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5139. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5140. $this->assertHtml($expected, $result);
  5141. unset($this->Form->request->data['Model']['field']);
  5142. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5143. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5144. $this->assertHtml($expected, $result);
  5145. $Articles = TableRegistry::get('Articles');
  5146. $Articles->schema()->addColumn(
  5147. 'published',
  5148. ['type' => 'boolean', 'null' => false, 'default' => true]
  5149. );
  5150. $this->Form->create($Articles->newEntity());
  5151. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5152. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5153. $this->assertHtml($expected, $result);
  5154. }
  5155. /**
  5156. * Test checkbox being checked or having errors.
  5157. *
  5158. * @return void
  5159. */
  5160. public function testCheckboxCheckedAndError()
  5161. {
  5162. $this->article['errors'] = [
  5163. 'published' => true
  5164. ];
  5165. $this->Form->request->data['published'] = 'myvalue';
  5166. $this->Form->create($this->article);
  5167. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5168. $expected = [
  5169. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5170. ['input' => [
  5171. 'type' => 'checkbox',
  5172. 'name' => 'published',
  5173. 'value' => 'myvalue',
  5174. 'id' => 'theID',
  5175. 'checked' => 'checked',
  5176. 'class' => 'form-error'
  5177. ]]
  5178. ];
  5179. $this->assertHtml($expected, $result);
  5180. $this->Form->request->data['published'] = '';
  5181. $result = $this->Form->checkbox('published');
  5182. $expected = [
  5183. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5184. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5185. ];
  5186. $this->assertHtml($expected, $result);
  5187. }
  5188. /**
  5189. * test checkbox() with a custom name attribute
  5190. *
  5191. * @return void
  5192. */
  5193. public function testCheckboxCustomNameAttribute()
  5194. {
  5195. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5196. $expected = [
  5197. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5198. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5199. ];
  5200. $this->assertHtml($expected, $result);
  5201. }
  5202. /**
  5203. * Test that the hidden input for checkboxes can be omitted or set to a
  5204. * specific value.
  5205. *
  5206. * @return void
  5207. */
  5208. public function testCheckboxHiddenField()
  5209. {
  5210. $result = $this->Form->checkbox('UserForm.something', [
  5211. 'hiddenField' => false
  5212. ]);
  5213. $expected = [
  5214. 'input' => [
  5215. 'type' => 'checkbox',
  5216. 'name' => 'UserForm[something]',
  5217. 'value' => '1'
  5218. ],
  5219. ];
  5220. $this->assertHtml($expected, $result);
  5221. $result = $this->Form->checkbox('UserForm.something', [
  5222. 'value' => 'Y',
  5223. 'hiddenField' => 'N',
  5224. ]);
  5225. $expected = [
  5226. ['input' => [
  5227. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5228. 'value' => 'N'
  5229. ]],
  5230. ['input' => [
  5231. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5232. 'value' => 'Y'
  5233. ]],
  5234. ];
  5235. $this->assertHtml($expected, $result);
  5236. }
  5237. /**
  5238. * Test the time type.
  5239. *
  5240. * @return void
  5241. */
  5242. public function testTime()
  5243. {
  5244. $result = $this->Form->time('start_time', [
  5245. 'timeFormat' => 12,
  5246. 'interval' => 5,
  5247. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5248. ]);
  5249. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5250. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5251. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5252. $this->assertNotContains('year', $result);
  5253. $this->assertNotContains('month', $result);
  5254. $this->assertNotContains('day', $result);
  5255. $result = $this->Form->time('start_time', [
  5256. 'timeFormat' => 12,
  5257. 'interval' => 5,
  5258. 'value' => '2014-03-08 16:30:00'
  5259. ]);
  5260. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5261. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5262. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5263. $this->assertNotContains('year', $result);
  5264. $this->assertNotContains('month', $result);
  5265. $this->assertNotContains('day', $result);
  5266. }
  5267. /**
  5268. * Ensure that timeFormat=24 has no merdian.
  5269. *
  5270. * @return void.
  5271. */
  5272. public function testTimeFormat24NoMeridian()
  5273. {
  5274. $result = $this->Form->time('start_time', [
  5275. 'timeFormat' => 24,
  5276. 'interval' => 5,
  5277. 'value' => '2014-03-08 16:30:00'
  5278. ]);
  5279. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5280. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5281. $this->assertNotContains('meridian', $result);
  5282. $this->assertNotContains('pm', $result);
  5283. $this->assertNotContains('year', $result);
  5284. $this->assertNotContains('month', $result);
  5285. $this->assertNotContains('day', $result);
  5286. }
  5287. /**
  5288. * Test the date type.
  5289. *
  5290. * @return void
  5291. */
  5292. public function testDate()
  5293. {
  5294. $result = $this->Form->date('start_day', [
  5295. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5296. ]);
  5297. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5298. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5299. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5300. $this->assertNotContains('hour', $result);
  5301. $this->assertNotContains('minute', $result);
  5302. $this->assertNotContains('second', $result);
  5303. $this->assertNotContains('meridian', $result);
  5304. }
  5305. /**
  5306. * testDateTime method
  5307. *
  5308. * Test generation of date/time select elements
  5309. *
  5310. * @return void
  5311. */
  5312. public function testDateTime()
  5313. {
  5314. extract($this->dateRegex);
  5315. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5316. $now = strtotime('now');
  5317. $expected = [
  5318. ['select' => ['name' => 'Contact[date][year]']],
  5319. ['option' => ['value' => '']],
  5320. '/option',
  5321. $yearsRegex,
  5322. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5323. date('Y', $now),
  5324. '/option',
  5325. '*/select',
  5326. ['select' => ['name' => 'Contact[date][month]']],
  5327. ['option' => ['value' => '']],
  5328. '/option',
  5329. $monthsRegex,
  5330. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5331. date('F', $now),
  5332. '/option',
  5333. '*/select',
  5334. ['select' => ['name' => 'Contact[date][day]']],
  5335. ['option' => ['value' => '']],
  5336. '/option',
  5337. $daysRegex,
  5338. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5339. date('j', $now),
  5340. '/option',
  5341. '*/select',
  5342. ['select' => ['name' => 'Contact[date][hour]']],
  5343. ['option' => ['value' => '']],
  5344. '/option',
  5345. $hoursRegex,
  5346. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5347. date('G', $now),
  5348. '/option',
  5349. '*/select',
  5350. ['select' => ['name' => 'Contact[date][minute]']],
  5351. ['option' => ['value' => '']],
  5352. '/option',
  5353. $minutesRegex,
  5354. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5355. date('i', $now),
  5356. '/option',
  5357. '*/select',
  5358. ];
  5359. $this->assertHtml($expected, $result);
  5360. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5361. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5362. $now = strtotime('now');
  5363. $expected = [
  5364. ['select' => ['name' => 'Contact[date][year]']],
  5365. $yearsRegex,
  5366. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5367. date('Y', $now),
  5368. '/option',
  5369. '*/select',
  5370. ['select' => ['name' => 'Contact[date][month]']],
  5371. $monthsRegex,
  5372. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5373. date('F', $now),
  5374. '/option',
  5375. '*/select',
  5376. ['select' => ['name' => 'Contact[date][day]']],
  5377. $daysRegex,
  5378. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5379. date('j', $now),
  5380. '/option',
  5381. '*/select',
  5382. ['select' => ['name' => 'Contact[date][hour]']],
  5383. $hoursRegex,
  5384. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5385. date('G', $now),
  5386. '/option',
  5387. '*/select',
  5388. ['select' => ['name' => 'Contact[date][minute]']],
  5389. $minutesRegex,
  5390. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5391. date('i', $now),
  5392. '/option',
  5393. '*/select',
  5394. ];
  5395. $this->assertHtml($expected, $result);
  5396. }
  5397. /**
  5398. * Test that datetime fields are added to protected fields list.
  5399. *
  5400. * @return void
  5401. */
  5402. public function testDateTimeSecured()
  5403. {
  5404. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5405. $this->Form->dateTime('Contact.date');
  5406. $expected = [
  5407. 'Contact.date.year',
  5408. 'Contact.date.month',
  5409. 'Contact.date.day',
  5410. 'Contact.date.hour',
  5411. 'Contact.date.minute',
  5412. ];
  5413. $this->assertEquals($expected, $this->Form->fields);
  5414. $this->Form->fields = [];
  5415. $this->Form->date('Contact.published');
  5416. $expected = [
  5417. 'Contact.published.year',
  5418. 'Contact.published.month',
  5419. 'Contact.published.day',
  5420. ];
  5421. $this->assertEquals($expected, $this->Form->fields);
  5422. }
  5423. /**
  5424. * Test that datetime fields are added to protected fields list.
  5425. *
  5426. * @return void
  5427. */
  5428. public function testDateTimeSecuredDisabled()
  5429. {
  5430. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5431. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5432. $expected = [];
  5433. $this->assertEquals($expected, $this->Form->fields);
  5434. $this->Form->fields = [];
  5435. $this->Form->date('Contact.published', ['secure' => false]);
  5436. $expected = [];
  5437. $this->assertEquals($expected, $this->Form->fields);
  5438. }
  5439. /**
  5440. * Test empty defaulting to true for datetime.
  5441. *
  5442. * @return void
  5443. */
  5444. public function testDatetimeEmpty()
  5445. {
  5446. extract($this->dateRegex);
  5447. $now = strtotime('now');
  5448. $result = $this->Form->dateTime('Contact.date', [
  5449. 'timeFormat' => 12,
  5450. 'empty' => true,
  5451. 'default' => true
  5452. ]);
  5453. $expected = [
  5454. ['select' => ['name' => 'Contact[date][year]']],
  5455. $yearsRegex,
  5456. ['option' => ['value' => '']],
  5457. '/option',
  5458. '*/select',
  5459. ['select' => ['name' => 'Contact[date][month]']],
  5460. $monthsRegex,
  5461. ['option' => ['value' => '']],
  5462. '/option',
  5463. '*/select',
  5464. ['select' => ['name' => 'Contact[date][day]']],
  5465. $daysRegex,
  5466. ['option' => ['value' => '']],
  5467. '/option',
  5468. '*/select',
  5469. ['select' => ['name' => 'Contact[date][hour]']],
  5470. $hoursRegex,
  5471. ['option' => ['value' => '']],
  5472. '/option',
  5473. '*/select',
  5474. ['select' => ['name' => 'Contact[date][minute]']],
  5475. $minutesRegex,
  5476. ['option' => ['value' => '']],
  5477. '/option',
  5478. '*/select',
  5479. ['select' => ['name' => 'Contact[date][meridian]']],
  5480. $meridianRegex,
  5481. ['option' => ['value' => '']],
  5482. '/option',
  5483. '*/select'
  5484. ];
  5485. $this->assertHtml($expected, $result);
  5486. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5487. }
  5488. /**
  5489. * Test datetime with interval option.
  5490. *
  5491. * @return void
  5492. */
  5493. public function testDatetimeMinuteInterval()
  5494. {
  5495. extract($this->dateRegex);
  5496. $now = strtotime('now');
  5497. $result = $this->Form->dateTime('Contact.date', [
  5498. 'interval' => 5,
  5499. 'value' => ''
  5500. ]);
  5501. $expected = [
  5502. ['select' => ['name' => 'Contact[date][year]']],
  5503. $yearsRegex,
  5504. ['option' => ['selected' => 'selected', 'value' => '']],
  5505. '/option',
  5506. '*/select',
  5507. ['select' => ['name' => 'Contact[date][month]']],
  5508. $monthsRegex,
  5509. ['option' => ['selected' => 'selected', 'value' => '']],
  5510. '/option',
  5511. '*/select',
  5512. ['select' => ['name' => 'Contact[date][day]']],
  5513. $daysRegex,
  5514. ['option' => ['selected' => 'selected', 'value' => '']],
  5515. '/option',
  5516. '*/select',
  5517. ['select' => ['name' => 'Contact[date][hour]']],
  5518. $hoursRegex,
  5519. ['option' => ['selected' => 'selected', 'value' => '']],
  5520. '/option',
  5521. '*/select',
  5522. ['select' => ['name' => 'Contact[date][minute]']],
  5523. $minutesRegex,
  5524. ['option' => ['selected' => 'selected', 'value' => '']],
  5525. '/option',
  5526. ['option' => ['value' => '00']],
  5527. '00',
  5528. '/option',
  5529. ['option' => ['value' => '05']],
  5530. '05',
  5531. '/option',
  5532. ['option' => ['value' => '10']],
  5533. '10',
  5534. '/option',
  5535. '*/select',
  5536. ];
  5537. $this->assertHtml($expected, $result);
  5538. }
  5539. /**
  5540. * Test dateTime with rounding
  5541. *
  5542. * @return void
  5543. */
  5544. public function testDateTimeRounding()
  5545. {
  5546. $this->Form->request->data['Contact'] = [
  5547. 'date' => [
  5548. 'day' => '13',
  5549. 'month' => '12',
  5550. 'year' => '2010',
  5551. 'hour' => '04',
  5552. 'minute' => '19',
  5553. 'meridian' => 'AM'
  5554. ]
  5555. ];
  5556. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5557. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5558. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5559. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5560. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5561. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5562. }
  5563. /**
  5564. * test that datetime() and default values work.
  5565. *
  5566. * @return void
  5567. */
  5568. public function testDatetimeWithDefault()
  5569. {
  5570. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5571. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5572. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5573. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5574. $result = $this->Form->dateTime('Contact.updated', [
  5575. 'default' => '2009-06-01 11:15:30'
  5576. ]);
  5577. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5578. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5579. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5580. }
  5581. /**
  5582. * testDateTime all zeros
  5583. *
  5584. * @return void
  5585. */
  5586. public function testDateTimeAllZeros()
  5587. {
  5588. $result = $this->Form->dateTime('Contact.date', [
  5589. 'timeFormat' => false,
  5590. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5591. 'value' => '0000-00-00'
  5592. ]);
  5593. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5594. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5595. }
  5596. /**
  5597. * testDateTimeEmptyAsArray
  5598. *
  5599. * @return void
  5600. */
  5601. public function testDateTimeEmptyAsArray()
  5602. {
  5603. $result = $this->Form->dateTime('Contact.date', [
  5604. 'empty' => [
  5605. 'day' => 'DAY',
  5606. 'month' => 'MONTH',
  5607. 'year' => 'YEAR',
  5608. 'hour' => 'HOUR',
  5609. 'minute' => 'MINUTE',
  5610. 'meridian' => false
  5611. ],
  5612. 'default' => true
  5613. ]);
  5614. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5615. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5616. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5617. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5618. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5619. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5620. $result = $this->Form->dateTime('Contact.date', [
  5621. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5622. 'default' => true
  5623. ]);
  5624. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5625. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5626. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5627. }
  5628. /**
  5629. * testFormDateTimeMulti method
  5630. *
  5631. * test multiple datetime element generation
  5632. *
  5633. * @return void
  5634. */
  5635. public function testFormDateTimeMulti()
  5636. {
  5637. extract($this->dateRegex);
  5638. $result = $this->Form->dateTime('Contact.1.updated');
  5639. $this->assertContains('Contact[1][updated][month]', $result);
  5640. $this->assertContains('Contact[1][updated][day]', $result);
  5641. $this->assertContains('Contact[1][updated][year]', $result);
  5642. $this->assertContains('Contact[1][updated][hour]', $result);
  5643. $this->assertContains('Contact[1][updated][minute]', $result);
  5644. }
  5645. /**
  5646. * When changing the date format, the label should always focus the first select box when
  5647. * clicked.
  5648. *
  5649. * @return void
  5650. */
  5651. public function testDateTimeLabelIdMatchesFirstInput()
  5652. {
  5653. $result = $this->Form->input('Model.date', ['type' => 'date']);
  5654. $this->assertContains('<label>Date</label>', $result);
  5655. $result = $this->Form->input('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  5656. $this->assertContains('<label>Date</label>', $result);
  5657. $result = $this->Form->input('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  5658. $this->assertContains('<label>Date</label>', $result);
  5659. }
  5660. /**
  5661. * test datetime second=true
  5662. *
  5663. * @return void
  5664. */
  5665. public function testDateTimeSecondOptions()
  5666. {
  5667. $result = $this->Form->dateTime('updated', ['second' => true]);
  5668. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5669. $result = $this->Form->dateTime('updated', ['second' => []]);
  5670. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5671. $result = $this->Form->dateTime('updated', ['second' => null]);
  5672. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5673. $result = $this->Form->dateTime('updated', ['second' => false]);
  5674. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5675. }
  5676. /**
  5677. * testMonth method
  5678. *
  5679. * @return void
  5680. */
  5681. public function testMonth()
  5682. {
  5683. $result = $this->Form->month('Model.field', ['value' => '']);
  5684. $expected = [
  5685. ['select' => ['name' => 'Model[field][month]']],
  5686. ['option' => ['value' => '', 'selected' => 'selected']],
  5687. '/option',
  5688. ['option' => ['value' => '01']],
  5689. date('F', strtotime('2008-01-01 00:00:00')),
  5690. '/option',
  5691. ['option' => ['value' => '02']],
  5692. date('F', strtotime('2008-02-01 00:00:00')),
  5693. '/option',
  5694. '*/select',
  5695. ];
  5696. $this->assertHtml($expected, $result);
  5697. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  5698. $expected = [
  5699. ['select' => ['name' => 'Model[field][month]']],
  5700. ['option' => ['selected' => 'selected', 'value' => '']],
  5701. '/option',
  5702. ['option' => ['value' => '01']],
  5703. date('F', strtotime('2008-01-01 00:00:00')),
  5704. '/option',
  5705. ['option' => ['value' => '02']],
  5706. date('F', strtotime('2008-02-01 00:00:00')),
  5707. '/option',
  5708. '*/select',
  5709. ];
  5710. $this->assertHtml($expected, $result);
  5711. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  5712. $expected = [
  5713. ['select' => ['name' => 'Model[field][month]']],
  5714. ['option' => ['selected' => 'selected', 'value' => '']],
  5715. '/option',
  5716. ['option' => ['value' => '01']],
  5717. '1',
  5718. '/option',
  5719. ['option' => ['value' => '02']],
  5720. '2',
  5721. '/option',
  5722. '*/select',
  5723. ];
  5724. $this->assertHtml($expected, $result);
  5725. $monthNames = [
  5726. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5727. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  5728. ];
  5729. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  5730. $expected = [
  5731. ['select' => ['name' => 'Model[field][month]']],
  5732. ['option' => ['value' => '']],
  5733. '/option',
  5734. ['option' => ['value' => '01', 'selected' => 'selected']],
  5735. 'Jan',
  5736. '/option',
  5737. ['option' => ['value' => '02']],
  5738. 'Feb',
  5739. '/option',
  5740. '*/select',
  5741. ];
  5742. $this->assertHtml($expected, $result);
  5743. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5744. $result = $this->Form->month('Project.release');
  5745. $expected = [
  5746. ['select' => ['name' => 'Project[release][month]']],
  5747. ['option' => ['value' => '']],
  5748. '/option',
  5749. ['option' => ['value' => '01']],
  5750. 'January',
  5751. '/option',
  5752. ['option' => ['value' => '02', 'selected' => 'selected']],
  5753. 'February',
  5754. '/option',
  5755. '*/select',
  5756. ];
  5757. $this->assertHtml($expected, $result);
  5758. $result = $this->Form->month('Contact.published', [
  5759. 'empty' => 'Published on',
  5760. ]);
  5761. $this->assertContains('Published on', $result);
  5762. }
  5763. /**
  5764. * testDay method
  5765. *
  5766. * @return void
  5767. */
  5768. public function testDay()
  5769. {
  5770. extract($this->dateRegex);
  5771. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  5772. $expected = [
  5773. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  5774. ['option' => ['selected' => 'selected', 'value' => '']],
  5775. '/option',
  5776. ['option' => ['value' => '01']],
  5777. '1',
  5778. '/option',
  5779. ['option' => ['value' => '02']],
  5780. '2',
  5781. '/option',
  5782. $daysRegex,
  5783. '/select',
  5784. ];
  5785. $this->assertHtml($expected, $result);
  5786. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5787. $result = $this->Form->day('Model.field');
  5788. $expected = [
  5789. ['select' => ['name' => 'Model[field][day]']],
  5790. ['option' => ['value' => '']],
  5791. '/option',
  5792. ['option' => ['value' => '01']],
  5793. '1',
  5794. '/option',
  5795. ['option' => ['value' => '02']],
  5796. '2',
  5797. '/option',
  5798. $daysRegex,
  5799. ['option' => ['value' => '10', 'selected' => 'selected']],
  5800. '10',
  5801. '/option',
  5802. $daysRegex,
  5803. '/select',
  5804. ];
  5805. $this->assertHtml($expected, $result);
  5806. $this->Form->request->data['Model']['field'] = '';
  5807. $result = $this->Form->day('Model.field', ['value' => '10']);
  5808. $expected = [
  5809. ['select' => ['name' => 'Model[field][day]']],
  5810. ['option' => ['value' => '']],
  5811. '/option',
  5812. ['option' => ['value' => '01']],
  5813. '1',
  5814. '/option',
  5815. ['option' => ['value' => '02']],
  5816. '2',
  5817. '/option',
  5818. $daysRegex,
  5819. ['option' => ['value' => '10', 'selected' => 'selected']],
  5820. '10',
  5821. '/option',
  5822. $daysRegex,
  5823. '/select',
  5824. ];
  5825. $this->assertHtml($expected, $result);
  5826. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5827. $result = $this->Form->day('Project.release');
  5828. $expected = [
  5829. ['select' => ['name' => 'Project[release][day]']],
  5830. ['option' => ['value' => '']],
  5831. '/option',
  5832. ['option' => ['value' => '01']],
  5833. '1',
  5834. '/option',
  5835. ['option' => ['value' => '02']],
  5836. '2',
  5837. '/option',
  5838. $daysRegex,
  5839. ['option' => ['value' => '10', 'selected' => 'selected']],
  5840. '10',
  5841. '/option',
  5842. $daysRegex,
  5843. '/select',
  5844. ];
  5845. $this->assertHtml($expected, $result);
  5846. $result = $this->Form->day('Contact.published', [
  5847. 'empty' => 'Published on',
  5848. ]);
  5849. $this->assertContains('Published on', $result);
  5850. }
  5851. /**
  5852. * testMinute method
  5853. *
  5854. * @return void
  5855. */
  5856. public function testMinute()
  5857. {
  5858. extract($this->dateRegex);
  5859. $result = $this->Form->minute('Model.field', ['value' => '']);
  5860. $expected = [
  5861. ['select' => ['name' => 'Model[field][minute]']],
  5862. ['option' => ['selected' => 'selected', 'value' => '']],
  5863. '/option',
  5864. ['option' => ['value' => '00']],
  5865. '00',
  5866. '/option',
  5867. ['option' => ['value' => '01']],
  5868. '01',
  5869. '/option',
  5870. ['option' => ['value' => '02']],
  5871. '02',
  5872. '/option',
  5873. $minutesRegex,
  5874. '/select',
  5875. ];
  5876. $this->assertHtml($expected, $result);
  5877. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5878. $result = $this->Form->minute('Model.field');
  5879. $expected = [
  5880. ['select' => ['name' => 'Model[field][minute]']],
  5881. ['option' => ['value' => '']],
  5882. '/option',
  5883. ['option' => ['value' => '00']],
  5884. '00',
  5885. '/option',
  5886. ['option' => ['value' => '01']],
  5887. '01',
  5888. '/option',
  5889. ['option' => ['value' => '02']],
  5890. '02',
  5891. '/option',
  5892. $minutesRegex,
  5893. ['option' => ['value' => '12', 'selected' => 'selected']],
  5894. '12',
  5895. '/option',
  5896. $minutesRegex,
  5897. '/select',
  5898. ];
  5899. $this->assertHtml($expected, $result);
  5900. $this->Form->request->data['Model']['field'] = '';
  5901. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  5902. $expected = [
  5903. ['select' => ['name' => 'Model[field][minute]']],
  5904. ['option' => ['selected' => 'selected', 'value' => '']],
  5905. '/option',
  5906. ['option' => ['value' => '00']],
  5907. '00',
  5908. '/option',
  5909. ['option' => ['value' => '05']],
  5910. '05',
  5911. '/option',
  5912. ['option' => ['value' => '10']],
  5913. '10',
  5914. '/option',
  5915. $minutesRegex,
  5916. '/select',
  5917. ];
  5918. $this->assertHtml($expected, $result);
  5919. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5920. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  5921. $expected = [
  5922. ['select' => ['name' => 'Model[field][minute]']],
  5923. ['option' => ['value' => '']],
  5924. '/option',
  5925. ['option' => ['value' => '00']],
  5926. '00',
  5927. '/option',
  5928. ['option' => ['value' => '05']],
  5929. '05',
  5930. '/option',
  5931. ['option' => ['value' => '10', 'selected' => 'selected']],
  5932. '10',
  5933. '/option',
  5934. $minutesRegex,
  5935. '/select',
  5936. ];
  5937. $this->assertHtml($expected, $result);
  5938. }
  5939. /**
  5940. * Test generating an input for the meridian.
  5941. *
  5942. * @return void
  5943. */
  5944. public function testMeridian()
  5945. {
  5946. extract($this->dateRegex);
  5947. $now = new \DateTime();
  5948. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  5949. $expected = [
  5950. ['select' => ['name' => 'Model[field][meridian]']],
  5951. ['option' => ['value' => '']],
  5952. '/option',
  5953. $meridianRegex,
  5954. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  5955. $now->format('a'),
  5956. '/option',
  5957. '*/select'
  5958. ];
  5959. $this->assertHtml($expected, $result);
  5960. }
  5961. /**
  5962. * testHour method
  5963. *
  5964. * @return void
  5965. */
  5966. public function testHour()
  5967. {
  5968. extract($this->dateRegex);
  5969. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  5970. $expected = [
  5971. ['select' => ['name' => 'Model[field][hour]']],
  5972. ['option' => ['selected' => 'selected', 'value' => '']],
  5973. '/option',
  5974. ['option' => ['value' => '01']],
  5975. '1',
  5976. '/option',
  5977. ['option' => ['value' => '02']],
  5978. '2',
  5979. '/option',
  5980. $hoursRegex,
  5981. '/select',
  5982. ];
  5983. $this->assertHtml($expected, $result);
  5984. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5985. $result = $this->Form->hour('Model.field', ['format' => 12]);
  5986. $expected = [
  5987. ['select' => ['name' => 'Model[field][hour]']],
  5988. ['option' => ['value' => '']],
  5989. '/option',
  5990. ['option' => ['value' => '01']],
  5991. '1',
  5992. '/option',
  5993. ['option' => ['value' => '02']],
  5994. '2',
  5995. '/option',
  5996. $hoursRegex,
  5997. ['option' => ['value' => '12', 'selected' => 'selected']],
  5998. '12',
  5999. '/option',
  6000. '/select',
  6001. ];
  6002. $this->assertHtml($expected, $result);
  6003. $this->Form->request->data['Model']['field'] = '';
  6004. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6005. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6006. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6007. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6008. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6009. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6010. $expected = [
  6011. ['select' => ['name' => 'Model[field][hour]']],
  6012. ['option' => ['value' => '']],
  6013. '/option',
  6014. ['option' => ['value' => '00', 'selected' => 'selected']],
  6015. '0',
  6016. '/option',
  6017. ['option' => ['value' => '01']],
  6018. '1',
  6019. '/option',
  6020. ['option' => ['value' => '02']],
  6021. '2',
  6022. '/option',
  6023. $hoursRegex,
  6024. '/select',
  6025. ];
  6026. $this->assertHtml($expected, $result);
  6027. unset($this->Form->request->data['Model']['field']);
  6028. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6029. $thisHour = date('H');
  6030. $optValue = date('G');
  6031. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6032. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6033. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6034. $expected = [
  6035. ['select' => ['name' => 'Model[field][hour]']],
  6036. ['option' => ['value' => '']],
  6037. '/option',
  6038. ['option' => ['value' => '00']],
  6039. '0',
  6040. '/option',
  6041. ['option' => ['value' => '01', 'selected' => 'selected']],
  6042. '1',
  6043. '/option',
  6044. ['option' => ['value' => '02']],
  6045. '2',
  6046. '/option',
  6047. $hoursRegex,
  6048. '/select',
  6049. ];
  6050. $this->assertHtml($expected, $result);
  6051. }
  6052. /**
  6053. * testYear method
  6054. *
  6055. * @return void
  6056. */
  6057. public function testYear()
  6058. {
  6059. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6060. $expected = [
  6061. ['select' => ['name' => 'Model[field][year]']],
  6062. ['option' => ['selected' => 'selected', 'value' => '']],
  6063. '/option',
  6064. ['option' => ['value' => '2007']],
  6065. '2007',
  6066. '/option',
  6067. ['option' => ['value' => '2006']],
  6068. '2006',
  6069. '/option',
  6070. '/select',
  6071. ];
  6072. $this->assertHtml($expected, $result);
  6073. $result = $this->Form->year('Model.field', [
  6074. 'value' => '',
  6075. 'minYear' => 2006,
  6076. 'maxYear' => 2007,
  6077. 'orderYear' => 'asc'
  6078. ]);
  6079. $expected = [
  6080. ['select' => ['name' => 'Model[field][year]']],
  6081. ['option' => ['selected' => 'selected', 'value' => '']],
  6082. '/option',
  6083. ['option' => ['value' => '2006']],
  6084. '2006',
  6085. '/option',
  6086. ['option' => ['value' => '2007']],
  6087. '2007',
  6088. '/option',
  6089. '/select',
  6090. ];
  6091. $this->assertHtml($expected, $result);
  6092. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6093. $result = $this->Form->year('Contact.published', [
  6094. 'empty' => false,
  6095. 'minYear' => 2006,
  6096. 'maxYear' => 2007,
  6097. ]);
  6098. $expected = [
  6099. ['select' => ['name' => 'Contact[published][year]']],
  6100. ['option' => ['value' => '2007']],
  6101. '2007',
  6102. '/option',
  6103. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6104. '2006',
  6105. '/option',
  6106. '/select',
  6107. ];
  6108. $this->assertHtml($expected, $result);
  6109. $result = $this->Form->year('Contact.published', [
  6110. 'empty' => 'Published on',
  6111. ]);
  6112. $this->assertContains('Published on', $result);
  6113. }
  6114. /**
  6115. * Test minYear being prior to the unix epoch
  6116. *
  6117. * @return void
  6118. */
  6119. public function testInputDatetimePreEpoch()
  6120. {
  6121. $start = date('Y') - 80;
  6122. $end = date('Y') - 18;
  6123. $result = $this->Form->input('birth_year', [
  6124. 'type' => 'date',
  6125. 'label' => 'Birth Year',
  6126. 'minYear' => $start,
  6127. 'maxYear' => $end,
  6128. 'month' => false,
  6129. 'day' => false,
  6130. ]);
  6131. $this->assertContains('value="' . $start . '">' . $start, $result);
  6132. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6133. $this->assertNotContains('value="00">00', $result);
  6134. }
  6135. /**
  6136. * testYearAutoExpandRange method
  6137. *
  6138. * @return void
  6139. */
  6140. public function testYearAutoExpandRange()
  6141. {
  6142. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6143. $result = $this->Form->year('User.birthday');
  6144. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6145. $result = $matches[1];
  6146. $expected = range(date('Y') + 5, 1930);
  6147. $this->assertEquals($expected, $result);
  6148. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6149. $result = $this->Form->year('Project.release');
  6150. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6151. $result = $matches[1];
  6152. $expected = range(2050, date('Y') - 5);
  6153. $this->assertEquals($expected, $result);
  6154. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6155. $result = $this->Form->year('Project.release', [
  6156. 'minYear' => 1890,
  6157. 'maxYear' => 1900
  6158. ]);
  6159. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6160. $result = $matches[1];
  6161. $expected = range(1900, 1881);
  6162. $this->assertEquals($expected, $result);
  6163. }
  6164. /**
  6165. * Test that input() accepts the type of date and passes options in.
  6166. *
  6167. * @return void
  6168. */
  6169. public function testInputDate()
  6170. {
  6171. $this->Form->request->data = [
  6172. 'month_year' => ['month' => date('m')],
  6173. ];
  6174. $this->Form->create($this->article);
  6175. $result = $this->Form->input('month_year', [
  6176. 'label' => false,
  6177. 'type' => 'date',
  6178. 'minYear' => 2006,
  6179. 'maxYear' => 2008
  6180. ]);
  6181. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6182. $this->assertNotContains('value="2008" selected="selected"', $result);
  6183. }
  6184. /**
  6185. * Test the label option being set to false.
  6186. *
  6187. * @return void
  6188. */
  6189. public function testInputLabelFalse()
  6190. {
  6191. $this->Form->create($this->article);
  6192. $result = $this->Form->input('title', ['label' => false]);
  6193. $expected = [
  6194. 'div' => ['class' => 'input text required'],
  6195. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6196. '/div'
  6197. ];
  6198. $this->assertHtml($expected, $result);
  6199. }
  6200. /**
  6201. * testInputDateMaxYear method
  6202. *
  6203. * Let's say we want to only allow users born from 2006 to 2008 to register
  6204. * This being the first signup page, we still don't have any data
  6205. *
  6206. * @return void
  6207. */
  6208. public function testInputDateMaxYear()
  6209. {
  6210. $this->Form->request->data = [];
  6211. $this->Form->create($this->article);
  6212. $result = $this->Form->input('birthday', [
  6213. 'label' => false,
  6214. 'type' => 'date',
  6215. 'minYear' => 2006,
  6216. 'maxYear' => 2008,
  6217. 'default' => true
  6218. ]);
  6219. $this->assertContains('value="2008" selected="selected"', $result);
  6220. $this->assertContains('value="2006"', $result);
  6221. $this->assertNotContains('value="2005"', $result);
  6222. $this->assertNotContains('value="2009"', $result);
  6223. }
  6224. /**
  6225. * testTextArea method
  6226. *
  6227. * @return void
  6228. */
  6229. public function testTextArea()
  6230. {
  6231. $this->Form->request->data = ['field' => 'some test data'];
  6232. $result = $this->Form->textarea('field');
  6233. $expected = [
  6234. 'textarea' => ['name' => 'field', 'rows' => 5],
  6235. 'some test data',
  6236. '/textarea',
  6237. ];
  6238. $this->assertHtml($expected, $result);
  6239. $result = $this->Form->textarea('user.bio');
  6240. $expected = [
  6241. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6242. '/textarea',
  6243. ];
  6244. $this->assertHtml($expected, $result);
  6245. $this->Form->request->data = ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'];
  6246. $result = $this->Form->textarea('field');
  6247. $expected = [
  6248. 'textarea' => ['name' => 'field', 'rows' => 5],
  6249. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6250. '/textarea',
  6251. ];
  6252. $this->assertHtml($expected, $result);
  6253. $this->Form->request->data = [
  6254. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  6255. ];
  6256. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6257. $expected = [
  6258. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6259. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6260. '/textarea',
  6261. ];
  6262. $this->assertHtml($expected, $result);
  6263. $result = $this->Form->textarea('0.OtherModel.field');
  6264. $expected = [
  6265. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6266. '/textarea'
  6267. ];
  6268. $this->assertHtml($expected, $result);
  6269. }
  6270. /**
  6271. * testTextAreaWithStupidCharacters method
  6272. *
  6273. * test text area with non-ascii characters
  6274. *
  6275. * @return void
  6276. */
  6277. public function testTextAreaWithStupidCharacters()
  6278. {
  6279. $result = $this->Form->textarea('Post.content', [
  6280. 'value' => "GREAT®",
  6281. 'rows' => '15',
  6282. 'cols' => '75'
  6283. ]);
  6284. $expected = [
  6285. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6286. 'GREAT®',
  6287. '/textarea',
  6288. ];
  6289. $this->assertHtml($expected, $result);
  6290. }
  6291. /**
  6292. * Test textareas waxlength read from schema.
  6293. *
  6294. * @return void
  6295. */
  6296. public function testTextAreaMaxLength()
  6297. {
  6298. $this->Form->create([
  6299. 'schema' => [
  6300. 'stuff' => ['type' => 'string', 'length' => 10],
  6301. ]
  6302. ]);
  6303. $result = $this->Form->input('other', ['type' => 'textarea']);
  6304. $expected = [
  6305. 'div' => ['class' => 'input textarea'],
  6306. 'label' => ['for' => 'other'],
  6307. 'Other',
  6308. '/label',
  6309. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6310. '/textarea',
  6311. '/div'
  6312. ];
  6313. $this->assertHtml($expected, $result);
  6314. $result = $this->Form->input('stuff', ['type' => 'textarea']);
  6315. $expected = [
  6316. 'div' => ['class' => 'input textarea'],
  6317. 'label' => ['for' => 'stuff'],
  6318. 'Stuff',
  6319. '/label',
  6320. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6321. '/textarea',
  6322. '/div'
  6323. ];
  6324. $this->assertHtml($expected, $result);
  6325. }
  6326. /**
  6327. * testHiddenField method
  6328. *
  6329. * @return void
  6330. */
  6331. public function testHiddenField()
  6332. {
  6333. $this->article['errors'] = [
  6334. 'field' => true
  6335. ];
  6336. $this->Form->request->data['field'] = 'test';
  6337. $this->Form->create($this->article);
  6338. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6339. $expected = [
  6340. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6341. $this->assertHtml($expected, $result);
  6342. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6343. $expected = [
  6344. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6345. ];
  6346. $this->assertHtml($expected, $result);
  6347. }
  6348. /**
  6349. * testFileUploadField method
  6350. *
  6351. * @return void
  6352. */
  6353. public function testFileUploadField()
  6354. {
  6355. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6356. $result = $this->Form->file('Model.upload');
  6357. $this->assertHtml($expected, $result);
  6358. $this->Form->request->data['Model']['upload'] = [
  6359. 'name' => '', 'type' => '', 'tmp_name' => '',
  6360. 'error' => 4, 'size' => 0
  6361. ];
  6362. $result = $this->Form->file('Model.upload');
  6363. $this->assertHtml($expected, $result);
  6364. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6365. $result = $this->Form->file('Model.upload');
  6366. $this->assertHtml($expected, $result);
  6367. }
  6368. /**
  6369. * test File upload input on a model not used in create();
  6370. *
  6371. * @return void
  6372. */
  6373. public function testFileUploadOnOtherModel()
  6374. {
  6375. $this->Form->create($this->article, ['type' => 'file']);
  6376. $result = $this->Form->file('ValidateProfile.city');
  6377. $expected = [
  6378. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6379. ];
  6380. $this->assertHtml($expected, $result);
  6381. }
  6382. /**
  6383. * testButton method
  6384. *
  6385. * @return void
  6386. */
  6387. public function testButton()
  6388. {
  6389. $result = $this->Form->button('Hi');
  6390. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6391. $this->assertHtml($expected, $result);
  6392. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6393. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6394. $this->assertHtml($expected, $result);
  6395. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6396. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6397. $this->assertHtml($expected, $result);
  6398. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6399. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6400. $this->assertHtml($expected, $result);
  6401. $result = $this->Form->button('No type', ['type' => false]);
  6402. $expected = ['button' => [], 'No type', '/button'];
  6403. $this->assertHtml($expected, $result);
  6404. $result = $this->Form->button('Upload Text', [
  6405. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6406. 'escape' => false
  6407. ]);
  6408. $this->assertNotRegExp('/\&039/', $result);
  6409. }
  6410. /**
  6411. * Test that button() makes unlocked fields by default.
  6412. *
  6413. * @return void
  6414. */
  6415. public function testButtonUnlockedByDefault()
  6416. {
  6417. $this->Form->request->params['_csrfToken'] = 'secured';
  6418. $this->Form->button('Save', ['name' => 'save']);
  6419. $this->Form->button('Clear');
  6420. $result = $this->Form->unlockField();
  6421. $this->assertEquals(['save'], $result);
  6422. }
  6423. /**
  6424. * testPostButton method
  6425. *
  6426. * @return void
  6427. */
  6428. public function testPostButton()
  6429. {
  6430. $result = $this->Form->postButton('Hi', '/controller/action');
  6431. $expected = [
  6432. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6433. 'div' => ['style' => 'display:none;'],
  6434. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6435. '/div',
  6436. 'button' => ['type' => 'submit'],
  6437. 'Hi',
  6438. '/button',
  6439. '/form'
  6440. ];
  6441. $this->assertHtml($expected, $result);
  6442. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6443. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  6444. }
  6445. public function testPostButtonMethodType()
  6446. {
  6447. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6448. $expected = [
  6449. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6450. 'div' => ['style' => 'display:none;'],
  6451. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6452. '/div',
  6453. 'button' => ['type' => 'submit'],
  6454. 'Hi',
  6455. '/button',
  6456. '/form'
  6457. ];
  6458. $this->assertHtml($expected, $result);
  6459. }
  6460. public function testPostButtonFormOptions()
  6461. {
  6462. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6463. $expected = [
  6464. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6465. 'div' => ['style' => 'display:none;'],
  6466. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6467. '/div',
  6468. 'button' => ['type' => 'submit'],
  6469. 'Hi',
  6470. '/button',
  6471. '/form'
  6472. ];
  6473. $this->assertHtml($expected, $result);
  6474. }
  6475. /**
  6476. * Test using postButton with N dimensional data.
  6477. *
  6478. * @return void
  6479. */
  6480. public function testPostButtonNestedData()
  6481. {
  6482. $data = [
  6483. 'one' => [
  6484. 'two' => [
  6485. 3, 4, 5
  6486. ]
  6487. ]
  6488. ];
  6489. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6490. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6491. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6492. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6493. }
  6494. /**
  6495. * Test that postButton adds _Token fields.
  6496. *
  6497. * @return void
  6498. */
  6499. public function testSecurePostButton()
  6500. {
  6501. $this->Form->request->params['_csrfToken'] = 'testkey';
  6502. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  6503. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6504. $tokenDebug = urlencode(json_encode([
  6505. '/posts/delete/1',
  6506. [],
  6507. []
  6508. ]));
  6509. $expected = [
  6510. 'form' => [
  6511. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6512. ],
  6513. ['div' => ['style' => 'display:none;']],
  6514. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6515. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6516. '/div',
  6517. 'button' => ['type' => 'submit'],
  6518. 'Delete',
  6519. '/button',
  6520. ['div' => ['style' => 'display:none;']],
  6521. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6522. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6523. ['input' => [
  6524. 'type' => 'hidden', 'name' => '_Token[debug]',
  6525. 'value' => $tokenDebug,
  6526. ]],
  6527. '/div',
  6528. '/form',
  6529. ];
  6530. $this->assertHtml($expected, $result);
  6531. }
  6532. /**
  6533. * testPostLink method
  6534. *
  6535. * @return void
  6536. */
  6537. public function testPostLink()
  6538. {
  6539. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6540. $expected = [
  6541. 'form' => [
  6542. 'method' => 'post', 'action' => '/posts/delete/1',
  6543. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6544. ],
  6545. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6546. '/form',
  6547. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6548. 'Delete',
  6549. '/a'
  6550. ];
  6551. $this->assertHtml($expected, $result);
  6552. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6553. $expected = [
  6554. 'form' => [
  6555. 'method' => 'post', 'action' => '/posts/delete/1',
  6556. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6557. ],
  6558. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6559. '/form',
  6560. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6561. 'Delete',
  6562. '/a'
  6563. ];
  6564. $this->assertHtml($expected, $result);
  6565. $result = $this->Form->postLink(
  6566. 'Delete',
  6567. '/posts/delete/1',
  6568. ['target' => '_blank', 'class' => 'btn btn-danger']
  6569. );
  6570. $expected = [
  6571. 'form' => [
  6572. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6573. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6574. ],
  6575. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6576. '/form',
  6577. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6578. 'Delete',
  6579. '/a'
  6580. ];
  6581. $this->assertHtml($expected, $result);
  6582. }
  6583. /**
  6584. * Test the confirm option for postLink()
  6585. *
  6586. * @return void
  6587. */
  6588. public function testPostLinkWithConfirm()
  6589. {
  6590. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6591. $expected = [
  6592. 'form' => [
  6593. 'method' => 'post', 'action' => '/posts/delete/1',
  6594. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6595. ],
  6596. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6597. '/form',
  6598. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  6599. 'Delete',
  6600. '/a'
  6601. ];
  6602. $this->assertHtml($expected, $result);
  6603. $result = $this->Form->postLink(
  6604. 'Delete',
  6605. '/posts/delete/1',
  6606. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  6607. );
  6608. $expected = [
  6609. 'form' => [
  6610. 'method' => 'post', 'action' => '/posts/delete/1',
  6611. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6612. ],
  6613. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6614. '/form',
  6615. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\nthis \\\\&quot;deletion\\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  6616. 'Delete',
  6617. '/a'
  6618. ];
  6619. $this->assertHtml($expected, $result);
  6620. }
  6621. /**
  6622. * test postLink() with query string args.
  6623. *
  6624. * @return void
  6625. */
  6626. public function testPostLinkWithQuery()
  6627. {
  6628. $result = $this->Form->postLink(
  6629. 'Delete',
  6630. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6631. );
  6632. $expected = [
  6633. 'form' => [
  6634. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  6635. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6636. ],
  6637. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6638. '/form',
  6639. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6640. 'Delete',
  6641. '/a'
  6642. ];
  6643. $this->assertHtml($expected, $result);
  6644. }
  6645. /**
  6646. * Test postLink with additional data.
  6647. *
  6648. * @return void
  6649. */
  6650. public function testPostLinkWithData()
  6651. {
  6652. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6653. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  6654. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6655. $this->Form->create($entity);
  6656. $this->Form->end();
  6657. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6658. $this->assertContains(
  6659. '<input type="hidden" name="name" value="show"',
  6660. $result,
  6661. 'should not contain entity data.'
  6662. );
  6663. }
  6664. /**
  6665. * Test that security hashes for postLink include the url.
  6666. *
  6667. * @return void
  6668. */
  6669. public function testPostLinkSecurityHash()
  6670. {
  6671. $hash = Security::hash(
  6672. '/posts/delete/1' .
  6673. serialize(['id' => '1']) .
  6674. '' .
  6675. Security::salt()
  6676. );
  6677. $hash .= '%3Aid';
  6678. $this->Form->request->params['_Token']['key'] = 'test';
  6679. $result = $this->Form->postLink(
  6680. 'Delete',
  6681. '/posts/delete/1',
  6682. ['data' => ['id' => 1]]
  6683. );
  6684. $tokenDebug = urlencode(json_encode([
  6685. '/posts/delete/1',
  6686. [
  6687. 'id' => 1
  6688. ],
  6689. []
  6690. ]));
  6691. $expected = [
  6692. 'form' => [
  6693. 'method' => 'post', 'action' => '/posts/delete/1',
  6694. 'name', 'style' => 'display:none;'
  6695. ],
  6696. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6697. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6698. 'div' => ['style' => 'display:none;'],
  6699. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6700. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6701. ['input' => [
  6702. 'type' => 'hidden', 'name' => '_Token[debug]',
  6703. 'value' => $tokenDebug,
  6704. ]],
  6705. '/div',
  6706. '/form',
  6707. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6708. 'Delete',
  6709. '/a'
  6710. ];
  6711. $this->assertHtml($expected, $result);
  6712. }
  6713. /**
  6714. * Test that postLink doesn't modify the fields in the containing form.
  6715. *
  6716. * postLink() calls inside open forms should not modify the field list
  6717. * for the form.
  6718. *
  6719. * @return void
  6720. */
  6721. public function testPostLinkSecurityHashBlockMode()
  6722. {
  6723. $hash = Security::hash(
  6724. '/posts/delete/1' .
  6725. serialize([]) .
  6726. '' .
  6727. Security::salt()
  6728. );
  6729. $hash .= '%3A';
  6730. $this->Form->request->params['_Token']['key'] = 'test';
  6731. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  6732. $this->Form->input('title');
  6733. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6734. $result = $this->View->fetch('postLink');
  6735. $this->assertEquals(['title'], $this->Form->fields);
  6736. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  6737. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  6738. }
  6739. /**
  6740. * Test that security does not include debug token if debug is false.
  6741. *
  6742. * @return void
  6743. */
  6744. public function testPostLinkSecurityHashNoDebugMode()
  6745. {
  6746. Configure::write('debug', false);
  6747. $hash = Security::hash(
  6748. '/posts/delete/1' .
  6749. serialize(['id' => '1']) .
  6750. '' .
  6751. Security::salt()
  6752. );
  6753. $hash .= '%3Aid';
  6754. $this->Form->request->params['_Token']['key'] = 'test';
  6755. $result = $this->Form->postLink(
  6756. 'Delete',
  6757. '/posts/delete/1',
  6758. ['data' => ['id' => 1]]
  6759. );
  6760. $expected = [
  6761. 'form' => [
  6762. 'method' => 'post', 'action' => '/posts/delete/1',
  6763. 'name', 'style' => 'display:none;'
  6764. ],
  6765. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6766. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6767. 'div' => ['style' => 'display:none;'],
  6768. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6769. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6770. '/div',
  6771. '/form',
  6772. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6773. 'Delete',
  6774. '/a'
  6775. ];
  6776. $this->assertHtml($expected, $result);
  6777. }
  6778. /**
  6779. * Test using postLink with N dimensional data.
  6780. *
  6781. * @return void
  6782. */
  6783. public function testPostLinkNestedData()
  6784. {
  6785. $data = [
  6786. 'one' => [
  6787. 'two' => [
  6788. 3, 4, 5
  6789. ]
  6790. ]
  6791. ];
  6792. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  6793. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6794. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6795. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6796. }
  6797. /**
  6798. * test creating postLinks after a GET form.
  6799. *
  6800. * @return void
  6801. */
  6802. public function testPostLinkAfterGetForm()
  6803. {
  6804. $this->Form->request->params['_csrfToken'] = 'testkey';
  6805. $this->Form->request->params['_Token'] = 'val';
  6806. $this->Form->create($this->article, ['type' => 'get']);
  6807. $this->Form->end();
  6808. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6809. $tokenDebug = urlencode(json_encode([
  6810. '/posts/delete/1',
  6811. [],
  6812. []
  6813. ]));
  6814. $expected = [
  6815. 'form' => [
  6816. 'method' => 'post', 'action' => '/posts/delete/1',
  6817. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6818. ],
  6819. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6820. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6821. 'div' => ['style' => 'display:none;'],
  6822. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6823. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6824. ['input' => [
  6825. 'type' => 'hidden', 'name' => '_Token[debug]',
  6826. 'value' => $tokenDebug,
  6827. ]],
  6828. '/div',
  6829. '/form',
  6830. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6831. 'Delete',
  6832. '/a'
  6833. ];
  6834. $this->assertHtml($expected, $result);
  6835. }
  6836. /**
  6837. * Test that postLink adds form tags to view block
  6838. *
  6839. * @return void
  6840. */
  6841. public function testPostLinkFormBuffer()
  6842. {
  6843. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6844. $expected = [
  6845. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6846. 'Delete',
  6847. '/a'
  6848. ];
  6849. $this->assertHtml($expected, $result);
  6850. $result = $this->View->fetch('postLink');
  6851. $expected = [
  6852. 'form' => [
  6853. 'method' => 'post', 'action' => '/posts/delete/1',
  6854. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6855. ],
  6856. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6857. '/form'
  6858. ];
  6859. $this->assertHtml($expected, $result);
  6860. $result = $this->Form->postLink(
  6861. 'Delete',
  6862. '/posts/delete/2',
  6863. ['block' => true, 'method' => 'DELETE']
  6864. );
  6865. $expected = [
  6866. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6867. 'Delete',
  6868. '/a'
  6869. ];
  6870. $this->assertHtml($expected, $result);
  6871. $result = $this->View->fetch('postLink');
  6872. $expected = [
  6873. 'form' => [
  6874. 'method' => 'post', 'action' => '/posts/delete/1',
  6875. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6876. ],
  6877. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6878. '/form',
  6879. [
  6880. 'form' => [
  6881. 'method' => 'post', 'action' => '/posts/delete/2',
  6882. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6883. ],
  6884. ],
  6885. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  6886. '/form'
  6887. ];
  6888. $this->assertHtml($expected, $result);
  6889. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  6890. $expected = [
  6891. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6892. 'Delete',
  6893. '/a'
  6894. ];
  6895. $this->assertHtml($expected, $result);
  6896. $result = $this->View->fetch('foobar');
  6897. $expected = [
  6898. 'form' => [
  6899. 'method' => 'post', 'action' => '/posts/delete/1',
  6900. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6901. ],
  6902. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6903. '/form'
  6904. ];
  6905. $this->assertHtml($expected, $result);
  6906. }
  6907. /**
  6908. * testSubmitButton method
  6909. *
  6910. * @return void
  6911. */
  6912. public function testSubmitButton()
  6913. {
  6914. $result = $this->Form->submit('');
  6915. $expected = [
  6916. 'div' => ['class' => 'submit'],
  6917. 'input' => ['type' => 'submit', 'value' => ''],
  6918. '/div'
  6919. ];
  6920. $this->assertHtml($expected, $result);
  6921. $result = $this->Form->submit('Test Submit');
  6922. $expected = [
  6923. 'div' => ['class' => 'submit'],
  6924. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  6925. '/div'
  6926. ];
  6927. $this->assertHtml($expected, $result);
  6928. $result = $this->Form->submit('Next >');
  6929. $expected = [
  6930. 'div' => ['class' => 'submit'],
  6931. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  6932. '/div'
  6933. ];
  6934. $this->assertHtml($expected, $result);
  6935. $result = $this->Form->submit('Next >', ['escape' => false]);
  6936. $expected = [
  6937. 'div' => ['class' => 'submit'],
  6938. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  6939. '/div'
  6940. ];
  6941. $this->assertHtml($expected, $result);
  6942. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  6943. $expected = [
  6944. 'div' => ['class' => 'submit'],
  6945. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  6946. '/div'
  6947. ];
  6948. $this->assertHtml($expected, $result);
  6949. }
  6950. /**
  6951. * test image submit types.
  6952. *
  6953. * @return void
  6954. */
  6955. public function testSubmitImage()
  6956. {
  6957. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6958. $expected = [
  6959. 'div' => ['class' => 'submit'],
  6960. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6961. '/div'
  6962. ];
  6963. $this->assertHtml($expected, $result);
  6964. $result = $this->Form->submit('/relative/cake.power.gif');
  6965. $expected = [
  6966. 'div' => ['class' => 'submit'],
  6967. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6968. '/div'
  6969. ];
  6970. $this->assertHtml($expected, $result);
  6971. $result = $this->Form->submit('cake.power.gif');
  6972. $expected = [
  6973. 'div' => ['class' => 'submit'],
  6974. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6975. '/div'
  6976. ];
  6977. $this->assertHtml($expected, $result);
  6978. $result = $this->Form->submit('Not.an.image');
  6979. $expected = [
  6980. 'div' => ['class' => 'submit'],
  6981. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  6982. '/div'
  6983. ];
  6984. $this->assertHtml($expected, $result);
  6985. }
  6986. /**
  6987. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6988. * be submitted at a time.
  6989. *
  6990. * @return void
  6991. */
  6992. public function testSubmitUnlockedByDefault()
  6993. {
  6994. $this->Form->request->params['_Token'] = 'secured';
  6995. $this->Form->submit('Go go');
  6996. $this->Form->submit('Save', ['name' => 'save']);
  6997. $result = $this->Form->unlockField();
  6998. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  6999. }
  7000. /**
  7001. * Test submit image with timestamps.
  7002. *
  7003. * @return void
  7004. */
  7005. public function testSubmitImageTimestamp()
  7006. {
  7007. Configure::write('Asset.timestamp', 'force');
  7008. $result = $this->Form->submit('cake.power.gif');
  7009. $expected = [
  7010. 'div' => ['class' => 'submit'],
  7011. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7012. '/div'
  7013. ];
  7014. $this->assertHtml($expected, $result);
  7015. }
  7016. /**
  7017. * test that datetime() works with GET style forms.
  7018. *
  7019. * @return void
  7020. */
  7021. public function testDateTimeWithGetForms()
  7022. {
  7023. extract($this->dateRegex);
  7024. $this->Form->create($this->article, ['type' => 'get']);
  7025. $result = $this->Form->datetime('created');
  7026. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7027. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7028. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7029. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7030. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7031. }
  7032. /**
  7033. * testForMagicInputNonExistingNorValidated method
  7034. *
  7035. * @return void
  7036. */
  7037. public function testForMagicInputNonExistingNorValidated()
  7038. {
  7039. $result = $this->Form->create($this->article);
  7040. $this->Form->templates(['inputContainer' => '{{content}}']);
  7041. $result = $this->Form->input('non_existing_nor_validated');
  7042. $expected = [
  7043. 'label' => ['for' => 'non-existing-nor-validated'],
  7044. 'Non Existing Nor Validated',
  7045. '/label',
  7046. 'input' => [
  7047. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7048. 'id' => 'non-existing-nor-validated'
  7049. ]
  7050. ];
  7051. $this->assertHtml($expected, $result);
  7052. $result = $this->Form->input('non_existing_nor_validated', [
  7053. 'val' => 'my value'
  7054. ]);
  7055. $expected = [
  7056. 'label' => ['for' => 'non-existing-nor-validated'],
  7057. 'Non Existing Nor Validated',
  7058. '/label',
  7059. 'input' => [
  7060. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7061. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7062. ]
  7063. ];
  7064. $this->assertHtml($expected, $result);
  7065. $this->Form->request->data = ['non_existing_nor_validated' => 'CakePHP magic'];
  7066. $result = $this->Form->input('non_existing_nor_validated');
  7067. $expected = [
  7068. 'label' => ['for' => 'non-existing-nor-validated'],
  7069. 'Non Existing Nor Validated',
  7070. '/label',
  7071. 'input' => [
  7072. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7073. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7074. ]
  7075. ];
  7076. $this->assertHtml($expected, $result);
  7077. }
  7078. /**
  7079. * testFormMagicInputLabel method
  7080. *
  7081. * @return void
  7082. */
  7083. public function testFormMagicInputLabel()
  7084. {
  7085. TableRegistry::get('Contacts', [
  7086. 'className' => __NAMESPACE__ . '\ContactsTable'
  7087. ]);
  7088. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7089. $this->Form->templates(['inputContainer' => '{{content}}']);
  7090. $result = $this->Form->input('Contacts.name', ['label' => 'My label']);
  7091. $expected = [
  7092. 'label' => ['for' => 'contacts-name'],
  7093. 'My label',
  7094. '/label',
  7095. 'input' => [
  7096. 'type' => 'text', 'name' => 'Contacts[name]',
  7097. 'id' => 'contacts-name', 'maxlength' => '255'
  7098. ]
  7099. ];
  7100. $this->assertHtml($expected, $result);
  7101. $result = $this->Form->input('name', [
  7102. 'label' => ['class' => 'mandatory']
  7103. ]);
  7104. $expected = [
  7105. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7106. 'Name',
  7107. '/label',
  7108. 'input' => [
  7109. 'type' => 'text', 'name' => 'name',
  7110. 'id' => 'name', 'maxlength' => '255'
  7111. ]
  7112. ];
  7113. $this->assertHtml($expected, $result);
  7114. $result = $this->Form->input('name', [
  7115. 'div' => false,
  7116. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7117. ]);
  7118. $expected = [
  7119. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7120. 'My label',
  7121. '/label',
  7122. 'input' => [
  7123. 'type' => 'text', 'name' => 'name',
  7124. 'id' => 'name', 'maxlength' => '255'
  7125. ]
  7126. ];
  7127. $this->assertHtml($expected, $result);
  7128. $result = $this->Form->input('Contact.name', [
  7129. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7130. ]);
  7131. $expected = [
  7132. 'label' => ['for' => 'my_id'],
  7133. 'Name',
  7134. '/label',
  7135. 'input' => [
  7136. 'type' => 'text', 'name' => 'Contact[name]',
  7137. 'id' => 'my_id', 'maxlength' => '255'
  7138. ]
  7139. ];
  7140. $this->assertHtml($expected, $result);
  7141. $result = $this->Form->input('1.id');
  7142. $expected = ['input' => [
  7143. 'type' => 'hidden', 'name' => '1[id]',
  7144. 'id' => '1-id'
  7145. ]];
  7146. $this->assertHtml($expected, $result);
  7147. $result = $this->Form->input("1.name");
  7148. $expected = [
  7149. 'label' => ['for' => '1-name'],
  7150. 'Name',
  7151. '/label',
  7152. 'input' => [
  7153. 'type' => 'text', 'name' => '1[name]',
  7154. 'id' => '1-name', 'maxlength' => '255'
  7155. ]
  7156. ];
  7157. $this->assertHtml($expected, $result);
  7158. }
  7159. /**
  7160. * testFormEnd method
  7161. *
  7162. * @return void
  7163. */
  7164. public function testFormEnd()
  7165. {
  7166. $this->assertEquals('</form>', $this->Form->end());
  7167. }
  7168. /**
  7169. * Test the generation of fields for a multi record form.
  7170. *
  7171. * @return void
  7172. */
  7173. public function testMultiRecordForm()
  7174. {
  7175. $this->loadFixtures('Articles', 'Comments');
  7176. $articles = TableRegistry::get('Articles');
  7177. $articles->hasMany('Comments');
  7178. $comment = new Entity(['comment' => 'Value']);
  7179. $article = new Article(['comments' => [$comment]]);
  7180. $this->Form->create([$article]);
  7181. $result = $this->Form->input('0.comments.1.comment');
  7182. //@codingStandardsIgnoreStart
  7183. $expected = [
  7184. 'div' => ['class' => 'input textarea'],
  7185. 'label' => ['for' => '0-comments-1-comment'],
  7186. 'Comment',
  7187. '/label',
  7188. 'textarea' => [
  7189. 'name',
  7190. 'id' => '0-comments-1-comment',
  7191. 'rows' => 5
  7192. ],
  7193. '/textarea',
  7194. '/div'
  7195. ];
  7196. //@codingStandardsIgnoreEnd
  7197. $this->assertHtml($expected, $result);
  7198. $result = $this->Form->input('0.comments.0.comment');
  7199. //@codingStandardsIgnoreStart
  7200. $expected = [
  7201. 'div' => ['class' => 'input textarea'],
  7202. 'label' => ['for' => '0-comments-0-comment'],
  7203. 'Comment',
  7204. '/label',
  7205. 'textarea' => [
  7206. 'name',
  7207. 'id' => '0-comments-0-comment',
  7208. 'rows' => 5
  7209. ],
  7210. 'Value',
  7211. '/textarea',
  7212. '/div'
  7213. ];
  7214. //@codingStandardsIgnoreEnd
  7215. $this->assertHtml($expected, $result);
  7216. $comment->errors('comment', ['Not valid']);
  7217. $result = $this->Form->input('0.comments.0.comment');
  7218. //@codingStandardsIgnoreStart
  7219. $expected = [
  7220. 'div' => ['class' => 'input textarea error'],
  7221. 'label' => ['for' => '0-comments-0-comment'],
  7222. 'Comment',
  7223. '/label',
  7224. 'textarea' => [
  7225. 'name',
  7226. 'class' => 'form-error',
  7227. 'id' => '0-comments-0-comment',
  7228. 'rows' => 5
  7229. ],
  7230. 'Value',
  7231. '/textarea',
  7232. ['div' => ['class' => 'error-message']],
  7233. 'Not valid',
  7234. '/div',
  7235. '/div'
  7236. ];
  7237. //@codingStandardsIgnoreEnd
  7238. $this->assertHtml($expected, $result);
  7239. TableRegistry::get('Comments')
  7240. ->validator('default')
  7241. ->allowEmpty('comment', false);
  7242. $result = $this->Form->input('0.comments.1.comment');
  7243. //@codingStandardsIgnoreStart
  7244. $expected = [
  7245. 'div' => ['class' => 'input textarea required'],
  7246. 'label' => ['for' => '0-comments-1-comment'],
  7247. 'Comment',
  7248. '/label',
  7249. 'textarea' => [
  7250. 'name',
  7251. 'required' => 'required',
  7252. 'id' => '0-comments-1-comment',
  7253. 'rows' => 5
  7254. ],
  7255. '/textarea',
  7256. '/div'
  7257. ];
  7258. //@codingStandardsIgnoreEnd
  7259. $this->assertHtml($expected, $result);
  7260. }
  7261. /**
  7262. * test that some html5 inputs + FormHelper::__call() work
  7263. *
  7264. * @return void
  7265. */
  7266. public function testHtml5Inputs()
  7267. {
  7268. $result = $this->Form->email('User.email');
  7269. $expected = [
  7270. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7271. ];
  7272. $this->assertHtml($expected, $result);
  7273. $result = $this->Form->search('User.query');
  7274. $expected = [
  7275. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7276. ];
  7277. $this->assertHtml($expected, $result);
  7278. $result = $this->Form->search('User.query', ['value' => 'test']);
  7279. $expected = [
  7280. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7281. ];
  7282. $this->assertHtml($expected, $result);
  7283. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7284. $expected = [
  7285. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7286. ];
  7287. $this->assertHtml($expected, $result);
  7288. }
  7289. /**
  7290. * Test accessing html5 inputs through input().
  7291. *
  7292. * @return void
  7293. */
  7294. public function testHtml5InputWithInput()
  7295. {
  7296. $this->Form->create();
  7297. $this->Form->templates(['inputContainer' => '{{content}}']);
  7298. $result = $this->Form->input('website', [
  7299. 'type' => 'url',
  7300. 'val' => 'http://domain.tld',
  7301. 'label' => false
  7302. ]);
  7303. $expected = [
  7304. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7305. ];
  7306. $this->assertHtml($expected, $result);
  7307. }
  7308. /**
  7309. * Test errors when field name is missing.
  7310. *
  7311. * @expectedException \Cake\Core\Exception\Exception
  7312. * @return void
  7313. */
  7314. public function testHtml5InputException()
  7315. {
  7316. $this->Form->email();
  7317. }
  7318. /**
  7319. * Tests that formhelper sets required attributes.
  7320. *
  7321. * @return void
  7322. */
  7323. public function testRequiredAttribute()
  7324. {
  7325. $this->article['required'] = [
  7326. 'title' => true,
  7327. 'body' => false,
  7328. ];
  7329. $this->Form->create($this->article);
  7330. $result = $this->Form->input('title');
  7331. $expected = [
  7332. 'div' => ['class' => 'input text required'],
  7333. 'label' => ['for' => 'title'],
  7334. 'Title',
  7335. '/label',
  7336. 'input' => [
  7337. 'type' => 'text',
  7338. 'name' => 'title',
  7339. 'id' => 'title',
  7340. 'required' => 'required',
  7341. ],
  7342. '/div'
  7343. ];
  7344. $this->assertHtml($expected, $result);
  7345. $result = $this->Form->input('title', ['required' => false]);
  7346. $this->assertNotContains('required', $result);
  7347. $result = $this->Form->input('body');
  7348. $expected = [
  7349. 'div' => ['class' => 'input text'],
  7350. 'label' => ['for' => 'body'],
  7351. 'Body',
  7352. '/label',
  7353. 'input' => [
  7354. 'type' => 'text',
  7355. 'name' => 'body',
  7356. 'id' => 'body',
  7357. ],
  7358. '/div'
  7359. ];
  7360. $this->assertHtml($expected, $result);
  7361. $result = $this->Form->input('body', ['required' => true]);
  7362. $this->assertContains('required', $result);
  7363. }
  7364. /**
  7365. * Tests that it is possible to put inputs outside of the label
  7366. *
  7367. * @return void
  7368. */
  7369. public function testInputsNotNested()
  7370. {
  7371. $this->Form->templates([
  7372. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7373. 'formGroup' => '{{input}}{{label}}',
  7374. ]);
  7375. $result = $this->Form->input('foo', ['type' => 'checkbox']);
  7376. $expected = [
  7377. 'div' => ['class' => 'input checkbox'],
  7378. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7379. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7380. 'label' => ['for' => 'foo'],
  7381. 'Foo',
  7382. '/label',
  7383. '/div'
  7384. ];
  7385. $this->assertHtml($expected, $result);
  7386. $result = $this->Form->input('foo', ['type' => 'checkbox', 'label' => false]);
  7387. $expected = [
  7388. 'div' => ['class' => 'input checkbox'],
  7389. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7390. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7391. '/div'
  7392. ];
  7393. $this->assertHtml($expected, $result);
  7394. $result = $this->Form->input('confirm', [
  7395. 'type' => 'radio',
  7396. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7397. ]);
  7398. $expected = [
  7399. 'div' => ['class' => 'input radio'],
  7400. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7401. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7402. ['label' => ['for' => 'confirm-y']],
  7403. 'Yes',
  7404. '/label',
  7405. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7406. ['label' => ['for' => 'confirm-n']],
  7407. 'No',
  7408. '/label',
  7409. '<label',
  7410. 'Confirm',
  7411. '/label',
  7412. '/div',
  7413. ];
  7414. $this->assertHtml($expected, $result);
  7415. $result = $this->Form->select('category', ['1', '2'], [
  7416. 'multiple' => 'checkbox',
  7417. 'name' => 'fish',
  7418. ]);
  7419. $expected = [
  7420. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7421. ['div' => ['class' => 'checkbox']],
  7422. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7423. ['label' => ['for' => 'fish-0']],
  7424. '1',
  7425. '/label',
  7426. '/div',
  7427. ['div' => ['class' => 'checkbox']],
  7428. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7429. ['label' => ['for' => 'fish-1']],
  7430. '2',
  7431. '/label',
  7432. '/div'
  7433. ];
  7434. $this->assertHtml($expected, $result);
  7435. }
  7436. /**
  7437. * Test that *Container templates are used by input.
  7438. *
  7439. * @return void
  7440. */
  7441. public function testInputContainerTemplates()
  7442. {
  7443. $this->Form->templates([
  7444. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7445. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7446. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7447. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7448. ]);
  7449. $this->article['errors'] = [
  7450. 'Article' => ['published' => 'error message']
  7451. ];
  7452. $this->Form->create($this->article);
  7453. $result = $this->Form->input('accept', [
  7454. 'type' => 'checkbox'
  7455. ]);
  7456. $expected = [
  7457. 'div' => ['class' => 'check'],
  7458. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7459. 'label' => ['for' => 'accept'],
  7460. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7461. 'Accept',
  7462. '/label',
  7463. '/div'
  7464. ];
  7465. $this->assertHtml($expected, $result);
  7466. $result = $this->Form->input('accept', [
  7467. 'type' => 'radio',
  7468. 'options' => ['Y', 'N']
  7469. ]);
  7470. $this->assertContains('<div class="rad">', $result);
  7471. $result = $this->Form->input('Article.published', [
  7472. 'type' => 'radio',
  7473. 'options' => ['Y', 'N']
  7474. ]);
  7475. $this->assertContains('<div class="rad err">', $result);
  7476. $result = $this->Form->input('Article.created', [
  7477. 'type' => 'datetime',
  7478. ]);
  7479. $this->assertContains('<div class="dt">', $result);
  7480. }
  7481. /**
  7482. * Test that *Container templates are used by input.
  7483. *
  7484. * @return void
  7485. */
  7486. public function testFormGroupTemplates()
  7487. {
  7488. $this->Form->templates([
  7489. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7490. ]);
  7491. $this->Form->create($this->article);
  7492. $result = $this->Form->input('accept', [
  7493. 'type' => 'radio',
  7494. 'options' => ['Y', 'N']
  7495. ]);
  7496. $this->assertContains('<div class="radio">', $result);
  7497. }
  7498. /**
  7499. * Test resetting templates.
  7500. *
  7501. * @return void
  7502. */
  7503. public function testResetTemplates()
  7504. {
  7505. $this->Form->templates(['input' => '<input/>']);
  7506. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  7507. $this->assertNull($this->Form->resetTemplates());
  7508. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7509. }
  7510. /**
  7511. * Test the context method.
  7512. *
  7513. * @return void
  7514. */
  7515. public function testContext()
  7516. {
  7517. $result = $this->Form->context();
  7518. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7519. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7520. $this->assertSame($mock, $this->Form->context($mock));
  7521. $this->assertSame($mock, $this->Form->context());
  7522. }
  7523. /**
  7524. * testAutoDomId
  7525. *
  7526. * @return void
  7527. */
  7528. public function testAutoDomId()
  7529. {
  7530. $result = $this->Form->text('field', ['id' => true]);
  7531. $expected = [
  7532. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7533. ];
  7534. $this->assertHtml($expected, $result);
  7535. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7536. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7537. $expected = [
  7538. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7539. ['label' => ['for' => 'field-0']],
  7540. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7541. 'option A',
  7542. '/label',
  7543. ['label' => ['for' => 'field-1']],
  7544. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7545. 'option B',
  7546. '/label',
  7547. ];
  7548. $this->assertHtml($expected, $result);
  7549. $result = $this->Form->select(
  7550. 'multi_field',
  7551. ['first', 'second'],
  7552. ['multiple' => 'checkbox', 'id' => true]
  7553. );
  7554. $expected = [
  7555. 'input' => [
  7556. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  7557. ],
  7558. ['div' => ['class' => 'checkbox']],
  7559. ['label' => ['for' => 'multi-field-0']],
  7560. ['input' => [
  7561. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7562. 'value' => '0', 'id' => 'multi-field-0'
  7563. ]],
  7564. 'first',
  7565. '/label',
  7566. '/div',
  7567. ['div' => ['class' => 'checkbox']],
  7568. ['label' => ['for' => 'multi-field-1']],
  7569. ['input' => [
  7570. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7571. 'value' => '1', 'id' => 'multi-field-1'
  7572. ]],
  7573. 'second',
  7574. '/label',
  7575. '/div',
  7576. ];
  7577. $this->assertHtml($expected, $result);
  7578. }
  7579. /**
  7580. * Test the basic setters and getters for value sources
  7581. *
  7582. * @return void
  7583. */
  7584. public function testFormValueSourcesSettersGetters()
  7585. {
  7586. $expected = ['context'];
  7587. $result = $this->Form->getValueSources();
  7588. $this->assertEquals($expected, $result);
  7589. $expected = null;
  7590. $result = $this->Form->getSourceValue('id');
  7591. $this->assertEquals($expected, $result);
  7592. $expected = ['query', 'data', 'context'];
  7593. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7594. $result = $this->Form->getValueSources();
  7595. $this->assertEquals($expected, $result);
  7596. $this->Form->request->data['id'] = '1';
  7597. $this->Form->request->query['id'] = '2';
  7598. $this->Form->setValueSources(['context']);
  7599. $expected = '1';
  7600. $result = $this->Form->getSourceValue('id');
  7601. $this->assertEquals($expected, $result);
  7602. $this->Form->setValueSources('query');
  7603. $expected = ['query'];
  7604. $result = $this->Form->getValueSources();
  7605. $this->assertEquals($expected, $result);
  7606. $expected = '2';
  7607. $result = $this->Form->getSourceValue('id');
  7608. $this->assertEquals($expected, $result);
  7609. $this->Form->setValueSources(['data']);
  7610. $expected = '1';
  7611. $result = $this->Form->getSourceValue('id');
  7612. $this->assertEquals($expected, $result);
  7613. $this->Form->setValueSources(['query', 'data']);
  7614. $expected = '2';
  7615. $result = $this->Form->getSourceValue('id');
  7616. $this->assertEquals($expected, $result);
  7617. }
  7618. /**
  7619. * Tests the different input rendering values based on sources values switching
  7620. *
  7621. * @return void
  7622. */
  7623. public function testFormValueSourcesSingleSwitchRendering()
  7624. {
  7625. $this->loadFixtures('Articles');
  7626. $articles = TableRegistry::get('Articles');
  7627. $article = new Article();
  7628. $articles->patchEntity($article, ['id' => '3']);
  7629. $this->Form->create($article);
  7630. $this->Form->setValueSources(['context']);
  7631. $result = $this->Form->input('id');
  7632. $expected = [
  7633. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7634. ];
  7635. $this->assertHtml($expected, $result);
  7636. $this->Form->request->query['id'] = '5';
  7637. $this->Form->setValueSources(['query']);
  7638. $result = $this->Form->input('id');
  7639. $expected = [
  7640. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7641. ];
  7642. $this->assertHtml($expected, $result);
  7643. $this->Form->request->query['id'] = '5a';
  7644. $this->Form->request->data['id'] = '5b';
  7645. $this->Form->setValueSources(['context']);
  7646. $this->Form->create($article);
  7647. $result = $this->Form->input('id');
  7648. $expected = [
  7649. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7650. ];
  7651. $this->assertHtml($expected, $result);
  7652. $this->Form->setValueSources(['data']);
  7653. $this->Form->create($article);
  7654. $result = $this->Form->input('id');
  7655. $expected = [
  7656. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7657. ];
  7658. $this->assertHtml($expected, $result);
  7659. $this->Form->setValueSources(['query']);
  7660. $result = $this->Form->input('id');
  7661. $expected = [
  7662. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7663. ];
  7664. $this->assertHtml($expected, $result);
  7665. }
  7666. /**
  7667. * Tests the different input rendering values based on sources values switching while supplying
  7668. * an entity (base context) and multiple sources (such as data, query)
  7669. *
  7670. * @return void
  7671. */
  7672. public function testFormValueSourcesListSwitchRendering()
  7673. {
  7674. $this->loadFixtures('Articles');
  7675. $articles = TableRegistry::get('Articles');
  7676. $article = new Article();
  7677. $articles->patchEntity($article, ['id' => '3']);
  7678. $this->Form->request->query['id'] = '9';
  7679. $this->Form->create($article);
  7680. $this->Form->setValueSources(['context', 'query']);
  7681. $result = $this->Form->input('id');
  7682. $expected = [
  7683. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7684. ];
  7685. $this->assertHtml($expected, $result);
  7686. $this->Form->setValueSources(['query', 'context']);
  7687. $result = $this->Form->input('id');
  7688. $expected = [
  7689. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7690. ];
  7691. $this->assertHtml($expected, $result);
  7692. $this->Form->setValueSources(['data', 'query', 'context']);
  7693. $result = $this->Form->input('id');
  7694. $expected = [
  7695. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7696. ];
  7697. $this->assertHtml($expected, $result);
  7698. $this->Form->request->data['id'] = '8';
  7699. $this->Form->request->query['id'] = '9';
  7700. $this->Form->setValueSources(['data', 'query', 'context']);
  7701. $result = $this->Form->input('id');
  7702. $expected = [
  7703. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  7704. ];
  7705. $this->assertHtml($expected, $result);
  7706. }
  7707. /**
  7708. * Test the different form input renderings based on values sources switchings through form options
  7709. *
  7710. * @return void
  7711. */
  7712. public function testFormValueSourcesSwitchViaOptionsRendering()
  7713. {
  7714. $this->loadFixtures('Articles');
  7715. $articles = TableRegistry::get('Articles');
  7716. $article = new Article();
  7717. $articles->patchEntity($article, ['id' => '3']);
  7718. $this->Form->request->data['id'] = '4';
  7719. $this->Form->request->query['id'] = '5';
  7720. $this->Form->create($article, ['valueSources' => 'query']);
  7721. $result = $this->Form->input('id');
  7722. $expected = [
  7723. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7724. ];
  7725. $this->assertHtml($expected, $result);
  7726. $result = $this->Form->getSourceValue('id');
  7727. $this->assertEquals('5', $result);
  7728. $this->Form->setValueSources(['context']);
  7729. $this->Form->create($article, ['valueSources' => 'query']);
  7730. $result = $this->Form->input('id');
  7731. $expected = [
  7732. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7733. ];
  7734. $this->assertHtml($expected, $result);
  7735. $result = $this->Form->getSourceValue('id');
  7736. $this->assertEquals('5', $result);
  7737. $this->Form->setValueSources(['query']);
  7738. $this->Form->create($article, ['valueSources' => 'data']);
  7739. $result = $this->Form->input('id');
  7740. $expected = [
  7741. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7742. ];
  7743. $this->assertHtml($expected, $result);
  7744. $result = $this->Form->getSourceValue('id');
  7745. $this->assertEquals('4', $result);
  7746. $this->Form->setValueSources(['query']);
  7747. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  7748. $result = $this->Form->input('id');
  7749. $expected = [
  7750. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7751. ];
  7752. $this->assertHtml($expected, $result);
  7753. $result = $this->Form->getSourceValue('id');
  7754. $this->assertEquals('4', $result);
  7755. }
  7756. /**
  7757. * Test the different form input renderings based on values sources switchings through form options
  7758. *
  7759. * @return void
  7760. */
  7761. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  7762. {
  7763. $this->loadFixtures('Articles');
  7764. $articles = TableRegistry::get('Articles');
  7765. $article = new Article();
  7766. $articles->patchEntity($article, ['id' => '3']);
  7767. $this->Form->request->data['id'] = '10';
  7768. $this->Form->request->query['id'] = '11';
  7769. $this->Form->setValueSources(['context'])
  7770. ->create($article, ['valueSources' => ['query', 'data']]);
  7771. $result = $this->Form->input('id');
  7772. $expected = [
  7773. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  7774. ];
  7775. $this->assertHtml($expected, $result);
  7776. $result = $this->Form->getSourceValue('id');
  7777. $this->assertEquals('11', $result);
  7778. unset($this->Form->request->query['id']);
  7779. $this->Form->setValueSources(['context'])
  7780. ->create($article, ['valueSources' => ['query', 'data']]);
  7781. $result = $this->Form->input('id');
  7782. $expected = [
  7783. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  7784. ];
  7785. $this->assertHtml($expected, $result);
  7786. $result = $this->Form->getSourceValue('id');
  7787. $this->assertEquals('10', $result);
  7788. }
  7789. /**
  7790. * Test the different form values sources resetting through From::end();
  7791. *
  7792. * @return void
  7793. */
  7794. public function testFormValueSourcesResetViaEnd()
  7795. {
  7796. $expected = ['context'];
  7797. $result = $this->Form->getValueSources();
  7798. $this->assertEquals($expected, $result);
  7799. $expected = ['query', 'context', 'data'];
  7800. $this->Form->setValueSources(['query', 'context', 'data']);
  7801. $result = $this->Form->getValueSources();
  7802. $this->assertEquals($expected, $result);
  7803. $this->Form->create();
  7804. $result = $this->Form->getValueSources();
  7805. $this->assertEquals($expected, $result);
  7806. $this->Form->end();
  7807. $result = $this->Form->getValueSources();
  7808. $this->assertEquals(['context'], $result);
  7809. }
  7810. /**
  7811. * Test sources values defaults handling
  7812. *
  7813. * @return void
  7814. */
  7815. public function testFormValueSourcesDefaults()
  7816. {
  7817. $this->Form->request->query['password'] = 'open Sesame';
  7818. $this->Form->create();
  7819. $result = $this->Form->password('password');
  7820. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  7821. $this->assertHtml($expected, $result);
  7822. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7823. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7824. $this->assertHtml($expected, $result);
  7825. $this->Form->setValueSources('query');
  7826. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7827. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  7828. $this->assertHtml($expected, $result);
  7829. $this->Form->setValueSources('data');
  7830. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7831. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7832. $this->assertHtml($expected, $result);
  7833. }
  7834. /**
  7835. * Test sources values schema defaults handling
  7836. *
  7837. * @return void
  7838. */
  7839. public function testSourcesValueDoesntExistPassThrough()
  7840. {
  7841. $this->Form->request->query['category'] = 'sesame-cookies';
  7842. $articles = TableRegistry::get('Articles');
  7843. $entity = $articles->newEntity();
  7844. $this->Form->create($entity);
  7845. $this->Form->setValueSources(['query', 'context']);
  7846. $result = $this->Form->getSourceValue('category');
  7847. $this->assertEquals('sesame-cookies', $result);
  7848. $this->Form->setValueSources(['context', 'query']);
  7849. $result = $this->Form->getSourceValue('category');
  7850. $this->assertEquals('sesame-cookies', $result);
  7851. }
  7852. }