FormHelperTest.php 242 KB

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