FormHelperTest.php 276 KB

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