FormHelperTest.php 228 KB

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