FormHelperTest.php 243 KB

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