FormHelperTest.php 241 KB

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