FormHelperTest.php 239 KB

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