FormHelperTest.php 282 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558
  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 = new Collection([$entity]);
  368. $emptyCollection = new Collection([]);
  369. $emptyArray = [];
  370. $arrayObject = new \ArrayObject([]);
  371. $data = [
  372. 'schema' => [
  373. 'title' => ['type' => 'string']
  374. ]
  375. ];
  376. $form = new Form();
  377. return [
  378. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  379. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  380. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  381. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  382. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  383. 'form' => [$form, 'Cake\View\Form\FormContext'],
  384. 'none' => [null, 'Cake\View\Form\NullContext'],
  385. 'false' => [false, 'Cake\View\Form\NullContext'],
  386. ];
  387. }
  388. /**
  389. * Test default context selection in create()
  390. *
  391. * @dataProvider contextSelectionProvider
  392. * @return void
  393. */
  394. public function testCreateContextSelectionBuiltIn($data, $class)
  395. {
  396. $this->loadFixtures('Articles');
  397. $this->Form->create($data);
  398. $this->assertInstanceOf($class, $this->Form->context());
  399. }
  400. /**
  401. * Data provider for type option.
  402. *
  403. * @return array
  404. */
  405. public static function requestTypeProvider()
  406. {
  407. return [
  408. // type, method, override
  409. ['post', 'post', 'POST'],
  410. ['put', 'post', 'PUT'],
  411. ['patch', 'post', 'PATCH'],
  412. ['delete', 'post', 'DELETE'],
  413. ];
  414. }
  415. /**
  416. * Test creating file forms.
  417. *
  418. * @return void
  419. */
  420. public function testCreateFile()
  421. {
  422. $encoding = strtolower(Configure::read('App.encoding'));
  423. $result = $this->Form->create(false, ['type' => 'file']);
  424. $expected = [
  425. 'form' => [
  426. 'method' => 'post', 'action' => '/articles/add',
  427. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  428. ],
  429. 'div' => ['style' => 'display:none;'],
  430. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  431. '/div'
  432. ];
  433. $this->assertHtml($expected, $result);
  434. }
  435. /**
  436. * Test creating GET forms.
  437. *
  438. * @return void
  439. */
  440. public function testCreateGet()
  441. {
  442. $encoding = strtolower(Configure::read('App.encoding'));
  443. $result = $this->Form->create(false, ['type' => 'get']);
  444. $expected = ['form' => [
  445. 'method' => 'get', 'action' => '/articles/add',
  446. 'accept-charset' => $encoding
  447. ]];
  448. $this->assertHtml($expected, $result);
  449. }
  450. /**
  451. * Test explicit method/enctype options.
  452. *
  453. * Explicit method overwrites inferred method from 'type'
  454. *
  455. * @return void
  456. */
  457. public function testCreateExplictMethodEnctype()
  458. {
  459. $encoding = strtolower(Configure::read('App.encoding'));
  460. $result = $this->Form->create(false, [
  461. 'type' => 'get',
  462. 'method' => 'put',
  463. 'enctype' => 'multipart/form-data'
  464. ]);
  465. $expected = ['form' => [
  466. 'method' => 'put',
  467. 'action' => '/articles/add',
  468. 'enctype' => 'multipart/form-data',
  469. 'accept-charset' => $encoding
  470. ]];
  471. $this->assertHtml($expected, $result);
  472. }
  473. /**
  474. * Test create() with the templates option.
  475. *
  476. * @return void
  477. */
  478. public function testCreateTemplatesArray()
  479. {
  480. $result = $this->Form->create($this->article, [
  481. 'templates' => [
  482. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  483. ]
  484. ]);
  485. $expected = [
  486. 'form' => [
  487. 'class' => 'form-horizontal',
  488. 'method' => 'post',
  489. 'action' => '/articles/add',
  490. 'accept-charset' => 'utf-8'
  491. ]
  492. ];
  493. $this->assertHtml($expected, $result);
  494. }
  495. /**
  496. * Test create() with the templates option.
  497. *
  498. * @return void
  499. */
  500. public function testCreateTemplatesFile()
  501. {
  502. $result = $this->Form->create($this->article, [
  503. 'templates' => 'htmlhelper_tags',
  504. ]);
  505. $expected = [
  506. 'start form',
  507. 'div' => ['class' => 'hidden'],
  508. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  509. '/div'
  510. ];
  511. $this->assertHtml($expected, $result);
  512. }
  513. /**
  514. * Test that create() and end() restore templates.
  515. *
  516. * @return void
  517. */
  518. public function testCreateEndRestoreTemplates()
  519. {
  520. $this->Form->create($this->article, [
  521. 'templates' => ['input' => 'custom input element']
  522. ]);
  523. $this->Form->end();
  524. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  525. }
  526. /**
  527. * Test using template vars in various templates used by input() method.
  528. *
  529. * @return void
  530. */
  531. public function testInputTemplateVars()
  532. {
  533. $result = $this->Form->input('text', [
  534. 'templates' => [
  535. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  536. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  537. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  538. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  539. ],
  540. 'templateVars' => [
  541. 'forinput' => 'in-input',
  542. 'forlabel' => 'in-label',
  543. 'forgroup' => 'in-group',
  544. 'forcontainer' => 'in-container'
  545. ]
  546. ]);
  547. $expected = [
  548. 'div' => ['class'],
  549. 'label' => ['for'],
  550. 'Text in-label',
  551. '/label',
  552. 'in-group',
  553. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  554. 'in-container',
  555. '/div',
  556. ];
  557. $this->assertHtml($expected, $result);
  558. }
  559. /**
  560. * Test ensuring template variables work in template files loaded
  561. * during input().
  562. *
  563. * @return void
  564. */
  565. public function testInputTemplatesFromFile()
  566. {
  567. $result = $this->Form->input('title', [
  568. 'templates' => 'test_templates',
  569. 'templateVars' => [
  570. 'forcontainer' => 'container-data'
  571. ]
  572. ]);
  573. $expected = [
  574. 'div' => ['class'],
  575. 'label' => ['for'],
  576. 'Title',
  577. '/label',
  578. 'input' => ['name', 'type' => 'text', 'id'],
  579. 'container-data',
  580. '/div',
  581. ];
  582. $this->assertHtml($expected, $result);
  583. }
  584. /**
  585. * Test using template vars in inputSubmit and submitContainer template.
  586. *
  587. * @return void
  588. */
  589. public function testSubmitTemplateVars()
  590. {
  591. $this->Form->templates([
  592. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  593. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  594. ]);
  595. $result = $this->Form->submit('Submit', [
  596. 'templateVars' => [
  597. 'forinput' => 'in-input',
  598. 'forcontainer' => 'in-container'
  599. ]
  600. ]);
  601. $expected = [
  602. 'div' => ['class'],
  603. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  604. 'in-container',
  605. '/div',
  606. ];
  607. $this->assertHtml($expected, $result);
  608. }
  609. /**
  610. * test the create() method
  611. *
  612. * @dataProvider requestTypeProvider
  613. * @return void
  614. */
  615. public function testCreateTypeOptions($type, $method, $override)
  616. {
  617. $encoding = strtolower(Configure::read('App.encoding'));
  618. $result = $this->Form->create(false, ['type' => $type]);
  619. $expected = [
  620. 'form' => [
  621. 'method' => $method, 'action' => '/articles/add',
  622. 'accept-charset' => $encoding
  623. ],
  624. 'div' => ['style' => 'display:none;'],
  625. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  626. '/div'
  627. ];
  628. $this->assertHtml($expected, $result);
  629. }
  630. /**
  631. * Test using template vars in Create (formStart template)
  632. *
  633. * @return void
  634. */
  635. public function testCreateTemplateVars()
  636. {
  637. $result = $this->Form->create($this->article, [
  638. 'templates' => [
  639. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  640. ],
  641. 'templateVars' => ['header' => 'headertext']
  642. ]);
  643. $expected = [
  644. 'h4' => ['class'],
  645. 'headertext',
  646. '/h4',
  647. 'form' => [
  648. 'method' => 'post',
  649. 'action' => '/articles/add',
  650. 'accept-charset' => 'utf-8'
  651. ]
  652. ];
  653. $this->assertHtml($expected, $result);
  654. }
  655. /**
  656. * Test opening a form for an update operation.
  657. *
  658. * @return void
  659. */
  660. public function testCreateUpdateForm()
  661. {
  662. $encoding = strtolower(Configure::read('App.encoding'));
  663. $this->Form->request->here = '/articles/edit/1';
  664. $this->Form->request->params['action'] = 'edit';
  665. $this->article['defaults']['id'] = 1;
  666. $result = $this->Form->create($this->article);
  667. $expected = [
  668. 'form' => [
  669. 'method' => 'post',
  670. 'action' => '/articles/edit/1',
  671. 'accept-charset' => $encoding
  672. ],
  673. 'div' => ['style' => 'display:none;'],
  674. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  675. '/div'
  676. ];
  677. $this->assertHtml($expected, $result);
  678. }
  679. /**
  680. * test create() with automatic url generation
  681. *
  682. * @return void
  683. */
  684. public function testCreateAutoUrl()
  685. {
  686. $encoding = strtolower(Configure::read('App.encoding'));
  687. $this->Form->request->params['action'] = 'delete';
  688. $this->Form->request->here = '/articles/delete/10';
  689. $this->Form->request->base = '';
  690. $result = $this->Form->create($this->article);
  691. $expected = [
  692. 'form' => [
  693. 'method' => 'post', 'action' => '/articles/delete/10',
  694. 'accept-charset' => $encoding
  695. ],
  696. 'div' => ['style' => 'display:none;'],
  697. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  698. '/div'
  699. ];
  700. $this->assertHtml($expected, $result);
  701. $this->article['defaults'] = ['id' => 1];
  702. $this->Form->request->here = '/articles/edit/1';
  703. $this->Form->request->params['action'] = 'delete';
  704. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  705. $expected = [
  706. 'form' => [
  707. 'method' => 'post',
  708. 'action' => '/articles/edit/1',
  709. 'accept-charset' => $encoding
  710. ],
  711. 'div' => ['style' => 'display:none;'],
  712. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  713. '/div'
  714. ];
  715. $this->assertHtml($expected, $result);
  716. $this->Form->request->params['action'] = 'add';
  717. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  718. $expected = [
  719. 'form' => [
  720. 'method' => 'post',
  721. 'action' => '/articles/publish/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. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  730. $expected = [
  731. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  732. 'div' => ['style' => 'display:none;'],
  733. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  734. '/div'
  735. ];
  736. $this->assertHtml($expected, $result);
  737. $this->Form->request->params['controller'] = 'Pages';
  738. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  739. $expected = [
  740. 'form' => [
  741. 'method' => 'post', 'action' => '/Pages/signup/1',
  742. 'accept-charset' => $encoding
  743. ],
  744. 'div' => ['style' => 'display:none;'],
  745. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  746. '/div'
  747. ];
  748. $this->assertHtml($expected, $result);
  749. }
  750. /**
  751. * Test create() with no URL (no "action" attribute for <form> tag)
  752. *
  753. * @return void
  754. */
  755. public function testCreateNoUrl()
  756. {
  757. $result = $this->Form->create(false, ['url' => false]);
  758. $expected = [
  759. 'form' => [
  760. 'method' => 'post',
  761. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  762. ],
  763. 'div' => ['style' => 'display:none;'],
  764. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  765. '/div'
  766. ];
  767. $this->assertHtml($expected, $result);
  768. }
  769. /**
  770. * test create() with a custom route
  771. *
  772. * @return void
  773. */
  774. public function testCreateCustomRoute()
  775. {
  776. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  777. $encoding = strtolower(Configure::read('App.encoding'));
  778. $this->Form->request->params['controller'] = 'users';
  779. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  780. $expected = [
  781. 'form' => [
  782. 'method' => 'post', 'action' => '/login',
  783. 'accept-charset' => $encoding
  784. ],
  785. 'div' => ['style' => 'display:none;'],
  786. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  787. '/div'
  788. ];
  789. $this->assertHtml($expected, $result);
  790. Router::connect(
  791. '/new-article',
  792. ['controller' => 'articles', 'action' => 'myaction'],
  793. ['_name' => 'my-route']
  794. );
  795. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  796. $expected = [
  797. 'form' => [
  798. 'method' => 'post', 'action' => '/new-article',
  799. 'accept-charset' => $encoding,
  800. ],
  801. 'div' => ['style' => 'display:none;'],
  802. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  803. '/div'
  804. ];
  805. $this->assertHtml($expected, $result);
  806. }
  807. /**
  808. * test automatic accept-charset overriding
  809. *
  810. * @return void
  811. */
  812. public function testCreateWithAcceptCharset()
  813. {
  814. $result = $this->Form->create(
  815. $this->article,
  816. [
  817. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  818. ]
  819. );
  820. $expected = [
  821. 'form' => [
  822. 'method' => 'post', 'action' => '/articles',
  823. 'accept-charset' => 'iso-8859-1'
  824. ],
  825. 'div' => ['style' => 'display:none;'],
  826. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  827. '/div'
  828. ];
  829. $this->assertHtml($expected, $result);
  830. }
  831. /**
  832. * Test base form URL when url param is passed with multiple parameters (&)
  833. *
  834. */
  835. public function testCreateQuerystringrequest()
  836. {
  837. $encoding = strtolower(Configure::read('App.encoding'));
  838. $result = $this->Form->create($this->article, [
  839. 'type' => 'post',
  840. 'escape' => false,
  841. 'url' => [
  842. 'controller' => 'controller',
  843. 'action' => 'action',
  844. '?' => ['param1' => 'value1', 'param2' => 'value2']
  845. ]
  846. ]);
  847. $expected = [
  848. 'form' => [
  849. 'method' => 'post',
  850. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  851. 'accept-charset' => $encoding
  852. ],
  853. 'div' => ['style' => 'display:none;'],
  854. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  855. '/div'
  856. ];
  857. $this->assertHtml($expected, $result);
  858. $result = $this->Form->create($this->article, [
  859. 'type' => 'post',
  860. 'url' => [
  861. 'controller' => 'controller',
  862. 'action' => 'action',
  863. '?' => ['param1' => 'value1', 'param2' => 'value2']
  864. ]
  865. ]);
  866. $this->assertHtml($expected, $result);
  867. }
  868. /**
  869. * test that create() doesn't cause errors by multiple id's being in the primary key
  870. * as could happen with multiple select or checkboxes.
  871. *
  872. * @return void
  873. */
  874. public function testCreateWithMultipleIdInData()
  875. {
  876. $encoding = strtolower(Configure::read('App.encoding'));
  877. $this->Form->request->data['Article']['id'] = [1, 2];
  878. $result = $this->Form->create($this->article);
  879. $expected = [
  880. 'form' => [
  881. 'method' => 'post',
  882. 'action' => '/articles/add',
  883. 'accept-charset' => $encoding
  884. ],
  885. 'div' => ['style' => 'display:none;'],
  886. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  887. '/div'
  888. ];
  889. $this->assertHtml($expected, $result);
  890. }
  891. /**
  892. * test that create() doesn't add in extra passed params.
  893. *
  894. * @return void
  895. */
  896. public function testCreatePassedArgs()
  897. {
  898. $encoding = strtolower(Configure::read('App.encoding'));
  899. $this->Form->request->data['Article']['id'] = 1;
  900. $result = $this->Form->create($this->article, [
  901. 'type' => 'post',
  902. 'escape' => false,
  903. 'url' => [
  904. 'action' => 'edit',
  905. 'myparam'
  906. ]
  907. ]);
  908. $expected = [
  909. 'form' => [
  910. 'method' => 'post',
  911. 'action' => '/articles/edit/myparam',
  912. 'accept-charset' => $encoding
  913. ],
  914. 'div' => ['style' => 'display:none;'],
  915. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  916. '/div'
  917. ];
  918. $this->assertHtml($expected, $result);
  919. }
  920. /**
  921. * test creating a get form, and get form inputs.
  922. *
  923. * @return void
  924. */
  925. public function testGetFormCreate()
  926. {
  927. $encoding = strtolower(Configure::read('App.encoding'));
  928. $result = $this->Form->create($this->article, ['type' => 'get']);
  929. $expected = ['form' => [
  930. 'method' => 'get', 'action' => '/articles/add',
  931. 'accept-charset' => $encoding
  932. ]];
  933. $this->assertHtml($expected, $result);
  934. $result = $this->Form->text('title');
  935. $expected = ['input' => [
  936. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  937. ]];
  938. $this->assertHtml($expected, $result);
  939. $result = $this->Form->password('password');
  940. $expected = ['input' => [
  941. 'name' => 'password', 'type' => 'password'
  942. ]];
  943. $this->assertHtml($expected, $result);
  944. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  945. $result = $this->Form->text('user_form');
  946. $expected = ['input' => [
  947. 'name' => 'user_form', 'type' => 'text'
  948. ]];
  949. $this->assertHtml($expected, $result);
  950. }
  951. /**
  952. * test get form, and inputs when the model param is false
  953. *
  954. * @return void
  955. */
  956. public function testGetFormWithFalseModel()
  957. {
  958. $encoding = strtolower(Configure::read('App.encoding'));
  959. $this->Form->request->params['controller'] = 'contact_test';
  960. $result = $this->Form->create(false, [
  961. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  962. ]);
  963. $expected = ['form' => [
  964. 'method' => 'get', 'action' => '/contact_test/add',
  965. 'accept-charset' => $encoding
  966. ]];
  967. $this->assertHtml($expected, $result);
  968. $result = $this->Form->text('reason');
  969. $expected = [
  970. 'input' => ['type' => 'text', 'name' => 'reason']
  971. ];
  972. $this->assertHtml($expected, $result);
  973. }
  974. /**
  975. * testFormCreateWithSecurity method
  976. *
  977. * Test form->create() with security key.
  978. *
  979. * @return void
  980. */
  981. public function testCreateWithSecurity()
  982. {
  983. $this->Form->request->params['_csrfToken'] = 'testKey';
  984. $encoding = strtolower(Configure::read('App.encoding'));
  985. $result = $this->Form->create($this->article, [
  986. 'url' => '/articles/publish',
  987. ]);
  988. $expected = [
  989. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  990. 'div' => ['style' => 'display:none;'],
  991. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  992. ['input' => [
  993. 'type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testKey'
  994. ]],
  995. '/div'
  996. ];
  997. $this->assertHtml($expected, $result);
  998. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  999. $expected['form']['id'] = 'MyForm';
  1000. $this->assertHtml($expected, $result);
  1001. }
  1002. /**
  1003. * testFormCreateGetNoSecurity method
  1004. *
  1005. * Test form->create() with no security key as its a get form
  1006. *
  1007. * @return void
  1008. */
  1009. public function testCreateEndGetNoSecurity()
  1010. {
  1011. $this->Form->request->params['_csrfToken'] = 'testKey';
  1012. $encoding = strtolower(Configure::read('App.encoding'));
  1013. $article = new Article();
  1014. $result = $this->Form->create($article, [
  1015. 'type' => 'get',
  1016. 'url' => '/contacts/add'
  1017. ]);
  1018. $this->assertNotContains('testKey', $result);
  1019. $result = $this->Form->end();
  1020. $this->assertNotContains('testKey', $result);
  1021. }
  1022. /**
  1023. * test that create() clears the fields property so it starts fresh
  1024. *
  1025. * @return void
  1026. */
  1027. public function testCreateClearingFields()
  1028. {
  1029. $this->Form->fields = ['model_id'];
  1030. $this->Form->create($this->article);
  1031. $this->assertEquals([], $this->Form->fields);
  1032. }
  1033. /**
  1034. * Tests form hash generation with model-less data
  1035. *
  1036. * @return void
  1037. */
  1038. public function testValidateHashNoModel()
  1039. {
  1040. $this->Form->request->params['_Token'] = 'foo';
  1041. $result = $this->Form->secure(['anything']);
  1042. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  1043. }
  1044. /**
  1045. * Tests that hidden fields generated for checkboxes don't get locked
  1046. *
  1047. * @return void
  1048. */
  1049. public function testNoCheckboxLocking()
  1050. {
  1051. $this->Form->request->params['_Token'] = 'foo';
  1052. $this->assertSame([], $this->Form->fields);
  1053. $this->Form->checkbox('check', ['value' => '1']);
  1054. $this->assertSame($this->Form->fields, ['check']);
  1055. }
  1056. /**
  1057. * testFormSecurityFields method
  1058. *
  1059. * Test generation of secure form hash generation.
  1060. *
  1061. * @return void
  1062. */
  1063. public function testFormSecurityFields()
  1064. {
  1065. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1066. $this->Form->request->params['_Token'] = 'testKey';
  1067. $result = $this->Form->secure($fields);
  1068. $hash = Security::hash(serialize($fields) . Security::salt());
  1069. $hash .= ':' . 'Model.valid';
  1070. $hash = urlencode($hash);
  1071. $tokenDebug = urlencode(json_encode([
  1072. '',
  1073. $fields,
  1074. []
  1075. ]));
  1076. $expected = [
  1077. 'div' => ['style' => 'display:none;'],
  1078. ['input' => [
  1079. 'type' => 'hidden',
  1080. 'name' => '_Token[fields]',
  1081. 'value' => $hash
  1082. ]],
  1083. ['input' => [
  1084. 'type' => 'hidden',
  1085. 'name' => '_Token[unlocked]',
  1086. 'value' => '',
  1087. ]],
  1088. ['input' => [
  1089. 'type' => 'hidden',
  1090. 'name' => '_Token[debug]',
  1091. 'value' => $tokenDebug
  1092. ]],
  1093. '/div'
  1094. ];
  1095. $this->assertHtml($expected, $result);
  1096. }
  1097. /**
  1098. * testFormSecurityFields method
  1099. *
  1100. * Test debug token is not generated if debug is false
  1101. *
  1102. * @return void
  1103. */
  1104. public function testFormSecurityFieldsNoDebugMode()
  1105. {
  1106. Configure::write('debug', false);
  1107. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1108. $this->Form->request->params['_Token'] = 'testKey';
  1109. $result = $this->Form->secure($fields);
  1110. $hash = Security::hash(serialize($fields) . Security::salt());
  1111. $hash .= ':' . 'Model.valid';
  1112. $hash = urlencode($hash);
  1113. $expected = [
  1114. 'div' => ['style' => 'display:none;'],
  1115. ['input' => [
  1116. 'type' => 'hidden',
  1117. 'name' => '_Token[fields]',
  1118. 'value' => $hash
  1119. ]],
  1120. ['input' => [
  1121. 'type' => 'hidden',
  1122. 'name' => '_Token[unlocked]',
  1123. 'value' => '',
  1124. ]],
  1125. '/div'
  1126. ];
  1127. $this->assertHtml($expected, $result);
  1128. }
  1129. /**
  1130. * Tests correct generation of number fields for double and float fields
  1131. *
  1132. * @return void
  1133. */
  1134. public function testTextFieldGenerationForFloats()
  1135. {
  1136. $this->article['schema'] = [
  1137. 'foo' => [
  1138. 'type' => 'float',
  1139. 'null' => false,
  1140. 'default' => null,
  1141. 'length' => 10
  1142. ]
  1143. ];
  1144. $this->Form->create($this->article);
  1145. $result = $this->Form->input('foo');
  1146. $expected = [
  1147. 'div' => ['class' => 'input number'],
  1148. 'label' => ['for' => 'foo'],
  1149. 'Foo',
  1150. '/label',
  1151. ['input' => [
  1152. 'type' => 'number',
  1153. 'name' => 'foo',
  1154. 'id' => 'foo',
  1155. 'step' => 'any'
  1156. ]],
  1157. '/div'
  1158. ];
  1159. $this->assertHtml($expected, $result);
  1160. $result = $this->Form->input('foo', ['step' => 0.5]);
  1161. $expected = [
  1162. 'div' => ['class' => 'input number'],
  1163. 'label' => ['for' => 'foo'],
  1164. 'Foo',
  1165. '/label',
  1166. ['input' => [
  1167. 'type' => 'number',
  1168. 'name' => 'foo',
  1169. 'id' => 'foo',
  1170. 'step' => '0.5'
  1171. ]],
  1172. '/div'
  1173. ];
  1174. $this->assertHtml($expected, $result);
  1175. }
  1176. /**
  1177. * Tests correct generation of number fields for integer fields
  1178. *
  1179. * @return void
  1180. */
  1181. public function testTextFieldTypeNumberGenerationForIntegers()
  1182. {
  1183. TableRegistry::get('Contacts', [
  1184. 'className' => __NAMESPACE__ . '\ContactsTable'
  1185. ]);
  1186. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1187. $result = $this->Form->input('age');
  1188. $expected = [
  1189. 'div' => ['class' => 'input number'],
  1190. 'label' => ['for' => 'age'],
  1191. 'Age',
  1192. '/label',
  1193. ['input' => [
  1194. 'type' => 'number', 'name' => 'age',
  1195. 'id' => 'age'
  1196. ]],
  1197. '/div'
  1198. ];
  1199. $this->assertHtml($expected, $result);
  1200. }
  1201. /**
  1202. * Tests correct generation of file upload fields for binary fields
  1203. *
  1204. * @return void
  1205. */
  1206. public function testFileUploadFieldTypeGenerationForBinaries()
  1207. {
  1208. $table = TableRegistry::get('Contacts', [
  1209. 'className' => __NAMESPACE__ . '\ContactsTable'
  1210. ]);
  1211. $table->schema(['foo' => [
  1212. 'type' => 'binary',
  1213. 'null' => false,
  1214. 'default' => null,
  1215. 'length' => 1024
  1216. ]]);
  1217. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1218. $result = $this->Form->input('foo');
  1219. $expected = [
  1220. 'div' => ['class' => 'input file'],
  1221. 'label' => ['for' => 'foo'],
  1222. 'Foo',
  1223. '/label',
  1224. ['input' => [
  1225. 'type' => 'file', 'name' => 'foo',
  1226. 'id' => 'foo'
  1227. ]],
  1228. '/div'
  1229. ];
  1230. $this->assertHtml($expected, $result);
  1231. }
  1232. /**
  1233. * testFormSecurityMultipleFields method
  1234. *
  1235. * Test secure() with multiple row form. Ensure hash is correct.
  1236. *
  1237. * @return void
  1238. */
  1239. public function testFormSecurityMultipleFields()
  1240. {
  1241. $this->Form->request->params['_Token'] = 'foo';
  1242. $fields = [
  1243. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1244. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1245. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1246. ];
  1247. $result = $this->Form->secure($fields);
  1248. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  1249. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  1250. $tokenDebug = urlencode(json_encode([
  1251. '',
  1252. $fields,
  1253. []
  1254. ]));
  1255. $expected = [
  1256. 'div' => ['style' => 'display:none;'],
  1257. ['input' => [
  1258. 'type' => 'hidden', 'name' => '_Token[fields]',
  1259. 'value' => $hash
  1260. ]],
  1261. ['input' => [
  1262. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1263. 'value' => ''
  1264. ]],
  1265. ['input' => [
  1266. 'type' => 'hidden', 'name' => '_Token[debug]',
  1267. 'value' => $tokenDebug,
  1268. ]],
  1269. '/div'
  1270. ];
  1271. $this->assertHtml($expected, $result);
  1272. }
  1273. /**
  1274. * testFormSecurityMultipleSubmitButtons
  1275. *
  1276. * test form submit generation and ensure that _Token is only created on end()
  1277. *
  1278. * @return void
  1279. */
  1280. public function testFormSecurityMultipleSubmitButtons()
  1281. {
  1282. $this->Form->request->params['_Token'] = 'testKey';
  1283. $this->Form->create($this->article);
  1284. $this->Form->text('Address.title');
  1285. $this->Form->text('Address.first_name');
  1286. $result = $this->Form->submit('Save', ['name' => 'save']);
  1287. $expected = [
  1288. 'div' => ['class' => 'submit'],
  1289. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1290. '/div',
  1291. ];
  1292. $this->assertHtml($expected, $result);
  1293. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1294. $expected = [
  1295. 'div' => ['class' => 'submit'],
  1296. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1297. '/div',
  1298. ];
  1299. $this->assertHtml($expected, $result);
  1300. $result = $this->Form->end();
  1301. $tokenDebug = urlencode(json_encode([
  1302. '/articles/add',
  1303. [
  1304. 'Address.title',
  1305. 'Address.first_name',
  1306. ],
  1307. ['save', 'cancel']
  1308. ]));
  1309. $expected = [
  1310. 'div' => ['style' => 'display:none;'],
  1311. ['input' => [
  1312. 'type' => 'hidden',
  1313. 'name' => '_Token[fields]',
  1314. 'value'
  1315. ]],
  1316. ['input' => [
  1317. 'type' => 'hidden',
  1318. 'name' => '_Token[unlocked]',
  1319. 'value' => 'cancel%7Csave'
  1320. ]],
  1321. ['input' => [
  1322. 'type' => 'hidden', 'name' => '_Token[debug]',
  1323. 'value' => $tokenDebug,
  1324. ]],
  1325. '/div'
  1326. ];
  1327. $this->assertHtml($expected, $result);
  1328. }
  1329. /**
  1330. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1331. *
  1332. * @return void
  1333. */
  1334. public function testSecurityButtonNestedNamed()
  1335. {
  1336. $key = 'testKey';
  1337. $this->Form->request->params['_csrfToken'] = $key;
  1338. $this->Form->create('Addresses');
  1339. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1340. $result = $this->Form->unlockField();
  1341. $this->assertEquals(['Address.button'], $result);
  1342. }
  1343. /**
  1344. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1345. *
  1346. * @return void
  1347. */
  1348. public function testSecuritySubmitNestedNamed()
  1349. {
  1350. $this->Form->request->params['_Token'] = 'testKey';
  1351. $this->Form->create($this->article);
  1352. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1353. $result = $this->Form->unlockField();
  1354. $this->assertEquals(['Address.button'], $result);
  1355. }
  1356. /**
  1357. * Test that the correct fields are unlocked for image submits with no names.
  1358. *
  1359. * @return void
  1360. */
  1361. public function testSecuritySubmitImageNoName()
  1362. {
  1363. $key = 'testKey';
  1364. $this->Form->request->params['_Token'] = 'testKey';
  1365. $this->Form->create(false);
  1366. $result = $this->Form->submit('save.png');
  1367. $expected = [
  1368. 'div' => ['class' => 'submit'],
  1369. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1370. '/div'
  1371. ];
  1372. $this->assertHtml($expected, $result);
  1373. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1374. }
  1375. /**
  1376. * Test that the correct fields are unlocked for image submits with names.
  1377. *
  1378. * @return void
  1379. */
  1380. public function testSecuritySubmitImageName()
  1381. {
  1382. $this->Form->request->params['_Token'] = 'testKey';
  1383. $this->Form->create(null);
  1384. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1385. $expected = [
  1386. 'div' => ['class' => 'submit'],
  1387. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1388. '/div'
  1389. ];
  1390. $this->assertHtml($expected, $result);
  1391. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1392. }
  1393. /**
  1394. * testFormSecurityMultipleInputFields method
  1395. *
  1396. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1397. *
  1398. * @return void
  1399. */
  1400. public function testFormSecurityMultipleInputFields()
  1401. {
  1402. $this->Form->request->params['_Token'] = 'testKey';
  1403. $this->Form->create();
  1404. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1405. $this->Form->input('Addresses.0.title');
  1406. $this->Form->input('Addresses.0.first_name');
  1407. $this->Form->input('Addresses.0.last_name');
  1408. $this->Form->input('Addresses.0.address');
  1409. $this->Form->input('Addresses.0.city');
  1410. $this->Form->input('Addresses.0.phone');
  1411. $this->Form->input('Addresses.0.primary', ['type' => 'checkbox']);
  1412. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1413. $this->Form->input('Addresses.1.title');
  1414. $this->Form->input('Addresses.1.first_name');
  1415. $this->Form->input('Addresses.1.last_name');
  1416. $this->Form->input('Addresses.1.address');
  1417. $this->Form->input('Addresses.1.city');
  1418. $this->Form->input('Addresses.1.phone');
  1419. $this->Form->input('Addresses.1.primary', ['type' => 'checkbox']);
  1420. $result = $this->Form->secure($this->Form->fields);
  1421. $hash = '8bd3911b07b507408b1a969b31ee90c47b7d387e%3AAddresses.0.id%7CAddresses.1.id';
  1422. $tokenDebug = urlencode(json_encode([
  1423. '/articles/add',
  1424. [
  1425. 'Addresses.0.id' => '123456',
  1426. 'Addresses.0.title',
  1427. 'Addresses.0.first_name',
  1428. 'Addresses.0.last_name',
  1429. 'Addresses.0.address',
  1430. 'Addresses.0.city',
  1431. 'Addresses.0.phone',
  1432. 'Addresses.0.primary',
  1433. 'Addresses.1.id' => '654321',
  1434. 'Addresses.1.title',
  1435. 'Addresses.1.first_name',
  1436. 'Addresses.1.last_name',
  1437. 'Addresses.1.address',
  1438. 'Addresses.1.city',
  1439. 'Addresses.1.phone',
  1440. 'Addresses.1.primary',
  1441. ],
  1442. []
  1443. ]));
  1444. $expected = [
  1445. 'div' => ['style' => 'display:none;'],
  1446. ['input' => [
  1447. 'type' => 'hidden', 'name' => '_Token[fields]',
  1448. 'value' => $hash
  1449. ]],
  1450. ['input' => [
  1451. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1452. 'value' => ''
  1453. ]],
  1454. ['input' => [
  1455. 'type' => 'hidden', 'name' => '_Token[debug]',
  1456. 'value' => $tokenDebug,
  1457. ]],
  1458. '/div'
  1459. ];
  1460. $this->assertHtml($expected, $result);
  1461. }
  1462. /**
  1463. * Test form security with Model.field.0 style inputs
  1464. *
  1465. * @return void
  1466. */
  1467. public function testFormSecurityArrayFields()
  1468. {
  1469. $this->Form->request->params['_Token'] = 'testKey';
  1470. $this->Form->create();
  1471. $this->Form->text('Address.primary.1');
  1472. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1473. $this->Form->text('Address.secondary.1.0');
  1474. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1475. }
  1476. /**
  1477. * testFormSecurityMultipleInputDisabledFields method
  1478. *
  1479. * test secure form generation with multiple records and disabled fields.
  1480. *
  1481. * @return void
  1482. */
  1483. public function testFormSecurityMultipleInputDisabledFields()
  1484. {
  1485. $this->Form->request->params['_Token'] = [
  1486. 'unlockedFields' => ['first_name', 'address']
  1487. ];
  1488. $this->Form->create();
  1489. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1490. $this->Form->text('Addresses.0.title');
  1491. $this->Form->text('Addresses.0.first_name');
  1492. $this->Form->text('Addresses.0.last_name');
  1493. $this->Form->text('Addresses.0.address');
  1494. $this->Form->text('Addresses.0.city');
  1495. $this->Form->text('Addresses.0.phone');
  1496. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1497. $this->Form->text('Addresses.1.title');
  1498. $this->Form->text('Addresses.1.first_name');
  1499. $this->Form->text('Addresses.1.last_name');
  1500. $this->Form->text('Addresses.1.address');
  1501. $this->Form->text('Addresses.1.city');
  1502. $this->Form->text('Addresses.1.phone');
  1503. $result = $this->Form->secure($this->Form->fields);
  1504. $hash = '4fb10b46873df4ddd4ef5c3a19944a2f29b38991%3AAddresses.0.id%7CAddresses.1.id';
  1505. $tokenDebug = urlencode(json_encode([
  1506. '/articles/add',
  1507. [
  1508. 'Addresses.0.id' => '123456',
  1509. 'Addresses.0.title',
  1510. 'Addresses.0.last_name',
  1511. 'Addresses.0.city',
  1512. 'Addresses.0.phone',
  1513. 'Addresses.1.id' => '654321',
  1514. 'Addresses.1.title',
  1515. 'Addresses.1.last_name',
  1516. 'Addresses.1.city',
  1517. 'Addresses.1.phone'
  1518. ],
  1519. [
  1520. 'first_name',
  1521. 'address'
  1522. ]
  1523. ]));
  1524. $expected = [
  1525. 'div' => ['style' => 'display:none;'],
  1526. ['input' => [
  1527. 'type' => 'hidden',
  1528. 'name' => '_Token[fields]',
  1529. 'value' => $hash
  1530. ]],
  1531. ['input' => [
  1532. 'type' => 'hidden',
  1533. 'name' => '_Token[unlocked]',
  1534. 'value' => 'address%7Cfirst_name',
  1535. ]],
  1536. ['input' => [
  1537. 'type' => 'hidden', 'name' => '_Token[debug]',
  1538. 'value' => $tokenDebug,
  1539. ]],
  1540. '/div'
  1541. ];
  1542. $this->assertHtml($expected, $result);
  1543. }
  1544. /**
  1545. * testFormSecurityInputDisabledFields method
  1546. *
  1547. * Test single record form with disabled fields.
  1548. *
  1549. * @return void
  1550. */
  1551. public function testFormSecurityInputUnlockedFields()
  1552. {
  1553. $this->Form->request->params['_Token'] = [
  1554. 'unlockedFields' => ['first_name', 'address']
  1555. ];
  1556. $this->Form->create();
  1557. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1558. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1559. $this->Form->text('Addresses.title');
  1560. $this->Form->text('Addresses.first_name');
  1561. $this->Form->text('Addresses.last_name');
  1562. $this->Form->text('Addresses.address');
  1563. $this->Form->text('Addresses.city');
  1564. $this->Form->text('Addresses.phone');
  1565. $result = $this->Form->fields;
  1566. $expected = [
  1567. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1568. 'Addresses.city', 'Addresses.phone'
  1569. ];
  1570. $this->assertEquals($expected, $result);
  1571. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1572. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1573. $tokenDebug = urlencode(json_encode([
  1574. '/articles/add',
  1575. [
  1576. 'Addresses.id' => '123456',
  1577. 'Addresses.title',
  1578. 'Addresses.last_name',
  1579. 'Addresses.city',
  1580. 'Addresses.phone'
  1581. ],
  1582. [
  1583. 'first_name',
  1584. 'address'
  1585. ]
  1586. ]));
  1587. $expected = [
  1588. 'div' => ['style' => 'display:none;'],
  1589. ['input' => [
  1590. 'type' => 'hidden',
  1591. 'name' => '_Token[fields]',
  1592. 'value' => $hash,
  1593. 'data-foo' => 'bar',
  1594. ]],
  1595. ['input' => [
  1596. 'type' => 'hidden',
  1597. 'name' => '_Token[unlocked]',
  1598. 'value' => 'address%7Cfirst_name',
  1599. 'data-foo' => 'bar',
  1600. ]],
  1601. ['input' => [
  1602. 'type' => 'hidden', 'name' => '_Token[debug]',
  1603. 'value' => $tokenDebug,
  1604. 'data-foo' => 'bar'
  1605. ]],
  1606. '/div'
  1607. ];
  1608. $this->assertHtml($expected, $result);
  1609. }
  1610. /**
  1611. * testFormSecurityInputUnlockedFieldsDebugSecurityTrue method
  1612. *
  1613. * Test single record form with debugSecurity param.
  1614. *
  1615. * @return void
  1616. */
  1617. public function testFormSecurityInputUnlockedFieldsDebugSecurityTrue()
  1618. {
  1619. $this->Form->request->params['_Token'] = [
  1620. 'unlockedFields' => ['first_name', 'address']
  1621. ];
  1622. $this->Form->create();
  1623. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1624. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1625. $this->Form->text('Addresses.title');
  1626. $this->Form->text('Addresses.first_name');
  1627. $this->Form->text('Addresses.last_name');
  1628. $this->Form->text('Addresses.address');
  1629. $this->Form->text('Addresses.city');
  1630. $this->Form->text('Addresses.phone');
  1631. $result = $this->Form->fields;
  1632. $expected = [
  1633. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1634. 'Addresses.city', 'Addresses.phone'
  1635. ];
  1636. $this->assertEquals($expected, $result);
  1637. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1638. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1639. $tokenDebug = urlencode(json_encode([
  1640. '/articles/add',
  1641. [
  1642. 'Addresses.id' => '123456',
  1643. 'Addresses.title',
  1644. 'Addresses.last_name',
  1645. 'Addresses.city',
  1646. 'Addresses.phone'
  1647. ],
  1648. [
  1649. 'first_name',
  1650. 'address'
  1651. ]
  1652. ]));
  1653. $expected = [
  1654. 'div' => ['style' => 'display:none;'],
  1655. ['input' => [
  1656. 'type' => 'hidden',
  1657. 'name' => '_Token[fields]',
  1658. 'value' => $hash,
  1659. 'data-foo' => 'bar',
  1660. ]],
  1661. ['input' => [
  1662. 'type' => 'hidden',
  1663. 'name' => '_Token[unlocked]',
  1664. 'value' => 'address%7Cfirst_name',
  1665. 'data-foo' => 'bar',
  1666. ]],
  1667. ['input' => [
  1668. 'type' => 'hidden', 'name' => '_Token[debug]',
  1669. 'value' => $tokenDebug,
  1670. 'data-foo' => 'bar'
  1671. ]],
  1672. '/div'
  1673. ];
  1674. $this->assertHtml($expected, $result);
  1675. }
  1676. /**
  1677. * testFormSecurityInputUnlockedFieldsDebugSecurityFalse method
  1678. *
  1679. * Debug is false, debugSecurity is true -> no debug
  1680. *
  1681. * @return void
  1682. */
  1683. public function testFormSecurityInputUnlockedFieldsDebugSecurityDebugFalse()
  1684. {
  1685. $this->Form->request->params['_Token'] = [
  1686. 'unlockedFields' => ['first_name', 'address']
  1687. ];
  1688. $this->Form->create();
  1689. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1690. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1691. $this->Form->text('Addresses.title');
  1692. $this->Form->text('Addresses.first_name');
  1693. $this->Form->text('Addresses.last_name');
  1694. $this->Form->text('Addresses.address');
  1695. $this->Form->text('Addresses.city');
  1696. $this->Form->text('Addresses.phone');
  1697. $result = $this->Form->fields;
  1698. $expected = [
  1699. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1700. 'Addresses.city', 'Addresses.phone'
  1701. ];
  1702. $this->assertEquals($expected, $result);
  1703. Configure::write('debug', false);
  1704. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1705. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1706. $expected = [
  1707. 'div' => ['style' => 'display:none;'],
  1708. ['input' => [
  1709. 'type' => 'hidden',
  1710. 'name' => '_Token[fields]',
  1711. 'value' => $hash,
  1712. 'data-foo' => 'bar',
  1713. ]],
  1714. ['input' => [
  1715. 'type' => 'hidden',
  1716. 'name' => '_Token[unlocked]',
  1717. 'value' => 'address%7Cfirst_name',
  1718. 'data-foo' => 'bar',
  1719. ]],
  1720. '/div'
  1721. ];
  1722. $this->assertHtml($expected, $result);
  1723. }
  1724. /**
  1725. * testFormSecurityInputUnlockedFieldsDebugSecurityFalse method
  1726. *
  1727. * Test single record form with debugSecurity param.
  1728. *
  1729. * @return void
  1730. */
  1731. public function testFormSecurityInputUnlockedFieldsDebugSecurityFalse()
  1732. {
  1733. $this->Form->request->params['_Token'] = [
  1734. 'unlockedFields' => ['first_name', 'address']
  1735. ];
  1736. $this->Form->create();
  1737. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1738. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1739. $this->Form->text('Addresses.title');
  1740. $this->Form->text('Addresses.first_name');
  1741. $this->Form->text('Addresses.last_name');
  1742. $this->Form->text('Addresses.address');
  1743. $this->Form->text('Addresses.city');
  1744. $this->Form->text('Addresses.phone');
  1745. $result = $this->Form->fields;
  1746. $expected = [
  1747. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1748. 'Addresses.city', 'Addresses.phone'
  1749. ];
  1750. $this->assertEquals($expected, $result);
  1751. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1752. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1753. $expected = [
  1754. 'div' => ['style' => 'display:none;'],
  1755. ['input' => [
  1756. 'type' => 'hidden',
  1757. 'name' => '_Token[fields]',
  1758. 'value' => $hash,
  1759. 'data-foo' => 'bar',
  1760. ]],
  1761. ['input' => [
  1762. 'type' => 'hidden',
  1763. 'name' => '_Token[unlocked]',
  1764. 'value' => 'address%7Cfirst_name',
  1765. 'data-foo' => 'bar',
  1766. ]],
  1767. '/div'
  1768. ];
  1769. $this->assertHtml($expected, $result);
  1770. }
  1771. /**
  1772. * test securing inputs with custom name attributes.
  1773. *
  1774. * @return void
  1775. */
  1776. public function testFormSecureWithCustomNameAttribute()
  1777. {
  1778. $this->Form->request->params['_Token'] = 'testKey';
  1779. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1780. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1781. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1782. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1783. }
  1784. /**
  1785. * testFormSecuredInput method
  1786. *
  1787. * Test generation of entire secure form, assertions made on input() output.
  1788. *
  1789. * @return void
  1790. */
  1791. public function testFormSecuredInput()
  1792. {
  1793. $this->Form->request->params['_csrfToken'] = 'testKey';
  1794. $this->Form->request->params['_Token'] = 'stuff';
  1795. $this->article['schema'] = [
  1796. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1797. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1798. ];
  1799. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1800. $encoding = strtolower(Configure::read('App.encoding'));
  1801. $expected = [
  1802. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1803. 'div' => ['style' => 'display:none;'],
  1804. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1805. ['input' => [
  1806. 'type' => 'hidden',
  1807. 'name' => '_csrfToken',
  1808. 'value' => 'testKey'
  1809. ]],
  1810. '/div'
  1811. ];
  1812. $this->assertHtml($expected, $result);
  1813. $result = $this->Form->input('ratio');
  1814. $expected = [
  1815. 'div' => ['class'],
  1816. 'label' => ['for'],
  1817. 'Ratio',
  1818. '/label',
  1819. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1820. '/div',
  1821. ];
  1822. $this->assertHtml($expected, $result);
  1823. $result = $this->Form->input('population');
  1824. $expected = [
  1825. 'div' => ['class'],
  1826. 'label' => ['for'],
  1827. 'Population',
  1828. '/label',
  1829. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1830. '/div',
  1831. ];
  1832. $this->assertHtml($expected, $result);
  1833. $result = $this->Form->input('published', ['type' => 'text']);
  1834. $expected = [
  1835. 'div' => ['class' => 'input text'],
  1836. 'label' => ['for' => 'published'],
  1837. 'Published',
  1838. '/label',
  1839. ['input' => [
  1840. 'type' => 'text',
  1841. 'name' => 'published',
  1842. 'id' => 'published'
  1843. ]],
  1844. '/div'
  1845. ];
  1846. $this->assertHtml($expected, $result);
  1847. $result = $this->Form->input('other', ['type' => 'text']);
  1848. $expected = [
  1849. 'div' => ['class' => 'input text'],
  1850. 'label' => ['for' => 'other'],
  1851. 'Other',
  1852. '/label',
  1853. ['input' => [
  1854. 'type' => 'text',
  1855. 'name' => 'other',
  1856. 'id',
  1857. ]],
  1858. '/div'
  1859. ];
  1860. $this->assertHtml($expected, $result);
  1861. $result = $this->Form->hidden('stuff');
  1862. $expected = [
  1863. 'input' => [
  1864. 'type' => 'hidden',
  1865. 'name' => 'stuff'
  1866. ]
  1867. ];
  1868. $this->assertHtml($expected, $result);
  1869. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1870. $expected = ['input' => [
  1871. 'type' => 'hidden',
  1872. 'name' => 'hidden',
  1873. 'value' => '0'
  1874. ]];
  1875. $this->assertHtml($expected, $result);
  1876. $result = $this->Form->input('something', ['type' => 'checkbox']);
  1877. $expected = [
  1878. 'div' => ['class' => 'input checkbox'],
  1879. ['input' => [
  1880. 'type' => 'hidden',
  1881. 'name' => 'something',
  1882. 'value' => '0'
  1883. ]],
  1884. 'label' => ['for' => 'something'],
  1885. ['input' => [
  1886. 'type' => 'checkbox',
  1887. 'name' => 'something',
  1888. 'value' => '1',
  1889. 'id' => 'something'
  1890. ]],
  1891. 'Something',
  1892. '/label',
  1893. '/div'
  1894. ];
  1895. $this->assertHtml($expected, $result);
  1896. $result = $this->Form->fields;
  1897. $expectedFields = [
  1898. 'ratio',
  1899. 'population',
  1900. 'published',
  1901. 'other',
  1902. 'stuff' => '',
  1903. 'hidden' => '0',
  1904. 'something'
  1905. ];
  1906. $this->assertEquals($expectedFields, $result);
  1907. $result = $this->Form->secure($this->Form->fields);
  1908. $tokenDebug = urlencode(json_encode([
  1909. '/articles/add',
  1910. $expectedFields,
  1911. []
  1912. ]));
  1913. $expected = [
  1914. 'div' => ['style' => 'display:none;'],
  1915. ['input' => [
  1916. 'type' => 'hidden',
  1917. 'name' => '_Token[fields]',
  1918. 'value'
  1919. ]],
  1920. ['input' => [
  1921. 'type' => 'hidden',
  1922. 'name' => '_Token[unlocked]',
  1923. 'value' => ''
  1924. ]],
  1925. ['input' => [
  1926. 'type' => 'hidden', 'name' => '_Token[debug]',
  1927. 'value' => $tokenDebug,
  1928. ]],
  1929. '/div'
  1930. ];
  1931. $this->assertHtml($expected, $result);
  1932. }
  1933. /**
  1934. * Test secured inputs with custom names.
  1935. *
  1936. * @return void
  1937. */
  1938. public function testSecuredInputCustomName()
  1939. {
  1940. $this->Form->request->params['_Token'] = 'testKey';
  1941. $this->assertEquals([], $this->Form->fields);
  1942. $this->Form->text('text_input', [
  1943. 'name' => 'Option[General.default_role]',
  1944. ]);
  1945. $expected = ['Option.General.default_role'];
  1946. $this->assertEquals($expected, $this->Form->fields);
  1947. $this->Form->select('select_box', [1, 2], [
  1948. 'name' => 'Option[General.select_role]',
  1949. ]);
  1950. $expected[] = 'Option.General.select_role';
  1951. $this->assertEquals($expected, $this->Form->fields);
  1952. $this->Form->text('other.things[]');
  1953. $expected[] = 'other.things';
  1954. $this->assertEquals($expected, $this->Form->fields);
  1955. }
  1956. /**
  1957. * Test that a hidden field followed by a visible field
  1958. * undoes the hidden field locking.
  1959. *
  1960. * @return void
  1961. */
  1962. public function testSecuredInputDuplicate()
  1963. {
  1964. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  1965. $this->assertEquals([], $this->Form->fields);
  1966. $this->Form->input('text_val', [
  1967. 'type' => 'hidden',
  1968. 'value' => 'some text',
  1969. ]);
  1970. $expected = ['text_val' => 'some text'];
  1971. $this->assertEquals($expected, $this->Form->fields);
  1972. $this->Form->input('text_val', [
  1973. 'type' => 'text',
  1974. ]);
  1975. $expected = ['text_val'];
  1976. $this->assertEquals($expected, $this->Form->fields);
  1977. }
  1978. /**
  1979. * Tests that the correct keys are added to the field hash index
  1980. *
  1981. * @return void
  1982. */
  1983. public function testFormSecuredFileInput()
  1984. {
  1985. $this->assertEquals([], $this->Form->fields);
  1986. $this->Form->file('Attachment.file');
  1987. $expected = [
  1988. 'Attachment.file.name', 'Attachment.file.type',
  1989. 'Attachment.file.tmp_name', 'Attachment.file.error',
  1990. 'Attachment.file.size'
  1991. ];
  1992. $this->assertEquals($expected, $this->Form->fields);
  1993. }
  1994. /**
  1995. * test that multiple selects keys are added to field hash
  1996. *
  1997. * @return void
  1998. */
  1999. public function testFormSecuredMultipleSelect()
  2000. {
  2001. $this->Form->request->params['_csrfToken'] = 'testKey';
  2002. $this->assertEquals([], $this->Form->fields);
  2003. $options = ['1' => 'one', '2' => 'two'];
  2004. $this->Form->select('Model.select', $options);
  2005. $expected = ['Model.select'];
  2006. $this->assertEquals($expected, $this->Form->fields);
  2007. $this->Form->fields = [];
  2008. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2009. $this->assertEquals($expected, $this->Form->fields);
  2010. }
  2011. /**
  2012. * testFormSecuredRadio method
  2013. *
  2014. * @return void
  2015. */
  2016. public function testFormSecuredRadio()
  2017. {
  2018. $this->Form->request->params['_Token'] = 'testKey';
  2019. $this->assertEquals([], $this->Form->fields);
  2020. $options = ['1' => 'option1', '2' => 'option2'];
  2021. $this->Form->radio('Test.test', $options);
  2022. $expected = ['Test.test'];
  2023. $this->assertEquals($expected, $this->Form->fields);
  2024. $this->Form->radio('Test.all', $options, [
  2025. 'disabled' => ['option1', 'option2']
  2026. ]);
  2027. $expected = ['Test.test', 'Test.all' => ''];
  2028. $this->assertEquals($expected, $this->Form->fields);
  2029. $this->Form->radio('Test.some', $options, [
  2030. 'disabled' => ['option1']
  2031. ]);
  2032. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2033. $this->assertEquals($expected, $this->Form->fields);
  2034. }
  2035. /**
  2036. * Test that when disabled is in a list based attribute array it works.
  2037. *
  2038. * @return void
  2039. */
  2040. public function testFormSecuredAndDisabledNotAssoc()
  2041. {
  2042. $this->Form->request->params['_Token'] = 'testKey';
  2043. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2044. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2045. $this->Form->text('Model.text', ['disabled']);
  2046. $this->Form->textarea('Model.textarea', ['disabled']);
  2047. $this->Form->password('Model.password', ['disabled']);
  2048. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2049. $expected = [
  2050. 'Model.radio' => ''
  2051. ];
  2052. $this->assertEquals($expected, $this->Form->fields);
  2053. }
  2054. /**
  2055. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  2056. * hashing.
  2057. *
  2058. * @return void
  2059. */
  2060. public function testFormSecuredAndDisabled()
  2061. {
  2062. $this->Form->request->params['_Token'] = 'testKey';
  2063. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2064. $this->Form->text('Model.text', ['disabled' => true]);
  2065. $this->Form->password('Model.text', ['disabled' => true]);
  2066. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2067. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2068. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2069. $this->Form->year('Model.year', ['disabled' => true]);
  2070. $this->Form->month('Model.month', ['disabled' => true]);
  2071. $this->Form->day('Model.day', ['disabled' => true]);
  2072. $this->Form->hour('Model.hour', ['disabled' => true]);
  2073. $this->Form->minute('Model.minute', ['disabled' => true]);
  2074. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2075. $expected = [
  2076. 'Model.radio' => ''
  2077. ];
  2078. $this->assertEquals($expected, $this->Form->fields);
  2079. }
  2080. /**
  2081. * testDisableSecurityUsingForm method
  2082. *
  2083. * @return void
  2084. */
  2085. public function testDisableSecurityUsingForm()
  2086. {
  2087. $this->Form->request->params['_Token'] = [
  2088. 'disabledFields' => []
  2089. ];
  2090. $this->Form->create();
  2091. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2092. $this->Form->text('Addresses.title');
  2093. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2094. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2095. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2096. $result = $this->Form->fields;
  2097. $expected = [
  2098. 'Addresses.id' => '123456', 'Addresses.title',
  2099. ];
  2100. $this->assertEquals($expected, $result);
  2101. }
  2102. /**
  2103. * test disableField
  2104. *
  2105. * @return void
  2106. */
  2107. public function testUnlockFieldAddsToList()
  2108. {
  2109. $this->Form->request->params['_Token'] = [
  2110. 'unlockedFields' => []
  2111. ];
  2112. $this->Form->unlockField('Contact.name');
  2113. $this->Form->text('Contact.name');
  2114. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2115. $this->assertEquals([], $this->Form->fields);
  2116. }
  2117. /**
  2118. * test unlockField removing from fields array.
  2119. *
  2120. * @return void
  2121. */
  2122. public function testUnlockFieldRemovingFromFields()
  2123. {
  2124. $this->Form->request->params['_Token'] = [
  2125. 'unlockedFields' => []
  2126. ];
  2127. $this->Form->create($this->article);
  2128. $this->Form->hidden('Article.id', ['value' => 1]);
  2129. $this->Form->text('Article.title');
  2130. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2131. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  2132. $this->Form->unlockField('Article.title');
  2133. $this->Form->unlockField('Article.id');
  2134. $this->assertEquals([], $this->Form->fields);
  2135. }
  2136. /**
  2137. * test reset unlockFields, when create new form.
  2138. *
  2139. * @return void
  2140. */
  2141. public function testResetUnlockFields()
  2142. {
  2143. $this->Form->request->params['_Token'] = [
  2144. 'key' => 'testKey',
  2145. 'unlockedFields' => []
  2146. ];
  2147. $this->Form->unlockField('Contact.id');
  2148. $this->Form->create('Contact');
  2149. $this->Form->hidden('Contact.id', ['value' => 1]);
  2150. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2151. $this->Form->end();
  2152. $this->Form->create('Contact');
  2153. $this->Form->hidden('Contact.id', ['value' => 1]);
  2154. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2155. }
  2156. /**
  2157. * Test that only the path + query elements of a form's URL show up in their hash.
  2158. *
  2159. * @return void
  2160. */
  2161. public function testSecuredFormUrlIgnoresHost()
  2162. {
  2163. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2164. $expected = '0ff0c85cd70584d8fd18fa136846d22c66c21e2d%3A';
  2165. $this->Form->create($this->article, [
  2166. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2167. ]);
  2168. $result = $this->Form->secure();
  2169. $this->assertContains($expected, $result);
  2170. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2171. $result = $this->Form->secure();
  2172. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2173. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2174. $result = $this->Form->secure();
  2175. $this->assertContains($expected, $result, 'URL path + query should work.');
  2176. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2177. $result = $this->Form->secure();
  2178. $this->assertNotContains($expected, $result, 'URL is different');
  2179. }
  2180. /**
  2181. * Test that URL, HTML and identifer show up in their hashs.
  2182. *
  2183. * @return void
  2184. */
  2185. public function testSecuredFormUrlHasHtmlAndIdentifer()
  2186. {
  2187. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2188. $expected = 'ece0693fb1b19ca116133db1832ac29baaf41ce5%3A';
  2189. $res = $this->Form->create($this->article, [
  2190. 'url' => [
  2191. 'controller' => 'articles',
  2192. 'action' => 'view',
  2193. '?' => [
  2194. 'page' => 1,
  2195. 'limit' => 10,
  2196. 'html' => '<>"',
  2197. ],
  2198. '#' => 'result',
  2199. ],
  2200. ]);
  2201. $result = $this->Form->secure();
  2202. $this->assertContains($expected, $result);
  2203. $this->Form->create($this->article, [
  2204. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2205. ]);
  2206. $result = $this->Form->secure();
  2207. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2208. $this->Form->create($this->article, [
  2209. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2210. ]);
  2211. $result = $this->Form->secure();
  2212. $this->assertContains($expected, $result, 'URL path + query should work.');
  2213. }
  2214. /**
  2215. * test error message display
  2216. *
  2217. * @return void
  2218. */
  2219. public function testErrorMessageDisplay()
  2220. {
  2221. $this->article['errors'] = [
  2222. 'Article' => [
  2223. 'title' => 'error message',
  2224. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2225. ]
  2226. ];
  2227. $this->Form->create($this->article);
  2228. $result = $this->Form->input('Article.title');
  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' => ['class' => 'error-message']],
  2239. 'error message',
  2240. '/div',
  2241. '/div'
  2242. ];
  2243. $this->assertHtml($expected, $result);
  2244. $result = $this->Form->input('Article.title', [
  2245. 'templates' => [
  2246. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2247. ]
  2248. ]);
  2249. $expected = [
  2250. 'div' => ['class' => 'input text error'],
  2251. 'label' => ['for' => 'article-title'],
  2252. 'Title',
  2253. '/label',
  2254. 'input' => [
  2255. 'type' => 'text', 'name' => 'Article[title]',
  2256. 'id' => 'article-title', 'class' => 'form-error'
  2257. ],
  2258. '/div'
  2259. ];
  2260. $this->assertHtml($expected, $result);
  2261. $result = $this->Form->input('Article.content');
  2262. $expected = [
  2263. 'div' => ['class' => 'input text error'],
  2264. 'label' => ['for' => 'article-content'],
  2265. 'Content',
  2266. '/label',
  2267. 'input' => [
  2268. 'type' => 'text', 'name' => 'Article[content]',
  2269. 'id' => 'article-content', 'class' => 'form-error'
  2270. ],
  2271. ['div' => ['class' => 'error-message']],
  2272. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2273. '/div',
  2274. '/div'
  2275. ];
  2276. $this->assertHtml($expected, $result);
  2277. $result = $this->Form->input('Article.content', ['error' => ['escape' => true]]);
  2278. $expected = [
  2279. 'div' => ['class' => 'input text error'],
  2280. 'label' => ['for' => 'article-content'],
  2281. 'Content',
  2282. '/label',
  2283. 'input' => [
  2284. 'type' => 'text', 'name' => 'Article[content]',
  2285. 'id' => 'article-content', 'class' => 'form-error'
  2286. ],
  2287. ['div' => ['class' => 'error-message']],
  2288. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2289. '/div',
  2290. '/div'
  2291. ];
  2292. $this->assertHtml($expected, $result);
  2293. $result = $this->Form->input('Article.content', ['error' => ['escape' => false]]);
  2294. $expected = [
  2295. 'div' => ['class' => 'input text error'],
  2296. 'label' => ['for' => 'article-content'],
  2297. 'Content',
  2298. '/label',
  2299. 'input' => [
  2300. 'type' => 'text', 'name' => 'Article[content]',
  2301. 'id' => 'article-content', 'class' => 'form-error'
  2302. ],
  2303. ['div' => ['class' => 'error-message']],
  2304. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2305. '/div',
  2306. '/div'
  2307. ];
  2308. $this->assertHtml($expected, $result);
  2309. }
  2310. /**
  2311. * Test validation errors, when validation message is an empty string.
  2312. *
  2313. * @return void
  2314. */
  2315. public function testEmptyErrorValidation()
  2316. {
  2317. $this->article['errors'] = [
  2318. 'Article' => ['title' => '']
  2319. ];
  2320. $this->Form->create($this->article);
  2321. $result = $this->Form->input('Article.title');
  2322. $expected = [
  2323. 'div' => ['class' => 'input text error'],
  2324. 'label' => ['for' => 'article-title'],
  2325. 'Title',
  2326. '/label',
  2327. 'input' => [
  2328. 'type' => 'text', 'name' => 'Article[title]',
  2329. 'id' => 'article-title', 'class' => 'form-error'
  2330. ],
  2331. ['div' => ['class' => 'error-message']],
  2332. [],
  2333. '/div',
  2334. '/div'
  2335. ];
  2336. $this->assertHtml($expected, $result);
  2337. }
  2338. /**
  2339. * Test validation errors, when calling input() overriding validation message by an empty string.
  2340. *
  2341. * @return void
  2342. */
  2343. public function testEmptyInputErrorValidation()
  2344. {
  2345. $this->article['errors'] = [
  2346. 'Article' => ['title' => 'error message']
  2347. ];
  2348. $this->Form->create($this->article);
  2349. $result = $this->Form->input('Article.title', ['error' => '']);
  2350. $expected = [
  2351. 'div' => ['class' => 'input text error'],
  2352. 'label' => ['for' => 'article-title'],
  2353. 'Title',
  2354. '/label',
  2355. 'input' => [
  2356. 'type' => 'text', 'name' => 'Article[title]',
  2357. 'id' => 'article-title', 'class' => 'form-error'
  2358. ],
  2359. ['div' => ['class' => 'error-message']],
  2360. [],
  2361. '/div',
  2362. '/div'
  2363. ];
  2364. $this->assertHtml($expected, $result);
  2365. }
  2366. /**
  2367. * Test validation errors, when calling input() overriding validation messages
  2368. *
  2369. * @return void
  2370. */
  2371. public function testInputErrorMessage()
  2372. {
  2373. $this->article['errors'] = [
  2374. 'title' => ['error message']
  2375. ];
  2376. $this->Form->create($this->article);
  2377. $result = $this->Form->input('title', [
  2378. 'error' => 'Custom error!'
  2379. ]);
  2380. $expected = [
  2381. 'div' => ['class' => 'input text required error'],
  2382. 'label' => ['for' => 'title'],
  2383. 'Title',
  2384. '/label',
  2385. 'input' => [
  2386. 'type' => 'text', 'name' => 'title',
  2387. 'id' => 'title', 'class' => 'form-error',
  2388. 'required' => 'required',
  2389. ],
  2390. ['div' => ['class' => 'error-message']],
  2391. 'Custom error!',
  2392. '/div',
  2393. '/div'
  2394. ];
  2395. $this->assertHtml($expected, $result);
  2396. $result = $this->Form->input('title', [
  2397. 'error' => ['error message' => 'Custom error!']
  2398. ]);
  2399. $expected = [
  2400. 'div' => ['class' => 'input text required error'],
  2401. 'label' => ['for' => 'title'],
  2402. 'Title',
  2403. '/label',
  2404. 'input' => [
  2405. 'type' => 'text',
  2406. 'name' => 'title',
  2407. 'id' => 'title',
  2408. 'class' => 'form-error',
  2409. 'required' => 'required'
  2410. ],
  2411. ['div' => ['class' => 'error-message']],
  2412. 'Custom error!',
  2413. '/div',
  2414. '/div'
  2415. ];
  2416. $this->assertHtml($expected, $result);
  2417. }
  2418. /**
  2419. * Tests displaying errors for nested entities
  2420. *
  2421. * @return void
  2422. */
  2423. public function testFormValidationAssociated()
  2424. {
  2425. $nested = new Entity(['foo' => 'bar']);
  2426. $nested->errors('foo', ['not a valid bar']);
  2427. $entity = new Entity(['nested' => $nested]);
  2428. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2429. $result = $this->Form->error('nested.foo');
  2430. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2431. }
  2432. /**
  2433. * testFormValidationAssociatedSecondLevel method
  2434. *
  2435. * test form error display with associated model.
  2436. *
  2437. * @return void
  2438. */
  2439. public function testFormValidationAssociatedSecondLevel()
  2440. {
  2441. $inner = new Entity(['bar' => 'baz']);
  2442. $nested = new Entity(['foo' => $inner]);
  2443. $entity = new Entity(['nested' => $nested]);
  2444. $inner->errors('bar', ['not a valid one']);
  2445. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2446. $result = $this->Form->error('nested.foo.bar');
  2447. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2448. }
  2449. /**
  2450. * testFormValidationMultiRecord method
  2451. *
  2452. * test form error display with multiple records.
  2453. *
  2454. * @return void
  2455. */
  2456. public function testFormValidationMultiRecord()
  2457. {
  2458. $one = new Entity();
  2459. $two = new Entity();
  2460. TableRegistry::get('Contacts', [
  2461. 'className' => __NAMESPACE__ . '\ContactsTable'
  2462. ]);
  2463. $one->set('email', '');
  2464. $one->errors('email', ['invalid email']);
  2465. $two->set('name', '');
  2466. $two->errors('name', ['This is wrong']);
  2467. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2468. $result = $this->Form->input('0.email');
  2469. $expected = [
  2470. 'div' => ['class' => 'input email error'],
  2471. 'label' => ['for' => '0-email'],
  2472. 'Email',
  2473. '/label',
  2474. 'input' => [
  2475. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2476. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2477. ],
  2478. ['div' => ['class' => 'error-message']],
  2479. 'invalid email',
  2480. '/div',
  2481. '/div'
  2482. ];
  2483. $this->assertHtml($expected, $result);
  2484. $result = $this->Form->input('1.name');
  2485. $expected = [
  2486. 'div' => ['class' => 'input text error'],
  2487. 'label' => ['for' => '1-name'],
  2488. 'Name',
  2489. '/label',
  2490. 'input' => [
  2491. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2492. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2493. ],
  2494. ['div' => ['class' => 'error-message']],
  2495. 'This is wrong',
  2496. '/div',
  2497. '/div'
  2498. ];
  2499. $this->assertHtml($expected, $result);
  2500. }
  2501. /**
  2502. * testInput method
  2503. *
  2504. * Test various incarnations of input().
  2505. *
  2506. * @return void
  2507. */
  2508. public function testInput()
  2509. {
  2510. TableRegistry::get('ValidateUsers', [
  2511. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2512. ]);
  2513. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2514. $result = $this->Form->input('ValidateUsers.balance');
  2515. $expected = [
  2516. 'div' => ['class'],
  2517. 'label' => ['for'],
  2518. 'Balance',
  2519. '/label',
  2520. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2521. '/div',
  2522. ];
  2523. $this->assertHtml($expected, $result);
  2524. $result = $this->Form->input('ValidateUser.cost_decimal');
  2525. $expected = [
  2526. 'div' => ['class'],
  2527. 'label' => ['for'],
  2528. 'Cost Decimal',
  2529. '/label',
  2530. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2531. '/div',
  2532. ];
  2533. $this->assertHtml($expected, $result);
  2534. $result = $this->Form->input('ValidateUser.null_decimal');
  2535. $expected = [
  2536. 'div' => ['class'],
  2537. 'label' => ['for'],
  2538. 'Null Decimal',
  2539. '/label',
  2540. 'input' => ['name', 'type' => 'number', 'id'],
  2541. '/div',
  2542. ];
  2543. $this->assertHtml($expected, $result);
  2544. }
  2545. /**
  2546. * Tests the input method and passing custom options
  2547. *
  2548. * @return void
  2549. */
  2550. public function testInputCustomization()
  2551. {
  2552. TableRegistry::get('Contacts', [
  2553. 'className' => __NAMESPACE__ . '\ContactsTable'
  2554. ]);
  2555. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2556. $result = $this->Form->input('Contact.email', ['id' => 'custom']);
  2557. $expected = [
  2558. 'div' => ['class' => 'input email'],
  2559. 'label' => ['for' => 'custom'],
  2560. 'Email',
  2561. '/label',
  2562. ['input' => [
  2563. 'type' => 'email', 'name' => 'Contact[email]',
  2564. 'id' => 'custom', 'maxlength' => 255
  2565. ]],
  2566. '/div'
  2567. ];
  2568. $this->assertHtml($expected, $result);
  2569. $result = $this->Form->input('Contact.email', [
  2570. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2571. ]);
  2572. $expected = [
  2573. '<div',
  2574. 'label' => ['for' => 'contact-email'],
  2575. 'Email',
  2576. '/label',
  2577. ['input' => [
  2578. 'type' => 'email', 'name' => 'Contact[email]',
  2579. 'id' => 'contact-email', 'maxlength' => 255
  2580. ]],
  2581. '/div'
  2582. ];
  2583. $this->assertHtml($expected, $result);
  2584. $result = $this->Form->input('Contact.email', ['type' => 'text']);
  2585. $expected = [
  2586. 'div' => ['class' => 'input text'],
  2587. 'label' => ['for' => 'contact-email'],
  2588. 'Email',
  2589. '/label',
  2590. ['input' => [
  2591. 'type' => 'text', 'name' => 'Contact[email]',
  2592. 'id' => 'contact-email', 'maxlength' => '255'
  2593. ]],
  2594. '/div'
  2595. ];
  2596. $this->assertHtml($expected, $result);
  2597. $result = $this->Form->input('Contact.5.email', ['type' => 'text']);
  2598. $expected = [
  2599. 'div' => ['class' => 'input text'],
  2600. 'label' => ['for' => 'contact-5-email'],
  2601. 'Email',
  2602. '/label',
  2603. ['input' => [
  2604. 'type' => 'text', 'name' => 'Contact[5][email]',
  2605. 'id' => 'contact-5-email', 'maxlength' => '255'
  2606. ]],
  2607. '/div'
  2608. ];
  2609. $this->assertHtml($expected, $result);
  2610. $result = $this->Form->input('Contact.password');
  2611. $expected = [
  2612. 'div' => ['class' => 'input password'],
  2613. 'label' => ['for' => 'contact-password'],
  2614. 'Password',
  2615. '/label',
  2616. ['input' => [
  2617. 'type' => 'password', 'name' => 'Contact[password]',
  2618. 'id' => 'contact-password'
  2619. ]],
  2620. '/div'
  2621. ];
  2622. $this->assertHtml($expected, $result);
  2623. $result = $this->Form->input('Contact.email', [
  2624. 'type' => 'file', 'class' => 'textbox'
  2625. ]);
  2626. $expected = [
  2627. 'div' => ['class' => 'input file'],
  2628. 'label' => ['for' => 'contact-email'],
  2629. 'Email',
  2630. '/label',
  2631. ['input' => [
  2632. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2633. 'id' => 'contact-email'
  2634. ]],
  2635. '/div'
  2636. ];
  2637. $this->assertHtml($expected, $result);
  2638. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2639. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2640. $result = $this->Form->input('phone');
  2641. $expected = [
  2642. 'div' => ['class' => 'input tel'],
  2643. 'label' => ['for' => 'phone'],
  2644. 'Phone',
  2645. '/label',
  2646. ['input' => [
  2647. 'type' => 'tel', 'name' => 'phone',
  2648. 'value' => 'Hello &amp; World &gt; weird chars',
  2649. 'id' => 'phone', 'maxlength' => 255
  2650. ]],
  2651. '/div'
  2652. ];
  2653. $this->assertHtml($expected, $result);
  2654. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2655. $this->Form->create();
  2656. $result = $this->Form->input('Model.0.OtherModel.field', ['id' => 'myId']);
  2657. $expected = [
  2658. 'div' => ['class' => 'input text'],
  2659. 'label' => ['for' => 'myId'],
  2660. 'Field',
  2661. '/label',
  2662. 'input' => [
  2663. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2664. 'value' => 'My value', 'id' => 'myId'
  2665. ],
  2666. '/div'
  2667. ];
  2668. $this->assertHtml($expected, $result);
  2669. $this->Form->request->data = [];
  2670. $entity->errors('field', 'Badness!');
  2671. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2672. $result = $this->Form->input('field');
  2673. $expected = [
  2674. 'div' => ['class' => 'input text error'],
  2675. 'label' => ['for' => 'field'],
  2676. 'Field',
  2677. '/label',
  2678. 'input' => [
  2679. 'type' => 'text', 'name' => 'field',
  2680. 'id' => 'field', 'class' => 'form-error'
  2681. ],
  2682. ['div' => ['class' => 'error-message']],
  2683. 'Badness!',
  2684. '/div',
  2685. '/div'
  2686. ];
  2687. $this->assertHtml($expected, $result);
  2688. $result = $this->Form->input('field', [
  2689. 'templates' => [
  2690. 'inputContainerError' => '{{content}}{{error}}',
  2691. 'error' => '<span class="error-message">{{content}}</span>'
  2692. ]
  2693. ]);
  2694. $expected = [
  2695. 'label' => ['for' => 'field'],
  2696. 'Field',
  2697. '/label',
  2698. 'input' => [
  2699. 'type' => 'text', 'name' => 'field',
  2700. 'id' => 'field', 'class' => 'form-error'
  2701. ],
  2702. ['span' => ['class' => 'error-message']],
  2703. 'Badness!',
  2704. '/span'
  2705. ];
  2706. $this->assertHtml($expected, $result);
  2707. $entity->errors('field', ['minLength'], true);
  2708. $result = $this->Form->input('field', [
  2709. 'error' => [
  2710. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2711. 'maxLength' => 'login too large'
  2712. ]
  2713. ]);
  2714. $expected = [
  2715. 'div' => ['class' => 'input text error'],
  2716. 'label' => ['for' => 'field'],
  2717. 'Field',
  2718. '/label',
  2719. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2720. ['div' => ['class' => 'error-message']],
  2721. 'Le login doit contenir au moins 2 caractères',
  2722. '/div',
  2723. '/div'
  2724. ];
  2725. $this->assertHtml($expected, $result);
  2726. $entity->errors('field', ['maxLength'], true);
  2727. $result = $this->Form->input('field', [
  2728. 'error' => [
  2729. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2730. 'maxLength' => 'login too large',
  2731. ]
  2732. ]);
  2733. $expected = [
  2734. 'div' => ['class' => 'input text error'],
  2735. 'label' => ['for' => 'field'],
  2736. 'Field',
  2737. '/label',
  2738. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2739. ['div' => ['class' => 'error-message']],
  2740. 'login too large',
  2741. '/div',
  2742. '/div'
  2743. ];
  2744. $this->assertHtml($expected, $result);
  2745. }
  2746. /**
  2747. * Test that input() accepts a template file.
  2748. *
  2749. * @return void
  2750. */
  2751. public function testInputWithTemplateFile()
  2752. {
  2753. $result = $this->Form->input('field', [
  2754. 'templates' => 'htmlhelper_tags'
  2755. ]);
  2756. $expected = [
  2757. 'label' => ['for' => 'field'],
  2758. 'Field',
  2759. '/label',
  2760. 'input' => [
  2761. 'type' => 'text', 'name' => 'field',
  2762. 'id' => 'field'
  2763. ],
  2764. ];
  2765. $this->assertHtml($expected, $result);
  2766. }
  2767. /**
  2768. * Test that nested inputs end with brackets
  2769. *
  2770. * @return void
  2771. */
  2772. public function testNestedInputsEndWithBrackets()
  2773. {
  2774. $result = $this->Form->text('nested.text[]');
  2775. $expected = [
  2776. 'input' => [
  2777. 'type' => 'text', 'name' => 'nested[text][]'
  2778. ],
  2779. ];
  2780. $result = $this->Form->file('nested.file[]');
  2781. $expected = [
  2782. 'input' => [
  2783. 'type' => 'file', 'name' => 'nested[file][]'
  2784. ],
  2785. ];
  2786. $this->assertHtml($expected, $result);
  2787. }
  2788. /**
  2789. * Test id prefix
  2790. *
  2791. * @return void
  2792. */
  2793. public function testCreateIdPrefix()
  2794. {
  2795. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2796. $result = $this->Form->input('field');
  2797. $expected = [
  2798. 'div' => ['class' => 'input text'],
  2799. 'label' => ['for' => 'prefix-field'],
  2800. 'Field',
  2801. '/label',
  2802. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2803. '/div'
  2804. ];
  2805. $this->assertHtml($expected, $result);
  2806. $result = $this->Form->input('field', ['id' => 'custom-id']);
  2807. $expected = [
  2808. 'div' => ['class' => 'input text'],
  2809. 'label' => ['for' => 'custom-id'],
  2810. 'Field',
  2811. '/label',
  2812. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2813. '/div'
  2814. ];
  2815. $this->assertHtml($expected, $result);
  2816. $result = $this->Form->radio('Model.field', ['option A']);
  2817. $expected = [
  2818. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2819. 'label' => ['for' => 'prefix-model-field-0'],
  2820. ['input' => [
  2821. 'type' => 'radio',
  2822. 'name' => 'Model[field]',
  2823. 'value' => '0',
  2824. 'id' => 'prefix-model-field-0'
  2825. ]],
  2826. 'option A',
  2827. '/label'
  2828. ];
  2829. $this->assertHtml($expected, $result);
  2830. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2831. $expected = [
  2832. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2833. 'label' => ['for' => 'prefix-model-field-0'],
  2834. ['input' => [
  2835. 'type' => 'radio',
  2836. 'name' => 'Model[field]',
  2837. 'value' => '0',
  2838. 'id' => 'prefix-model-field-0'
  2839. ]],
  2840. 'option A',
  2841. '/label'
  2842. ];
  2843. $this->assertHtml($expected, $result);
  2844. $result = $this->Form->select(
  2845. 'Model.multi_field',
  2846. ['first'],
  2847. ['multiple' => 'checkbox']
  2848. );
  2849. $expected = [
  2850. 'input' => [
  2851. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2852. ],
  2853. ['div' => ['class' => 'checkbox']],
  2854. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2855. ['input' => [
  2856. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2857. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2858. ]],
  2859. 'first',
  2860. '/label',
  2861. '/div',
  2862. ];
  2863. $this->assertHtml($expected, $result);
  2864. $this->Form->end();
  2865. $result = $this->Form->input('field');
  2866. $expected = [
  2867. 'div' => ['class' => 'input text'],
  2868. 'label' => ['for' => 'field'],
  2869. 'Field',
  2870. '/label',
  2871. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2872. '/div'
  2873. ];
  2874. $this->assertHtml($expected, $result);
  2875. }
  2876. /**
  2877. * Test that inputs with 0 can be created.
  2878. *
  2879. * @return void
  2880. */
  2881. public function testInputZero()
  2882. {
  2883. TableRegistry::get('Contacts', [
  2884. 'className' => __NAMESPACE__ . '\ContactsTable'
  2885. ]);
  2886. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2887. $result = $this->Form->input('0');
  2888. $expected = [
  2889. 'div' => ['class' => 'input text'],
  2890. 'label' => ['for' => '0'], '/label',
  2891. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2892. '/div'
  2893. ];
  2894. $this->assertHtml($expected, $result);
  2895. }
  2896. /**
  2897. * test input() with checkbox creation
  2898. *
  2899. * @return void
  2900. */
  2901. public function testInputCheckbox()
  2902. {
  2903. $result = $this->Form->input('User.active', ['label' => false, 'checked' => true]);
  2904. $expected = [
  2905. 'div' => ['class' => 'input checkbox'],
  2906. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2907. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2908. '/div'
  2909. ];
  2910. $this->assertHtml($expected, $result);
  2911. $result = $this->Form->input('User.active', ['label' => false, 'checked' => 1]);
  2912. $expected = [
  2913. 'div' => ['class' => 'input checkbox'],
  2914. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2915. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2916. '/div'
  2917. ];
  2918. $this->assertHtml($expected, $result);
  2919. $result = $this->Form->input('User.active', ['label' => false, 'checked' => '1']);
  2920. $expected = [
  2921. 'div' => ['class' => 'input checkbox'],
  2922. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2923. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2924. '/div'
  2925. ];
  2926. $this->assertHtml($expected, $result);
  2927. $result = $this->Form->input('User.disabled', [
  2928. 'label' => 'Disabled',
  2929. 'type' => 'checkbox',
  2930. 'data-foo' => 'disabled'
  2931. ]);
  2932. $expected = [
  2933. 'div' => ['class' => 'input checkbox'],
  2934. 'input' => ['type' => 'hidden', 'name' => 'User[disabled]', 'value' => '0'],
  2935. 'label' => ['for' => 'user-disabled'],
  2936. ['input' => [
  2937. 'type' => 'checkbox',
  2938. 'name' => 'User[disabled]',
  2939. 'value' => '1',
  2940. 'id' => 'user-disabled',
  2941. 'data-foo' => 'disabled'
  2942. ]],
  2943. 'Disabled',
  2944. '/label',
  2945. '/div'
  2946. ];
  2947. $this->assertHtml($expected, $result);
  2948. $result = $this->Form->input('User.confirm', [
  2949. 'label' => 'Confirm <b>me</b>!',
  2950. 'type' => 'checkbox',
  2951. 'escape' => false
  2952. ]);
  2953. $expected = [
  2954. 'div' => ['class' => 'input checkbox'],
  2955. 'input' => ['type' => 'hidden', 'name' => 'User[confirm]', 'value' => '0'],
  2956. 'label' => ['for' => 'user-confirm'],
  2957. ['input' => [
  2958. 'type' => 'checkbox',
  2959. 'name' => 'User[confirm]',
  2960. 'value' => '1',
  2961. 'id' => 'user-confirm',
  2962. ]],
  2963. 'Confirm <b>me</b>!',
  2964. '/label',
  2965. '/div'
  2966. ];
  2967. $this->assertHtml($expected, $result);
  2968. }
  2969. /**
  2970. * Test that input() does not create wrapping div and label tag for hidden fields
  2971. *
  2972. * @return void
  2973. */
  2974. public function testInputHidden()
  2975. {
  2976. TableRegistry::get('ValidateUsers', [
  2977. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2978. ]);
  2979. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2980. $result = $this->Form->input('ValidateUser.id');
  2981. $expected = [
  2982. 'input' => ['name', 'type' => 'hidden', 'id']
  2983. ];
  2984. $this->assertHtml($expected, $result);
  2985. $result = $this->Form->input('ValidateUser.custom', ['type' => 'hidden']);
  2986. $expected = [
  2987. 'input' => ['name', 'type' => 'hidden', 'id']
  2988. ];
  2989. $this->assertHtml($expected, $result);
  2990. }
  2991. /**
  2992. * test form->input() with datetime
  2993. *
  2994. * @return void
  2995. */
  2996. public function testInputDatetime()
  2997. {
  2998. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  2999. ->setMethods(['datetime'])
  3000. ->setConstructorArgs([new View()])
  3001. ->getMock();
  3002. $this->Form->expects($this->once())->method('datetime')
  3003. ->with('prueba', [
  3004. 'type' => 'datetime',
  3005. 'timeFormat' => 24,
  3006. 'minYear' => 2008,
  3007. 'maxYear' => 2011,
  3008. 'interval' => 15,
  3009. 'options' => null,
  3010. 'empty' => false,
  3011. 'id' => 'prueba',
  3012. 'required' => false,
  3013. 'templateVars' => []
  3014. ])
  3015. ->will($this->returnValue('This is it!'));
  3016. $result = $this->Form->input('prueba', [
  3017. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3018. 'maxYear' => 2011, 'interval' => 15
  3019. ]);
  3020. $expected = [
  3021. 'div' => ['class' => 'input datetime'],
  3022. '<label',
  3023. 'Prueba',
  3024. '/label',
  3025. 'This is it!',
  3026. '/div'
  3027. ];
  3028. $this->assertHtml($expected, $result);
  3029. }
  3030. /**
  3031. * test form->input() with datetime with id prefix
  3032. *
  3033. * @return void
  3034. */
  3035. public function testInputDatetimeIdPrefix()
  3036. {
  3037. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3038. ->setMethods(['datetime'])
  3039. ->setConstructorArgs([new View()])
  3040. ->getMock();
  3041. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3042. $this->Form->expects($this->once())->method('datetime')
  3043. ->with('prueba', [
  3044. 'type' => 'datetime',
  3045. 'timeFormat' => 24,
  3046. 'minYear' => 2008,
  3047. 'maxYear' => 2011,
  3048. 'interval' => 15,
  3049. 'options' => null,
  3050. 'empty' => false,
  3051. 'id' => 'prefix-prueba',
  3052. 'required' => false,
  3053. 'templateVars' => []
  3054. ])
  3055. ->will($this->returnValue('This is it!'));
  3056. $result = $this->Form->input('prueba', [
  3057. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3058. 'maxYear' => 2011, 'interval' => 15
  3059. ]);
  3060. $expected = [
  3061. 'div' => ['class' => 'input datetime'],
  3062. '<label',
  3063. 'Prueba',
  3064. '/label',
  3065. 'This is it!',
  3066. '/div'
  3067. ];
  3068. $this->assertHtml($expected, $result);
  3069. }
  3070. /**
  3071. * Test generating checkboxes with disabled elements.
  3072. *
  3073. * @return void
  3074. */
  3075. public function testInputCheckboxWithDisabledElements()
  3076. {
  3077. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3078. $result = $this->Form->input('Contact.multiple', [
  3079. 'multiple' => 'checkbox',
  3080. 'disabled' => 'disabled',
  3081. 'options' => $options
  3082. ]);
  3083. $expected = [
  3084. ['div' => ['class' => 'input select']],
  3085. ['label' => ['for' => "contact-multiple"]],
  3086. 'Multiple',
  3087. '/label',
  3088. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'disabled' => 'disabled', 'value' => '']],
  3089. ['div' => ['class' => 'checkbox']],
  3090. ['label' => ['for' => "contact-multiple-1"]],
  3091. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "contact-multiple-1"]],
  3092. 'One',
  3093. '/label',
  3094. '/div',
  3095. ['div' => ['class' => 'checkbox']],
  3096. ['label' => ['for' => "contact-multiple-2"]],
  3097. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "contact-multiple-2"]],
  3098. 'Two',
  3099. '/label',
  3100. '/div',
  3101. ['div' => ['class' => 'checkbox']],
  3102. ['label' => ['for' => "contact-multiple-3"]],
  3103. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "contact-multiple-3"]],
  3104. 'Three',
  3105. '/label',
  3106. '/div',
  3107. '/div'
  3108. ];
  3109. $this->assertHtml($expected, $result);
  3110. // make sure 50 does only disable 50, and not 50f5c0cf
  3111. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3112. $disabled = [50];
  3113. $expected = [
  3114. ['div' => ['class' => 'input select']],
  3115. ['label' => ['for' => "contact-multiple"]],
  3116. 'Multiple',
  3117. '/label',
  3118. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '']],
  3119. ['div' => ['class' => 'checkbox']],
  3120. ['label' => ['for' => "contact-multiple-50"]],
  3121. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "contact-multiple-50"]],
  3122. 'Fifty',
  3123. '/label',
  3124. '/div',
  3125. ['div' => ['class' => 'checkbox']],
  3126. ['label' => ['for' => "contact-multiple-50f5c0cf"]],
  3127. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "contact-multiple-50f5c0cf"]],
  3128. 'Stringy',
  3129. '/label',
  3130. '/div',
  3131. '/div'
  3132. ];
  3133. $result = $this->Form->input('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3134. $this->assertHtml($expected, $result);
  3135. }
  3136. /**
  3137. * test input name with leading integer, ensure attributes are generated correctly.
  3138. *
  3139. * @return void
  3140. */
  3141. public function testInputWithLeadingInteger()
  3142. {
  3143. $result = $this->Form->text('0.Node.title');
  3144. $expected = [
  3145. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3146. ];
  3147. $this->assertHtml($expected, $result);
  3148. }
  3149. /**
  3150. * test form->input() with select type inputs.
  3151. *
  3152. * @return void
  3153. */
  3154. public function testInputSelectType()
  3155. {
  3156. $result = $this->Form->input(
  3157. 'email',
  3158. [
  3159. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3160. );
  3161. $expected = [
  3162. 'div' => ['class' => 'input select'],
  3163. 'label' => ['for' => 'email'],
  3164. 'Email',
  3165. '/label',
  3166. ['select' => ['name' => 'email', 'id' => 'email']],
  3167. ['option' => ['value' => '']],
  3168. '/option',
  3169. ['option' => ['value' => 'è']],
  3170. 'Firést',
  3171. '/option',
  3172. ['option' => ['value' => 'é']],
  3173. 'Secoènd',
  3174. '/option',
  3175. '/select',
  3176. '/div'
  3177. ];
  3178. $this->assertHtml($expected, $result);
  3179. $result = $this->Form->input(
  3180. 'email',
  3181. [
  3182. 'options' => ['First', 'Second'], 'empty' => true]
  3183. );
  3184. $expected = [
  3185. 'div' => ['class' => 'input select'],
  3186. 'label' => ['for' => 'email'],
  3187. 'Email',
  3188. '/label',
  3189. ['select' => ['name' => 'email', 'id' => 'email']],
  3190. ['option' => ['value' => '']],
  3191. '/option',
  3192. ['option' => ['value' => '0']],
  3193. 'First',
  3194. '/option',
  3195. ['option' => ['value' => '1']],
  3196. 'Second',
  3197. '/option',
  3198. '/select',
  3199. '/div'
  3200. ];
  3201. $this->assertHtml($expected, $result);
  3202. $result = $this->Form->input('email', [
  3203. 'type' => 'select',
  3204. 'options' => new \ArrayObject(['First', 'Second']),
  3205. 'empty' => true
  3206. ]);
  3207. $this->assertHtml($expected, $result);
  3208. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3209. $this->Form->request->data = ['Model' => ['user_id' => 'value']];
  3210. $result = $this->Form->input('Model.user_id', ['empty' => true]);
  3211. $expected = [
  3212. 'div' => ['class' => 'input select'],
  3213. 'label' => ['for' => 'model-user-id'],
  3214. 'User',
  3215. '/label',
  3216. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3217. ['option' => ['value' => '']],
  3218. '/option',
  3219. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3220. 'good',
  3221. '/option',
  3222. ['option' => ['value' => 'other']],
  3223. 'bad',
  3224. '/option',
  3225. '/select',
  3226. '/div'
  3227. ];
  3228. $this->assertHtml($expected, $result);
  3229. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3230. $this->Form->request->data = ['Thing' => ['user_id' => null]];
  3231. $result = $this->Form->input('Thing.user_id', ['empty' => 'Some Empty']);
  3232. $expected = [
  3233. 'div' => ['class' => 'input select'],
  3234. 'label' => ['for' => 'thing-user-id'],
  3235. 'User',
  3236. '/label',
  3237. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3238. ['option' => ['value' => '']],
  3239. 'Some Empty',
  3240. '/option',
  3241. ['option' => ['value' => 'value']],
  3242. 'good',
  3243. '/option',
  3244. ['option' => ['value' => 'other']],
  3245. 'bad',
  3246. '/option',
  3247. '/select',
  3248. '/div'
  3249. ];
  3250. $this->assertHtml($expected, $result);
  3251. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3252. $this->Form->request->data = ['Thing' => ['user_id' => 'value']];
  3253. $result = $this->Form->input('Thing.user_id', ['empty' => 'Some Empty']);
  3254. $expected = [
  3255. 'div' => ['class' => 'input select'],
  3256. 'label' => ['for' => 'thing-user-id'],
  3257. 'User',
  3258. '/label',
  3259. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3260. ['option' => ['value' => '']],
  3261. 'Some Empty',
  3262. '/option',
  3263. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3264. 'good',
  3265. '/option',
  3266. ['option' => ['value' => 'other']],
  3267. 'bad',
  3268. '/option',
  3269. '/select',
  3270. '/div'
  3271. ];
  3272. $this->assertHtml($expected, $result);
  3273. $this->Form->data = [];
  3274. $result = $this->Form->input('Publisher.id', [
  3275. 'label' => 'Publisher',
  3276. 'type' => 'select',
  3277. 'multiple' => 'checkbox',
  3278. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3279. ]);
  3280. $expected = [
  3281. ['div' => ['class' => 'input select']],
  3282. ['label' => ['for' => 'publisher-id']],
  3283. 'Publisher',
  3284. '/label',
  3285. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3286. ['div' => ['class' => 'checkbox']],
  3287. ['label' => ['for' => 'publisher-id-value-1']],
  3288. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3289. 'Label 1',
  3290. '/label',
  3291. '/div',
  3292. ['div' => ['class' => 'checkbox']],
  3293. ['label' => ['for' => 'publisher-id-value-2']],
  3294. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3295. 'Label 2',
  3296. '/label',
  3297. '/div',
  3298. '/div'
  3299. ];
  3300. $this->assertHtml($expected, $result);
  3301. }
  3302. /**
  3303. * test that input() and a non standard primary key makes a hidden input by default.
  3304. *
  3305. * @return void
  3306. */
  3307. public function testInputWithNonStandardPrimaryKeyMakesHidden()
  3308. {
  3309. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3310. $this->Form->create($this->article);
  3311. $result = $this->Form->input('title');
  3312. $expected = [
  3313. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3314. ];
  3315. $this->assertHtml($expected, $result);
  3316. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3317. $this->Form->create($this->article);
  3318. $result = $this->Form->input('title');
  3319. $expected = [
  3320. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3321. ];
  3322. $this->assertHtml($expected, $result);
  3323. $result = $this->Form->input('body');
  3324. $expected = [
  3325. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3326. ];
  3327. $this->assertHtml($expected, $result);
  3328. }
  3329. /**
  3330. * test that overriding the magic select type widget is possible
  3331. *
  3332. * @return void
  3333. */
  3334. public function testInputOverridingMagicSelectType()
  3335. {
  3336. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3337. $result = $this->Form->input('Model.user_id', ['type' => 'text']);
  3338. $expected = [
  3339. 'div' => ['class' => 'input text'],
  3340. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3341. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3342. '/div'
  3343. ];
  3344. $this->assertHtml($expected, $result);
  3345. //Check that magic types still work for plural/singular vars
  3346. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3347. $result = $this->Form->input('Model.type');
  3348. $expected = [
  3349. 'div' => ['class' => 'input select'],
  3350. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3351. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3352. ['option' => ['value' => 'value']], 'good', '/option',
  3353. ['option' => ['value' => 'other']], 'bad', '/option',
  3354. '/select',
  3355. '/div'
  3356. ];
  3357. $this->assertHtml($expected, $result);
  3358. }
  3359. /**
  3360. * Test that inferred types do not override developer input
  3361. *
  3362. * @return void
  3363. */
  3364. public function testInputMagicTypeDoesNotOverride()
  3365. {
  3366. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3367. $result = $this->Form->input('Model.user', ['type' => 'checkbox']);
  3368. $expected = [
  3369. 'div' => ['class' => 'input checkbox'],
  3370. ['input' => [
  3371. 'type' => 'hidden',
  3372. 'name' => 'Model[user]',
  3373. 'value' => 0,
  3374. ]],
  3375. 'label' => ['for' => 'model-user'],
  3376. ['input' => [
  3377. 'name' => 'Model[user]',
  3378. 'type' => 'checkbox',
  3379. 'id' => 'model-user',
  3380. 'value' => 1
  3381. ]],
  3382. 'User',
  3383. '/label',
  3384. '/div'
  3385. ];
  3386. $this->assertHtml($expected, $result);
  3387. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3388. $options = [
  3389. 1 => 'blue',
  3390. 2 => 'red'
  3391. ];
  3392. $result = $this->Form->input('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3393. $expected = [
  3394. 'div' => ['class' => 'input select'],
  3395. 'label' => ['for' => 'tags-ids'],
  3396. 'Tags',
  3397. '/label',
  3398. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3399. ['div' => ['class' => 'checkbox']],
  3400. ['label' => ['for' => 'tags-ids-1']],
  3401. ['input' => [
  3402. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3403. 'value' => '1', 'name' => 'tags[_ids][]'
  3404. ]],
  3405. 'blue',
  3406. '/label',
  3407. '/div',
  3408. ['div' => ['class' => 'checkbox']],
  3409. ['label' => ['for' => 'tags-ids-2']],
  3410. ['input' => [
  3411. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3412. 'value' => '2', 'name' => 'tags[_ids][]'
  3413. ]],
  3414. 'red',
  3415. '/label',
  3416. '/div',
  3417. '/div'
  3418. ];
  3419. $this->assertHtml($expected, $result);
  3420. }
  3421. /**
  3422. * Test that magic input() selects are created for type=number
  3423. *
  3424. * @return void
  3425. */
  3426. public function testInputMagicSelectForTypeNumber()
  3427. {
  3428. TableRegistry::get('ValidateUsers', [
  3429. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3430. ]);
  3431. $entity = new Entity(['balance' => 1]);
  3432. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3433. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3434. $result = $this->Form->input('balance');
  3435. $expected = [
  3436. 'div' => ['class' => 'input select'],
  3437. 'label' => ['for' => 'balance'],
  3438. 'Balance',
  3439. '/label',
  3440. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3441. ['option' => ['value' => '0']],
  3442. 'nothing',
  3443. '/option',
  3444. ['option' => ['value' => '1', 'selected' => 'selected']],
  3445. 'some',
  3446. '/option',
  3447. ['option' => ['value' => '100']],
  3448. 'a lot',
  3449. '/option',
  3450. '/select',
  3451. '/div'
  3452. ];
  3453. $this->assertHtml($expected, $result);
  3454. }
  3455. /**
  3456. * Test invalid 'input' type option to input() function.
  3457. *
  3458. * @expectedException \RuntimeException
  3459. * @expectedExceptionMessage Invalid type 'input' used for field 'text'
  3460. * @return void
  3461. */
  3462. public function testInvalidInputTypeOption()
  3463. {
  3464. $this->Form->input('text', ['type' => 'input']);
  3465. }
  3466. /**
  3467. * Test that magic input() selects can easily be converted into radio types without error.
  3468. *
  3469. * @return void
  3470. */
  3471. public function testInputMagicSelectChangeToRadio()
  3472. {
  3473. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3474. $result = $this->Form->input('Model.user_id', ['type' => 'radio']);
  3475. $this->assertContains('input type="radio"', $result);
  3476. }
  3477. /**
  3478. * testFormInputSubmit method
  3479. *
  3480. * test correct results for form::input() and type submit.
  3481. *
  3482. * @return void
  3483. */
  3484. public function testFormInputSubmit()
  3485. {
  3486. $result = $this->Form->input('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3487. $expected = [
  3488. 'div' => ['class' => 'submit'],
  3489. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3490. '/div'
  3491. ];
  3492. $this->assertHtml($expected, $result);
  3493. }
  3494. /**
  3495. * testFormInputs method
  3496. *
  3497. * test correct results from form::inputs().
  3498. *
  3499. * @return void
  3500. */
  3501. public function testFormInputsLegendFieldset()
  3502. {
  3503. $this->Form->create($this->article);
  3504. $result = $this->Form->allInputs([], ['legend' => 'The Legend']);
  3505. $expected = [
  3506. '<fieldset',
  3507. '<legend',
  3508. 'The Legend',
  3509. '/legend',
  3510. '*/fieldset',
  3511. ];
  3512. $this->assertHtml($expected, $result);
  3513. $result = $this->Form->allInputs([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3514. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3515. $this->assertContains('<fieldset>', $result);
  3516. $result = $this->Form->allInputs([], ['fieldset' => false, 'legend' => false]);
  3517. $this->assertNotContains('<legend>', $result);
  3518. $this->assertNotContains('<fieldset>', $result);
  3519. $result = $this->Form->allInputs([], ['fieldset' => false, 'legend' => 'Hello']);
  3520. $this->assertNotContains('<legend>', $result);
  3521. $this->assertNotContains('<fieldset>', $result);
  3522. $this->Form->create($this->article);
  3523. $this->Form->request->params['prefix'] = 'admin';
  3524. $this->Form->request->params['action'] = 'admin_edit';
  3525. $this->Form->request->params['controller'] = 'articles';
  3526. $result = $this->Form->allInputs();
  3527. $expected = [
  3528. '<fieldset',
  3529. '<legend',
  3530. 'New Article',
  3531. '/legend',
  3532. '*/fieldset',
  3533. ];
  3534. $this->assertHtml($expected, $result);
  3535. $this->Form->create($this->article);
  3536. $result = $this->Form->allInputs([], ['fieldset' => [], 'legend' => 'The Legend']);
  3537. $expected = [
  3538. '<fieldset',
  3539. '<legend',
  3540. 'The Legend',
  3541. '/legend',
  3542. '*/fieldset',
  3543. ];
  3544. $this->assertHtml($expected, $result);
  3545. $this->Form->create($this->article);
  3546. $result = $this->Form->allInputs([], [
  3547. 'fieldset' => [
  3548. 'class' => 'some-class some-other-class',
  3549. 'disabled' => true,
  3550. 'data-param' => 'a-param'
  3551. ],
  3552. 'legend' => 'The Legend'
  3553. ]);
  3554. $expected = [
  3555. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3556. '<legend',
  3557. 'The Legend',
  3558. '/legend',
  3559. '*/fieldset',
  3560. ];
  3561. $this->assertHtml($expected, $result);
  3562. }
  3563. /**
  3564. * Test the inputs() method.
  3565. *
  3566. * @return void
  3567. */
  3568. public function testFormInputs()
  3569. {
  3570. $this->Form->create($this->article);
  3571. $result = $this->Form->allInputs();
  3572. $expected = [
  3573. '<fieldset',
  3574. '<legend', 'New Article', '/legend',
  3575. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3576. ['div' => ['class' => 'input select required']],
  3577. '*/div',
  3578. ['div' => ['class' => 'input text required']],
  3579. '*/div',
  3580. ['div' => ['class' => 'input text']],
  3581. '*/div',
  3582. ['div' => ['class' => 'input text']],
  3583. '*/div',
  3584. '/fieldset',
  3585. ];
  3586. $this->assertHtml($expected, $result);
  3587. $result = $this->Form->allInputs([
  3588. 'published' => ['type' => 'boolean']
  3589. ]);
  3590. $expected = [
  3591. '<fieldset',
  3592. '<legend', 'New Article', '/legend',
  3593. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3594. ['div' => ['class' => 'input select required']],
  3595. '*/div',
  3596. ['div' => ['class' => 'input text required']],
  3597. '*/div',
  3598. ['div' => ['class' => 'input text']],
  3599. '*/div',
  3600. ['div' => ['class' => 'input boolean']],
  3601. '*/div',
  3602. '/fieldset',
  3603. ];
  3604. $this->assertHtml($expected, $result);
  3605. $this->Form->create($this->article);
  3606. $result = $this->Form->allInputs([], ['legend' => 'Hello']);
  3607. $expected = [
  3608. 'fieldset' => [],
  3609. 'legend' => [],
  3610. 'Hello',
  3611. '/legend',
  3612. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3613. ['div' => ['class' => 'input select required']],
  3614. '*/div',
  3615. ['div' => ['class' => 'input text required']],
  3616. '*/div',
  3617. ['div' => ['class' => 'input text']],
  3618. '*/div',
  3619. ['div' => ['class' => 'input text']],
  3620. '*/div',
  3621. '/fieldset'
  3622. ];
  3623. $this->assertHtml($expected, $result);
  3624. $this->Form->create(false);
  3625. $expected = [
  3626. 'fieldset' => [],
  3627. ['div' => ['class' => 'input text']],
  3628. 'label' => ['for' => 'foo'],
  3629. 'Foo',
  3630. '/label',
  3631. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3632. '*/div',
  3633. '/fieldset'
  3634. ];
  3635. $result = $this->Form->allInputs(
  3636. ['foo' => ['type' => 'text']],
  3637. ['legend' => false]
  3638. );
  3639. $this->assertHtml($expected, $result);
  3640. }
  3641. /**
  3642. * testFormInputsBlacklist
  3643. *
  3644. * @return void
  3645. */
  3646. public function testFormInputsBlacklist()
  3647. {
  3648. $this->Form->create($this->article);
  3649. $result = $this->Form->allInputs([
  3650. 'id' => false
  3651. ]);
  3652. $expected = [
  3653. '<fieldset',
  3654. '<legend', 'New Article', '/legend',
  3655. ['div' => ['class' => 'input select required']],
  3656. '*/div',
  3657. ['div' => ['class' => 'input text required']],
  3658. '*/div',
  3659. ['div' => ['class' => 'input text']],
  3660. '*/div',
  3661. ['div' => ['class' => 'input text']],
  3662. '*/div',
  3663. '/fieldset',
  3664. ];
  3665. $this->assertHtml($expected, $result);
  3666. $this->Form->create($this->article);
  3667. $result = $this->Form->allInputs([
  3668. 'id' => []
  3669. ]);
  3670. $expected = [
  3671. '<fieldset',
  3672. '<legend', 'New Article', '/legend',
  3673. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3674. ['div' => ['class' => 'input select required']],
  3675. '*/div',
  3676. ['div' => ['class' => 'input text required']],
  3677. '*/div',
  3678. ['div' => ['class' => 'input text']],
  3679. '*/div',
  3680. ['div' => ['class' => 'input text']],
  3681. '*/div',
  3682. '/fieldset',
  3683. ];
  3684. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3685. }
  3686. /**
  3687. * testSelectAsCheckbox method
  3688. *
  3689. * test multi-select widget with checkbox formatting.
  3690. *
  3691. * @return void
  3692. */
  3693. public function testSelectAsCheckbox()
  3694. {
  3695. $result = $this->Form->select(
  3696. 'Model.multi_field',
  3697. ['first', 'second', 'third'],
  3698. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3699. );
  3700. $expected = [
  3701. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3702. ['div' => ['class' => 'checkbox']],
  3703. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3704. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3705. 'first',
  3706. '/label',
  3707. '/div',
  3708. ['div' => ['class' => 'checkbox']],
  3709. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3710. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3711. 'second',
  3712. '/label',
  3713. '/div',
  3714. ['div' => ['class' => 'checkbox']],
  3715. ['label' => ['for' => 'model-multi-field-2']],
  3716. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3717. 'third',
  3718. '/label',
  3719. '/div',
  3720. ];
  3721. $this->assertHtml($expected, $result);
  3722. $result = $this->Form->select(
  3723. 'Model.multi_field',
  3724. ['1/2' => 'half'],
  3725. ['multiple' => 'checkbox']
  3726. );
  3727. $expected = [
  3728. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3729. ['div' => ['class' => 'checkbox']],
  3730. ['label' => ['for' => 'model-multi-field-1-2']],
  3731. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3732. 'half',
  3733. '/label',
  3734. '/div',
  3735. ];
  3736. $this->assertHtml($expected, $result);
  3737. }
  3738. /**
  3739. * testLabel method
  3740. *
  3741. * test label generation.
  3742. *
  3743. * @return void
  3744. */
  3745. public function testLabel()
  3746. {
  3747. $result = $this->Form->label('Person.name');
  3748. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3749. $this->assertHtml($expected, $result);
  3750. $result = $this->Form->label('Person.name');
  3751. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3752. $this->assertHtml($expected, $result);
  3753. $result = $this->Form->label('Person.first_name');
  3754. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3755. $this->assertHtml($expected, $result);
  3756. $result = $this->Form->label('Person.first_name', 'Your first name');
  3757. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3758. $this->assertHtml($expected, $result);
  3759. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3760. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3761. $this->assertHtml($expected, $result);
  3762. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3763. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3764. $this->assertHtml($expected, $result);
  3765. $result = $this->Form->label('Person.first_name', '');
  3766. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3767. $this->assertHtml($expected, $result);
  3768. $result = $this->Form->label('Person.2.name', '');
  3769. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3770. $this->assertHtml($expected, $result);
  3771. }
  3772. /**
  3773. * Test that label() can accept an input with the correct template vars.
  3774. *
  3775. * @return void
  3776. */
  3777. public function testLabelContainInput()
  3778. {
  3779. $this->Form->templates([
  3780. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3781. ]);
  3782. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3783. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3784. ]);
  3785. $expected = [
  3786. 'label' => ['for' => 'person-accept-terms'],
  3787. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3788. 'Accept',
  3789. '/label',
  3790. ];
  3791. $this->assertHtml($expected, $result);
  3792. }
  3793. /**
  3794. * testTextbox method
  3795. *
  3796. * test textbox element generation
  3797. *
  3798. * @return void
  3799. */
  3800. public function testTextbox()
  3801. {
  3802. $result = $this->Form->text('Model.field');
  3803. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3804. $this->assertHtml($expected, $result);
  3805. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3806. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3807. $this->assertHtml($expected, $result);
  3808. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3809. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3810. $this->assertHtml($expected, $result);
  3811. }
  3812. /**
  3813. * Test that text() hooks up with request data and error fields.
  3814. *
  3815. * @return void
  3816. */
  3817. public function testTextBoxDataAndError()
  3818. {
  3819. $this->article['errors'] = [
  3820. 'Contact' => ['text' => 'wrong']
  3821. ];
  3822. $this->Form->create($this->article);
  3823. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3824. $result = $this->Form->text('Model.text');
  3825. $expected = [
  3826. 'input' => [
  3827. 'type' => 'text',
  3828. 'name' => 'Model[text]',
  3829. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3830. ]
  3831. ];
  3832. $this->assertHtml($expected, $result);
  3833. $this->Form->request->data['Contact']['text'] = 'test';
  3834. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3835. $expected = [
  3836. 'input' => [
  3837. 'type' => 'text',
  3838. 'name' => 'Contact[text]',
  3839. 'value' => 'test',
  3840. 'id' => 'theID',
  3841. 'class' => 'form-error'
  3842. ]
  3843. ];
  3844. $this->assertHtml($expected, $result);
  3845. }
  3846. /**
  3847. * testDefaultValue method
  3848. *
  3849. * Test default value setting
  3850. *
  3851. * @return void
  3852. */
  3853. public function testTextDefaultValue()
  3854. {
  3855. $this->Form->request->data['Model']['field'] = 'test';
  3856. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3857. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3858. $this->assertHtml($expected, $result);
  3859. unset($this->Form->request->data['Model']['field']);
  3860. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3861. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3862. $this->assertHtml($expected, $result);
  3863. $Articles = TableRegistry::get('Articles');
  3864. $title = $Articles->schema()->column('title');
  3865. $Articles->schema()->addColumn(
  3866. 'title',
  3867. ['default' => 'default title'] + $title
  3868. );
  3869. $entity = $Articles->newEntity();
  3870. $this->Form->create($entity);
  3871. // Get default value from schema
  3872. $result = $this->Form->text('title');
  3873. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  3874. $this->assertHtml($expected, $result);
  3875. // Don't get value from schema
  3876. $result = $this->Form->text('title', ['schemaDefault' => false]);
  3877. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  3878. $this->assertHtml($expected, $result);
  3879. // Custom default value overrides default value from schema
  3880. $result = $this->Form->text('title', ['default' => 'override default']);
  3881. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  3882. $this->assertHtml($expected, $result);
  3883. // Default value from schema is used only for new entities.
  3884. $entity->isNew(false);
  3885. $result = $this->Form->text('title');
  3886. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  3887. $this->assertHtml($expected, $result);
  3888. }
  3889. /**
  3890. * testError method
  3891. *
  3892. * Test field error generation
  3893. *
  3894. * @return void
  3895. */
  3896. public function testError()
  3897. {
  3898. $this->article['errors'] = [
  3899. 'Article' => ['field' => 'email']
  3900. ];
  3901. $this->Form->create($this->article);
  3902. $result = $this->Form->error('Article.field');
  3903. $expected = [
  3904. ['div' => ['class' => 'error-message']],
  3905. 'email',
  3906. '/div',
  3907. ];
  3908. $this->assertHtml($expected, $result);
  3909. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  3910. $expected = [
  3911. ['div' => ['class' => 'error-message']],
  3912. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3913. '/div',
  3914. ];
  3915. $this->assertHtml($expected, $result);
  3916. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  3917. $expected = [
  3918. ['div' => ['class' => 'error-message']],
  3919. '<strong', 'Badness!', '/strong',
  3920. '/div',
  3921. ];
  3922. $this->assertHtml($expected, $result);
  3923. }
  3924. /**
  3925. * test error translation can use rule names for translating.
  3926. *
  3927. * @return void
  3928. */
  3929. public function testErrorRuleName()
  3930. {
  3931. $this->article['errors'] = [
  3932. 'Article' => [
  3933. 'field' => ['email' => 'Your email was not good']
  3934. ]
  3935. ];
  3936. $this->Form->create($this->article);
  3937. $result = $this->Form->error('Article.field');
  3938. $expected = [
  3939. ['div' => ['class' => 'error-message']],
  3940. 'Your email was not good',
  3941. '/div',
  3942. ];
  3943. $this->assertHtml($expected, $result);
  3944. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  3945. $expected = [
  3946. ['div' => ['class' => 'error-message']],
  3947. 'Email in use',
  3948. '/div',
  3949. ];
  3950. $this->assertHtml($expected, $result);
  3951. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  3952. $expected = [
  3953. ['div' => ['class' => 'error-message']],
  3954. 'Email in use',
  3955. '/div',
  3956. ];
  3957. $this->assertHtml($expected, $result);
  3958. $result = $this->Form->error('Article.field', [
  3959. 'email' => 'Key is preferred',
  3960. 'Your email was not good' => 'Email in use'
  3961. ]);
  3962. $expected = [
  3963. ['div' => ['class' => 'error-message']],
  3964. 'Key is preferred',
  3965. '/div',
  3966. ];
  3967. $this->assertHtml($expected, $result);
  3968. }
  3969. /**
  3970. * Test error with nested lists.
  3971. *
  3972. * @return void
  3973. */
  3974. public function testErrorMessages()
  3975. {
  3976. $this->article['errors'] = [
  3977. 'Article' => ['field' => 'email']
  3978. ];
  3979. $this->Form->create($this->article);
  3980. $result = $this->Form->error('Article.field', [
  3981. 'email' => 'No good!'
  3982. ]);
  3983. $expected = [
  3984. 'div' => ['class' => 'error-message'],
  3985. 'No good!',
  3986. '/div'
  3987. ];
  3988. $this->assertHtml($expected, $result);
  3989. }
  3990. /**
  3991. * test error() with multiple messages.
  3992. *
  3993. * @return void
  3994. */
  3995. public function testErrorMultipleMessages()
  3996. {
  3997. $this->article['errors'] = [
  3998. 'field' => ['notBlank', 'email', 'Something else']
  3999. ];
  4000. $this->Form->create($this->article);
  4001. $result = $this->Form->error('field', [
  4002. 'notBlank' => 'Cannot be empty',
  4003. 'email' => 'No good!'
  4004. ]);
  4005. $expected = [
  4006. 'div' => ['class' => 'error-message'],
  4007. 'ul' => [],
  4008. '<li', 'Cannot be empty', '/li',
  4009. '<li', 'No good!', '/li',
  4010. '<li', 'Something else', '/li',
  4011. '/ul',
  4012. '/div'
  4013. ];
  4014. $this->assertHtml($expected, $result);
  4015. }
  4016. /**
  4017. * testPassword method
  4018. *
  4019. * Test password element generation
  4020. *
  4021. * @return void
  4022. */
  4023. public function testPassword()
  4024. {
  4025. $this->article['errors'] = [
  4026. 'Contact' => [
  4027. 'passwd' => 1
  4028. ]
  4029. ];
  4030. $this->Form->create($this->article);
  4031. $result = $this->Form->password('Contact.field');
  4032. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4033. $this->assertHtml($expected, $result);
  4034. $this->Form->request->data['Contact']['passwd'] = 'test';
  4035. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4036. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4037. $this->assertHtml($expected, $result);
  4038. }
  4039. /**
  4040. * testRadio method
  4041. *
  4042. * Test radio element set generation
  4043. *
  4044. * @return void
  4045. */
  4046. public function testRadio()
  4047. {
  4048. $result = $this->Form->radio('Model.field', ['option A']);
  4049. $expected = [
  4050. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4051. 'label' => ['for' => 'model-field-0'],
  4052. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4053. 'option A',
  4054. '/label'
  4055. ];
  4056. $this->assertHtml($expected, $result);
  4057. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4058. $this->assertHtml($expected, $result);
  4059. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4060. $expected = [
  4061. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4062. ['label' => ['for' => 'model-field-0']],
  4063. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4064. 'option A',
  4065. '/label',
  4066. ['label' => ['for' => 'model-field-1']],
  4067. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4068. 'option B',
  4069. '/label',
  4070. ];
  4071. $this->assertHtml($expected, $result);
  4072. $result = $this->Form->radio(
  4073. 'Employee.gender',
  4074. ['male' => 'Male', 'female' => 'Female'],
  4075. ['form' => 'my-form']
  4076. );
  4077. $expected = [
  4078. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4079. ['label' => ['for' => 'employee-gender-male']],
  4080. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4081. 'Male',
  4082. '/label',
  4083. ['label' => ['for' => 'employee-gender-female']],
  4084. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4085. 'Female',
  4086. '/label',
  4087. ];
  4088. $this->assertHtml($expected, $result);
  4089. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4090. $expected = [
  4091. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4092. ['label' => ['for' => 'model-custom-0']],
  4093. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4094. 'option A',
  4095. '/label',
  4096. ['label' => ['for' => 'model-custom-1']],
  4097. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4098. 'option B',
  4099. '/label',
  4100. ];
  4101. $this->assertHtml($expected, $result);
  4102. $result = $this->Form->radio(
  4103. 'Employee.gender',
  4104. [
  4105. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4106. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4107. ]
  4108. );
  4109. $expected = [
  4110. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4111. ['label' => ['for' => 'employee-gender-male']],
  4112. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4113. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4114. 'Male',
  4115. '/label',
  4116. ['label' => ['for' => 'employee-gender-female']],
  4117. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4118. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4119. 'Female',
  4120. '/label',
  4121. ];
  4122. $this->assertHtml($expected, $result);
  4123. }
  4124. /**
  4125. * Test default value setting on radio() method
  4126. *
  4127. * @return void
  4128. */
  4129. public function testRadioDefaultValue()
  4130. {
  4131. $Articles = TableRegistry::get('Articles');
  4132. $title = $Articles->schema()->column('title');
  4133. $Articles->schema()->addColumn(
  4134. 'title',
  4135. ['default' => '1'] + $title
  4136. );
  4137. $this->Form->create($Articles->newEntity());
  4138. $result = $this->Form->radio('title', ['option A', 'option B']);
  4139. $expected = [
  4140. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4141. ['label' => ['for' => 'title-0']],
  4142. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4143. 'option A',
  4144. '/label',
  4145. ['label' => ['for' => 'title-1']],
  4146. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4147. 'option B',
  4148. '/label',
  4149. ];
  4150. $this->assertHtml($expected, $result);
  4151. }
  4152. /**
  4153. * Test that input works with radio types
  4154. *
  4155. * @return void
  4156. */
  4157. public function testInputRadio()
  4158. {
  4159. $result = $this->Form->input('test', [
  4160. 'type' => 'radio',
  4161. 'options' => ['A', 'B'],
  4162. ]);
  4163. $expected = [
  4164. ['div' => ['class' => 'input radio']],
  4165. '<label',
  4166. 'Test',
  4167. '/label',
  4168. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4169. ['label' => ['for' => 'test-0']],
  4170. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4171. 'A',
  4172. '/label',
  4173. ['label' => ['for' => 'test-1']],
  4174. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4175. 'B',
  4176. '/label',
  4177. '/div',
  4178. ];
  4179. $this->assertHtml($expected, $result);
  4180. $result = $this->Form->input('test', [
  4181. 'type' => 'radio',
  4182. 'options' => ['A', 'B'],
  4183. 'value' => '0'
  4184. ]);
  4185. $expected = [
  4186. ['div' => ['class' => 'input radio']],
  4187. '<label',
  4188. 'Test',
  4189. '/label',
  4190. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4191. ['label' => ['for' => 'test-0']],
  4192. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4193. 'A',
  4194. '/label',
  4195. ['label' => ['for' => 'test-1']],
  4196. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4197. 'B',
  4198. '/label',
  4199. '/div',
  4200. ];
  4201. $this->assertHtml($expected, $result);
  4202. $result = $this->Form->input('test', [
  4203. 'type' => 'radio',
  4204. 'options' => ['A', 'B'],
  4205. 'label' => false
  4206. ]);
  4207. $expected = [
  4208. ['div' => ['class' => 'input radio']],
  4209. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4210. ['label' => ['for' => 'test-0']],
  4211. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4212. 'A',
  4213. '/label',
  4214. ['label' => ['for' => 'test-1']],
  4215. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4216. 'B',
  4217. '/label',
  4218. '/div',
  4219. ];
  4220. $this->assertHtml($expected, $result);
  4221. }
  4222. /**
  4223. * Test that radio() works with label = false
  4224. *
  4225. * @return void
  4226. */
  4227. public function testRadioNoLabel()
  4228. {
  4229. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4230. $expected = [
  4231. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4232. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4233. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4234. ];
  4235. $this->assertHtml($expected, $result);
  4236. }
  4237. /**
  4238. * test generating radio input inside label ala twitter bootstrap
  4239. *
  4240. * @return void
  4241. */
  4242. public function testRadioInputInsideLabel()
  4243. {
  4244. $this->Form->templates([
  4245. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4246. 'radioWrapper' => '{{label}}'
  4247. ]);
  4248. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4249. //@codingStandardsIgnoreStart
  4250. $expected = [
  4251. ['input' => [
  4252. 'type' => 'hidden',
  4253. 'name' => 'Model[field]',
  4254. 'value' => ''
  4255. ]],
  4256. ['label' => ['for' => 'model-field-0']],
  4257. ['input' => [
  4258. 'type' => 'radio',
  4259. 'name' => 'Model[field]',
  4260. 'value' => '0',
  4261. 'id' => 'model-field-0'
  4262. ]],
  4263. 'option A',
  4264. '/label',
  4265. ['label' => ['for' => 'model-field-1']],
  4266. ['input' => [
  4267. 'type' => 'radio',
  4268. 'name' => 'Model[field]',
  4269. 'value' => '1',
  4270. 'id' => 'model-field-1'
  4271. ]],
  4272. 'option B',
  4273. '/label',
  4274. ];
  4275. //@codingStandardsIgnoreEnd
  4276. $this->assertHtml($expected, $result);
  4277. }
  4278. /**
  4279. * test disabling the hidden input for radio buttons
  4280. *
  4281. * @return void
  4282. */
  4283. public function testRadioHiddenInputDisabling()
  4284. {
  4285. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4286. $expected = [
  4287. 'label' => ['for' => 'model-1-field-0'],
  4288. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4289. 'option A',
  4290. '/label'
  4291. ];
  4292. $this->assertHtml($expected, $result);
  4293. }
  4294. /**
  4295. * testRadio method
  4296. *
  4297. * Test radio element set generation
  4298. *
  4299. * @return void
  4300. */
  4301. public function testRadioOutOfRange()
  4302. {
  4303. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4304. $expected = [
  4305. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4306. 'label' => ['for' => 'model-field-v'],
  4307. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4308. 'value',
  4309. '/label'
  4310. ];
  4311. $this->assertHtml($expected, $result);
  4312. }
  4313. /**
  4314. * testSelect method
  4315. *
  4316. * Test select element generation.
  4317. *
  4318. * @return void
  4319. */
  4320. public function testSelect()
  4321. {
  4322. $result = $this->Form->select('Model.field', []);
  4323. $expected = [
  4324. 'select' => ['name' => 'Model[field]'],
  4325. '/select'
  4326. ];
  4327. $this->assertHtml($expected, $result);
  4328. $this->Form->request->data = ['Model' => ['field' => 'value']];
  4329. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4330. $expected = [
  4331. 'select' => ['name' => 'Model[field]'],
  4332. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4333. 'good',
  4334. '/option',
  4335. ['option' => ['value' => 'other']],
  4336. 'bad',
  4337. '/option',
  4338. '/select'
  4339. ];
  4340. $this->assertHtml($expected, $result);
  4341. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4342. $this->assertHtml($expected, $result);
  4343. $this->Form->request->data = [];
  4344. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4345. $expected = [
  4346. 'select' => ['name' => 'Model[field]'],
  4347. ['option' => ['value' => 'value']],
  4348. 'good',
  4349. '/option',
  4350. ['option' => ['value' => 'other']],
  4351. 'bad',
  4352. '/option',
  4353. '/select'
  4354. ];
  4355. $this->assertHtml($expected, $result);
  4356. $options = [
  4357. ['value' => 'first', 'text' => 'First'],
  4358. ['value' => 'first', 'text' => 'Another First'],
  4359. ];
  4360. $result = $this->Form->select(
  4361. 'Model.field',
  4362. $options,
  4363. ['escape' => false, 'empty' => false]
  4364. );
  4365. $expected = [
  4366. 'select' => ['name' => 'Model[field]'],
  4367. ['option' => ['value' => 'first']],
  4368. 'First',
  4369. '/option',
  4370. ['option' => ['value' => 'first']],
  4371. 'Another First',
  4372. '/option',
  4373. '/select'
  4374. ];
  4375. $this->assertHtml($expected, $result);
  4376. $this->Form->request->data = ['Model' => ['contact_id' => 228]];
  4377. $result = $this->Form->select(
  4378. 'Model.contact_id',
  4379. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4380. ['escape' => false, 'empty' => 'pick something']
  4381. );
  4382. $expected = [
  4383. 'select' => ['name' => 'Model[contact_id]'],
  4384. ['option' => ['value' => '']], 'pick something', '/option',
  4385. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4386. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4387. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4388. '/select'
  4389. ];
  4390. $this->assertHtml($expected, $result);
  4391. $this->Form->request->data['Model']['field'] = 0;
  4392. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4393. $expected = [
  4394. 'select' => ['name' => 'Model[field]'],
  4395. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4396. ['option' => ['value' => '1']], 'Yes', '/option',
  4397. '/select'
  4398. ];
  4399. $this->assertHtml($expected, $result);
  4400. }
  4401. /**
  4402. * Test that select() escapes HTML.
  4403. *
  4404. * @return void
  4405. */
  4406. public function testSelectEscapeHtml()
  4407. {
  4408. $result = $this->Form->select(
  4409. 'Model.field',
  4410. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4411. ['empty' => false]
  4412. );
  4413. $expected = [
  4414. 'select' => ['name' => 'Model[field]'],
  4415. ['option' => ['value' => 'first']],
  4416. 'first &quot;html&quot; &lt;chars&gt;',
  4417. '/option',
  4418. ['option' => ['value' => 'second']],
  4419. 'value',
  4420. '/option',
  4421. '/select'
  4422. ];
  4423. $this->assertHtml($expected, $result);
  4424. $result = $this->Form->select(
  4425. 'Model.field',
  4426. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4427. ['escape' => false, 'empty' => false]
  4428. );
  4429. $expected = [
  4430. 'select' => ['name' => 'Model[field]'],
  4431. ['option' => ['value' => 'first']],
  4432. 'first "html" <chars>',
  4433. '/option',
  4434. ['option' => ['value' => 'second']],
  4435. 'value',
  4436. '/option',
  4437. '/select'
  4438. ];
  4439. $this->assertHtml($expected, $result);
  4440. }
  4441. /**
  4442. * test select() with required and disabled attributes.
  4443. *
  4444. * @return void
  4445. */
  4446. public function testSelectRequired()
  4447. {
  4448. $this->article['required'] = [
  4449. 'user_id' => true
  4450. ];
  4451. $this->Form->create($this->article);
  4452. $result = $this->Form->select('user_id', ['option A']);
  4453. $expected = [
  4454. 'select' => [
  4455. 'name' => 'user_id',
  4456. 'required' => 'required'
  4457. ],
  4458. ['option' => ['value' => '0']], 'option A', '/option',
  4459. '/select'
  4460. ];
  4461. $this->assertHtml($expected, $result);
  4462. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4463. $expected = [
  4464. 'select' => [
  4465. 'name' => 'user_id',
  4466. 'disabled' => 'disabled'
  4467. ],
  4468. ['option' => ['value' => '0']], 'option A', '/option',
  4469. '/select'
  4470. ];
  4471. $this->assertHtml($expected, $result);
  4472. }
  4473. /**
  4474. * testNestedSelect method
  4475. *
  4476. * test select element generation with optgroups
  4477. *
  4478. * @return void
  4479. */
  4480. public function testNestedSelect()
  4481. {
  4482. $result = $this->Form->select(
  4483. 'Model.field',
  4484. [1 => 'One', 2 => 'Two', 'Three' => [
  4485. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4486. ]],
  4487. ['empty' => false]
  4488. );
  4489. $expected = [
  4490. 'select' => ['name' => 'Model[field]'],
  4491. ['option' => ['value' => 1]],
  4492. 'One',
  4493. '/option',
  4494. ['option' => ['value' => 2]],
  4495. 'Two',
  4496. '/option',
  4497. ['optgroup' => ['label' => 'Three']],
  4498. ['option' => ['value' => 3]],
  4499. 'Three',
  4500. '/option',
  4501. ['option' => ['value' => 4]],
  4502. 'Four',
  4503. '/option',
  4504. ['option' => ['value' => 5]],
  4505. 'Five',
  4506. '/option',
  4507. '/optgroup',
  4508. '/select'
  4509. ];
  4510. $this->assertHtml($expected, $result);
  4511. }
  4512. /**
  4513. * testSelectMultiple method
  4514. *
  4515. * test generation of multiple select elements
  4516. *
  4517. * @return void
  4518. */
  4519. public function testSelectMultiple()
  4520. {
  4521. $options = ['first', 'second', 'third'];
  4522. $result = $this->Form->select(
  4523. 'Model.multi_field',
  4524. $options,
  4525. ['form' => 'my-form', 'multiple' => true]
  4526. );
  4527. $expected = [
  4528. 'input' => [
  4529. 'type' => 'hidden',
  4530. 'name' => 'Model[multi_field]',
  4531. 'value' => '',
  4532. 'form' => 'my-form',
  4533. ],
  4534. 'select' => [
  4535. 'name' => 'Model[multi_field][]',
  4536. 'multiple' => 'multiple',
  4537. 'form' => 'my-form',
  4538. ],
  4539. ['option' => ['value' => '0']],
  4540. 'first',
  4541. '/option',
  4542. ['option' => ['value' => '1']],
  4543. 'second',
  4544. '/option',
  4545. ['option' => ['value' => '2']],
  4546. 'third',
  4547. '/option',
  4548. '/select'
  4549. ];
  4550. $this->assertHtml($expected, $result);
  4551. $result = $this->Form->select(
  4552. 'Model.multi_field',
  4553. $options,
  4554. ['multiple' => 'multiple', 'form' => 'my-form']
  4555. );
  4556. $this->assertHtml($expected, $result);
  4557. }
  4558. /**
  4559. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4560. *
  4561. * @return void
  4562. */
  4563. public function testCheckboxZeroValue()
  4564. {
  4565. $result = $this->Form->input('User.get_spam', [
  4566. 'type' => 'checkbox',
  4567. 'value' => '0',
  4568. 'hiddenField' => '1',
  4569. ]);
  4570. $expected = [
  4571. 'div' => ['class' => 'input checkbox'],
  4572. 'label' => ['for' => 'user-get-spam'],
  4573. ['input' => [
  4574. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4575. 'value' => '1'
  4576. ]],
  4577. ['input' => [
  4578. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4579. 'value' => '0', 'id' => 'user-get-spam'
  4580. ]],
  4581. 'Get Spam',
  4582. '/label',
  4583. '/div'
  4584. ];
  4585. $this->assertHtml($expected, $result);
  4586. }
  4587. /**
  4588. * test generation of habtm select boxes.
  4589. *
  4590. * @return void
  4591. */
  4592. public function testHabtmSelectBox()
  4593. {
  4594. $this->loadFixtures('Articles');
  4595. $options = [
  4596. 1 => 'blue',
  4597. 2 => 'red',
  4598. 3 => 'green'
  4599. ];
  4600. $tags = [
  4601. new Entity(['id' => 1, 'name' => 'blue']),
  4602. new Entity(['id' => 3, 'name' => 'green'])
  4603. ];
  4604. $article = new Article(['tags' => $tags]);
  4605. $this->Form->create($article);
  4606. $result = $this->Form->input('tags._ids', ['options' => $options]);
  4607. $expected = [
  4608. 'div' => ['class' => 'input select'],
  4609. 'label' => ['for' => 'tags-ids'],
  4610. 'Tags',
  4611. '/label',
  4612. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4613. 'select' => [
  4614. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4615. 'multiple' => 'multiple'
  4616. ],
  4617. ['option' => ['value' => '1', 'selected' => 'selected']],
  4618. 'blue',
  4619. '/option',
  4620. ['option' => ['value' => '2']],
  4621. 'red',
  4622. '/option',
  4623. ['option' => ['value' => '3', 'selected' => 'selected']],
  4624. 'green',
  4625. '/option',
  4626. '/select',
  4627. '/div'
  4628. ];
  4629. $this->assertHtml($expected, $result);
  4630. // make sure only 50 is selected, and not 50f5c0cf
  4631. $options = [
  4632. '1' => 'blue',
  4633. '50f5c0cf' => 'red',
  4634. '50' => 'green'
  4635. ];
  4636. $tags = [
  4637. new Entity(['id' => 1, 'name' => 'blue']),
  4638. new Entity(['id' => 50, 'name' => 'green'])
  4639. ];
  4640. $article = new Article(['tags' => $tags]);
  4641. $this->Form->create($article);
  4642. $result = $this->Form->input('tags._ids', ['options' => $options]);
  4643. $expected = [
  4644. 'div' => ['class' => 'input select'],
  4645. 'label' => ['for' => 'tags-ids'],
  4646. 'Tags',
  4647. '/label',
  4648. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4649. 'select' => [
  4650. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4651. 'multiple' => 'multiple'
  4652. ],
  4653. ['option' => ['value' => '1', 'selected' => 'selected']],
  4654. 'blue',
  4655. '/option',
  4656. ['option' => ['value' => '50f5c0cf']],
  4657. 'red',
  4658. '/option',
  4659. ['option' => ['value' => '50', 'selected' => 'selected']],
  4660. 'green',
  4661. '/option',
  4662. '/select',
  4663. '/div'
  4664. ];
  4665. $this->assertHtml($expected, $result);
  4666. $spacecraft = [
  4667. 1 => 'Orion',
  4668. 2 => 'Helios'
  4669. ];
  4670. $this->View->viewVars['spacecraft'] = $spacecraft;
  4671. $this->Form->create();
  4672. $result = $this->Form->input('spacecraft._ids');
  4673. $expected = [
  4674. 'div' => ['class' => 'input select'],
  4675. 'label' => ['for' => 'spacecraft-ids'],
  4676. 'Spacecraft',
  4677. '/label',
  4678. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4679. 'select' => [
  4680. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4681. 'multiple' => 'multiple'
  4682. ],
  4683. ['option' => ['value' => '1']],
  4684. 'Orion',
  4685. '/option',
  4686. ['option' => ['value' => '2']],
  4687. 'Helios',
  4688. '/option',
  4689. '/select',
  4690. '/div'
  4691. ];
  4692. $this->assertHtml($expected, $result);
  4693. }
  4694. /**
  4695. * Tests that errors for belongsToMany select fields are being
  4696. * picked up properly.
  4697. *
  4698. * @return void
  4699. */
  4700. public function testErrorsForBelongsToManySelect()
  4701. {
  4702. $spacecraft = [
  4703. 1 => 'Orion',
  4704. 2 => 'Helios'
  4705. ];
  4706. $this->View->viewVars['spacecraft'] = $spacecraft;
  4707. $article = new Article();
  4708. $article->errors('spacecraft', ['Invalid']);
  4709. $this->Form->create($article);
  4710. $result = $this->Form->input('spacecraft._ids');
  4711. $expected = [
  4712. ['div' => ['class' => 'input select error']],
  4713. 'label' => ['for' => 'spacecraft-ids'],
  4714. 'Spacecraft',
  4715. '/label',
  4716. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4717. 'select' => [
  4718. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4719. 'multiple' => 'multiple'
  4720. ],
  4721. ['option' => ['value' => '1']],
  4722. 'Orion',
  4723. '/option',
  4724. ['option' => ['value' => '2']],
  4725. 'Helios',
  4726. '/option',
  4727. '/select',
  4728. ['div' => ['class' => 'error-message']],
  4729. 'Invalid',
  4730. '/div',
  4731. '/div'
  4732. ];
  4733. $this->assertHtml($expected, $result);
  4734. }
  4735. /**
  4736. * test generation of multi select elements in checkbox format
  4737. *
  4738. * @return void
  4739. */
  4740. public function testSelectMultipleCheckboxes()
  4741. {
  4742. $result = $this->Form->select(
  4743. 'Model.multi_field',
  4744. ['first', 'second', 'third'],
  4745. ['multiple' => 'checkbox']
  4746. );
  4747. $expected = [
  4748. 'input' => [
  4749. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4750. ],
  4751. ['div' => ['class' => 'checkbox']],
  4752. ['label' => ['for' => 'model-multi-field-0']],
  4753. ['input' => [
  4754. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4755. 'value' => '0', 'id' => 'model-multi-field-0'
  4756. ]],
  4757. 'first',
  4758. '/label',
  4759. '/div',
  4760. ['div' => ['class' => 'checkbox']],
  4761. ['label' => ['for' => 'model-multi-field-1']],
  4762. ['input' => [
  4763. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4764. 'value' => '1', 'id' => 'model-multi-field-1'
  4765. ]],
  4766. 'second',
  4767. '/label',
  4768. '/div',
  4769. ['div' => ['class' => 'checkbox']],
  4770. ['label' => ['for' => 'model-multi-field-2']],
  4771. ['input' => [
  4772. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4773. 'value' => '2', 'id' => 'model-multi-field-2'
  4774. ]],
  4775. 'third',
  4776. '/label',
  4777. '/div'
  4778. ];
  4779. $this->assertHtml($expected, $result);
  4780. $result = $this->Form->select(
  4781. 'Model.multi_field',
  4782. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  4783. ['multiple' => 'checkbox']
  4784. );
  4785. $expected = [
  4786. 'input' => [
  4787. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4788. ],
  4789. ['div' => ['class' => 'checkbox']],
  4790. ['label' => ['for' => 'model-multi-field-a+']],
  4791. ['input' => [
  4792. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4793. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  4794. ]],
  4795. 'first',
  4796. '/label',
  4797. '/div',
  4798. ['div' => ['class' => 'checkbox']],
  4799. ['label' => ['for' => 'model-multi-field-a++']],
  4800. ['input' => [
  4801. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4802. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  4803. ]],
  4804. 'second',
  4805. '/label',
  4806. '/div',
  4807. ['div' => ['class' => 'checkbox']],
  4808. ['label' => ['for' => 'model-multi-field-a+++']],
  4809. ['input' => [
  4810. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4811. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  4812. ]],
  4813. 'third',
  4814. '/label',
  4815. '/div'
  4816. ];
  4817. $this->assertHtml($expected, $result);
  4818. $result = $this->Form->select(
  4819. 'Model.multi_field',
  4820. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  4821. ['multiple' => 'checkbox']
  4822. );
  4823. $expected = [
  4824. 'input' => [
  4825. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4826. ],
  4827. ['div' => ['class' => 'checkbox']],
  4828. ['label' => ['for' => 'model-multi-field-a-b']],
  4829. ['input' => [
  4830. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4831. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  4832. ]],
  4833. 'first',
  4834. '/label',
  4835. '/div',
  4836. ['div' => ['class' => 'checkbox']],
  4837. ['label' => ['for' => 'model-multi-field-a-b1']],
  4838. ['input' => [
  4839. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4840. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  4841. ]],
  4842. 'second',
  4843. '/label',
  4844. '/div',
  4845. ['div' => ['class' => 'checkbox']],
  4846. ['label' => ['for' => 'model-multi-field-a-b2']],
  4847. ['input' => [
  4848. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4849. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  4850. ]],
  4851. 'third',
  4852. '/label',
  4853. '/div'
  4854. ];
  4855. $this->assertHtml($expected, $result);
  4856. }
  4857. /**
  4858. * Ensure that multiCheckbox reads from the request data.
  4859. *
  4860. * @return void
  4861. */
  4862. public function testSelectMultipleCheckboxRequestData()
  4863. {
  4864. $this->Form->request->data = ['Model' => ['tags' => [1]]];
  4865. $result = $this->Form->select(
  4866. 'Model.tags',
  4867. ['1' => 'first', 'Array' => 'Array'],
  4868. ['multiple' => 'checkbox']
  4869. );
  4870. $expected = [
  4871. 'input' => [
  4872. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  4873. ],
  4874. ['div' => ['class' => 'checkbox']],
  4875. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  4876. ['input' => [
  4877. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4878. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  4879. ]],
  4880. 'first',
  4881. '/label',
  4882. '/div',
  4883. ['div' => ['class' => 'checkbox']],
  4884. ['label' => ['for' => 'model-tags-array']],
  4885. ['input' => [
  4886. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4887. 'value' => 'Array', 'id' => 'model-tags-array'
  4888. ]],
  4889. 'Array',
  4890. '/label',
  4891. '/div'
  4892. ];
  4893. $this->assertHtml($expected, $result);
  4894. }
  4895. /**
  4896. * Checks the security hash array generated for multiple-input checkbox elements
  4897. *
  4898. * @return void
  4899. */
  4900. public function testSelectMultipleCheckboxSecurity()
  4901. {
  4902. $this->Form->request->params['_Token'] = 'testKey';
  4903. $this->assertEquals([], $this->Form->fields);
  4904. $result = $this->Form->select(
  4905. 'Model.multi_field',
  4906. ['1' => 'first', '2' => 'second', '3' => 'third'],
  4907. ['multiple' => 'checkbox']
  4908. );
  4909. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  4910. $result = $this->Form->secure($this->Form->fields);
  4911. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4912. $this->assertRegExp('/"' . $key . '"/', $result);
  4913. }
  4914. /**
  4915. * Multiple select elements should always be secured as they always participate
  4916. * in the POST data.
  4917. *
  4918. * @return void
  4919. */
  4920. public function testSelectMultipleSecureWithNoOptions()
  4921. {
  4922. $this->assertEquals([], $this->Form->fields);
  4923. $this->Form->select(
  4924. 'Model.select',
  4925. [],
  4926. ['multiple' => true]
  4927. );
  4928. $this->assertEquals(['Model.select'], $this->Form->fields);
  4929. }
  4930. /**
  4931. * When a select box has no options it should not be added to the fields list
  4932. * as it always fail post validation.
  4933. *
  4934. * @return void
  4935. */
  4936. public function testSelectNoSecureWithNoOptions()
  4937. {
  4938. $this->Form->request->params['_Token'] = 'testkey';
  4939. $this->assertEquals([], $this->Form->fields);
  4940. $this->Form->select(
  4941. 'Model.select',
  4942. []
  4943. );
  4944. $this->assertEquals([], $this->Form->fields);
  4945. $this->Form->select(
  4946. 'Model.user_id',
  4947. [],
  4948. ['empty' => true]
  4949. );
  4950. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  4951. }
  4952. /**
  4953. * testInputMultipleCheckboxes method
  4954. *
  4955. * test input() resulting in multi select elements being generated.
  4956. *
  4957. * @return void
  4958. */
  4959. public function testInputMultipleCheckboxes()
  4960. {
  4961. $result = $this->Form->input('Model.multi_field', [
  4962. 'options' => ['first', 'second', 'third'],
  4963. 'multiple' => 'checkbox'
  4964. ]);
  4965. $expected = [
  4966. ['div' => ['class' => 'input select']],
  4967. ['label' => ['for' => 'model-multi-field']],
  4968. 'Multi Field',
  4969. '/label',
  4970. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  4971. ['div' => ['class' => 'checkbox']],
  4972. ['label' => ['for' => 'model-multi-field-0']],
  4973. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  4974. 'first',
  4975. '/label',
  4976. '/div',
  4977. ['div' => ['class' => 'checkbox']],
  4978. ['label' => ['for' => 'model-multi-field-1']],
  4979. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  4980. 'second',
  4981. '/label',
  4982. '/div',
  4983. ['div' => ['class' => 'checkbox']],
  4984. ['label' => ['for' => 'model-multi-field-2']],
  4985. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  4986. 'third',
  4987. '/label',
  4988. '/div',
  4989. '/div'
  4990. ];
  4991. $this->assertHtml($expected, $result);
  4992. $result = $this->Form->input('Model.multi_field', [
  4993. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  4994. 'multiple' => 'checkbox'
  4995. ]);
  4996. $expected = [
  4997. ['div' => ['class' => 'input select']],
  4998. ['label' => ['for' => 'model-multi-field']],
  4999. 'Multi Field',
  5000. '/label',
  5001. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5002. ['div' => ['class' => 'checkbox']],
  5003. ['label' => ['for' => 'model-multi-field-a']],
  5004. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5005. 'first',
  5006. '/label',
  5007. '/div',
  5008. ['div' => ['class' => 'checkbox']],
  5009. ['label' => ['for' => 'model-multi-field-b']],
  5010. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5011. 'second',
  5012. '/label',
  5013. '/div',
  5014. ['div' => ['class' => 'checkbox']],
  5015. ['label' => ['for' => 'model-multi-field-c']],
  5016. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5017. 'third',
  5018. '/label',
  5019. '/div',
  5020. '/div'
  5021. ];
  5022. $this->assertHtml($expected, $result);
  5023. }
  5024. /**
  5025. * testSelectHiddenFieldOmission method
  5026. *
  5027. * test that select() with 'hiddenField' => false omits the hidden field
  5028. *
  5029. * @return void
  5030. */
  5031. public function testSelectHiddenFieldOmission()
  5032. {
  5033. $result = $this->Form->select(
  5034. 'Model.multi_field',
  5035. ['first', 'second'],
  5036. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5037. );
  5038. $this->assertNotContains('type="hidden"', $result);
  5039. }
  5040. /**
  5041. * test that select() with multiple = checkbox works with overriding name attribute.
  5042. *
  5043. * @return void
  5044. */
  5045. public function testSelectCheckboxMultipleOverrideName()
  5046. {
  5047. $result = $this->Form->select('category', ['1', '2'], [
  5048. 'multiple' => 'checkbox',
  5049. 'name' => 'fish',
  5050. ]);
  5051. $expected = [
  5052. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5053. ['div' => ['class' => 'checkbox']],
  5054. ['label' => ['for' => 'fish-0']],
  5055. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5056. '1',
  5057. '/label',
  5058. '/div',
  5059. ['div' => ['class' => 'checkbox']],
  5060. ['label' => ['for' => 'fish-1']],
  5061. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5062. '2',
  5063. '/label',
  5064. '/div'
  5065. ];
  5066. $this->assertHtml($expected, $result);
  5067. $result = $this->Form->multiCheckbox(
  5068. 'category',
  5069. new Collection(['1', '2']),
  5070. ['name' => 'fish']
  5071. );
  5072. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5073. 'name' => 'fish',
  5074. ]);
  5075. $this->assertHtml($expected, $result);
  5076. }
  5077. /**
  5078. * Test that input() works with multicheckbox.
  5079. *
  5080. * @return void
  5081. */
  5082. public function testInputMultiCheckbox()
  5083. {
  5084. $result = $this->Form->input('category', [
  5085. 'type' => 'multicheckbox',
  5086. 'options' => ['1', '2'],
  5087. ]);
  5088. $expected = [
  5089. ['div' => ['class' => 'input multicheckbox']],
  5090. '<label',
  5091. 'Category',
  5092. '/label',
  5093. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5094. ['div' => ['class' => 'checkbox']],
  5095. ['label' => ['for' => 'category-0']],
  5096. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5097. '1',
  5098. '/label',
  5099. '/div',
  5100. ['div' => ['class' => 'checkbox']],
  5101. ['label' => ['for' => 'category-1']],
  5102. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5103. '2',
  5104. '/label',
  5105. '/div',
  5106. '/div',
  5107. ];
  5108. $this->assertHtml($expected, $result);
  5109. }
  5110. /**
  5111. * testCheckbox method
  5112. *
  5113. * Test generation of checkboxes
  5114. *
  5115. * @return void
  5116. */
  5117. public function testCheckbox()
  5118. {
  5119. $result = $this->Form->checkbox('Model.field');
  5120. $expected = [
  5121. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5122. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5123. ];
  5124. $this->assertHtml($expected, $result);
  5125. $result = $this->Form->checkbox('Model.field', [
  5126. 'id' => 'theID',
  5127. 'value' => 'myvalue',
  5128. 'form' => 'my-form',
  5129. ]);
  5130. $expected = [
  5131. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5132. ['input' => [
  5133. 'type' => 'checkbox', 'name' => 'Model[field]',
  5134. 'value' => 'myvalue', 'id' => 'theID',
  5135. 'form' => 'my-form',
  5136. ]]
  5137. ];
  5138. $this->assertHtml($expected, $result);
  5139. }
  5140. /**
  5141. * testCheckboxDefaultValue method
  5142. *
  5143. * Test default value setting on checkbox() method
  5144. *
  5145. * @return void
  5146. */
  5147. public function testCheckboxDefaultValue()
  5148. {
  5149. $this->Form->request->data['Model']['field'] = false;
  5150. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5151. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5152. $this->assertHtml($expected, $result);
  5153. unset($this->Form->request->data['Model']['field']);
  5154. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5155. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5156. $this->assertHtml($expected, $result);
  5157. $this->Form->request->data['Model']['field'] = true;
  5158. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5159. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5160. $this->assertHtml($expected, $result);
  5161. unset($this->Form->request->data['Model']['field']);
  5162. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5163. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5164. $this->assertHtml($expected, $result);
  5165. $Articles = TableRegistry::get('Articles');
  5166. $Articles->schema()->addColumn(
  5167. 'published',
  5168. ['type' => 'boolean', 'null' => false, 'default' => true]
  5169. );
  5170. $this->Form->create($Articles->newEntity());
  5171. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5172. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5173. $this->assertHtml($expected, $result);
  5174. }
  5175. /**
  5176. * Test checkbox being checked or having errors.
  5177. *
  5178. * @return void
  5179. */
  5180. public function testCheckboxCheckedAndError()
  5181. {
  5182. $this->article['errors'] = [
  5183. 'published' => true
  5184. ];
  5185. $this->Form->request->data['published'] = 'myvalue';
  5186. $this->Form->create($this->article);
  5187. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5188. $expected = [
  5189. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5190. ['input' => [
  5191. 'type' => 'checkbox',
  5192. 'name' => 'published',
  5193. 'value' => 'myvalue',
  5194. 'id' => 'theID',
  5195. 'checked' => 'checked',
  5196. 'class' => 'form-error'
  5197. ]]
  5198. ];
  5199. $this->assertHtml($expected, $result);
  5200. $this->Form->request->data['published'] = '';
  5201. $result = $this->Form->checkbox('published');
  5202. $expected = [
  5203. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5204. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5205. ];
  5206. $this->assertHtml($expected, $result);
  5207. }
  5208. /**
  5209. * test checkbox() with a custom name attribute
  5210. *
  5211. * @return void
  5212. */
  5213. public function testCheckboxCustomNameAttribute()
  5214. {
  5215. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5216. $expected = [
  5217. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5218. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5219. ];
  5220. $this->assertHtml($expected, $result);
  5221. }
  5222. /**
  5223. * Test that the hidden input for checkboxes can be omitted or set to a
  5224. * specific value.
  5225. *
  5226. * @return void
  5227. */
  5228. public function testCheckboxHiddenField()
  5229. {
  5230. $result = $this->Form->checkbox('UserForm.something', [
  5231. 'hiddenField' => false
  5232. ]);
  5233. $expected = [
  5234. 'input' => [
  5235. 'type' => 'checkbox',
  5236. 'name' => 'UserForm[something]',
  5237. 'value' => '1'
  5238. ],
  5239. ];
  5240. $this->assertHtml($expected, $result);
  5241. $result = $this->Form->checkbox('UserForm.something', [
  5242. 'value' => 'Y',
  5243. 'hiddenField' => 'N',
  5244. ]);
  5245. $expected = [
  5246. ['input' => [
  5247. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5248. 'value' => 'N'
  5249. ]],
  5250. ['input' => [
  5251. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5252. 'value' => 'Y'
  5253. ]],
  5254. ];
  5255. $this->assertHtml($expected, $result);
  5256. }
  5257. /**
  5258. * Test the time type.
  5259. *
  5260. * @return void
  5261. */
  5262. public function testTime()
  5263. {
  5264. $result = $this->Form->time('start_time', [
  5265. 'timeFormat' => 12,
  5266. 'interval' => 5,
  5267. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5268. ]);
  5269. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5270. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5271. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5272. $this->assertNotContains('year', $result);
  5273. $this->assertNotContains('month', $result);
  5274. $this->assertNotContains('day', $result);
  5275. $result = $this->Form->time('start_time', [
  5276. 'timeFormat' => 12,
  5277. 'interval' => 5,
  5278. 'value' => '2014-03-08 16:30:00'
  5279. ]);
  5280. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5281. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5282. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5283. $this->assertNotContains('year', $result);
  5284. $this->assertNotContains('month', $result);
  5285. $this->assertNotContains('day', $result);
  5286. }
  5287. /**
  5288. * Ensure that timeFormat=24 has no merdian.
  5289. *
  5290. * @return void.
  5291. */
  5292. public function testTimeFormat24NoMeridian()
  5293. {
  5294. $result = $this->Form->time('start_time', [
  5295. 'timeFormat' => 24,
  5296. 'interval' => 5,
  5297. 'value' => '2014-03-08 16:30:00'
  5298. ]);
  5299. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5300. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5301. $this->assertNotContains('meridian', $result);
  5302. $this->assertNotContains('pm', $result);
  5303. $this->assertNotContains('year', $result);
  5304. $this->assertNotContains('month', $result);
  5305. $this->assertNotContains('day', $result);
  5306. }
  5307. /**
  5308. * Test the date type.
  5309. *
  5310. * @return void
  5311. */
  5312. public function testDate()
  5313. {
  5314. $result = $this->Form->date('start_day', [
  5315. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5316. ]);
  5317. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5318. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5319. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5320. $this->assertNotContains('hour', $result);
  5321. $this->assertNotContains('minute', $result);
  5322. $this->assertNotContains('second', $result);
  5323. $this->assertNotContains('meridian', $result);
  5324. }
  5325. /**
  5326. * testDateTime method
  5327. *
  5328. * Test generation of date/time select elements
  5329. *
  5330. * @return void
  5331. */
  5332. public function testDateTime()
  5333. {
  5334. extract($this->dateRegex);
  5335. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5336. $now = strtotime('now');
  5337. $expected = [
  5338. ['select' => ['name' => 'Contact[date][year]']],
  5339. ['option' => ['value' => '']],
  5340. '/option',
  5341. $yearsRegex,
  5342. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5343. date('Y', $now),
  5344. '/option',
  5345. '*/select',
  5346. ['select' => ['name' => 'Contact[date][month]']],
  5347. ['option' => ['value' => '']],
  5348. '/option',
  5349. $monthsRegex,
  5350. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5351. date('F', $now),
  5352. '/option',
  5353. '*/select',
  5354. ['select' => ['name' => 'Contact[date][day]']],
  5355. ['option' => ['value' => '']],
  5356. '/option',
  5357. $daysRegex,
  5358. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5359. date('j', $now),
  5360. '/option',
  5361. '*/select',
  5362. ['select' => ['name' => 'Contact[date][hour]']],
  5363. ['option' => ['value' => '']],
  5364. '/option',
  5365. $hoursRegex,
  5366. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5367. date('G', $now),
  5368. '/option',
  5369. '*/select',
  5370. ['select' => ['name' => 'Contact[date][minute]']],
  5371. ['option' => ['value' => '']],
  5372. '/option',
  5373. $minutesRegex,
  5374. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5375. date('i', $now),
  5376. '/option',
  5377. '*/select',
  5378. ];
  5379. $this->assertHtml($expected, $result);
  5380. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5381. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5382. $now = strtotime('now');
  5383. $expected = [
  5384. ['select' => ['name' => 'Contact[date][year]']],
  5385. $yearsRegex,
  5386. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5387. date('Y', $now),
  5388. '/option',
  5389. '*/select',
  5390. ['select' => ['name' => 'Contact[date][month]']],
  5391. $monthsRegex,
  5392. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5393. date('F', $now),
  5394. '/option',
  5395. '*/select',
  5396. ['select' => ['name' => 'Contact[date][day]']],
  5397. $daysRegex,
  5398. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5399. date('j', $now),
  5400. '/option',
  5401. '*/select',
  5402. ['select' => ['name' => 'Contact[date][hour]']],
  5403. $hoursRegex,
  5404. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5405. date('G', $now),
  5406. '/option',
  5407. '*/select',
  5408. ['select' => ['name' => 'Contact[date][minute]']],
  5409. $minutesRegex,
  5410. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5411. date('i', $now),
  5412. '/option',
  5413. '*/select',
  5414. ];
  5415. $this->assertHtml($expected, $result);
  5416. }
  5417. /**
  5418. * Test that datetime fields are added to protected fields list.
  5419. *
  5420. * @return void
  5421. */
  5422. public function testDateTimeSecured()
  5423. {
  5424. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5425. $this->Form->dateTime('Contact.date');
  5426. $expected = [
  5427. 'Contact.date.year',
  5428. 'Contact.date.month',
  5429. 'Contact.date.day',
  5430. 'Contact.date.hour',
  5431. 'Contact.date.minute',
  5432. ];
  5433. $this->assertEquals($expected, $this->Form->fields);
  5434. $this->Form->fields = [];
  5435. $this->Form->date('Contact.published');
  5436. $expected = [
  5437. 'Contact.published.year',
  5438. 'Contact.published.month',
  5439. 'Contact.published.day',
  5440. ];
  5441. $this->assertEquals($expected, $this->Form->fields);
  5442. }
  5443. /**
  5444. * Test that datetime fields are added to protected fields list.
  5445. *
  5446. * @return void
  5447. */
  5448. public function testDateTimeSecuredDisabled()
  5449. {
  5450. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5451. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5452. $expected = [];
  5453. $this->assertEquals($expected, $this->Form->fields);
  5454. $this->Form->fields = [];
  5455. $this->Form->date('Contact.published', ['secure' => false]);
  5456. $expected = [];
  5457. $this->assertEquals($expected, $this->Form->fields);
  5458. }
  5459. /**
  5460. * Test empty defaulting to true for datetime.
  5461. *
  5462. * @return void
  5463. */
  5464. public function testDatetimeEmpty()
  5465. {
  5466. extract($this->dateRegex);
  5467. $now = strtotime('now');
  5468. $result = $this->Form->dateTime('Contact.date', [
  5469. 'timeFormat' => 12,
  5470. 'empty' => true,
  5471. 'default' => true
  5472. ]);
  5473. $expected = [
  5474. ['select' => ['name' => 'Contact[date][year]']],
  5475. $yearsRegex,
  5476. ['option' => ['value' => '']],
  5477. '/option',
  5478. '*/select',
  5479. ['select' => ['name' => 'Contact[date][month]']],
  5480. $monthsRegex,
  5481. ['option' => ['value' => '']],
  5482. '/option',
  5483. '*/select',
  5484. ['select' => ['name' => 'Contact[date][day]']],
  5485. $daysRegex,
  5486. ['option' => ['value' => '']],
  5487. '/option',
  5488. '*/select',
  5489. ['select' => ['name' => 'Contact[date][hour]']],
  5490. $hoursRegex,
  5491. ['option' => ['value' => '']],
  5492. '/option',
  5493. '*/select',
  5494. ['select' => ['name' => 'Contact[date][minute]']],
  5495. $minutesRegex,
  5496. ['option' => ['value' => '']],
  5497. '/option',
  5498. '*/select',
  5499. ['select' => ['name' => 'Contact[date][meridian]']],
  5500. $meridianRegex,
  5501. ['option' => ['value' => '']],
  5502. '/option',
  5503. '*/select'
  5504. ];
  5505. $this->assertHtml($expected, $result);
  5506. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5507. }
  5508. /**
  5509. * Test datetime with interval option.
  5510. *
  5511. * @return void
  5512. */
  5513. public function testDatetimeMinuteInterval()
  5514. {
  5515. extract($this->dateRegex);
  5516. $now = strtotime('now');
  5517. $result = $this->Form->dateTime('Contact.date', [
  5518. 'interval' => 5,
  5519. 'value' => ''
  5520. ]);
  5521. $expected = [
  5522. ['select' => ['name' => 'Contact[date][year]']],
  5523. $yearsRegex,
  5524. ['option' => ['selected' => 'selected', 'value' => '']],
  5525. '/option',
  5526. '*/select',
  5527. ['select' => ['name' => 'Contact[date][month]']],
  5528. $monthsRegex,
  5529. ['option' => ['selected' => 'selected', 'value' => '']],
  5530. '/option',
  5531. '*/select',
  5532. ['select' => ['name' => 'Contact[date][day]']],
  5533. $daysRegex,
  5534. ['option' => ['selected' => 'selected', 'value' => '']],
  5535. '/option',
  5536. '*/select',
  5537. ['select' => ['name' => 'Contact[date][hour]']],
  5538. $hoursRegex,
  5539. ['option' => ['selected' => 'selected', 'value' => '']],
  5540. '/option',
  5541. '*/select',
  5542. ['select' => ['name' => 'Contact[date][minute]']],
  5543. $minutesRegex,
  5544. ['option' => ['selected' => 'selected', 'value' => '']],
  5545. '/option',
  5546. ['option' => ['value' => '00']],
  5547. '00',
  5548. '/option',
  5549. ['option' => ['value' => '05']],
  5550. '05',
  5551. '/option',
  5552. ['option' => ['value' => '10']],
  5553. '10',
  5554. '/option',
  5555. '*/select',
  5556. ];
  5557. $this->assertHtml($expected, $result);
  5558. }
  5559. /**
  5560. * Test dateTime with rounding
  5561. *
  5562. * @return void
  5563. */
  5564. public function testDateTimeRounding()
  5565. {
  5566. $this->Form->request->data['Contact'] = [
  5567. 'date' => [
  5568. 'day' => '13',
  5569. 'month' => '12',
  5570. 'year' => '2010',
  5571. 'hour' => '04',
  5572. 'minute' => '19',
  5573. 'meridian' => 'AM'
  5574. ]
  5575. ];
  5576. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5577. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5578. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5579. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5580. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5581. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5582. }
  5583. /**
  5584. * test that datetime() and default values work.
  5585. *
  5586. * @return void
  5587. */
  5588. public function testDatetimeWithDefault()
  5589. {
  5590. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5591. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5592. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5593. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5594. $result = $this->Form->dateTime('Contact.updated', [
  5595. 'default' => '2009-06-01 11:15:30'
  5596. ]);
  5597. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5598. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5599. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5600. }
  5601. /**
  5602. * testDateTime all zeros
  5603. *
  5604. * @return void
  5605. */
  5606. public function testDateTimeAllZeros()
  5607. {
  5608. $result = $this->Form->dateTime('Contact.date', [
  5609. 'timeFormat' => false,
  5610. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5611. 'value' => '0000-00-00'
  5612. ]);
  5613. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5614. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5615. }
  5616. /**
  5617. * testDateTimeEmptyAsArray
  5618. *
  5619. * @return void
  5620. */
  5621. public function testDateTimeEmptyAsArray()
  5622. {
  5623. $result = $this->Form->dateTime('Contact.date', [
  5624. 'empty' => [
  5625. 'day' => 'DAY',
  5626. 'month' => 'MONTH',
  5627. 'year' => 'YEAR',
  5628. 'hour' => 'HOUR',
  5629. 'minute' => 'MINUTE',
  5630. 'meridian' => false
  5631. ],
  5632. 'default' => true
  5633. ]);
  5634. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5635. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5636. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5637. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5638. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5639. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5640. $result = $this->Form->dateTime('Contact.date', [
  5641. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5642. 'default' => true
  5643. ]);
  5644. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5645. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5646. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5647. }
  5648. /**
  5649. * testFormDateTimeMulti method
  5650. *
  5651. * test multiple datetime element generation
  5652. *
  5653. * @return void
  5654. */
  5655. public function testFormDateTimeMulti()
  5656. {
  5657. extract($this->dateRegex);
  5658. $result = $this->Form->dateTime('Contact.1.updated');
  5659. $this->assertContains('Contact[1][updated][month]', $result);
  5660. $this->assertContains('Contact[1][updated][day]', $result);
  5661. $this->assertContains('Contact[1][updated][year]', $result);
  5662. $this->assertContains('Contact[1][updated][hour]', $result);
  5663. $this->assertContains('Contact[1][updated][minute]', $result);
  5664. }
  5665. /**
  5666. * When changing the date format, the label should always focus the first select box when
  5667. * clicked.
  5668. *
  5669. * @return void
  5670. */
  5671. public function testDateTimeLabelIdMatchesFirstInput()
  5672. {
  5673. $result = $this->Form->input('Model.date', ['type' => 'date']);
  5674. $this->assertContains('<label>Date</label>', $result);
  5675. $result = $this->Form->input('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  5676. $this->assertContains('<label>Date</label>', $result);
  5677. $result = $this->Form->input('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  5678. $this->assertContains('<label>Date</label>', $result);
  5679. }
  5680. /**
  5681. * test datetime second=true
  5682. *
  5683. * @return void
  5684. */
  5685. public function testDateTimeSecondOptions()
  5686. {
  5687. $result = $this->Form->dateTime('updated', ['second' => true]);
  5688. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5689. $result = $this->Form->dateTime('updated', ['second' => []]);
  5690. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5691. $result = $this->Form->dateTime('updated', ['second' => null]);
  5692. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5693. $result = $this->Form->dateTime('updated', ['second' => false]);
  5694. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5695. }
  5696. /**
  5697. * testMonth method
  5698. *
  5699. * @return void
  5700. */
  5701. public function testMonth()
  5702. {
  5703. $result = $this->Form->month('Model.field', ['value' => '']);
  5704. $expected = [
  5705. ['select' => ['name' => 'Model[field][month]']],
  5706. ['option' => ['value' => '', 'selected' => 'selected']],
  5707. '/option',
  5708. ['option' => ['value' => '01']],
  5709. date('F', strtotime('2008-01-01 00:00:00')),
  5710. '/option',
  5711. ['option' => ['value' => '02']],
  5712. date('F', strtotime('2008-02-01 00:00:00')),
  5713. '/option',
  5714. '*/select',
  5715. ];
  5716. $this->assertHtml($expected, $result);
  5717. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  5718. $expected = [
  5719. ['select' => ['name' => 'Model[field][month]']],
  5720. ['option' => ['selected' => 'selected', 'value' => '']],
  5721. '/option',
  5722. ['option' => ['value' => '01']],
  5723. date('F', strtotime('2008-01-01 00:00:00')),
  5724. '/option',
  5725. ['option' => ['value' => '02']],
  5726. date('F', strtotime('2008-02-01 00:00:00')),
  5727. '/option',
  5728. '*/select',
  5729. ];
  5730. $this->assertHtml($expected, $result);
  5731. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  5732. $expected = [
  5733. ['select' => ['name' => 'Model[field][month]']],
  5734. ['option' => ['selected' => 'selected', 'value' => '']],
  5735. '/option',
  5736. ['option' => ['value' => '01']],
  5737. '1',
  5738. '/option',
  5739. ['option' => ['value' => '02']],
  5740. '2',
  5741. '/option',
  5742. '*/select',
  5743. ];
  5744. $this->assertHtml($expected, $result);
  5745. $monthNames = [
  5746. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5747. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  5748. ];
  5749. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  5750. $expected = [
  5751. ['select' => ['name' => 'Model[field][month]']],
  5752. ['option' => ['value' => '']],
  5753. '/option',
  5754. ['option' => ['value' => '01', 'selected' => 'selected']],
  5755. 'Jan',
  5756. '/option',
  5757. ['option' => ['value' => '02']],
  5758. 'Feb',
  5759. '/option',
  5760. '*/select',
  5761. ];
  5762. $this->assertHtml($expected, $result);
  5763. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5764. $result = $this->Form->month('Project.release');
  5765. $expected = [
  5766. ['select' => ['name' => 'Project[release][month]']],
  5767. ['option' => ['value' => '']],
  5768. '/option',
  5769. ['option' => ['value' => '01']],
  5770. 'January',
  5771. '/option',
  5772. ['option' => ['value' => '02', 'selected' => 'selected']],
  5773. 'February',
  5774. '/option',
  5775. '*/select',
  5776. ];
  5777. $this->assertHtml($expected, $result);
  5778. $result = $this->Form->month('Contact.published', [
  5779. 'empty' => 'Published on',
  5780. ]);
  5781. $this->assertContains('Published on', $result);
  5782. }
  5783. /**
  5784. * testDay method
  5785. *
  5786. * @return void
  5787. */
  5788. public function testDay()
  5789. {
  5790. extract($this->dateRegex);
  5791. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  5792. $expected = [
  5793. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  5794. ['option' => ['selected' => 'selected', 'value' => '']],
  5795. '/option',
  5796. ['option' => ['value' => '01']],
  5797. '1',
  5798. '/option',
  5799. ['option' => ['value' => '02']],
  5800. '2',
  5801. '/option',
  5802. $daysRegex,
  5803. '/select',
  5804. ];
  5805. $this->assertHtml($expected, $result);
  5806. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5807. $result = $this->Form->day('Model.field');
  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['Model']['field'] = '';
  5827. $result = $this->Form->day('Model.field', ['value' => '10']);
  5828. $expected = [
  5829. ['select' => ['name' => 'Model[field][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. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5847. $result = $this->Form->day('Project.release');
  5848. $expected = [
  5849. ['select' => ['name' => 'Project[release][day]']],
  5850. ['option' => ['value' => '']],
  5851. '/option',
  5852. ['option' => ['value' => '01']],
  5853. '1',
  5854. '/option',
  5855. ['option' => ['value' => '02']],
  5856. '2',
  5857. '/option',
  5858. $daysRegex,
  5859. ['option' => ['value' => '10', 'selected' => 'selected']],
  5860. '10',
  5861. '/option',
  5862. $daysRegex,
  5863. '/select',
  5864. ];
  5865. $this->assertHtml($expected, $result);
  5866. $result = $this->Form->day('Contact.published', [
  5867. 'empty' => 'Published on',
  5868. ]);
  5869. $this->assertContains('Published on', $result);
  5870. }
  5871. /**
  5872. * testMinute method
  5873. *
  5874. * @return void
  5875. */
  5876. public function testMinute()
  5877. {
  5878. extract($this->dateRegex);
  5879. $result = $this->Form->minute('Model.field', ['value' => '']);
  5880. $expected = [
  5881. ['select' => ['name' => 'Model[field][minute]']],
  5882. ['option' => ['selected' => 'selected', 'value' => '']],
  5883. '/option',
  5884. ['option' => ['value' => '00']],
  5885. '00',
  5886. '/option',
  5887. ['option' => ['value' => '01']],
  5888. '01',
  5889. '/option',
  5890. ['option' => ['value' => '02']],
  5891. '02',
  5892. '/option',
  5893. $minutesRegex,
  5894. '/select',
  5895. ];
  5896. $this->assertHtml($expected, $result);
  5897. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5898. $result = $this->Form->minute('Model.field');
  5899. $expected = [
  5900. ['select' => ['name' => 'Model[field][minute]']],
  5901. ['option' => ['value' => '']],
  5902. '/option',
  5903. ['option' => ['value' => '00']],
  5904. '00',
  5905. '/option',
  5906. ['option' => ['value' => '01']],
  5907. '01',
  5908. '/option',
  5909. ['option' => ['value' => '02']],
  5910. '02',
  5911. '/option',
  5912. $minutesRegex,
  5913. ['option' => ['value' => '12', 'selected' => 'selected']],
  5914. '12',
  5915. '/option',
  5916. $minutesRegex,
  5917. '/select',
  5918. ];
  5919. $this->assertHtml($expected, $result);
  5920. $this->Form->request->data['Model']['field'] = '';
  5921. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  5922. $expected = [
  5923. ['select' => ['name' => 'Model[field][minute]']],
  5924. ['option' => ['selected' => 'selected', 'value' => '']],
  5925. '/option',
  5926. ['option' => ['value' => '00']],
  5927. '00',
  5928. '/option',
  5929. ['option' => ['value' => '05']],
  5930. '05',
  5931. '/option',
  5932. ['option' => ['value' => '10']],
  5933. '10',
  5934. '/option',
  5935. $minutesRegex,
  5936. '/select',
  5937. ];
  5938. $this->assertHtml($expected, $result);
  5939. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5940. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  5941. $expected = [
  5942. ['select' => ['name' => 'Model[field][minute]']],
  5943. ['option' => ['value' => '']],
  5944. '/option',
  5945. ['option' => ['value' => '00']],
  5946. '00',
  5947. '/option',
  5948. ['option' => ['value' => '05']],
  5949. '05',
  5950. '/option',
  5951. ['option' => ['value' => '10', 'selected' => 'selected']],
  5952. '10',
  5953. '/option',
  5954. $minutesRegex,
  5955. '/select',
  5956. ];
  5957. $this->assertHtml($expected, $result);
  5958. }
  5959. /**
  5960. * Test generating an input for the meridian.
  5961. *
  5962. * @return void
  5963. */
  5964. public function testMeridian()
  5965. {
  5966. extract($this->dateRegex);
  5967. $now = new \DateTime();
  5968. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  5969. $expected = [
  5970. ['select' => ['name' => 'Model[field][meridian]']],
  5971. ['option' => ['value' => '']],
  5972. '/option',
  5973. $meridianRegex,
  5974. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  5975. $now->format('a'),
  5976. '/option',
  5977. '*/select'
  5978. ];
  5979. $this->assertHtml($expected, $result);
  5980. }
  5981. /**
  5982. * testHour method
  5983. *
  5984. * @return void
  5985. */
  5986. public function testHour()
  5987. {
  5988. extract($this->dateRegex);
  5989. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  5990. $expected = [
  5991. ['select' => ['name' => 'Model[field][hour]']],
  5992. ['option' => ['selected' => 'selected', 'value' => '']],
  5993. '/option',
  5994. ['option' => ['value' => '01']],
  5995. '1',
  5996. '/option',
  5997. ['option' => ['value' => '02']],
  5998. '2',
  5999. '/option',
  6000. $hoursRegex,
  6001. '/select',
  6002. ];
  6003. $this->assertHtml($expected, $result);
  6004. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6005. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6006. $expected = [
  6007. ['select' => ['name' => 'Model[field][hour]']],
  6008. ['option' => ['value' => '']],
  6009. '/option',
  6010. ['option' => ['value' => '01']],
  6011. '1',
  6012. '/option',
  6013. ['option' => ['value' => '02']],
  6014. '2',
  6015. '/option',
  6016. $hoursRegex,
  6017. ['option' => ['value' => '12', 'selected' => 'selected']],
  6018. '12',
  6019. '/option',
  6020. '/select',
  6021. ];
  6022. $this->assertHtml($expected, $result);
  6023. $this->Form->request->data['Model']['field'] = '';
  6024. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6025. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6026. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6027. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6028. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6029. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6030. $expected = [
  6031. ['select' => ['name' => 'Model[field][hour]']],
  6032. ['option' => ['value' => '']],
  6033. '/option',
  6034. ['option' => ['value' => '00', 'selected' => 'selected']],
  6035. '0',
  6036. '/option',
  6037. ['option' => ['value' => '01']],
  6038. '1',
  6039. '/option',
  6040. ['option' => ['value' => '02']],
  6041. '2',
  6042. '/option',
  6043. $hoursRegex,
  6044. '/select',
  6045. ];
  6046. $this->assertHtml($expected, $result);
  6047. unset($this->Form->request->data['Model']['field']);
  6048. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6049. $thisHour = date('H');
  6050. $optValue = date('G');
  6051. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6052. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6053. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6054. $expected = [
  6055. ['select' => ['name' => 'Model[field][hour]']],
  6056. ['option' => ['value' => '']],
  6057. '/option',
  6058. ['option' => ['value' => '00']],
  6059. '0',
  6060. '/option',
  6061. ['option' => ['value' => '01', 'selected' => 'selected']],
  6062. '1',
  6063. '/option',
  6064. ['option' => ['value' => '02']],
  6065. '2',
  6066. '/option',
  6067. $hoursRegex,
  6068. '/select',
  6069. ];
  6070. $this->assertHtml($expected, $result);
  6071. }
  6072. /**
  6073. * testYear method
  6074. *
  6075. * @return void
  6076. */
  6077. public function testYear()
  6078. {
  6079. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6080. $expected = [
  6081. ['select' => ['name' => 'Model[field][year]']],
  6082. ['option' => ['selected' => 'selected', 'value' => '']],
  6083. '/option',
  6084. ['option' => ['value' => '2007']],
  6085. '2007',
  6086. '/option',
  6087. ['option' => ['value' => '2006']],
  6088. '2006',
  6089. '/option',
  6090. '/select',
  6091. ];
  6092. $this->assertHtml($expected, $result);
  6093. $result = $this->Form->year('Model.field', [
  6094. 'value' => '',
  6095. 'minYear' => 2006,
  6096. 'maxYear' => 2007,
  6097. 'orderYear' => 'asc'
  6098. ]);
  6099. $expected = [
  6100. ['select' => ['name' => 'Model[field][year]']],
  6101. ['option' => ['selected' => 'selected', 'value' => '']],
  6102. '/option',
  6103. ['option' => ['value' => '2006']],
  6104. '2006',
  6105. '/option',
  6106. ['option' => ['value' => '2007']],
  6107. '2007',
  6108. '/option',
  6109. '/select',
  6110. ];
  6111. $this->assertHtml($expected, $result);
  6112. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6113. $result = $this->Form->year('Contact.published', [
  6114. 'empty' => false,
  6115. 'minYear' => 2006,
  6116. 'maxYear' => 2007,
  6117. ]);
  6118. $expected = [
  6119. ['select' => ['name' => 'Contact[published][year]']],
  6120. ['option' => ['value' => '2007']],
  6121. '2007',
  6122. '/option',
  6123. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6124. '2006',
  6125. '/option',
  6126. '/select',
  6127. ];
  6128. $this->assertHtml($expected, $result);
  6129. $result = $this->Form->year('Contact.published', [
  6130. 'empty' => 'Published on',
  6131. ]);
  6132. $this->assertContains('Published on', $result);
  6133. }
  6134. /**
  6135. * Test minYear being prior to the unix epoch
  6136. *
  6137. * @return void
  6138. */
  6139. public function testInputDatetimePreEpoch()
  6140. {
  6141. $start = date('Y') - 80;
  6142. $end = date('Y') - 18;
  6143. $result = $this->Form->input('birth_year', [
  6144. 'type' => 'date',
  6145. 'label' => 'Birth Year',
  6146. 'minYear' => $start,
  6147. 'maxYear' => $end,
  6148. 'month' => false,
  6149. 'day' => false,
  6150. ]);
  6151. $this->assertContains('value="' . $start . '">' . $start, $result);
  6152. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6153. $this->assertNotContains('value="00">00', $result);
  6154. }
  6155. /**
  6156. * testYearAutoExpandRange method
  6157. *
  6158. * @return void
  6159. */
  6160. public function testYearAutoExpandRange()
  6161. {
  6162. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6163. $result = $this->Form->year('User.birthday');
  6164. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6165. $result = $matches[1];
  6166. $expected = range(date('Y') + 5, 1930);
  6167. $this->assertEquals($expected, $result);
  6168. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6169. $result = $this->Form->year('Project.release');
  6170. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6171. $result = $matches[1];
  6172. $expected = range(2050, date('Y') - 5);
  6173. $this->assertEquals($expected, $result);
  6174. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6175. $result = $this->Form->year('Project.release', [
  6176. 'minYear' => 1890,
  6177. 'maxYear' => 1900
  6178. ]);
  6179. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6180. $result = $matches[1];
  6181. $expected = range(1900, 1881);
  6182. $this->assertEquals($expected, $result);
  6183. }
  6184. /**
  6185. * Test that input() accepts the type of date and passes options in.
  6186. *
  6187. * @return void
  6188. */
  6189. public function testInputDate()
  6190. {
  6191. $this->Form->request->data = [
  6192. 'month_year' => ['month' => date('m')],
  6193. ];
  6194. $this->Form->create($this->article);
  6195. $result = $this->Form->input('month_year', [
  6196. 'label' => false,
  6197. 'type' => 'date',
  6198. 'minYear' => 2006,
  6199. 'maxYear' => 2008
  6200. ]);
  6201. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6202. $this->assertNotContains('value="2008" selected="selected"', $result);
  6203. }
  6204. /**
  6205. * Test the label option being set to false.
  6206. *
  6207. * @return void
  6208. */
  6209. public function testInputLabelFalse()
  6210. {
  6211. $this->Form->create($this->article);
  6212. $result = $this->Form->input('title', ['label' => false]);
  6213. $expected = [
  6214. 'div' => ['class' => 'input text required'],
  6215. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6216. '/div'
  6217. ];
  6218. $this->assertHtml($expected, $result);
  6219. }
  6220. /**
  6221. * testInputDateMaxYear method
  6222. *
  6223. * Let's say we want to only allow users born from 2006 to 2008 to register
  6224. * This being the first signup page, we still don't have any data
  6225. *
  6226. * @return void
  6227. */
  6228. public function testInputDateMaxYear()
  6229. {
  6230. $this->Form->request->data = [];
  6231. $this->Form->create($this->article);
  6232. $result = $this->Form->input('birthday', [
  6233. 'label' => false,
  6234. 'type' => 'date',
  6235. 'minYear' => 2006,
  6236. 'maxYear' => 2008,
  6237. 'default' => true
  6238. ]);
  6239. $this->assertContains('value="2008" selected="selected"', $result);
  6240. $this->assertContains('value="2006"', $result);
  6241. $this->assertNotContains('value="2005"', $result);
  6242. $this->assertNotContains('value="2009"', $result);
  6243. }
  6244. /**
  6245. * testTextArea method
  6246. *
  6247. * @return void
  6248. */
  6249. public function testTextArea()
  6250. {
  6251. $this->Form->request->data = ['field' => 'some test data'];
  6252. $result = $this->Form->textarea('field');
  6253. $expected = [
  6254. 'textarea' => ['name' => 'field', 'rows' => 5],
  6255. 'some test data',
  6256. '/textarea',
  6257. ];
  6258. $this->assertHtml($expected, $result);
  6259. $result = $this->Form->textarea('user.bio');
  6260. $expected = [
  6261. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6262. '/textarea',
  6263. ];
  6264. $this->assertHtml($expected, $result);
  6265. $this->Form->request->data = ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'];
  6266. $result = $this->Form->textarea('field');
  6267. $expected = [
  6268. 'textarea' => ['name' => 'field', 'rows' => 5],
  6269. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6270. '/textarea',
  6271. ];
  6272. $this->assertHtml($expected, $result);
  6273. $this->Form->request->data = [
  6274. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  6275. ];
  6276. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6277. $expected = [
  6278. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6279. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6280. '/textarea',
  6281. ];
  6282. $this->assertHtml($expected, $result);
  6283. $result = $this->Form->textarea('0.OtherModel.field');
  6284. $expected = [
  6285. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6286. '/textarea'
  6287. ];
  6288. $this->assertHtml($expected, $result);
  6289. }
  6290. /**
  6291. * testTextAreaWithStupidCharacters method
  6292. *
  6293. * test text area with non-ascii characters
  6294. *
  6295. * @return void
  6296. */
  6297. public function testTextAreaWithStupidCharacters()
  6298. {
  6299. $result = $this->Form->textarea('Post.content', [
  6300. 'value' => "GREAT®",
  6301. 'rows' => '15',
  6302. 'cols' => '75'
  6303. ]);
  6304. $expected = [
  6305. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6306. 'GREAT®',
  6307. '/textarea',
  6308. ];
  6309. $this->assertHtml($expected, $result);
  6310. }
  6311. /**
  6312. * Test textareas waxlength read from schema.
  6313. *
  6314. * @return void
  6315. */
  6316. public function testTextAreaMaxLength()
  6317. {
  6318. $this->Form->create([
  6319. 'schema' => [
  6320. 'stuff' => ['type' => 'string', 'length' => 10],
  6321. ]
  6322. ]);
  6323. $result = $this->Form->input('other', ['type' => 'textarea']);
  6324. $expected = [
  6325. 'div' => ['class' => 'input textarea'],
  6326. 'label' => ['for' => 'other'],
  6327. 'Other',
  6328. '/label',
  6329. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6330. '/textarea',
  6331. '/div'
  6332. ];
  6333. $this->assertHtml($expected, $result);
  6334. $result = $this->Form->input('stuff', ['type' => 'textarea']);
  6335. $expected = [
  6336. 'div' => ['class' => 'input textarea'],
  6337. 'label' => ['for' => 'stuff'],
  6338. 'Stuff',
  6339. '/label',
  6340. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6341. '/textarea',
  6342. '/div'
  6343. ];
  6344. $this->assertHtml($expected, $result);
  6345. }
  6346. /**
  6347. * testHiddenField method
  6348. *
  6349. * @return void
  6350. */
  6351. public function testHiddenField()
  6352. {
  6353. $this->article['errors'] = [
  6354. 'field' => true
  6355. ];
  6356. $this->Form->request->data['field'] = 'test';
  6357. $this->Form->create($this->article);
  6358. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6359. $expected = [
  6360. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6361. $this->assertHtml($expected, $result);
  6362. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6363. $expected = [
  6364. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6365. ];
  6366. $this->assertHtml($expected, $result);
  6367. }
  6368. /**
  6369. * testFileUploadField method
  6370. *
  6371. * @return void
  6372. */
  6373. public function testFileUploadField()
  6374. {
  6375. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6376. $result = $this->Form->file('Model.upload');
  6377. $this->assertHtml($expected, $result);
  6378. $this->Form->request->data['Model']['upload'] = [
  6379. 'name' => '', 'type' => '', 'tmp_name' => '',
  6380. 'error' => 4, 'size' => 0
  6381. ];
  6382. $result = $this->Form->file('Model.upload');
  6383. $this->assertHtml($expected, $result);
  6384. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6385. $result = $this->Form->file('Model.upload');
  6386. $this->assertHtml($expected, $result);
  6387. }
  6388. /**
  6389. * test File upload input on a model not used in create();
  6390. *
  6391. * @return void
  6392. */
  6393. public function testFileUploadOnOtherModel()
  6394. {
  6395. $this->Form->create($this->article, ['type' => 'file']);
  6396. $result = $this->Form->file('ValidateProfile.city');
  6397. $expected = [
  6398. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6399. ];
  6400. $this->assertHtml($expected, $result);
  6401. }
  6402. /**
  6403. * testButton method
  6404. *
  6405. * @return void
  6406. */
  6407. public function testButton()
  6408. {
  6409. $result = $this->Form->button('Hi');
  6410. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6411. $this->assertHtml($expected, $result);
  6412. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6413. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6414. $this->assertHtml($expected, $result);
  6415. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6416. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6417. $this->assertHtml($expected, $result);
  6418. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6419. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6420. $this->assertHtml($expected, $result);
  6421. $result = $this->Form->button('No type', ['type' => false]);
  6422. $expected = ['button' => [], 'No type', '/button'];
  6423. $this->assertHtml($expected, $result);
  6424. $result = $this->Form->button('Upload Text', [
  6425. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6426. 'escape' => false
  6427. ]);
  6428. $this->assertNotRegExp('/\&039/', $result);
  6429. }
  6430. /**
  6431. * Test that button() makes unlocked fields by default.
  6432. *
  6433. * @return void
  6434. */
  6435. public function testButtonUnlockedByDefault()
  6436. {
  6437. $this->Form->request->params['_csrfToken'] = 'secured';
  6438. $this->Form->button('Save', ['name' => 'save']);
  6439. $this->Form->button('Clear');
  6440. $result = $this->Form->unlockField();
  6441. $this->assertEquals(['save'], $result);
  6442. }
  6443. /**
  6444. * testPostButton method
  6445. *
  6446. * @return void
  6447. */
  6448. public function testPostButton()
  6449. {
  6450. $result = $this->Form->postButton('Hi', '/controller/action');
  6451. $expected = [
  6452. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6453. 'div' => ['style' => 'display:none;'],
  6454. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6455. '/div',
  6456. 'button' => ['type' => 'submit'],
  6457. 'Hi',
  6458. '/button',
  6459. '/form'
  6460. ];
  6461. $this->assertHtml($expected, $result);
  6462. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6463. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  6464. }
  6465. public function testPostButtonMethodType()
  6466. {
  6467. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6468. $expected = [
  6469. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6470. 'div' => ['style' => 'display:none;'],
  6471. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6472. '/div',
  6473. 'button' => ['type' => 'submit'],
  6474. 'Hi',
  6475. '/button',
  6476. '/form'
  6477. ];
  6478. $this->assertHtml($expected, $result);
  6479. }
  6480. public function testPostButtonFormOptions()
  6481. {
  6482. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6483. $expected = [
  6484. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6485. 'div' => ['style' => 'display:none;'],
  6486. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6487. '/div',
  6488. 'button' => ['type' => 'submit'],
  6489. 'Hi',
  6490. '/button',
  6491. '/form'
  6492. ];
  6493. $this->assertHtml($expected, $result);
  6494. }
  6495. /**
  6496. * Test using postButton with N dimensional data.
  6497. *
  6498. * @return void
  6499. */
  6500. public function testPostButtonNestedData()
  6501. {
  6502. $data = [
  6503. 'one' => [
  6504. 'two' => [
  6505. 3, 4, 5
  6506. ]
  6507. ]
  6508. ];
  6509. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6510. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6511. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6512. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6513. }
  6514. /**
  6515. * Test that postButton adds _Token fields.
  6516. *
  6517. * @return void
  6518. */
  6519. public function testSecurePostButton()
  6520. {
  6521. $this->Form->request->params['_csrfToken'] = 'testkey';
  6522. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  6523. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6524. $tokenDebug = urlencode(json_encode([
  6525. '/posts/delete/1',
  6526. [],
  6527. []
  6528. ]));
  6529. $expected = [
  6530. 'form' => [
  6531. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6532. ],
  6533. ['div' => ['style' => 'display:none;']],
  6534. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6535. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6536. '/div',
  6537. 'button' => ['type' => 'submit'],
  6538. 'Delete',
  6539. '/button',
  6540. ['div' => ['style' => 'display:none;']],
  6541. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6542. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6543. ['input' => [
  6544. 'type' => 'hidden', 'name' => '_Token[debug]',
  6545. 'value' => $tokenDebug,
  6546. ]],
  6547. '/div',
  6548. '/form',
  6549. ];
  6550. $this->assertHtml($expected, $result);
  6551. }
  6552. /**
  6553. * testPostLink method
  6554. *
  6555. * @return void
  6556. */
  6557. public function testPostLink()
  6558. {
  6559. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6560. $expected = [
  6561. 'form' => [
  6562. 'method' => 'post', 'action' => '/posts/delete/1',
  6563. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6564. ],
  6565. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6566. '/form',
  6567. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6568. 'Delete',
  6569. '/a'
  6570. ];
  6571. $this->assertHtml($expected, $result);
  6572. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6573. $expected = [
  6574. 'form' => [
  6575. 'method' => 'post', 'action' => '/posts/delete/1',
  6576. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6577. ],
  6578. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6579. '/form',
  6580. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6581. 'Delete',
  6582. '/a'
  6583. ];
  6584. $this->assertHtml($expected, $result);
  6585. $result = $this->Form->postLink(
  6586. 'Delete',
  6587. '/posts/delete/1',
  6588. ['target' => '_blank', 'class' => 'btn btn-danger']
  6589. );
  6590. $expected = [
  6591. 'form' => [
  6592. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6593. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6594. ],
  6595. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6596. '/form',
  6597. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6598. 'Delete',
  6599. '/a'
  6600. ];
  6601. $this->assertHtml($expected, $result);
  6602. }
  6603. /**
  6604. * Test the confirm option for postLink()
  6605. *
  6606. * @return void
  6607. */
  6608. public function testPostLinkWithConfirm()
  6609. {
  6610. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6611. $expected = [
  6612. 'form' => [
  6613. 'method' => 'post', 'action' => '/posts/delete/1',
  6614. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6615. ],
  6616. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6617. '/form',
  6618. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  6619. 'Delete',
  6620. '/a'
  6621. ];
  6622. $this->assertHtml($expected, $result);
  6623. $result = $this->Form->postLink(
  6624. 'Delete',
  6625. '/posts/delete/1',
  6626. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  6627. );
  6628. $expected = [
  6629. 'form' => [
  6630. 'method' => 'post', 'action' => '/posts/delete/1',
  6631. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6632. ],
  6633. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6634. '/form',
  6635. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\nthis \\\\&quot;deletion\\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  6636. 'Delete',
  6637. '/a'
  6638. ];
  6639. $this->assertHtml($expected, $result);
  6640. }
  6641. /**
  6642. * test postLink() with query string args.
  6643. *
  6644. * @return void
  6645. */
  6646. public function testPostLinkWithQuery()
  6647. {
  6648. $result = $this->Form->postLink(
  6649. 'Delete',
  6650. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6651. );
  6652. $expected = [
  6653. 'form' => [
  6654. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  6655. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6656. ],
  6657. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6658. '/form',
  6659. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6660. 'Delete',
  6661. '/a'
  6662. ];
  6663. $this->assertHtml($expected, $result);
  6664. }
  6665. /**
  6666. * Test postLink with additional data.
  6667. *
  6668. * @return void
  6669. */
  6670. public function testPostLinkWithData()
  6671. {
  6672. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6673. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  6674. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6675. $this->Form->create($entity);
  6676. $this->Form->end();
  6677. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6678. $this->assertContains(
  6679. '<input type="hidden" name="name" value="show"',
  6680. $result,
  6681. 'should not contain entity data.'
  6682. );
  6683. }
  6684. /**
  6685. * Test that security hashes for postLink include the url.
  6686. *
  6687. * @return void
  6688. */
  6689. public function testPostLinkSecurityHash()
  6690. {
  6691. $hash = Security::hash(
  6692. '/posts/delete/1' .
  6693. serialize(['id' => '1']) .
  6694. '' .
  6695. Security::salt()
  6696. );
  6697. $hash .= '%3Aid';
  6698. $this->Form->request->params['_Token']['key'] = 'test';
  6699. $result = $this->Form->postLink(
  6700. 'Delete',
  6701. '/posts/delete/1',
  6702. ['data' => ['id' => 1]]
  6703. );
  6704. $tokenDebug = urlencode(json_encode([
  6705. '/posts/delete/1',
  6706. [
  6707. 'id' => 1
  6708. ],
  6709. []
  6710. ]));
  6711. $expected = [
  6712. 'form' => [
  6713. 'method' => 'post', 'action' => '/posts/delete/1',
  6714. 'name', 'style' => 'display:none;'
  6715. ],
  6716. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6717. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6718. 'div' => ['style' => 'display:none;'],
  6719. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6720. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6721. ['input' => [
  6722. 'type' => 'hidden', 'name' => '_Token[debug]',
  6723. 'value' => $tokenDebug,
  6724. ]],
  6725. '/div',
  6726. '/form',
  6727. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6728. 'Delete',
  6729. '/a'
  6730. ];
  6731. $this->assertHtml($expected, $result);
  6732. }
  6733. /**
  6734. * Test that postLink doesn't modify the fields in the containing form.
  6735. *
  6736. * postLink() calls inside open forms should not modify the field list
  6737. * for the form.
  6738. *
  6739. * @return void
  6740. */
  6741. public function testPostLinkSecurityHashBlockMode()
  6742. {
  6743. $hash = Security::hash(
  6744. '/posts/delete/1' .
  6745. serialize([]) .
  6746. '' .
  6747. Security::salt()
  6748. );
  6749. $hash .= '%3A';
  6750. $this->Form->request->params['_Token']['key'] = 'test';
  6751. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  6752. $this->Form->input('title');
  6753. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6754. $result = $this->View->fetch('postLink');
  6755. $this->assertEquals(['title'], $this->Form->fields);
  6756. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  6757. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  6758. }
  6759. /**
  6760. * Test that security does not include debug token if debug is false.
  6761. *
  6762. * @return void
  6763. */
  6764. public function testPostLinkSecurityHashNoDebugMode()
  6765. {
  6766. Configure::write('debug', false);
  6767. $hash = Security::hash(
  6768. '/posts/delete/1' .
  6769. serialize(['id' => '1']) .
  6770. '' .
  6771. Security::salt()
  6772. );
  6773. $hash .= '%3Aid';
  6774. $this->Form->request->params['_Token']['key'] = 'test';
  6775. $result = $this->Form->postLink(
  6776. 'Delete',
  6777. '/posts/delete/1',
  6778. ['data' => ['id' => 1]]
  6779. );
  6780. $expected = [
  6781. 'form' => [
  6782. 'method' => 'post', 'action' => '/posts/delete/1',
  6783. 'name', 'style' => 'display:none;'
  6784. ],
  6785. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6786. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6787. 'div' => ['style' => 'display:none;'],
  6788. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6789. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6790. '/div',
  6791. '/form',
  6792. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6793. 'Delete',
  6794. '/a'
  6795. ];
  6796. $this->assertHtml($expected, $result);
  6797. }
  6798. /**
  6799. * Test using postLink with N dimensional data.
  6800. *
  6801. * @return void
  6802. */
  6803. public function testPostLinkNestedData()
  6804. {
  6805. $data = [
  6806. 'one' => [
  6807. 'two' => [
  6808. 3, 4, 5
  6809. ]
  6810. ]
  6811. ];
  6812. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  6813. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6814. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6815. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6816. }
  6817. /**
  6818. * test creating postLinks after a GET form.
  6819. *
  6820. * @return void
  6821. */
  6822. public function testPostLinkAfterGetForm()
  6823. {
  6824. $this->Form->request->params['_csrfToken'] = 'testkey';
  6825. $this->Form->request->params['_Token'] = 'val';
  6826. $this->Form->create($this->article, ['type' => 'get']);
  6827. $this->Form->end();
  6828. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6829. $tokenDebug = urlencode(json_encode([
  6830. '/posts/delete/1',
  6831. [],
  6832. []
  6833. ]));
  6834. $expected = [
  6835. 'form' => [
  6836. 'method' => 'post', 'action' => '/posts/delete/1',
  6837. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6838. ],
  6839. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6840. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6841. 'div' => ['style' => 'display:none;'],
  6842. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6843. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6844. ['input' => [
  6845. 'type' => 'hidden', 'name' => '_Token[debug]',
  6846. 'value' => $tokenDebug,
  6847. ]],
  6848. '/div',
  6849. '/form',
  6850. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6851. 'Delete',
  6852. '/a'
  6853. ];
  6854. $this->assertHtml($expected, $result);
  6855. }
  6856. /**
  6857. * Test that postLink adds form tags to view block
  6858. *
  6859. * @return void
  6860. */
  6861. public function testPostLinkFormBuffer()
  6862. {
  6863. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6864. $expected = [
  6865. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6866. 'Delete',
  6867. '/a'
  6868. ];
  6869. $this->assertHtml($expected, $result);
  6870. $result = $this->View->fetch('postLink');
  6871. $expected = [
  6872. 'form' => [
  6873. 'method' => 'post', 'action' => '/posts/delete/1',
  6874. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6875. ],
  6876. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6877. '/form'
  6878. ];
  6879. $this->assertHtml($expected, $result);
  6880. $result = $this->Form->postLink(
  6881. 'Delete',
  6882. '/posts/delete/2',
  6883. ['block' => true, 'method' => 'DELETE']
  6884. );
  6885. $expected = [
  6886. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6887. 'Delete',
  6888. '/a'
  6889. ];
  6890. $this->assertHtml($expected, $result);
  6891. $result = $this->View->fetch('postLink');
  6892. $expected = [
  6893. 'form' => [
  6894. 'method' => 'post', 'action' => '/posts/delete/1',
  6895. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6896. ],
  6897. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6898. '/form',
  6899. [
  6900. 'form' => [
  6901. 'method' => 'post', 'action' => '/posts/delete/2',
  6902. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6903. ],
  6904. ],
  6905. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  6906. '/form'
  6907. ];
  6908. $this->assertHtml($expected, $result);
  6909. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  6910. $expected = [
  6911. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6912. 'Delete',
  6913. '/a'
  6914. ];
  6915. $this->assertHtml($expected, $result);
  6916. $result = $this->View->fetch('foobar');
  6917. $expected = [
  6918. 'form' => [
  6919. 'method' => 'post', 'action' => '/posts/delete/1',
  6920. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6921. ],
  6922. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6923. '/form'
  6924. ];
  6925. $this->assertHtml($expected, $result);
  6926. }
  6927. /**
  6928. * testSubmitButton method
  6929. *
  6930. * @return void
  6931. */
  6932. public function testSubmitButton()
  6933. {
  6934. $result = $this->Form->submit('');
  6935. $expected = [
  6936. 'div' => ['class' => 'submit'],
  6937. 'input' => ['type' => 'submit', 'value' => ''],
  6938. '/div'
  6939. ];
  6940. $this->assertHtml($expected, $result);
  6941. $result = $this->Form->submit('Test Submit');
  6942. $expected = [
  6943. 'div' => ['class' => 'submit'],
  6944. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  6945. '/div'
  6946. ];
  6947. $this->assertHtml($expected, $result);
  6948. $result = $this->Form->submit('Next >');
  6949. $expected = [
  6950. 'div' => ['class' => 'submit'],
  6951. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  6952. '/div'
  6953. ];
  6954. $this->assertHtml($expected, $result);
  6955. $result = $this->Form->submit('Next >', ['escape' => false]);
  6956. $expected = [
  6957. 'div' => ['class' => 'submit'],
  6958. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  6959. '/div'
  6960. ];
  6961. $this->assertHtml($expected, $result);
  6962. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  6963. $expected = [
  6964. 'div' => ['class' => 'submit'],
  6965. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  6966. '/div'
  6967. ];
  6968. $this->assertHtml($expected, $result);
  6969. }
  6970. /**
  6971. * test image submit types.
  6972. *
  6973. * @return void
  6974. */
  6975. public function testSubmitImage()
  6976. {
  6977. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6978. $expected = [
  6979. 'div' => ['class' => 'submit'],
  6980. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6981. '/div'
  6982. ];
  6983. $this->assertHtml($expected, $result);
  6984. $result = $this->Form->submit('/relative/cake.power.gif');
  6985. $expected = [
  6986. 'div' => ['class' => 'submit'],
  6987. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6988. '/div'
  6989. ];
  6990. $this->assertHtml($expected, $result);
  6991. $result = $this->Form->submit('cake.power.gif');
  6992. $expected = [
  6993. 'div' => ['class' => 'submit'],
  6994. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6995. '/div'
  6996. ];
  6997. $this->assertHtml($expected, $result);
  6998. $result = $this->Form->submit('Not.an.image');
  6999. $expected = [
  7000. 'div' => ['class' => 'submit'],
  7001. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7002. '/div'
  7003. ];
  7004. $this->assertHtml($expected, $result);
  7005. }
  7006. /**
  7007. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7008. * be submitted at a time.
  7009. *
  7010. * @return void
  7011. */
  7012. public function testSubmitUnlockedByDefault()
  7013. {
  7014. $this->Form->request->params['_Token'] = 'secured';
  7015. $this->Form->submit('Go go');
  7016. $this->Form->submit('Save', ['name' => 'save']);
  7017. $result = $this->Form->unlockField();
  7018. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7019. }
  7020. /**
  7021. * Test submit image with timestamps.
  7022. *
  7023. * @return void
  7024. */
  7025. public function testSubmitImageTimestamp()
  7026. {
  7027. Configure::write('Asset.timestamp', 'force');
  7028. $result = $this->Form->submit('cake.power.gif');
  7029. $expected = [
  7030. 'div' => ['class' => 'submit'],
  7031. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7032. '/div'
  7033. ];
  7034. $this->assertHtml($expected, $result);
  7035. }
  7036. /**
  7037. * test that datetime() works with GET style forms.
  7038. *
  7039. * @return void
  7040. */
  7041. public function testDateTimeWithGetForms()
  7042. {
  7043. extract($this->dateRegex);
  7044. $this->Form->create($this->article, ['type' => 'get']);
  7045. $result = $this->Form->datetime('created');
  7046. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7047. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7048. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7049. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7050. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7051. }
  7052. /**
  7053. * testForMagicInputNonExistingNorValidated method
  7054. *
  7055. * @return void
  7056. */
  7057. public function testForMagicInputNonExistingNorValidated()
  7058. {
  7059. $result = $this->Form->create($this->article);
  7060. $this->Form->templates(['inputContainer' => '{{content}}']);
  7061. $result = $this->Form->input('non_existing_nor_validated');
  7062. $expected = [
  7063. 'label' => ['for' => 'non-existing-nor-validated'],
  7064. 'Non Existing Nor Validated',
  7065. '/label',
  7066. 'input' => [
  7067. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7068. 'id' => 'non-existing-nor-validated'
  7069. ]
  7070. ];
  7071. $this->assertHtml($expected, $result);
  7072. $result = $this->Form->input('non_existing_nor_validated', [
  7073. 'val' => 'my value'
  7074. ]);
  7075. $expected = [
  7076. 'label' => ['for' => 'non-existing-nor-validated'],
  7077. 'Non Existing Nor Validated',
  7078. '/label',
  7079. 'input' => [
  7080. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7081. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7082. ]
  7083. ];
  7084. $this->assertHtml($expected, $result);
  7085. $this->Form->request->data = ['non_existing_nor_validated' => 'CakePHP magic'];
  7086. $result = $this->Form->input('non_existing_nor_validated');
  7087. $expected = [
  7088. 'label' => ['for' => 'non-existing-nor-validated'],
  7089. 'Non Existing Nor Validated',
  7090. '/label',
  7091. 'input' => [
  7092. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7093. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7094. ]
  7095. ];
  7096. $this->assertHtml($expected, $result);
  7097. }
  7098. /**
  7099. * testFormMagicInputLabel method
  7100. *
  7101. * @return void
  7102. */
  7103. public function testFormMagicInputLabel()
  7104. {
  7105. TableRegistry::get('Contacts', [
  7106. 'className' => __NAMESPACE__ . '\ContactsTable'
  7107. ]);
  7108. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7109. $this->Form->templates(['inputContainer' => '{{content}}']);
  7110. $result = $this->Form->input('Contacts.name', ['label' => 'My label']);
  7111. $expected = [
  7112. 'label' => ['for' => 'contacts-name'],
  7113. 'My label',
  7114. '/label',
  7115. 'input' => [
  7116. 'type' => 'text', 'name' => 'Contacts[name]',
  7117. 'id' => 'contacts-name', 'maxlength' => '255'
  7118. ]
  7119. ];
  7120. $this->assertHtml($expected, $result);
  7121. $result = $this->Form->input('name', [
  7122. 'label' => ['class' => 'mandatory']
  7123. ]);
  7124. $expected = [
  7125. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7126. 'Name',
  7127. '/label',
  7128. 'input' => [
  7129. 'type' => 'text', 'name' => 'name',
  7130. 'id' => 'name', 'maxlength' => '255'
  7131. ]
  7132. ];
  7133. $this->assertHtml($expected, $result);
  7134. $result = $this->Form->input('name', [
  7135. 'div' => false,
  7136. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7137. ]);
  7138. $expected = [
  7139. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7140. 'My label',
  7141. '/label',
  7142. 'input' => [
  7143. 'type' => 'text', 'name' => 'name',
  7144. 'id' => 'name', 'maxlength' => '255'
  7145. ]
  7146. ];
  7147. $this->assertHtml($expected, $result);
  7148. $result = $this->Form->input('Contact.name', [
  7149. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7150. ]);
  7151. $expected = [
  7152. 'label' => ['for' => 'my_id'],
  7153. 'Name',
  7154. '/label',
  7155. 'input' => [
  7156. 'type' => 'text', 'name' => 'Contact[name]',
  7157. 'id' => 'my_id', 'maxlength' => '255'
  7158. ]
  7159. ];
  7160. $this->assertHtml($expected, $result);
  7161. $result = $this->Form->input('1.id');
  7162. $expected = ['input' => [
  7163. 'type' => 'hidden', 'name' => '1[id]',
  7164. 'id' => '1-id'
  7165. ]];
  7166. $this->assertHtml($expected, $result);
  7167. $result = $this->Form->input("1.name");
  7168. $expected = [
  7169. 'label' => ['for' => '1-name'],
  7170. 'Name',
  7171. '/label',
  7172. 'input' => [
  7173. 'type' => 'text', 'name' => '1[name]',
  7174. 'id' => '1-name', 'maxlength' => '255'
  7175. ]
  7176. ];
  7177. $this->assertHtml($expected, $result);
  7178. }
  7179. /**
  7180. * testFormEnd method
  7181. *
  7182. * @return void
  7183. */
  7184. public function testFormEnd()
  7185. {
  7186. $this->assertEquals('</form>', $this->Form->end());
  7187. }
  7188. /**
  7189. * Test the generation of fields for a multi record form.
  7190. *
  7191. * @return void
  7192. */
  7193. public function testMultiRecordForm()
  7194. {
  7195. $this->loadFixtures('Articles', 'Comments');
  7196. $articles = TableRegistry::get('Articles');
  7197. $articles->hasMany('Comments');
  7198. $comment = new Entity(['comment' => 'Value']);
  7199. $article = new Article(['comments' => [$comment]]);
  7200. $this->Form->create([$article]);
  7201. $result = $this->Form->input('0.comments.1.comment');
  7202. //@codingStandardsIgnoreStart
  7203. $expected = [
  7204. 'div' => ['class' => 'input textarea'],
  7205. 'label' => ['for' => '0-comments-1-comment'],
  7206. 'Comment',
  7207. '/label',
  7208. 'textarea' => [
  7209. 'name',
  7210. 'id' => '0-comments-1-comment',
  7211. 'rows' => 5
  7212. ],
  7213. '/textarea',
  7214. '/div'
  7215. ];
  7216. //@codingStandardsIgnoreEnd
  7217. $this->assertHtml($expected, $result);
  7218. $result = $this->Form->input('0.comments.0.comment');
  7219. //@codingStandardsIgnoreStart
  7220. $expected = [
  7221. 'div' => ['class' => 'input textarea'],
  7222. 'label' => ['for' => '0-comments-0-comment'],
  7223. 'Comment',
  7224. '/label',
  7225. 'textarea' => [
  7226. 'name',
  7227. 'id' => '0-comments-0-comment',
  7228. 'rows' => 5
  7229. ],
  7230. 'Value',
  7231. '/textarea',
  7232. '/div'
  7233. ];
  7234. //@codingStandardsIgnoreEnd
  7235. $this->assertHtml($expected, $result);
  7236. $comment->errors('comment', ['Not valid']);
  7237. $result = $this->Form->input('0.comments.0.comment');
  7238. //@codingStandardsIgnoreStart
  7239. $expected = [
  7240. 'div' => ['class' => 'input textarea error'],
  7241. 'label' => ['for' => '0-comments-0-comment'],
  7242. 'Comment',
  7243. '/label',
  7244. 'textarea' => [
  7245. 'name',
  7246. 'class' => 'form-error',
  7247. 'id' => '0-comments-0-comment',
  7248. 'rows' => 5
  7249. ],
  7250. 'Value',
  7251. '/textarea',
  7252. ['div' => ['class' => 'error-message']],
  7253. 'Not valid',
  7254. '/div',
  7255. '/div'
  7256. ];
  7257. //@codingStandardsIgnoreEnd
  7258. $this->assertHtml($expected, $result);
  7259. TableRegistry::get('Comments')
  7260. ->validator('default')
  7261. ->allowEmpty('comment', false);
  7262. $result = $this->Form->input('0.comments.1.comment');
  7263. //@codingStandardsIgnoreStart
  7264. $expected = [
  7265. 'div' => ['class' => 'input textarea required'],
  7266. 'label' => ['for' => '0-comments-1-comment'],
  7267. 'Comment',
  7268. '/label',
  7269. 'textarea' => [
  7270. 'name',
  7271. 'required' => 'required',
  7272. 'id' => '0-comments-1-comment',
  7273. 'rows' => 5
  7274. ],
  7275. '/textarea',
  7276. '/div'
  7277. ];
  7278. //@codingStandardsIgnoreEnd
  7279. $this->assertHtml($expected, $result);
  7280. }
  7281. /**
  7282. * test that some html5 inputs + FormHelper::__call() work
  7283. *
  7284. * @return void
  7285. */
  7286. public function testHtml5Inputs()
  7287. {
  7288. $result = $this->Form->email('User.email');
  7289. $expected = [
  7290. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7291. ];
  7292. $this->assertHtml($expected, $result);
  7293. $result = $this->Form->search('User.query');
  7294. $expected = [
  7295. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7296. ];
  7297. $this->assertHtml($expected, $result);
  7298. $result = $this->Form->search('User.query', ['value' => 'test']);
  7299. $expected = [
  7300. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7301. ];
  7302. $this->assertHtml($expected, $result);
  7303. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7304. $expected = [
  7305. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7306. ];
  7307. $this->assertHtml($expected, $result);
  7308. }
  7309. /**
  7310. * Test accessing html5 inputs through input().
  7311. *
  7312. * @return void
  7313. */
  7314. public function testHtml5InputWithInput()
  7315. {
  7316. $this->Form->create();
  7317. $this->Form->templates(['inputContainer' => '{{content}}']);
  7318. $result = $this->Form->input('website', [
  7319. 'type' => 'url',
  7320. 'val' => 'http://domain.tld',
  7321. 'label' => false
  7322. ]);
  7323. $expected = [
  7324. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7325. ];
  7326. $this->assertHtml($expected, $result);
  7327. }
  7328. /**
  7329. * Test errors when field name is missing.
  7330. *
  7331. * @expectedException \Cake\Core\Exception\Exception
  7332. * @return void
  7333. */
  7334. public function testHtml5InputException()
  7335. {
  7336. $this->Form->email();
  7337. }
  7338. /**
  7339. * Tests that formhelper sets required attributes.
  7340. *
  7341. * @return void
  7342. */
  7343. public function testRequiredAttribute()
  7344. {
  7345. $this->article['required'] = [
  7346. 'title' => true,
  7347. 'body' => false,
  7348. ];
  7349. $this->Form->create($this->article);
  7350. $result = $this->Form->input('title');
  7351. $expected = [
  7352. 'div' => ['class' => 'input text required'],
  7353. 'label' => ['for' => 'title'],
  7354. 'Title',
  7355. '/label',
  7356. 'input' => [
  7357. 'type' => 'text',
  7358. 'name' => 'title',
  7359. 'id' => 'title',
  7360. 'required' => 'required',
  7361. ],
  7362. '/div'
  7363. ];
  7364. $this->assertHtml($expected, $result);
  7365. $result = $this->Form->input('title', ['required' => false]);
  7366. $this->assertNotContains('required', $result);
  7367. $result = $this->Form->input('body');
  7368. $expected = [
  7369. 'div' => ['class' => 'input text'],
  7370. 'label' => ['for' => 'body'],
  7371. 'Body',
  7372. '/label',
  7373. 'input' => [
  7374. 'type' => 'text',
  7375. 'name' => 'body',
  7376. 'id' => 'body',
  7377. ],
  7378. '/div'
  7379. ];
  7380. $this->assertHtml($expected, $result);
  7381. $result = $this->Form->input('body', ['required' => true]);
  7382. $this->assertContains('required', $result);
  7383. }
  7384. /**
  7385. * Tests that it is possible to put inputs outside of the label
  7386. *
  7387. * @return void
  7388. */
  7389. public function testInputsNotNested()
  7390. {
  7391. $this->Form->templates([
  7392. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7393. 'formGroup' => '{{input}}{{label}}',
  7394. ]);
  7395. $result = $this->Form->input('foo', ['type' => 'checkbox']);
  7396. $expected = [
  7397. 'div' => ['class' => 'input checkbox'],
  7398. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7399. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7400. 'label' => ['for' => 'foo'],
  7401. 'Foo',
  7402. '/label',
  7403. '/div'
  7404. ];
  7405. $this->assertHtml($expected, $result);
  7406. $result = $this->Form->input('foo', ['type' => 'checkbox', 'label' => false]);
  7407. $expected = [
  7408. 'div' => ['class' => 'input checkbox'],
  7409. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7410. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7411. '/div'
  7412. ];
  7413. $this->assertHtml($expected, $result);
  7414. $result = $this->Form->input('confirm', [
  7415. 'type' => 'radio',
  7416. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7417. ]);
  7418. $expected = [
  7419. 'div' => ['class' => 'input radio'],
  7420. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7421. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7422. ['label' => ['for' => 'confirm-y']],
  7423. 'Yes',
  7424. '/label',
  7425. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7426. ['label' => ['for' => 'confirm-n']],
  7427. 'No',
  7428. '/label',
  7429. '<label',
  7430. 'Confirm',
  7431. '/label',
  7432. '/div',
  7433. ];
  7434. $this->assertHtml($expected, $result);
  7435. $result = $this->Form->select('category', ['1', '2'], [
  7436. 'multiple' => 'checkbox',
  7437. 'name' => 'fish',
  7438. ]);
  7439. $expected = [
  7440. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7441. ['div' => ['class' => 'checkbox']],
  7442. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7443. ['label' => ['for' => 'fish-0']],
  7444. '1',
  7445. '/label',
  7446. '/div',
  7447. ['div' => ['class' => 'checkbox']],
  7448. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7449. ['label' => ['for' => 'fish-1']],
  7450. '2',
  7451. '/label',
  7452. '/div'
  7453. ];
  7454. $this->assertHtml($expected, $result);
  7455. }
  7456. /**
  7457. * Test that *Container templates are used by input.
  7458. *
  7459. * @return void
  7460. */
  7461. public function testInputContainerTemplates()
  7462. {
  7463. $this->Form->templates([
  7464. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7465. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7466. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7467. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7468. ]);
  7469. $this->article['errors'] = [
  7470. 'Article' => ['published' => 'error message']
  7471. ];
  7472. $this->Form->create($this->article);
  7473. $result = $this->Form->input('accept', [
  7474. 'type' => 'checkbox'
  7475. ]);
  7476. $expected = [
  7477. 'div' => ['class' => 'check'],
  7478. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7479. 'label' => ['for' => 'accept'],
  7480. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7481. 'Accept',
  7482. '/label',
  7483. '/div'
  7484. ];
  7485. $this->assertHtml($expected, $result);
  7486. $result = $this->Form->input('accept', [
  7487. 'type' => 'radio',
  7488. 'options' => ['Y', 'N']
  7489. ]);
  7490. $this->assertContains('<div class="rad">', $result);
  7491. $result = $this->Form->input('Article.published', [
  7492. 'type' => 'radio',
  7493. 'options' => ['Y', 'N']
  7494. ]);
  7495. $this->assertContains('<div class="rad err">', $result);
  7496. $result = $this->Form->input('Article.created', [
  7497. 'type' => 'datetime',
  7498. ]);
  7499. $this->assertContains('<div class="dt">', $result);
  7500. }
  7501. /**
  7502. * Test that *Container templates are used by input.
  7503. *
  7504. * @return void
  7505. */
  7506. public function testFormGroupTemplates()
  7507. {
  7508. $this->Form->templates([
  7509. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7510. ]);
  7511. $this->Form->create($this->article);
  7512. $result = $this->Form->input('accept', [
  7513. 'type' => 'radio',
  7514. 'options' => ['Y', 'N']
  7515. ]);
  7516. $this->assertContains('<div class="radio">', $result);
  7517. }
  7518. /**
  7519. * Test resetting templates.
  7520. *
  7521. * @return void
  7522. */
  7523. public function testResetTemplates()
  7524. {
  7525. $this->Form->templates(['input' => '<input/>']);
  7526. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  7527. $this->assertNull($this->Form->resetTemplates());
  7528. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7529. }
  7530. /**
  7531. * Test the context method.
  7532. *
  7533. * @return void
  7534. */
  7535. public function testContext()
  7536. {
  7537. $result = $this->Form->context();
  7538. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7539. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7540. $this->assertSame($mock, $this->Form->context($mock));
  7541. $this->assertSame($mock, $this->Form->context());
  7542. }
  7543. /**
  7544. * testAutoDomId
  7545. *
  7546. * @return void
  7547. */
  7548. public function testAutoDomId()
  7549. {
  7550. $result = $this->Form->text('field', ['id' => true]);
  7551. $expected = [
  7552. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7553. ];
  7554. $this->assertHtml($expected, $result);
  7555. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7556. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7557. $expected = [
  7558. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7559. ['label' => ['for' => 'field-0']],
  7560. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7561. 'option A',
  7562. '/label',
  7563. ['label' => ['for' => 'field-1']],
  7564. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7565. 'option B',
  7566. '/label',
  7567. ];
  7568. $this->assertHtml($expected, $result);
  7569. $result = $this->Form->select(
  7570. 'multi_field',
  7571. ['first', 'second'],
  7572. ['multiple' => 'checkbox', 'id' => true]
  7573. );
  7574. $expected = [
  7575. 'input' => [
  7576. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  7577. ],
  7578. ['div' => ['class' => 'checkbox']],
  7579. ['label' => ['for' => 'multi-field-0']],
  7580. ['input' => [
  7581. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7582. 'value' => '0', 'id' => 'multi-field-0'
  7583. ]],
  7584. 'first',
  7585. '/label',
  7586. '/div',
  7587. ['div' => ['class' => 'checkbox']],
  7588. ['label' => ['for' => 'multi-field-1']],
  7589. ['input' => [
  7590. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7591. 'value' => '1', 'id' => 'multi-field-1'
  7592. ]],
  7593. 'second',
  7594. '/label',
  7595. '/div',
  7596. ];
  7597. $this->assertHtml($expected, $result);
  7598. }
  7599. /**
  7600. * Test the basic setters and getters for value sources
  7601. *
  7602. * @return void
  7603. */
  7604. public function testFormValueSourcesSettersGetters()
  7605. {
  7606. $expected = ['context'];
  7607. $result = $this->Form->getValueSources();
  7608. $this->assertEquals($expected, $result);
  7609. $expected = null;
  7610. $result = $this->Form->getSourceValue('id');
  7611. $this->assertEquals($expected, $result);
  7612. $expected = ['query', 'data', 'context'];
  7613. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7614. $result = $this->Form->getValueSources();
  7615. $this->assertEquals($expected, $result);
  7616. $this->Form->request->data['id'] = '1';
  7617. $this->Form->request->query['id'] = '2';
  7618. $this->Form->setValueSources(['context']);
  7619. $expected = '1';
  7620. $result = $this->Form->getSourceValue('id');
  7621. $this->assertEquals($expected, $result);
  7622. $this->Form->setValueSources('query');
  7623. $expected = ['query'];
  7624. $result = $this->Form->getValueSources();
  7625. $this->assertEquals($expected, $result);
  7626. $expected = '2';
  7627. $result = $this->Form->getSourceValue('id');
  7628. $this->assertEquals($expected, $result);
  7629. $this->Form->setValueSources(['data']);
  7630. $expected = '1';
  7631. $result = $this->Form->getSourceValue('id');
  7632. $this->assertEquals($expected, $result);
  7633. $this->Form->setValueSources(['query', 'data']);
  7634. $expected = '2';
  7635. $result = $this->Form->getSourceValue('id');
  7636. $this->assertEquals($expected, $result);
  7637. }
  7638. /**
  7639. * Tests the different input rendering values based on sources values switching
  7640. *
  7641. * @return void
  7642. */
  7643. public function testFormValueSourcesSingleSwitchRendering()
  7644. {
  7645. $this->loadFixtures('Articles');
  7646. $articles = TableRegistry::get('Articles');
  7647. $article = new Article();
  7648. $articles->patchEntity($article, ['id' => '3']);
  7649. $this->Form->create($article);
  7650. $this->Form->setValueSources(['context']);
  7651. $result = $this->Form->input('id');
  7652. $expected = [
  7653. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7654. ];
  7655. $this->assertHtml($expected, $result);
  7656. $this->Form->request->query['id'] = '5';
  7657. $this->Form->setValueSources(['query']);
  7658. $result = $this->Form->input('id');
  7659. $expected = [
  7660. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7661. ];
  7662. $this->assertHtml($expected, $result);
  7663. $this->Form->request->query['id'] = '5a';
  7664. $this->Form->request->data['id'] = '5b';
  7665. $this->Form->setValueSources(['context']);
  7666. $this->Form->create($article);
  7667. $result = $this->Form->input('id');
  7668. $expected = [
  7669. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7670. ];
  7671. $this->assertHtml($expected, $result);
  7672. $this->Form->setValueSources(['data']);
  7673. $this->Form->create($article);
  7674. $result = $this->Form->input('id');
  7675. $expected = [
  7676. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7677. ];
  7678. $this->assertHtml($expected, $result);
  7679. $this->Form->setValueSources(['query']);
  7680. $result = $this->Form->input('id');
  7681. $expected = [
  7682. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7683. ];
  7684. $this->assertHtml($expected, $result);
  7685. }
  7686. /**
  7687. * Tests the different input rendering values based on sources values switching while supplying
  7688. * an entity (base context) and multiple sources (such as data, query)
  7689. *
  7690. * @return void
  7691. */
  7692. public function testFormValueSourcesListSwitchRendering()
  7693. {
  7694. $this->loadFixtures('Articles');
  7695. $articles = TableRegistry::get('Articles');
  7696. $article = new Article();
  7697. $articles->patchEntity($article, ['id' => '3']);
  7698. $this->Form->request->query['id'] = '9';
  7699. $this->Form->create($article);
  7700. $this->Form->setValueSources(['context', 'query']);
  7701. $result = $this->Form->input('id');
  7702. $expected = [
  7703. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7704. ];
  7705. $this->assertHtml($expected, $result);
  7706. $this->Form->setValueSources(['query', 'context']);
  7707. $result = $this->Form->input('id');
  7708. $expected = [
  7709. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7710. ];
  7711. $this->assertHtml($expected, $result);
  7712. $this->Form->setValueSources(['data', 'query', 'context']);
  7713. $result = $this->Form->input('id');
  7714. $expected = [
  7715. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7716. ];
  7717. $this->assertHtml($expected, $result);
  7718. $this->Form->request->data['id'] = '8';
  7719. $this->Form->request->query['id'] = '9';
  7720. $this->Form->setValueSources(['data', 'query', 'context']);
  7721. $result = $this->Form->input('id');
  7722. $expected = [
  7723. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  7724. ];
  7725. $this->assertHtml($expected, $result);
  7726. }
  7727. /**
  7728. * Test the different form input renderings based on values sources switchings through form options
  7729. *
  7730. * @return void
  7731. */
  7732. public function testFormValueSourcesSwitchViaOptionsRendering()
  7733. {
  7734. $this->loadFixtures('Articles');
  7735. $articles = TableRegistry::get('Articles');
  7736. $article = new Article();
  7737. $articles->patchEntity($article, ['id' => '3']);
  7738. $this->Form->request->data['id'] = '4';
  7739. $this->Form->request->query['id'] = '5';
  7740. $this->Form->create($article, ['valueSources' => 'query']);
  7741. $result = $this->Form->input('id');
  7742. $expected = [
  7743. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7744. ];
  7745. $this->assertHtml($expected, $result);
  7746. $result = $this->Form->getSourceValue('id');
  7747. $this->assertEquals('5', $result);
  7748. $this->Form->setValueSources(['context']);
  7749. $this->Form->create($article, ['valueSources' => 'query']);
  7750. $result = $this->Form->input('id');
  7751. $expected = [
  7752. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7753. ];
  7754. $this->assertHtml($expected, $result);
  7755. $result = $this->Form->getSourceValue('id');
  7756. $this->assertEquals('5', $result);
  7757. $this->Form->setValueSources(['query']);
  7758. $this->Form->create($article, ['valueSources' => 'data']);
  7759. $result = $this->Form->input('id');
  7760. $expected = [
  7761. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7762. ];
  7763. $this->assertHtml($expected, $result);
  7764. $result = $this->Form->getSourceValue('id');
  7765. $this->assertEquals('4', $result);
  7766. $this->Form->setValueSources(['query']);
  7767. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  7768. $result = $this->Form->input('id');
  7769. $expected = [
  7770. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7771. ];
  7772. $this->assertHtml($expected, $result);
  7773. $result = $this->Form->getSourceValue('id');
  7774. $this->assertEquals('4', $result);
  7775. }
  7776. /**
  7777. * Test the different form input renderings based on values sources switchings through form options
  7778. *
  7779. * @return void
  7780. */
  7781. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  7782. {
  7783. $this->loadFixtures('Articles');
  7784. $articles = TableRegistry::get('Articles');
  7785. $article = new Article();
  7786. $articles->patchEntity($article, ['id' => '3']);
  7787. $this->Form->request->data['id'] = '10';
  7788. $this->Form->request->query['id'] = '11';
  7789. $this->Form->setValueSources(['context'])
  7790. ->create($article, ['valueSources' => ['query', 'data']]);
  7791. $result = $this->Form->input('id');
  7792. $expected = [
  7793. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  7794. ];
  7795. $this->assertHtml($expected, $result);
  7796. $result = $this->Form->getSourceValue('id');
  7797. $this->assertEquals('11', $result);
  7798. unset($this->Form->request->query['id']);
  7799. $this->Form->setValueSources(['context'])
  7800. ->create($article, ['valueSources' => ['query', 'data']]);
  7801. $result = $this->Form->input('id');
  7802. $expected = [
  7803. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  7804. ];
  7805. $this->assertHtml($expected, $result);
  7806. $result = $this->Form->getSourceValue('id');
  7807. $this->assertEquals('10', $result);
  7808. }
  7809. /**
  7810. * Test the different form values sources resetting through From::end();
  7811. *
  7812. * @return void
  7813. */
  7814. public function testFormValueSourcesResetViaEnd()
  7815. {
  7816. $expected = ['context'];
  7817. $result = $this->Form->getValueSources();
  7818. $this->assertEquals($expected, $result);
  7819. $expected = ['query', 'context', 'data'];
  7820. $this->Form->setValueSources(['query', 'context', 'data']);
  7821. $result = $this->Form->getValueSources();
  7822. $this->assertEquals($expected, $result);
  7823. $this->Form->create();
  7824. $result = $this->Form->getValueSources();
  7825. $this->assertEquals($expected, $result);
  7826. $this->Form->end();
  7827. $result = $this->Form->getValueSources();
  7828. $this->assertEquals(['context'], $result);
  7829. }
  7830. /**
  7831. * Test sources values defaults handling
  7832. *
  7833. * @return void
  7834. */
  7835. public function testFormValueSourcesDefaults()
  7836. {
  7837. $this->Form->request->query['password'] = 'open Sesame';
  7838. $this->Form->create();
  7839. $result = $this->Form->password('password');
  7840. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  7841. $this->assertHtml($expected, $result);
  7842. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7843. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7844. $this->assertHtml($expected, $result);
  7845. $this->Form->setValueSources('query');
  7846. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7847. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  7848. $this->assertHtml($expected, $result);
  7849. $this->Form->setValueSources('data');
  7850. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7851. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7852. $this->assertHtml($expected, $result);
  7853. }
  7854. /**
  7855. * Test sources values schema defaults handling
  7856. *
  7857. * @return void
  7858. */
  7859. public function testSourcesValueDoesntExistPassThrough()
  7860. {
  7861. $this->Form->request->query['category'] = 'sesame-cookies';
  7862. $articles = TableRegistry::get('Articles');
  7863. $entity = $articles->newEntity();
  7864. $this->Form->create($entity);
  7865. $this->Form->setValueSources(['query', 'context']);
  7866. $result = $this->Form->getSourceValue('category');
  7867. $this->assertEquals('sesame-cookies', $result);
  7868. $this->Form->setValueSources(['context', 'query']);
  7869. $result = $this->Form->getSourceValue('category');
  7870. $this->assertEquals('sesame-cookies', $result);
  7871. }
  7872. }