| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 1.2.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\View\Helper;
- use Cake\Collection\Collection;
- use Cake\Core\Configure;
- use Cake\Form\Form;
- use Cake\Http\ServerRequest;
- use Cake\ORM\Entity;
- use Cake\ORM\Table;
- use Cake\ORM\TableRegistry;
- use Cake\Routing\Router;
- use Cake\TestSuite\TestCase;
- use Cake\Utility\Security;
- use Cake\View\Helper\FormHelper;
- use Cake\View\View;
- /**
- * Test stub.
- */
- class Article extends Entity
- {
- }
- /**
- * Contact class
- */
- class ContactsTable extends Table
- {
- /**
- * Default schema
- *
- * @var array
- */
- protected $_schema = [
- 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
- 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
- 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
- 'phone' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
- 'password' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
- 'published' => ['type' => 'date', 'null' => true, 'default' => null, 'length' => null],
- 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
- 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
- 'age' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => null],
- '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
- ];
- /**
- * Initializes the schema
- *
- * @return void
- */
- public function initialize(array $config)
- {
- $this->setSchema($this->_schema);
- }
- }
- /**
- * ValidateUser class
- */
- class ValidateUsersTable extends Table
- {
- /**
- * schema method
- *
- * @var array
- */
- protected $_schema = [
- 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
- 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
- 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
- 'balance' => ['type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2],
- 'cost_decimal' => ['type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3],
- 'null_decimal' => ['type' => 'decimal', 'null' => false, 'length' => null, 'precision' => null],
- 'ratio' => ['type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6],
- 'population' => ['type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0],
- 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
- 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
- '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
- ];
- /**
- * Initializes the schema
- *
- * @return void
- */
- public function initialize(array $config)
- {
- $this->setSchema($this->_schema);
- }
- }
- /**
- * FormHelperTest class
- *
- * @property \Cake\View\Helper\FormHelper $Form
- * @property \Cake\View\View $View
- */
- class FormHelperTest extends TestCase
- {
- /**
- * Fixtures to be used
- *
- * @var array
- */
- public $fixtures = ['core.articles', 'core.comments'];
- /**
- * Do not load the fixtures by default
- *
- * @var bool
- */
- public $autoFixtures = false;
- /**
- * @var array
- */
- protected $article = [];
- /**
- * setUp method
- *
- * @return void
- */
- public function setUp()
- {
- parent::setUp();
- Configure::write('Config.language', 'eng');
- Configure::write('App.base', '');
- static::setAppNamespace('Cake\Test\TestCase\View\Helper');
- $this->View = new View();
- $this->Form = new FormHelper($this->View);
- $request = new ServerRequest([
- 'webroot' => '',
- 'base' => '',
- 'url' => '/articles/add',
- 'params' => [
- 'controller' => 'articles',
- 'action' => 'add',
- ]
- ]);
- $this->Form->Url->request = $this->Form->request = $request;
- $this->dateRegex = [
- 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
- 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
- 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
- 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
- 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
- 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
- ];
- $this->article = [
- 'schema' => [
- 'id' => ['type' => 'integer'],
- 'author_id' => ['type' => 'integer', 'null' => true],
- 'title' => ['type' => 'string', 'null' => true],
- 'body' => 'text',
- 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
- '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
- ],
- 'required' => [
- 'author_id' => true,
- 'title' => true,
- ]
- ];
- Security::setSalt('foo!');
- Router::connect('/:controller', ['action' => 'index']);
- Router::connect('/:controller/:action/*');
- }
- /**
- * tearDown method
- *
- * @return void
- */
- public function tearDown()
- {
- parent::tearDown();
- unset($this->Form, $this->Controller, $this->View);
- TableRegistry::clear();
- }
- /**
- * Test construct() with the templates option.
- *
- * @return void
- */
- public function testConstructTemplatesFile()
- {
- $helper = new FormHelper($this->View, [
- 'templates' => 'htmlhelper_tags'
- ]);
- $result = $helper->control('name');
- $this->assertContains('<input', $result);
- }
- /**
- * Test that when specifying custom widgets the config array for that widget
- * is overwritten instead of merged.
- *
- * @return void
- */
- public function testConstructWithWidgets()
- {
- $config = [
- 'widgets' => [
- 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
- ]
- ];
- $helper = new FormHelper($this->View, $config);
- $registry = $helper->widgetRegistry();
- $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('datetime'));
- }
- /**
- * Test that when specifying custom widgets config file and it should be
- * added to widgets array. WidgetRegistry will load widgets in constructor.
- *
- * @return void
- */
- public function testConstructWithWidgetsConfig()
- {
- $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
- $registry = $helper->widgetRegistry();
- $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('text'));
- }
- /**
- * Test registering a new widget class and rendering it.
- *
- * @return void
- */
- public function testAddWidgetAndRenderWidget()
- {
- $data = [
- 'val' => 1
- ];
- $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
- $this->Form->addWidget('test', $mock);
- $mock->expects($this->once())
- ->method('render')
- ->with($data)
- ->will($this->returnValue('HTML'));
- $result = $this->Form->widget('test', $data);
- $this->assertEquals('HTML', $result);
- }
- /**
- * Test that secureFields() of widget is called after calling render(),
- * not before.
- *
- * @return void
- */
- public function testOrderForRenderingWidgetAndFetchingSecureFields()
- {
- $data = [
- 'val' => 1,
- 'name' => 'test'
- ];
- $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
- $this->Form->addWidget('test', $mock);
- $mock->expects($this->at(0))
- ->method('render')
- ->with($data)
- ->will($this->returnValue('HTML'));
- $mock->expects($this->at(1))
- ->method('secureFields')
- ->with($data)
- ->will($this->returnValue(['test']));
- $result = $this->Form->widget('test', $data + ['secure' => true]);
- $this->assertEquals('HTML', $result);
- }
- /**
- * Test that empty string is not added to secure fields list when
- * rendering input widget without name.
- *
- * @return void
- */
- public function testRenderingWidgetWithEmptyName()
- {
- $this->assertEquals([], $this->Form->fields);
- $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
- $this->assertEquals('<select name=""></select>', $result);
- $this->assertEquals([], $this->Form->fields);
- $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
- $this->assertEquals('<select name="0"></select>', $result);
- $this->assertEquals(['0'], $this->Form->fields);
- }
- /**
- * Test registering an invalid widget class.
- *
- * @return void
- */
- public function testAddWidgetInvalid()
- {
- $this->expectException(\RuntimeException::class);
- $mock = new \StdClass();
- $this->Form->addWidget('test', $mock);
- $this->Form->widget('test');
- }
- /**
- * Test adding a new context class.
- *
- * @return void
- */
- public function testAddContextProvider()
- {
- $context = 'My data';
- $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
- $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
- $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
- $this->assertEquals($context, $data['entity']);
- return $stub;
- });
- $this->Form->create($context);
- $result = $this->Form->context();
- $this->assertSame($stub, $result);
- }
- /**
- * Test replacing a context class.
- *
- * @return void
- */
- public function testAddContextProviderReplace()
- {
- $entity = new Article();
- $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
- $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
- return $stub;
- });
- $this->Form->create($entity);
- $result = $this->Form->context();
- $this->assertSame($stub, $result);
- }
- /**
- * Test overriding a context class.
- *
- * @return void
- */
- public function testAddContextProviderAdd()
- {
- $entity = new Article();
- $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
- $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
- if ($data['entity'] instanceof Entity) {
- return $stub;
- }
- });
- $this->Form->create($entity);
- $result = $this->Form->context();
- $this->assertSame($stub, $result);
- }
- /**
- * Test adding an invalid context class.
- *
- * @return void
- */
- public function testAddContextProviderInvalid()
- {
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage('Context providers must return object implementing Cake\View\Form\ContextInterface. Got "stdClass" instead.');
- $context = 'My data';
- $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
- return new \StdClass();
- });
- $this->Form->create($context);
- }
- /**
- * Provides context options for create().
- *
- * @return array
- */
- public function contextSelectionProvider()
- {
- $entity = new Article();
- $collection = new Collection([$entity]);
- $emptyCollection = new Collection([]);
- $arrayObject = new \ArrayObject([]);
- $data = [
- 'schema' => [
- 'title' => ['type' => 'string']
- ]
- ];
- $form = new Form();
- $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
- return [
- 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
- 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
- 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
- 'array' => [$data, 'Cake\View\Form\ArrayContext'],
- 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
- 'form' => [$form, 'Cake\View\Form\FormContext'],
- 'none' => [null, 'Cake\View\Form\NullContext'],
- 'false' => [false, 'Cake\View\Form\NullContext'],
- 'custom' => [$custom, get_class($custom)]
- ];
- }
- /**
- * Test default context selection in create()
- *
- * @dataProvider contextSelectionProvider
- * @return void
- */
- public function testCreateContextSelectionBuiltIn($data, $class)
- {
- $this->loadFixtures('Articles');
- $this->Form->create($data);
- $this->assertInstanceOf($class, $this->Form->context());
- }
- /**
- * Data provider for type option.
- *
- * @return array
- */
- public static function requestTypeProvider()
- {
- return [
- // type, method, override
- ['post', 'post', 'POST'],
- ['put', 'post', 'PUT'],
- ['patch', 'post', 'PATCH'],
- ['delete', 'post', 'DELETE'],
- ];
- }
- /**
- * Test creating file forms.
- *
- * @return void
- */
- public function testCreateFile()
- {
- $encoding = strtolower(Configure::read('App.encoding'));
- $result = $this->Form->create(false, ['type' => 'file']);
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/articles/add',
- 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
- ],
- 'div' => ['style' => 'display:none;'],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test creating GET forms.
- *
- * @return void
- */
- public function testCreateGet()
- {
- $encoding = strtolower(Configure::read('App.encoding'));
- $result = $this->Form->create(false, ['type' => 'get']);
- $expected = ['form' => [
- 'method' => 'get', 'action' => '/articles/add',
- 'accept-charset' => $encoding
- ]];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test explicit method/enctype options.
- *
- * Explicit method overwrites inferred method from 'type'
- *
- * @return void
- */
- public function testCreateExplicitMethodEnctype()
- {
- $encoding = strtolower(Configure::read('App.encoding'));
- $result = $this->Form->create(false, [
- 'type' => 'get',
- 'method' => 'put',
- 'enctype' => 'multipart/form-data'
- ]);
- $expected = ['form' => [
- 'method' => 'put',
- 'action' => '/articles/add',
- 'enctype' => 'multipart/form-data',
- 'accept-charset' => $encoding
- ]];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test create() with the templates option.
- *
- * @return void
- */
- public function testCreateTemplatesArray()
- {
- $result = $this->Form->create($this->article, [
- 'templates' => [
- 'formStart' => '<form class="form-horizontal"{{attrs}}>',
- ]
- ]);
- $expected = [
- 'form' => [
- 'class' => 'form-horizontal',
- 'method' => 'post',
- 'action' => '/articles/add',
- 'accept-charset' => 'utf-8'
- ]
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test create() with the templates option.
- *
- * @return void
- */
- public function testCreateTemplatesFile()
- {
- $result = $this->Form->create($this->article, [
- 'templates' => 'htmlhelper_tags',
- ]);
- $expected = [
- 'start form',
- 'div' => ['class' => 'hidden'],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test that create() and end() restore templates.
- *
- * @return void
- */
- public function testCreateEndRestoreTemplates()
- {
- $this->Form->create($this->article, [
- 'templates' => ['input' => 'custom input element']
- ]);
- $this->Form->end();
- $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
- }
- /**
- * Test using template vars in various templates used by control() method.
- *
- * @return void
- */
- public function testControlTemplateVars()
- {
- $result = $this->Form->control('text', [
- 'templates' => [
- 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
- 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
- 'formGroup' => '{{label}}{{forgroup}}{{input}}',
- 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
- ],
- 'templateVars' => [
- 'forinput' => 'in-input',
- 'forlabel' => 'in-label',
- 'forgroup' => 'in-group',
- 'forcontainer' => 'in-container'
- ]
- ]);
- $expected = [
- 'div' => ['class'],
- 'label' => ['for'],
- 'Text in-label',
- '/label',
- 'in-group',
- 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
- 'in-container',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test ensuring template variables work in template files loaded
- * during control().
- *
- * @return void
- */
- public function testControlTemplatesFromFile()
- {
- $result = $this->Form->control('title', [
- 'templates' => 'test_templates',
- 'templateVars' => [
- 'forcontainer' => 'container-data'
- ]
- ]);
- $expected = [
- 'div' => ['class'],
- 'label' => ['for'],
- 'Title',
- '/label',
- 'input' => ['name', 'type' => 'text', 'id'],
- 'container-data',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test using template vars in inputSubmit and submitContainer template.
- *
- * @return void
- */
- public function testSubmitTemplateVars()
- {
- $this->Form->setTemplates([
- 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
- 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
- ]);
- $result = $this->Form->submit('Submit', [
- 'templateVars' => [
- 'forinput' => 'in-input',
- 'forcontainer' => 'in-container'
- ]
- ]);
- $expected = [
- 'div' => ['class'],
- 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
- 'in-container',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test the create() method
- *
- * @dataProvider requestTypeProvider
- * @return void
- */
- public function testCreateTypeOptions($type, $method, $override)
- {
- $encoding = strtolower(Configure::read('App.encoding'));
- $result = $this->Form->create(false, ['type' => $type]);
- $expected = [
- 'form' => [
- 'method' => $method, 'action' => '/articles/add',
- 'accept-charset' => $encoding
- ],
- 'div' => ['style' => 'display:none;'],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test using template vars in Create (formStart template)
- *
- * @return void
- */
- public function testCreateTemplateVars()
- {
- $result = $this->Form->create($this->article, [
- 'templates' => [
- 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
- ],
- 'templateVars' => ['header' => 'headertext']
- ]);
- $expected = [
- 'h4' => ['class'],
- 'headertext',
- '/h4',
- 'form' => [
- 'method' => 'post',
- 'action' => '/articles/add',
- 'accept-charset' => 'utf-8'
- ]
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test opening a form for an update operation.
- *
- * @return void
- */
- public function testCreateUpdateForm()
- {
- $encoding = strtolower(Configure::read('App.encoding'));
- $this->Form->request = $this->Form->request
- ->withRequestTarget('/articles/edit/1')
- ->withParam('action', 'edit');
- $this->article['defaults']['id'] = 1;
- $result = $this->Form->create($this->article);
- $expected = [
- 'form' => [
- 'method' => 'post',
- 'action' => '/articles/edit/1',
- 'accept-charset' => $encoding
- ],
- 'div' => ['style' => 'display:none;'],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test create() with automatic url generation
- *
- * @return void
- */
- public function testCreateAutoUrl()
- {
- $encoding = strtolower(Configure::read('App.encoding'));
- $this->Form->request = $this->Form->request
- ->withRequestTarget('/articles/delete/10')
- ->withParam('action', 'delete');
- $result = $this->Form->create($this->article);
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/articles/delete/10',
- 'accept-charset' => $encoding
- ],
- 'div' => ['style' => 'display:none;'],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $this->article['defaults'] = ['id' => 1];
- $this->Form->request->here = '/articles/edit/1';
- $this->Form->request->params['action'] = 'delete';
- $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
- $expected = [
- 'form' => [
- 'method' => 'post',
- 'action' => '/articles/edit/1',
- 'accept-charset' => $encoding
- ],
- 'div' => ['style' => 'display:none;'],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $this->Form->request->params['action'] = 'add';
- $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
- $expected = [
- 'form' => [
- 'method' => 'post',
- 'action' => '/articles/publish/1',
- 'accept-charset' => $encoding
- ],
- 'div' => ['style' => 'display:none;'],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
- $expected = [
- 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
- 'div' => ['style' => 'display:none;'],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $this->Form->request->params['controller'] = 'Pages';
- $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/Pages/signup/1',
- 'accept-charset' => $encoding
- ],
- 'div' => ['style' => 'display:none;'],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test create() with no URL (no "action" attribute for <form> tag)
- *
- * @return void
- */
- public function testCreateNoUrl()
- {
- $result = $this->Form->create(false, ['url' => false]);
- $expected = [
- 'form' => [
- 'method' => 'post',
- 'accept-charset' => strtolower(Configure::read('App.encoding'))
- ],
- 'div' => ['style' => 'display:none;'],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test create() with a custom route
- *
- * @return void
- */
- public function testCreateCustomRoute()
- {
- Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
- $encoding = strtolower(Configure::read('App.encoding'));
- $this->Form->request->params['controller'] = 'users';
- $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/login',
- 'accept-charset' => $encoding
- ],
- 'div' => ['style' => 'display:none;'],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- Router::connect(
- '/new-article',
- ['controller' => 'articles', 'action' => 'myaction'],
- ['_name' => 'my-route']
- );
- $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/new-article',
- 'accept-charset' => $encoding,
- ],
- 'div' => ['style' => 'display:none;'],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test automatic accept-charset overriding
- *
- * @return void
- */
- public function testCreateWithAcceptCharset()
- {
- $result = $this->Form->create(
- $this->article,
- [
- 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
- ]
- );
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/articles',
- 'accept-charset' => 'iso-8859-1'
- ],
- 'div' => ['style' => 'display:none;'],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test base form URL when url param is passed with multiple parameters (&)
- *
- */
- public function testCreateQueryStringRequest()
- {
- $encoding = strtolower(Configure::read('App.encoding'));
- $result = $this->Form->create($this->article, [
- 'type' => 'post',
- 'escape' => false,
- 'url' => [
- 'controller' => 'controller',
- 'action' => 'action',
- '?' => ['param1' => 'value1', 'param2' => 'value2']
- ]
- ]);
- $expected = [
- 'form' => [
- 'method' => 'post',
- 'action' => '/controller/action?param1=value1&param2=value2',
- 'accept-charset' => $encoding
- ],
- 'div' => ['style' => 'display:none;'],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->create($this->article, [
- 'type' => 'post',
- 'url' => [
- 'controller' => 'controller',
- 'action' => 'action',
- '?' => ['param1' => 'value1', 'param2' => 'value2']
- ]
- ]);
- $this->assertHtml($expected, $result);
- }
- /**
- * test that create() doesn't cause errors by multiple id's being in the primary key
- * as could happen with multiple select or checkboxes.
- *
- * @return void
- */
- public function testCreateWithMultipleIdInData()
- {
- $encoding = strtolower(Configure::read('App.encoding'));
- $this->Form->request->data['Article']['id'] = [1, 2];
- $result = $this->Form->create($this->article);
- $expected = [
- 'form' => [
- 'method' => 'post',
- 'action' => '/articles/add',
- 'accept-charset' => $encoding
- ],
- 'div' => ['style' => 'display:none;'],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test that create() doesn't add in extra passed params.
- *
- * @return void
- */
- public function testCreatePassedArgs()
- {
- $encoding = strtolower(Configure::read('App.encoding'));
- $this->Form->request->data['Article']['id'] = 1;
- $result = $this->Form->create($this->article, [
- 'type' => 'post',
- 'escape' => false,
- 'url' => [
- 'action' => 'edit',
- 'myparam'
- ]
- ]);
- $expected = [
- 'form' => [
- 'method' => 'post',
- 'action' => '/articles/edit/myparam',
- 'accept-charset' => $encoding
- ],
- 'div' => ['style' => 'display:none;'],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test creating a get form, and get form inputs.
- *
- * @return void
- */
- public function testGetFormCreate()
- {
- $encoding = strtolower(Configure::read('App.encoding'));
- $result = $this->Form->create($this->article, ['type' => 'get']);
- $expected = ['form' => [
- 'method' => 'get', 'action' => '/articles/add',
- 'accept-charset' => $encoding
- ]];
- $this->assertHtml($expected, $result);
- $result = $this->Form->text('title');
- $expected = ['input' => [
- 'name' => 'title', 'type' => 'text', 'required' => 'required'
- ]];
- $this->assertHtml($expected, $result);
- $result = $this->Form->password('password');
- $expected = ['input' => [
- 'name' => 'password', 'type' => 'password'
- ]];
- $this->assertHtml($expected, $result);
- $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
- $result = $this->Form->text('user_form');
- $expected = ['input' => [
- 'name' => 'user_form', 'type' => 'text'
- ]];
- $this->assertHtml($expected, $result);
- }
- /**
- * test get form, and inputs when the model param is false
- *
- * @return void
- */
- public function testGetFormWithFalseModel()
- {
- $encoding = strtolower(Configure::read('App.encoding'));
- $this->Form->request->params['controller'] = 'contact_test';
- $result = $this->Form->create(false, [
- 'type' => 'get', 'url' => ['controller' => 'contact_test']
- ]);
- $expected = ['form' => [
- 'method' => 'get', 'action' => '/contact_test/add',
- 'accept-charset' => $encoding
- ]];
- $this->assertHtml($expected, $result);
- $result = $this->Form->text('reason');
- $expected = [
- 'input' => ['type' => 'text', 'name' => 'reason']
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormCreateWithSecurity method
- *
- * Test form->create() with security key.
- *
- * @return void
- */
- public function testCreateWithSecurity()
- {
- $this->Form->request->params['_csrfToken'] = 'testKey';
- $encoding = strtolower(Configure::read('App.encoding'));
- $result = $this->Form->create($this->article, [
- 'url' => '/articles/publish',
- ]);
- $expected = [
- 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
- 'div' => ['style' => 'display:none;'],
- ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_csrfToken',
- 'value' => 'testKey',
- 'autocomplete' => 'off',
- ]],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
- $expected['form']['id'] = 'MyForm';
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormCreateGetNoSecurity method
- *
- * Test form->create() with no security key as its a get form
- *
- * @return void
- */
- public function testCreateEndGetNoSecurity()
- {
- $this->Form->request->params['_csrfToken'] = 'testKey';
- $article = new Article();
- $result = $this->Form->create($article, [
- 'type' => 'get',
- 'url' => '/contacts/add'
- ]);
- $this->assertNotContains('testKey', $result);
- $result = $this->Form->end();
- $this->assertNotContains('testKey', $result);
- }
- /**
- * test that create() clears the fields property so it starts fresh
- *
- * @return void
- */
- public function testCreateClearingFields()
- {
- $this->Form->fields = ['model_id'];
- $this->Form->create($this->article);
- $this->assertEquals([], $this->Form->fields);
- }
- /**
- * Tests form hash generation with model-less data
- *
- * @return void
- */
- public function testValidateHashNoModel()
- {
- $this->Form->request->params['_Token'] = 'foo';
- $fields = ['anything'];
- $result = $this->Form->secure($fields);
- $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
- $this->assertContains($hash, $result);
- }
- /**
- * Tests that hidden fields generated for checkboxes don't get locked
- *
- * @return void
- */
- public function testNoCheckboxLocking()
- {
- $this->Form->request->params['_Token'] = 'foo';
- $this->assertSame([], $this->Form->fields);
- $this->Form->checkbox('check', ['value' => '1']);
- $this->assertSame($this->Form->fields, ['check']);
- }
- /**
- * testFormSecurityFields method
- *
- * Test generation of secure form hash generation.
- *
- * @return void
- */
- public function testFormSecurityFields()
- {
- $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
- $this->Form->request->params['_Token'] = 'testKey';
- $result = $this->Form->secure($fields);
- $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
- $hash .= ':' . 'Model.valid';
- $hash = urlencode($hash);
- $tokenDebug = urlencode(json_encode([
- '',
- $fields,
- []
- ]));
- $expected = [
- 'div' => ['style' => 'display:none;'],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[fields]',
- 'value' => $hash,
- 'autocomplete' => 'off',
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[unlocked]',
- 'value' => '',
- 'autocomplete' => 'off',
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[debug]',
- 'value' => $tokenDebug,
- 'autocomplete' => 'off',
- ]],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormSecurityFields method
- *
- * Test debug token is not generated if debug is false
- *
- * @return void
- */
- public function testFormSecurityFieldsNoDebugMode()
- {
- Configure::write('debug', false);
- $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
- $this->Form->request->params['_Token'] = 'testKey';
- $result = $this->Form->secure($fields);
- $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
- $hash .= ':' . 'Model.valid';
- $hash = urlencode($hash);
- $expected = [
- 'div' => ['style' => 'display:none;'],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[fields]',
- 'autocomplete' => 'off',
- 'value' => $hash
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[unlocked]',
- 'autocomplete' => 'off',
- 'value' => '',
- ]],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Tests correct generation of number fields for smallint
- *
- * @return void
- */
- public function testTextFieldGenerationForSmallint()
- {
- $this->article['schema'] = [
- 'foo' => [
- 'type' => 'smallinteger',
- 'null' => false,
- 'default' => null,
- 'length' => 10
- ]
- ];
- $this->Form->create($this->article);
- $result = $this->Form->control('foo');
- $this->assertContains('class="input number"', $result);
- $this->assertContains('type="number"', $result);
- }
- /**
- * Tests correct generation of number fields for tinyint
- *
- * @return void
- */
- public function testTextFieldGenerationForTinyint()
- {
- $this->article['schema'] = [
- 'foo' => [
- 'type' => 'tinyinteger',
- 'null' => false,
- 'default' => null,
- 'length' => 10
- ]
- ];
- $this->Form->create($this->article);
- $result = $this->Form->control('foo');
- $this->assertContains('class="input number"', $result);
- $this->assertContains('type="number"', $result);
- }
- /**
- * Tests correct generation of number fields for double and float fields
- *
- * @return void
- */
- public function testTextFieldGenerationForFloats()
- {
- $this->article['schema'] = [
- 'foo' => [
- 'type' => 'float',
- 'null' => false,
- 'default' => null,
- 'length' => 10
- ]
- ];
- $this->Form->create($this->article);
- $result = $this->Form->control('foo');
- $expected = [
- 'div' => ['class' => 'input number'],
- 'label' => ['for' => 'foo'],
- 'Foo',
- '/label',
- ['input' => [
- 'type' => 'number',
- 'name' => 'foo',
- 'id' => 'foo',
- 'step' => 'any'
- ]],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('foo', ['step' => 0.5]);
- $expected = [
- 'div' => ['class' => 'input number'],
- 'label' => ['for' => 'foo'],
- 'Foo',
- '/label',
- ['input' => [
- 'type' => 'number',
- 'name' => 'foo',
- 'id' => 'foo',
- 'step' => '0.5'
- ]],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Tests correct generation of number fields for integer fields
- *
- * @return void
- */
- public function testTextFieldTypeNumberGenerationForIntegers()
- {
- TableRegistry::get('Contacts', [
- 'className' => __NAMESPACE__ . '\ContactsTable'
- ]);
- $this->Form->create([], ['context' => ['table' => 'Contacts']]);
- $result = $this->Form->control('age');
- $expected = [
- 'div' => ['class' => 'input number'],
- 'label' => ['for' => 'age'],
- 'Age',
- '/label',
- ['input' => [
- 'type' => 'number', 'name' => 'age',
- 'id' => 'age'
- ]],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Tests correct generation of file upload fields for binary fields
- *
- * @return void
- */
- public function testFileUploadFieldTypeGenerationForBinaries()
- {
- $table = TableRegistry::get('Contacts', [
- 'className' => __NAMESPACE__ . '\ContactsTable'
- ]);
- $table->setSchema(['foo' => [
- 'type' => 'binary',
- 'null' => false,
- 'default' => null,
- 'length' => 1024
- ]]);
- $this->Form->create([], ['context' => ['table' => 'Contacts']]);
- $result = $this->Form->control('foo');
- $expected = [
- 'div' => ['class' => 'input file'],
- 'label' => ['for' => 'foo'],
- 'Foo',
- '/label',
- ['input' => [
- 'type' => 'file', 'name' => 'foo',
- 'id' => 'foo'
- ]],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormSecurityMultipleFields method
- *
- * Test secure() with multiple row form. Ensure hash is correct.
- *
- * @return void
- */
- public function testFormSecurityMultipleFields()
- {
- $this->Form->request->params['_Token'] = 'foo';
- $fields = [
- 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
- 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
- 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
- ];
- $result = $this->Form->secure($fields);
- $hash = '8670192c3f040bf58680479060b4755b7a5c3596' .
- '%3AModel.0.hidden%7CModel.0.valid%7CModel.1.hidden%7CModel.1.valid';
- $tokenDebug = urlencode(json_encode([
- '',
- $fields,
- []
- ]));
- $expected = [
- 'div' => ['style' => 'display:none;'],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[fields]',
- 'value' => $hash,
- 'autocomplete' => 'off',
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[unlocked]',
- 'autocomplete' => 'off',
- 'value' => ''
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[debug]',
- 'value' => $tokenDebug,
- 'autocomplete' => 'off',
- ]],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormSecurityMultipleSubmitButtons
- *
- * test form submit generation and ensure that _Token is only created on end()
- *
- * @return void
- */
- public function testFormSecurityMultipleSubmitButtons()
- {
- $this->Form->request->params['_Token'] = 'testKey';
- $this->Form->create($this->article);
- $this->Form->text('Address.title');
- $this->Form->text('Address.first_name');
- $result = $this->Form->submit('Save', ['name' => 'save']);
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->end();
- $tokenDebug = urlencode(json_encode([
- '/articles/add',
- [
- 'Address.title',
- 'Address.first_name',
- ],
- ['save', 'cancel']
- ]));
- $expected = [
- 'div' => ['style' => 'display:none;'],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[fields]',
- 'autocomplete',
- 'value'
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[unlocked]',
- 'value' => 'cancel%7Csave',
- 'autocomplete' => 'off',
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[debug]',
- 'value' => $tokenDebug,
- 'autocomplete' => 'off',
- ]],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test that buttons created with foo[bar] name attributes are unlocked correctly.
- *
- * @return void
- */
- public function testSecurityButtonNestedNamed()
- {
- $key = 'testKey';
- $this->Form->request->params['_csrfToken'] = $key;
- $this->Form->create('Addresses');
- $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
- $result = $this->Form->unlockField();
- $this->assertEquals(['Address.button'], $result);
- }
- /**
- * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
- *
- * @return void
- */
- public function testSecuritySubmitNestedNamed()
- {
- $this->Form->request->params['_Token'] = 'testKey';
- $this->Form->create($this->article);
- $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
- $result = $this->Form->unlockField();
- $this->assertEquals(['Address.button'], $result);
- }
- /**
- * Test that the correct fields are unlocked for image submits with no names.
- *
- * @return void
- */
- public function testSecuritySubmitImageNoName()
- {
- $this->Form->request->params['_Token'] = 'testKey';
- $this->Form->create(false);
- $result = $this->Form->submit('save.png');
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'image', 'src' => 'img/save.png'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $this->assertEquals(['x', 'y'], $this->Form->unlockField());
- }
- /**
- * Test that the correct fields are unlocked for image submits with names.
- *
- * @return void
- */
- public function testSecuritySubmitImageName()
- {
- $this->Form->request->params['_Token'] = 'testKey';
- $this->Form->create(null);
- $result = $this->Form->submit('save.png', ['name' => 'test']);
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
- }
- /**
- * testFormSecurityMultipleControlFields method
- *
- * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
- *
- * @return void
- */
- public function testFormSecurityMultipleControlFields()
- {
- $this->Form->request->params['_Token'] = 'testKey';
- $this->Form->create();
- $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
- $this->Form->control('Addresses.0.title');
- $this->Form->control('Addresses.0.first_name');
- $this->Form->control('Addresses.0.last_name');
- $this->Form->control('Addresses.0.address');
- $this->Form->control('Addresses.0.city');
- $this->Form->control('Addresses.0.phone');
- $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
- $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
- $this->Form->control('Addresses.1.title');
- $this->Form->control('Addresses.1.first_name');
- $this->Form->control('Addresses.1.last_name');
- $this->Form->control('Addresses.1.address');
- $this->Form->control('Addresses.1.city');
- $this->Form->control('Addresses.1.phone');
- $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
- $result = $this->Form->secure($this->Form->fields);
- $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
- $tokenDebug = urlencode(json_encode([
- '/articles/add',
- [
- 'Addresses.0.id' => '123456',
- 'Addresses.0.title',
- 'Addresses.0.first_name',
- 'Addresses.0.last_name',
- 'Addresses.0.address',
- 'Addresses.0.city',
- 'Addresses.0.phone',
- 'Addresses.0.primary',
- 'Addresses.1.id' => '654321',
- 'Addresses.1.title',
- 'Addresses.1.first_name',
- 'Addresses.1.last_name',
- 'Addresses.1.address',
- 'Addresses.1.city',
- 'Addresses.1.phone',
- 'Addresses.1.primary',
- ],
- []
- ]));
- $expected = [
- 'div' => ['style' => 'display:none;'],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[fields]',
- 'value' => $hash,
- 'autocomplete' => 'off',
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[unlocked]',
- 'autocomplete' => 'off',
- 'value' => ''
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[debug]',
- 'value' => $tokenDebug,
- 'autocomplete' => 'off',
- ]],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormSecurityArrayFields method
- *
- * Test form security with Model.field.0 style inputs.
- *
- * @return void
- */
- public function testFormSecurityArrayFields()
- {
- $this->Form->request->params['_Token'] = 'testKey';
- $this->Form->create();
- $this->Form->text('Address.primary.1');
- $this->assertEquals('Address.primary', $this->Form->fields[0]);
- $this->Form->text('Address.secondary.1.0');
- $this->assertEquals('Address.secondary', $this->Form->fields[1]);
- }
- /**
- * testFormSecurityMultipleControlDisabledFields method
- *
- * Test secure form generation with multiple records and disabled fields.
- *
- * @return void
- */
- public function testFormSecurityMultipleControlDisabledFields()
- {
- $this->Form->request->params['_Token'] = [
- 'unlockedFields' => ['first_name', 'address']
- ];
- $this->Form->create();
- $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
- $this->Form->text('Addresses.0.title');
- $this->Form->text('Addresses.0.first_name');
- $this->Form->text('Addresses.0.last_name');
- $this->Form->text('Addresses.0.address');
- $this->Form->text('Addresses.0.city');
- $this->Form->text('Addresses.0.phone');
- $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
- $this->Form->text('Addresses.1.title');
- $this->Form->text('Addresses.1.first_name');
- $this->Form->text('Addresses.1.last_name');
- $this->Form->text('Addresses.1.address');
- $this->Form->text('Addresses.1.city');
- $this->Form->text('Addresses.1.phone');
- $result = $this->Form->secure($this->Form->fields);
- $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
- $tokenDebug = urlencode(json_encode([
- '/articles/add',
- [
- 'Addresses.0.id' => '123456',
- 'Addresses.0.title',
- 'Addresses.0.last_name',
- 'Addresses.0.city',
- 'Addresses.0.phone',
- 'Addresses.1.id' => '654321',
- 'Addresses.1.title',
- 'Addresses.1.last_name',
- 'Addresses.1.city',
- 'Addresses.1.phone'
- ],
- [
- 'first_name',
- 'address'
- ]
- ]));
- $expected = [
- 'div' => ['style' => 'display:none;'],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[fields]',
- 'autocomplete' => 'off',
- 'value' => $hash
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[unlocked]',
- 'autocomplete' => 'off',
- 'value' => 'address%7Cfirst_name',
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[debug]',
- 'autocomplete' => 'off',
- 'value' => $tokenDebug,
- ]],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormSecurityControlDisabledFields method
- *
- * Test single record form with disabled fields.
- *
- * @return void
- */
- public function testFormSecurityControlUnlockedFields()
- {
- $this->Form->request->params['_Token'] = [
- 'unlockedFields' => ['first_name', 'address']
- ];
- $this->Form->create();
- $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
- $this->Form->hidden('Addresses.id', ['value' => '123456']);
- $this->Form->text('Addresses.title');
- $this->Form->text('Addresses.first_name');
- $this->Form->text('Addresses.last_name');
- $this->Form->text('Addresses.address');
- $this->Form->text('Addresses.city');
- $this->Form->text('Addresses.phone');
- $result = $this->Form->fields;
- $expected = [
- 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
- 'Addresses.city', 'Addresses.phone'
- ];
- $this->assertEquals($expected, $result);
- $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
- $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
- $tokenDebug = urlencode(json_encode([
- '/articles/add',
- [
- 'Addresses.id' => '123456',
- 'Addresses.title',
- 'Addresses.last_name',
- 'Addresses.city',
- 'Addresses.phone'
- ],
- [
- 'first_name',
- 'address'
- ]
- ]));
- $expected = [
- 'div' => ['style' => 'display:none;'],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[fields]',
- 'value' => $hash,
- 'autocomplete' => 'off',
- 'data-foo' => 'bar',
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[unlocked]',
- 'value' => 'address%7Cfirst_name',
- 'autocomplete' => 'off',
- 'data-foo' => 'bar',
- ]],
- ['input' => [
- 'type' => 'hidden', 'name' => '_Token[debug]',
- 'value' => $tokenDebug,
- 'autocomplete' => 'off',
- 'data-foo' => 'bar'
- ]],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
- *
- * Test single record form with debugSecurity param.
- *
- * @return void
- */
- public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
- {
- $this->Form->request->params['_Token'] = [
- 'unlockedFields' => ['first_name', 'address']
- ];
- $this->Form->create();
- $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
- $this->Form->hidden('Addresses.id', ['value' => '123456']);
- $this->Form->text('Addresses.title');
- $this->Form->text('Addresses.first_name');
- $this->Form->text('Addresses.last_name');
- $this->Form->text('Addresses.address');
- $this->Form->text('Addresses.city');
- $this->Form->text('Addresses.phone');
- $result = $this->Form->fields;
- $expected = [
- 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
- 'Addresses.city', 'Addresses.phone'
- ];
- $this->assertEquals($expected, $result);
- $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
- $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
- $tokenDebug = urlencode(json_encode([
- '/articles/add',
- [
- 'Addresses.id' => '123456',
- 'Addresses.title',
- 'Addresses.last_name',
- 'Addresses.city',
- 'Addresses.phone'
- ],
- [
- 'first_name',
- 'address'
- ]
- ]));
- $expected = [
- 'div' => ['style' => 'display:none;'],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[fields]',
- 'value' => $hash,
- 'autocomplete' => 'off',
- 'data-foo' => 'bar',
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[unlocked]',
- 'value' => 'address%7Cfirst_name',
- 'autocomplete' => 'off',
- 'data-foo' => 'bar',
- ]],
- ['input' => [
- 'type' => 'hidden', 'name' => '_Token[debug]',
- 'value' => $tokenDebug,
- 'autocomplete' => 'off',
- 'data-foo' => 'bar'
- ]],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
- *
- * Debug is false, debugSecurity is true -> no debug
- *
- * @return void
- */
- public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
- {
- $this->Form->request->params['_Token'] = [
- 'unlockedFields' => ['first_name', 'address']
- ];
- $this->Form->create();
- $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
- $this->Form->hidden('Addresses.id', ['value' => '123456']);
- $this->Form->text('Addresses.title');
- $this->Form->text('Addresses.first_name');
- $this->Form->text('Addresses.last_name');
- $this->Form->text('Addresses.address');
- $this->Form->text('Addresses.city');
- $this->Form->text('Addresses.phone');
- $result = $this->Form->fields;
- $expected = [
- 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
- 'Addresses.city', 'Addresses.phone'
- ];
- $this->assertEquals($expected, $result);
- Configure::write('debug', false);
- $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
- $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
- $expected = [
- 'div' => ['style' => 'display:none;'],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[fields]',
- 'value' => $hash,
- 'autocomplete' => 'off',
- 'data-foo' => 'bar',
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[unlocked]',
- 'value' => 'address%7Cfirst_name',
- 'autocomplete' => 'off',
- 'data-foo' => 'bar',
- ]],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
- *
- * Test single record form with debugSecurity param.
- *
- * @return void
- */
- public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
- {
- $this->Form->request->params['_Token'] = [
- 'unlockedFields' => ['first_name', 'address']
- ];
- $this->Form->create();
- $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
- $this->Form->hidden('Addresses.id', ['value' => '123456']);
- $this->Form->text('Addresses.title');
- $this->Form->text('Addresses.first_name');
- $this->Form->text('Addresses.last_name');
- $this->Form->text('Addresses.address');
- $this->Form->text('Addresses.city');
- $this->Form->text('Addresses.phone');
- $result = $this->Form->fields;
- $expected = [
- 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
- 'Addresses.city', 'Addresses.phone'
- ];
- $this->assertEquals($expected, $result);
- $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
- $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
- $expected = [
- 'div' => ['style' => 'display:none;'],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[fields]',
- 'value' => $hash,
- 'autocomplete' => 'off',
- 'data-foo' => 'bar',
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[unlocked]',
- 'value' => 'address%7Cfirst_name',
- 'autocomplete' => 'off',
- 'data-foo' => 'bar',
- ]],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormSecureWithCustomNameAttribute method
- *
- * Test securing inputs with custom name attributes.
- *
- * @return void
- */
- public function testFormSecureWithCustomNameAttribute()
- {
- $this->Form->request->params['_Token'] = 'testKey';
- $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
- $this->assertEquals('User.custom', $this->Form->fields[0]);
- $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
- $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
- }
- /**
- * testFormSecuredControl method
- *
- * Test generation of entire secure form, assertions made on control() output.
- *
- * @return void
- */
- public function testFormSecuredControl()
- {
- $this->Form->request->params['_csrfToken'] = 'testKey';
- $this->Form->request->params['_Token'] = 'stuff';
- $this->article['schema'] = [
- 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
- 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
- ];
- $result = $this->Form->create($this->article, ['url' => '/articles/add']);
- $encoding = strtolower(Configure::read('App.encoding'));
- $expected = [
- 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
- 'div' => ['style' => 'display:none;'],
- ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_csrfToken',
- 'value' => 'testKey',
- 'autocomplete' => 'off'
- ]],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('ratio');
- $expected = [
- 'div' => ['class'],
- 'label' => ['for'],
- 'Ratio',
- '/label',
- 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('population');
- $expected = [
- 'div' => ['class'],
- 'label' => ['for'],
- 'Population',
- '/label',
- 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('published', ['type' => 'text']);
- $expected = [
- 'div' => ['class' => 'input text'],
- 'label' => ['for' => 'published'],
- 'Published',
- '/label',
- ['input' => [
- 'type' => 'text',
- 'name' => 'published',
- 'id' => 'published'
- ]],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('other', ['type' => 'text']);
- $expected = [
- 'div' => ['class' => 'input text'],
- 'label' => ['for' => 'other'],
- 'Other',
- '/label',
- ['input' => [
- 'type' => 'text',
- 'name' => 'other',
- 'id',
- ]],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->hidden('stuff');
- $expected = [
- 'input' => [
- 'type' => 'hidden',
- 'name' => 'stuff'
- ]
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->hidden('hidden', ['value' => '0']);
- $expected = ['input' => [
- 'type' => 'hidden',
- 'name' => 'hidden',
- 'value' => '0'
- ]];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('something', ['type' => 'checkbox']);
- $expected = [
- 'div' => ['class' => 'input checkbox'],
- ['input' => [
- 'type' => 'hidden',
- 'name' => 'something',
- 'value' => '0'
- ]],
- 'label' => ['for' => 'something'],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'something',
- 'value' => '1',
- 'id' => 'something'
- ]],
- 'Something',
- '/label',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->fields;
- $expectedFields = [
- 'ratio',
- 'population',
- 'published',
- 'other',
- 'stuff' => '',
- 'hidden' => '0',
- 'something'
- ];
- $this->assertEquals($expectedFields, $result);
- $result = $this->Form->secure($this->Form->fields);
- $tokenDebug = urlencode(json_encode([
- '/articles/add',
- $expectedFields,
- []
- ]));
- $expected = [
- 'div' => ['style' => 'display:none;'],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[fields]',
- 'value',
- 'autocomplete'
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[unlocked]',
- 'value' => '',
- 'autocomplete' => 'off'
- ]],
- ['input' => [
- 'type' => 'hidden', 'name' => '_Token[debug]',
- 'value' => $tokenDebug,
- 'autocomplete' => 'off'
- ]],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testSecuredControlCustomName method
- *
- * Test secured inputs with custom names.
- *
- * @return void
- */
- public function testSecuredControlCustomName()
- {
- $this->Form->request->params['_Token'] = 'testKey';
- $this->assertEquals([], $this->Form->fields);
- $this->Form->text('text_input', [
- 'name' => 'Option[General.default_role]',
- ]);
- $expected = ['Option.General.default_role'];
- $this->assertEquals($expected, $this->Form->fields);
- $this->Form->select('select_box', [1, 2], [
- 'name' => 'Option[General.select_role]',
- ]);
- $expected[] = 'Option.General.select_role';
- $this->assertEquals($expected, $this->Form->fields);
- $this->Form->text('other.things[]');
- $expected[] = 'other.things';
- $this->assertEquals($expected, $this->Form->fields);
- }
- /**
- * testSecuredControlDuplicate method
- *
- * Test that a hidden field followed by a visible field
- * undoes the hidden field locking.
- *
- * @return void
- */
- public function testSecuredControlDuplicate()
- {
- $this->Form->request->params['_Token'] = ['key' => 'testKey'];
- $this->assertEquals([], $this->Form->fields);
- $this->Form->control('text_val', [
- 'type' => 'hidden',
- 'value' => 'some text',
- ]);
- $expected = ['text_val' => 'some text'];
- $this->assertEquals($expected, $this->Form->fields);
- $this->Form->control('text_val', [
- 'type' => 'text',
- ]);
- $expected = ['text_val'];
- $this->assertEquals($expected, $this->Form->fields);
- }
- /**
- * testFormSecuredFileControl method
- *
- * Tests that the correct keys are added to the field hash index.
- *
- * @return void
- */
- public function testFormSecuredFileControl()
- {
- $this->assertEquals([], $this->Form->fields);
- $this->Form->file('Attachment.file');
- $expected = [
- 'Attachment.file.name', 'Attachment.file.type',
- 'Attachment.file.tmp_name', 'Attachment.file.error',
- 'Attachment.file.size'
- ];
- $this->assertEquals($expected, $this->Form->fields);
- }
- /**
- * testFormSecuredMultipleSelect method
- *
- * Test that multiple selects keys are added to field hash.
- *
- * @return void
- */
- public function testFormSecuredMultipleSelect()
- {
- $this->Form->request->params['_csrfToken'] = 'testKey';
- $this->assertEquals([], $this->Form->fields);
- $options = ['1' => 'one', '2' => 'two'];
- $this->Form->select('Model.select', $options);
- $expected = ['Model.select'];
- $this->assertEquals($expected, $this->Form->fields);
- $this->Form->fields = [];
- $this->Form->select('Model.select', $options, ['multiple' => true]);
- $this->assertEquals($expected, $this->Form->fields);
- }
- /**
- * testFormSecuredRadio method
- *
- * @return void
- */
- public function testFormSecuredRadio()
- {
- $this->Form->request->params['_Token'] = 'testKey';
- $this->assertEquals([], $this->Form->fields);
- $options = ['1' => 'option1', '2' => 'option2'];
- $this->Form->radio('Test.test', $options);
- $expected = ['Test.test'];
- $this->assertEquals($expected, $this->Form->fields);
- $this->Form->radio('Test.all', $options, [
- 'disabled' => ['option1', 'option2']
- ]);
- $expected = ['Test.test', 'Test.all' => ''];
- $this->assertEquals($expected, $this->Form->fields);
- $this->Form->radio('Test.some', $options, [
- 'disabled' => ['option1']
- ]);
- $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
- $this->assertEquals($expected, $this->Form->fields);
- }
- /**
- * testFormSecuredAndDisabledNotAssoc method
- *
- * Test that when disabled is in a list based attribute array it works.
- *
- * @return void
- */
- public function testFormSecuredAndDisabledNotAssoc()
- {
- $this->Form->request->params['_Token'] = 'testKey';
- $this->Form->select('Model.select', [1, 2], ['disabled']);
- $this->Form->checkbox('Model.checkbox', ['disabled']);
- $this->Form->text('Model.text', ['disabled']);
- $this->Form->textarea('Model.textarea', ['disabled']);
- $this->Form->password('Model.password', ['disabled']);
- $this->Form->radio('Model.radio', [1, 2], ['disabled']);
- $expected = [
- 'Model.radio' => ''
- ];
- $this->assertEquals($expected, $this->Form->fields);
- }
- /**
- * testFormSecuredAndDisabled method
- *
- * Test that forms with disabled inputs + secured forms leave off the inputs from the form
- * hashing.
- *
- * @return void
- */
- public function testFormSecuredAndDisabled()
- {
- $this->Form->request->params['_Token'] = 'testKey';
- $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
- $this->Form->text('Model.text', ['disabled' => true]);
- $this->Form->password('Model.text', ['disabled' => true]);
- $this->Form->textarea('Model.textarea', ['disabled' => true]);
- $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
- $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
- $this->Form->year('Model.year', ['disabled' => true]);
- $this->Form->month('Model.month', ['disabled' => true]);
- $this->Form->day('Model.day', ['disabled' => true]);
- $this->Form->hour('Model.hour', ['disabled' => true]);
- $this->Form->minute('Model.minute', ['disabled' => true]);
- $this->Form->meridian('Model.meridian', ['disabled' => true]);
- $expected = [
- 'Model.radio' => ''
- ];
- $this->assertEquals($expected, $this->Form->fields);
- }
- /**
- * testDisableSecurityUsingForm method
- *
- * @return void
- */
- public function testDisableSecurityUsingForm()
- {
- $this->Form->request->params['_Token'] = [
- 'disabledFields' => []
- ];
- $this->Form->create();
- $this->Form->hidden('Addresses.id', ['value' => '123456']);
- $this->Form->text('Addresses.title');
- $this->Form->text('Addresses.first_name', ['secure' => false]);
- $this->Form->textarea('Addresses.city', ['secure' => false]);
- $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
- $result = $this->Form->fields;
- $expected = [
- 'Addresses.id' => '123456', 'Addresses.title',
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * testUnlockFieldAddsToList method
- *
- * Test disableField.
- *
- * @return void
- */
- public function testUnlockFieldAddsToList()
- {
- $this->Form->request->params['_Token'] = [
- 'unlockedFields' => []
- ];
- $this->Form->unlockField('Contact.name');
- $this->Form->text('Contact.name');
- $this->assertEquals(['Contact.name'], $this->Form->unlockField());
- $this->assertEquals([], $this->Form->fields);
- }
- /**
- * testUnlockFieldRemovingFromFields method
- *
- * Test unlockField removing from fields array.
- *
- * @return void
- */
- public function testUnlockFieldRemovingFromFields()
- {
- $this->Form->request->params['_Token'] = [
- 'unlockedFields' => []
- ];
- $this->Form->create($this->article);
- $this->Form->hidden('Article.id', ['value' => 1]);
- $this->Form->text('Article.title');
- $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
- $this->assertContains('Article.title', $this->Form->fields, 'Field should be secured.');
- $this->Form->unlockField('Article.title');
- $this->Form->unlockField('Article.id');
- $this->assertEquals([], $this->Form->fields);
- }
- /**
- * testResetUnlockFields method
- *
- * Test reset unlockFields, when create new form.
- *
- * @return void
- */
- public function testResetUnlockFields()
- {
- $this->Form->request->params['_Token'] = [
- 'key' => 'testKey',
- 'unlockedFields' => []
- ];
- $this->Form->unlockField('Contact.id');
- $this->Form->create('Contact');
- $this->Form->hidden('Contact.id', ['value' => 1]);
- $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
- $this->Form->end();
- $this->Form->create('Contact');
- $this->Form->hidden('Contact.id', ['value' => 1]);
- $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
- }
- /**
- * testSecuredFormUrlIgnoresHost method
- *
- * Test that only the path + query elements of a form's URL show up in their hash.
- *
- * @return void
- */
- public function testSecuredFormUrlIgnoresHost()
- {
- $this->Form->request->params['_Token'] = ['key' => 'testKey'];
- $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
- $this->Form->create($this->article, [
- 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
- ]);
- $result = $this->Form->secure();
- $this->assertContains($expected, $result);
- $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
- $result = $this->Form->secure();
- $this->assertContains($expected, $result, 'Full URL should only use path and query.');
- $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
- $result = $this->Form->secure();
- $this->assertContains($expected, $result, 'URL path + query should work.');
- $this->Form->create($this->article, ['url' => '/articles/view/1']);
- $result = $this->Form->secure();
- $this->assertNotContains($expected, $result, 'URL is different');
- }
- /**
- * testSecuredFormUrlHasHtmlAndIdentifier method
- *
- * Test that URL, HTML and identifier show up in their hashes.
- *
- * @return void
- */
- public function testSecuredFormUrlHasHtmlAndIdentifier()
- {
- $this->Form->request->params['_Token'] = ['key' => 'testKey'];
- $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
- $this->Form->create($this->article, [
- 'url' => [
- 'controller' => 'articles',
- 'action' => 'view',
- '?' => [
- 'page' => 1,
- 'limit' => 10,
- 'html' => '<>"',
- ],
- '#' => 'result',
- ],
- ]);
- $result = $this->Form->secure();
- $this->assertContains($expected, $result);
- $this->Form->create($this->article, [
- 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
- ]);
- $result = $this->Form->secure();
- $this->assertContains($expected, $result, 'Full URL should only use path and query.');
- $this->Form->create($this->article, [
- 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
- ]);
- $result = $this->Form->secure();
- $this->assertContains($expected, $result, 'URL path + query should work.');
- }
- /**
- * testErrorMessageDisplay method
- *
- * Test error message display.
- *
- * @return void
- */
- public function testErrorMessageDisplay()
- {
- $this->article['errors'] = [
- 'Article' => [
- 'title' => 'error message',
- 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
- ]
- ];
- $this->Form->create($this->article);
- $result = $this->Form->control('Article.title');
- $expected = [
- 'div' => ['class' => 'input text error'],
- 'label' => ['for' => 'article-title'],
- 'Title',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => 'Article[title]',
- 'id' => 'article-title', 'class' => 'form-error'
- ],
- ['div' => ['class' => 'error-message']],
- 'error message',
- '/div',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Article.title', [
- 'templates' => [
- 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
- ]
- ]);
- $expected = [
- 'div' => ['class' => 'input text error'],
- 'label' => ['for' => 'article-title'],
- 'Title',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => 'Article[title]',
- 'id' => 'article-title', 'class' => 'form-error'
- ],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Article.content');
- $expected = [
- 'div' => ['class' => 'input text error'],
- 'label' => ['for' => 'article-content'],
- 'Content',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => 'Article[content]',
- 'id' => 'article-content', 'class' => 'form-error'
- ],
- ['div' => ['class' => 'error-message']],
- 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
- '/div',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
- $expected = [
- 'div' => ['class' => 'input text error'],
- 'label' => ['for' => 'article-content'],
- 'Content',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => 'Article[content]',
- 'id' => 'article-content', 'class' => 'form-error'
- ],
- ['div' => ['class' => 'error-message']],
- 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
- '/div',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
- $expected = [
- 'div' => ['class' => 'input text error'],
- 'label' => ['for' => 'article-content'],
- 'Content',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => 'Article[content]',
- 'id' => 'article-content', 'class' => 'form-error'
- ],
- ['div' => ['class' => 'error-message']],
- 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
- '/div',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testEmptyErrorValidation method
- *
- * Test validation errors, when validation message is an empty string.
- *
- * @return void
- */
- public function testEmptyErrorValidation()
- {
- $this->article['errors'] = [
- 'Article' => ['title' => '']
- ];
- $this->Form->create($this->article);
- $result = $this->Form->control('Article.title');
- $expected = [
- 'div' => ['class' => 'input text error'],
- 'label' => ['for' => 'article-title'],
- 'Title',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => 'Article[title]',
- 'id' => 'article-title', 'class' => 'form-error'
- ],
- ['div' => ['class' => 'error-message']],
- [],
- '/div',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testEmptyControlErrorValidation method
- *
- * Test validation errors, when calling control() overriding validation message by an empty string.
- *
- * @return void
- */
- public function testEmptyControlErrorValidation()
- {
- $this->article['errors'] = [
- 'Article' => ['title' => 'error message']
- ];
- $this->Form->create($this->article);
- $result = $this->Form->control('Article.title', ['error' => '']);
- $expected = [
- 'div' => ['class' => 'input text error'],
- 'label' => ['for' => 'article-title'],
- 'Title',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => 'Article[title]',
- 'id' => 'article-title', 'class' => 'form-error'
- ],
- ['div' => ['class' => 'error-message']],
- [],
- '/div',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlErrorMessage method
- *
- * Test validation errors, when calling control() overriding validation messages.
- *
- * @return void
- */
- public function testControlErrorMessage()
- {
- $this->article['errors'] = [
- 'title' => ['error message']
- ];
- $this->Form->create($this->article);
- $result = $this->Form->control('title', [
- 'error' => 'Custom error!'
- ]);
- $expected = [
- 'div' => ['class' => 'input text required error'],
- 'label' => ['for' => 'title'],
- 'Title',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => 'title',
- 'id' => 'title', 'class' => 'form-error',
- 'required' => 'required',
- ],
- ['div' => ['class' => 'error-message']],
- 'Custom error!',
- '/div',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('title', [
- 'error' => ['error message' => 'Custom error!']
- ]);
- $expected = [
- 'div' => ['class' => 'input text required error'],
- 'label' => ['for' => 'title'],
- 'Title',
- '/label',
- 'input' => [
- 'type' => 'text',
- 'name' => 'title',
- 'id' => 'title',
- 'class' => 'form-error',
- 'required' => 'required'
- ],
- ['div' => ['class' => 'error-message']],
- 'Custom error!',
- '/div',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormValidationAssociated method
- *
- * Tests displaying errors for nested entities.
- *
- * @return void
- */
- public function testFormValidationAssociated()
- {
- $nested = new Entity(['foo' => 'bar']);
- $nested->setError('foo', ['not a valid bar']);
- $entity = new Entity(['nested' => $nested]);
- $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
- $result = $this->Form->error('nested.foo');
- $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
- }
- /**
- * testFormValidationAssociatedSecondLevel method
- *
- * Test form error display with associated model.
- *
- * @return void
- */
- public function testFormValidationAssociatedSecondLevel()
- {
- $inner = new Entity(['bar' => 'baz']);
- $nested = new Entity(['foo' => $inner]);
- $entity = new Entity(['nested' => $nested]);
- $inner->setError('bar', ['not a valid one']);
- $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
- $result = $this->Form->error('nested.foo.bar');
- $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
- }
- /**
- * testFormValidationMultiRecord method
- *
- * Test form error display with multiple records.
- *
- * @return void
- */
- public function testFormValidationMultiRecord()
- {
- $one = new Entity();
- $two = new Entity();
- TableRegistry::get('Contacts', [
- 'className' => __NAMESPACE__ . '\ContactsTable'
- ]);
- $one->set('email', '');
- $one->setError('email', ['invalid email']);
- $two->set('name', '');
- $two->setError('name', ['This is wrong']);
- $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
- $result = $this->Form->control('0.email');
- $expected = [
- 'div' => ['class' => 'input email error'],
- 'label' => ['for' => '0-email'],
- 'Email',
- '/label',
- 'input' => [
- 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
- 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
- ],
- ['div' => ['class' => 'error-message']],
- 'invalid email',
- '/div',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('1.name');
- $expected = [
- 'div' => ['class' => 'input text error'],
- 'label' => ['for' => '1-name'],
- 'Name',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
- 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
- ],
- ['div' => ['class' => 'error-message']],
- 'This is wrong',
- '/div',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControl method
- *
- * Test various incarnations of control().
- *
- * @return void
- */
- public function testControl()
- {
- TableRegistry::get('ValidateUsers', [
- 'className' => __NAMESPACE__ . '\ValidateUsersTable'
- ]);
- $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
- $result = $this->Form->control('ValidateUsers.balance');
- $expected = [
- 'div' => ['class'],
- 'label' => ['for'],
- 'Balance',
- '/label',
- 'input' => ['name', 'type' => 'number', 'id', 'step'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('ValidateUser.cost_decimal');
- $expected = [
- 'div' => ['class'],
- 'label' => ['for'],
- 'Cost Decimal',
- '/label',
- 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('ValidateUser.null_decimal');
- $expected = [
- 'div' => ['class'],
- 'label' => ['for'],
- 'Null Decimal',
- '/label',
- 'input' => ['name', 'type' => 'number', 'id'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlCustomization method
- *
- * Tests the input method and passing custom options.
- *
- * @return void
- */
- public function testControlCustomization()
- {
- TableRegistry::get('Contacts', [
- 'className' => __NAMESPACE__ . '\ContactsTable'
- ]);
- $this->Form->create([], ['context' => ['table' => 'Contacts']]);
- $result = $this->Form->control('Contact.email', ['id' => 'custom']);
- $expected = [
- 'div' => ['class' => 'input email'],
- 'label' => ['for' => 'custom'],
- 'Email',
- '/label',
- ['input' => [
- 'type' => 'email', 'name' => 'Contact[email]',
- 'id' => 'custom', 'maxlength' => 255
- ]],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Contact.email', [
- 'templates' => ['inputContainer' => '<div>{{content}}</div>']
- ]);
- $expected = [
- '<div',
- 'label' => ['for' => 'contact-email'],
- 'Email',
- '/label',
- ['input' => [
- 'type' => 'email', 'name' => 'Contact[email]',
- 'id' => 'contact-email', 'maxlength' => 255
- ]],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Contact.email', ['type' => 'text']);
- $expected = [
- 'div' => ['class' => 'input text'],
- 'label' => ['for' => 'contact-email'],
- 'Email',
- '/label',
- ['input' => [
- 'type' => 'text', 'name' => 'Contact[email]',
- 'id' => 'contact-email', 'maxlength' => '255'
- ]],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
- $expected = [
- 'div' => ['class' => 'input text'],
- 'label' => ['for' => 'contact-5-email'],
- 'Email',
- '/label',
- ['input' => [
- 'type' => 'text', 'name' => 'Contact[5][email]',
- 'id' => 'contact-5-email', 'maxlength' => '255'
- ]],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Contact.password');
- $expected = [
- 'div' => ['class' => 'input password'],
- 'label' => ['for' => 'contact-password'],
- 'Password',
- '/label',
- ['input' => [
- 'type' => 'password', 'name' => 'Contact[password]',
- 'id' => 'contact-password'
- ]],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Contact.email', [
- 'type' => 'file', 'class' => 'textbox'
- ]);
- $expected = [
- 'div' => ['class' => 'input file'],
- 'label' => ['for' => 'contact-email'],
- 'Email',
- '/label',
- ['input' => [
- 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
- 'id' => 'contact-email'
- ]],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $entity = new Entity(['phone' => 'Hello & World > weird chars']);
- $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
- $result = $this->Form->control('phone');
- $expected = [
- 'div' => ['class' => 'input tel'],
- 'label' => ['for' => 'phone'],
- 'Phone',
- '/label',
- ['input' => [
- 'type' => 'tel', 'name' => 'phone',
- 'value' => 'Hello & World > weird chars',
- 'id' => 'phone', 'maxlength' => 255
- ]],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
- $this->Form->create();
- $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
- $expected = [
- 'div' => ['class' => 'input text'],
- 'label' => ['for' => 'myId'],
- 'Field',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
- 'value' => 'My value', 'id' => 'myId'
- ],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $this->Form->request->data = [];
- $entity->setError('field', 'Badness!');
- $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
- $result = $this->Form->control('field');
- $expected = [
- 'div' => ['class' => 'input text error'],
- 'label' => ['for' => 'field'],
- 'Field',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => 'field',
- 'id' => 'field', 'class' => 'form-error'
- ],
- ['div' => ['class' => 'error-message']],
- 'Badness!',
- '/div',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('field', [
- 'templates' => [
- 'inputContainerError' => '{{content}}{{error}}',
- 'error' => '<span class="error-message">{{content}}</span>'
- ]
- ]);
- $expected = [
- 'label' => ['for' => 'field'],
- 'Field',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => 'field',
- 'id' => 'field', 'class' => 'form-error'
- ],
- ['span' => ['class' => 'error-message']],
- 'Badness!',
- '/span'
- ];
- $this->assertHtml($expected, $result);
- $entity->setError('field', ['minLength'], true);
- $result = $this->Form->control('field', [
- 'error' => [
- 'minLength' => 'Le login doit contenir au moins 2 caractères',
- 'maxLength' => 'login too large'
- ]
- ]);
- $expected = [
- 'div' => ['class' => 'input text error'],
- 'label' => ['for' => 'field'],
- 'Field',
- '/label',
- 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
- ['div' => ['class' => 'error-message']],
- 'Le login doit contenir au moins 2 caractères',
- '/div',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $entity->setError('field', ['maxLength'], true);
- $result = $this->Form->control('field', [
- 'error' => [
- 'minLength' => 'Le login doit contenir au moins 2 caractères',
- 'maxLength' => 'login too large',
- ]
- ]);
- $expected = [
- 'div' => ['class' => 'input text error'],
- 'label' => ['for' => 'field'],
- 'Field',
- '/label',
- 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
- ['div' => ['class' => 'error-message']],
- 'login too large',
- '/div',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlWithTemplateFile method
- *
- * Test that control() accepts a template file.
- *
- * @return void
- */
- public function testControlWithTemplateFile()
- {
- $result = $this->Form->control('field', [
- 'templates' => 'htmlhelper_tags'
- ]);
- $expected = [
- 'label' => ['for' => 'field'],
- 'Field',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => 'field',
- 'id' => 'field'
- ],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testNestedControlsEndWithBrackets method
- *
- * Test that nested inputs end with brackets.
- *
- * @return void
- */
- public function testNestedControlsEndWithBrackets()
- {
- $result = $this->Form->text('nested.text[]');
- $expected = [
- 'input' => [
- 'type' => 'text', 'name' => 'nested[text][]'
- ],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->file('nested.file[]');
- $expected = [
- 'input' => [
- 'type' => 'file', 'name' => 'nested[file][]'
- ],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testCreateIdPrefix method
- *
- * Test id prefix.
- *
- * @return void
- */
- public function testCreateIdPrefix()
- {
- $this->Form->create(false, ['idPrefix' => 'prefix']);
- $result = $this->Form->control('field');
- $expected = [
- 'div' => ['class' => 'input text'],
- 'label' => ['for' => 'prefix-field'],
- 'Field',
- '/label',
- 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('field', ['id' => 'custom-id']);
- $expected = [
- 'div' => ['class' => 'input text'],
- 'label' => ['for' => 'custom-id'],
- 'Field',
- '/label',
- 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->radio('Model.field', ['option A']);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
- 'label' => ['for' => 'prefix-model-field-0'],
- ['input' => [
- 'type' => 'radio',
- 'name' => 'Model[field]',
- 'value' => '0',
- 'id' => 'prefix-model-field-0'
- ]],
- 'option A',
- '/label'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->radio('Model.field', ['option A', 'option']);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
- 'label' => ['for' => 'prefix-model-field-0'],
- ['input' => [
- 'type' => 'radio',
- 'name' => 'Model[field]',
- 'value' => '0',
- 'id' => 'prefix-model-field-0'
- ]],
- 'option A',
- '/label'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->select(
- 'Model.multi_field',
- ['first'],
- ['multiple' => 'checkbox']
- );
- $expected = [
- 'input' => [
- 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
- ],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'prefix-model-multi-field-0']],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
- 'value' => '0', 'id' => 'prefix-model-multi-field-0'
- ]],
- 'first',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $this->Form->end();
- $result = $this->Form->control('field');
- $expected = [
- 'div' => ['class' => 'input text'],
- 'label' => ['for' => 'field'],
- 'Field',
- '/label',
- 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlZero method
- *
- * Test that inputs with 0 can be created.
- *
- * @return void
- */
- public function testControlZero()
- {
- TableRegistry::get('Contacts', [
- 'className' => __NAMESPACE__ . '\ContactsTable'
- ]);
- $this->Form->create([], ['context' => ['table' => 'Contacts']]);
- $result = $this->Form->control('0');
- $expected = [
- 'div' => ['class' => 'input text'],
- 'label' => ['for' => '0'], '/label',
- 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlCheckbox method
- *
- * Test control() with checkbox creation.
- *
- * @return void
- */
- public function testControlCheckbox()
- {
- $articles = TableRegistry::get('Articles');
- $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
- $article = $articles->newEntity();
- $this->Form->create($article);
- $result = $this->Form->control('Articles.active');
- $expected = [
- 'div' => ['class' => 'input checkbox'],
- 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
- 'label' => ['for' => 'articles-active'],
- ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
- 'Active',
- '/label',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
- $expected = [
- 'div' => ['class' => 'input checkbox'],
- 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
- ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
- $expected = [
- 'div' => ['class' => 'input checkbox'],
- 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
- ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
- $expected = [
- 'div' => ['class' => 'input checkbox'],
- 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
- ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Articles.disabled', [
- 'label' => 'Disabled',
- 'type' => 'checkbox',
- 'data-foo' => 'disabled'
- ]);
- $expected = [
- 'div' => ['class' => 'input checkbox'],
- 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
- 'label' => ['for' => 'articles-disabled'],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'Articles[disabled]',
- 'value' => '1',
- 'id' => 'articles-disabled',
- 'data-foo' => 'disabled'
- ]],
- 'Disabled',
- '/label',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Articles.confirm', [
- 'label' => 'Confirm <b>me</b>!',
- 'type' => 'checkbox',
- 'escape' => false
- ]);
- $expected = [
- 'div' => ['class' => 'input checkbox'],
- 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
- 'label' => ['for' => 'articles-confirm'],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'Articles[confirm]',
- 'value' => '1',
- 'id' => 'articles-confirm',
- ]],
- 'Confirm <b>me</b>!',
- '/label',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlHidden method
- *
- * Test that control() does not create wrapping div and label tag for hidden fields.
- *
- * @return void
- */
- public function testControlHidden()
- {
- TableRegistry::get('ValidateUsers', [
- 'className' => __NAMESPACE__ . '\ValidateUsersTable'
- ]);
- $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
- $result = $this->Form->control('ValidateUser.id');
- $expected = [
- 'input' => ['name', 'type' => 'hidden', 'id']
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
- $expected = [
- 'input' => ['name', 'type' => 'hidden', 'id']
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlDatetime method
- *
- * Test form->control() with datetime.
- *
- * @return void
- */
- public function testControlDatetime()
- {
- $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
- ->setMethods(['datetime'])
- ->setConstructorArgs([new View()])
- ->getMock();
- $this->Form->expects($this->once())->method('datetime')
- ->with('prueba', [
- 'type' => 'datetime',
- 'timeFormat' => 24,
- 'minYear' => 2008,
- 'maxYear' => 2011,
- 'interval' => 15,
- 'options' => null,
- 'empty' => false,
- 'id' => 'prueba',
- 'required' => false,
- 'templateVars' => []
- ])
- ->will($this->returnValue('This is it!'));
- $result = $this->Form->control('prueba', [
- 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
- 'maxYear' => 2011, 'interval' => 15
- ]);
- $expected = [
- 'div' => ['class' => 'input datetime'],
- '<label',
- 'Prueba',
- '/label',
- 'This is it!',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlDatetimeIdPrefix method
- *
- * Test form->control() with datetime with id prefix.
- *
- * @return void
- */
- public function testControlDatetimeIdPrefix()
- {
- $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
- ->setMethods(['datetime'])
- ->setConstructorArgs([new View()])
- ->getMock();
- $this->Form->create(false, ['idPrefix' => 'prefix']);
- $this->Form->expects($this->once())->method('datetime')
- ->with('prueba', [
- 'type' => 'datetime',
- 'timeFormat' => 24,
- 'minYear' => 2008,
- 'maxYear' => 2011,
- 'interval' => 15,
- 'options' => null,
- 'empty' => false,
- 'id' => 'prefix-prueba',
- 'required' => false,
- 'templateVars' => []
- ])
- ->will($this->returnValue('This is it!'));
- $result = $this->Form->control('prueba', [
- 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
- 'maxYear' => 2011, 'interval' => 15
- ]);
- $expected = [
- 'div' => ['class' => 'input datetime'],
- '<label',
- 'Prueba',
- '/label',
- 'This is it!',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlCheckboxWithDisabledElements method
- *
- * Test generating checkboxes with disabled elements.
- *
- * @return void
- */
- public function testControlCheckboxWithDisabledElements()
- {
- $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
- $result = $this->Form->control('Contact.multiple', [
- 'multiple' => 'checkbox',
- 'disabled' => 'disabled',
- 'options' => $options
- ]);
- $expected = [
- ['div' => ['class' => 'input select']],
- ['label' => ['for' => 'contact-multiple']],
- 'Multiple',
- '/label',
- ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'contact-multiple-1']],
- ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
- 'One',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'contact-multiple-2']],
- ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
- 'Two',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'contact-multiple-3']],
- ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
- 'Three',
- '/label',
- '/div',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- // make sure 50 does only disable 50, and not 50f5c0cf
- $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
- $disabled = [50];
- $expected = [
- ['div' => ['class' => 'input select']],
- ['label' => ['for' => 'contact-multiple']],
- 'Multiple',
- '/label',
- ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'contact-multiple-50']],
- ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
- 'Fifty',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'contact-multiple-50f5c0cf']],
- ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
- 'Stringy',
- '/label',
- '/div',
- '/div'
- ];
- $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlWithLeadingInteger method
- *
- * Test input name with leading integer, ensure attributes are generated correctly.
- *
- * @return void
- */
- public function testControlWithLeadingInteger()
- {
- $result = $this->Form->text('0.Node.title');
- $expected = [
- 'input' => ['name' => '0[Node][title]', 'type' => 'text']
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlSelectType method
- *
- * Test form->control() with select type inputs.
- *
- * @return void
- */
- public function testControlSelectType()
- {
- $result = $this->Form->control(
- 'email',
- [
- 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
- );
- $expected = [
- 'div' => ['class' => 'input select'],
- 'label' => ['for' => 'email'],
- 'Email',
- '/label',
- ['select' => ['name' => 'email', 'id' => 'email']],
- ['option' => ['value' => '']],
- '/option',
- ['option' => ['value' => 'è']],
- 'Firést',
- '/option',
- ['option' => ['value' => 'é']],
- 'Secoènd',
- '/option',
- '/select',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control(
- 'email',
- [
- 'options' => ['First', 'Second'], 'empty' => true]
- );
- $expected = [
- 'div' => ['class' => 'input select'],
- 'label' => ['for' => 'email'],
- 'Email',
- '/label',
- ['select' => ['name' => 'email', 'id' => 'email']],
- ['option' => ['value' => '']],
- '/option',
- ['option' => ['value' => '0']],
- 'First',
- '/option',
- ['option' => ['value' => '1']],
- 'Second',
- '/option',
- '/select',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('email', [
- 'type' => 'select',
- 'options' => new \ArrayObject(['First', 'Second']),
- 'empty' => true
- ]);
- $this->assertHtml($expected, $result);
- $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
- $this->Form->request->data = ['Model' => ['user_id' => 'value']];
- $result = $this->Form->control('Model.user_id', ['empty' => true]);
- $expected = [
- 'div' => ['class' => 'input select'],
- 'label' => ['for' => 'model-user-id'],
- 'User',
- '/label',
- 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
- ['option' => ['value' => '']],
- '/option',
- ['option' => ['value' => 'value', 'selected' => 'selected']],
- 'good',
- '/option',
- ['option' => ['value' => 'other']],
- 'bad',
- '/option',
- '/select',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
- $this->Form->request->data = ['Thing' => ['user_id' => null]];
- $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
- $expected = [
- 'div' => ['class' => 'input select'],
- 'label' => ['for' => 'thing-user-id'],
- 'User',
- '/label',
- 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
- ['option' => ['value' => '']],
- 'Some Empty',
- '/option',
- ['option' => ['value' => 'value']],
- 'good',
- '/option',
- ['option' => ['value' => 'other']],
- 'bad',
- '/option',
- '/select',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
- $this->Form->request->data = ['Thing' => ['user_id' => 'value']];
- $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
- $expected = [
- 'div' => ['class' => 'input select'],
- 'label' => ['for' => 'thing-user-id'],
- 'User',
- '/label',
- 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
- ['option' => ['value' => '']],
- 'Some Empty',
- '/option',
- ['option' => ['value' => 'value', 'selected' => 'selected']],
- 'good',
- '/option',
- ['option' => ['value' => 'other']],
- 'bad',
- '/option',
- '/select',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $this->Form->data = [];
- $result = $this->Form->control('Publisher.id', [
- 'label' => 'Publisher',
- 'type' => 'select',
- 'multiple' => 'checkbox',
- 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
- ]);
- $expected = [
- ['div' => ['class' => 'input select']],
- ['label' => ['for' => 'publisher-id']],
- 'Publisher',
- '/label',
- 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'publisher-id-value-1']],
- ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
- 'Label 1',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'publisher-id-value-2']],
- ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
- 'Label 2',
- '/label',
- '/div',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlWithNonStandardPrimaryKeyMakesHidden method
- *
- * Test that control() and a non standard primary key makes a hidden input by default.
- *
- * @return void
- */
- public function testControlWithNonStandardPrimaryKeyMakesHidden()
- {
- $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
- $this->Form->create($this->article);
- $result = $this->Form->control('title');
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
- ];
- $this->assertHtml($expected, $result);
- $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
- $this->Form->create($this->article);
- $result = $this->Form->control('title');
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('body');
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlOverridingMagicSelectType method
- *
- * Test that overriding the magic select type widget is possible.
- *
- * @return void
- */
- public function testControlOverridingMagicSelectType()
- {
- $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
- $result = $this->Form->control('Model.user_id', ['type' => 'text']);
- $expected = [
- 'div' => ['class' => 'input text'],
- 'label' => ['for' => 'model-user-id'], 'User', '/label',
- 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- //Check that magic types still work for plural/singular vars
- $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
- $result = $this->Form->control('Model.type');
- $expected = [
- 'div' => ['class' => 'input select'],
- 'label' => ['for' => 'model-type'], 'Type', '/label',
- 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
- ['option' => ['value' => 'value']], 'good', '/option',
- ['option' => ['value' => 'other']], 'bad', '/option',
- '/select',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlMagicTypeDoesNotOverride method
- *
- * Test that inferred types do not override developer input.
- *
- * @return void
- */
- public function testControlMagicTypeDoesNotOverride()
- {
- $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
- $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
- $expected = [
- 'div' => ['class' => 'input checkbox'],
- ['input' => [
- 'type' => 'hidden',
- 'name' => 'Model[user]',
- 'value' => 0,
- ]],
- 'label' => ['for' => 'model-user'],
- ['input' => [
- 'name' => 'Model[user]',
- 'type' => 'checkbox',
- 'id' => 'model-user',
- 'value' => 1
- ]],
- 'User',
- '/label',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- // make sure that for HABTM the multiple option is not being overwritten in case it's truly
- $options = [
- 1 => 'blue',
- 2 => 'red'
- ];
- $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
- $expected = [
- 'div' => ['class' => 'input select'],
- 'label' => ['for' => 'tags-ids'],
- 'Tags',
- '/label',
- 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'tags-ids-1']],
- ['input' => [
- 'id' => 'tags-ids-1', 'type' => 'checkbox',
- 'value' => '1', 'name' => 'tags[_ids][]'
- ]],
- 'blue',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'tags-ids-2']],
- ['input' => [
- 'id' => 'tags-ids-2', 'type' => 'checkbox',
- 'value' => '2', 'name' => 'tags[_ids][]'
- ]],
- 'red',
- '/label',
- '/div',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlMagicSelectForTypeNumber method
- *
- * Test that magic control() selects are created for type=number.
- *
- * @return void
- */
- public function testControlMagicSelectForTypeNumber()
- {
- TableRegistry::get('ValidateUsers', [
- 'className' => __NAMESPACE__ . '\ValidateUsersTable'
- ]);
- $entity = new Entity(['balance' => 1]);
- $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
- $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
- $result = $this->Form->control('balance');
- $expected = [
- 'div' => ['class' => 'input select'],
- 'label' => ['for' => 'balance'],
- 'Balance',
- '/label',
- 'select' => ['name' => 'balance', 'id' => 'balance'],
- ['option' => ['value' => '0']],
- 'nothing',
- '/option',
- ['option' => ['value' => '1', 'selected' => 'selected']],
- 'some',
- '/option',
- ['option' => ['value' => '100']],
- 'a lot',
- '/option',
- '/select',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testInvalidControlTypeOption method
- *
- * Test invalid 'input' type option to control() function.
- *
- * @return void
- */
- public function testInvalidControlTypeOption()
- {
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage('Invalid type \'input\' used for field \'text\'');
- $this->Form->control('text', ['type' => 'input']);
- }
- /**
- * testControlMagicSelectChangeToRadio method
- *
- * Test that magic control() selects can easily be converted into radio types without error.
- *
- * @return void
- */
- public function testControlMagicSelectChangeToRadio()
- {
- $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
- $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
- $this->assertContains('input type="radio"', $result);
- }
- /**
- * testFormControlSubmit method
- *
- * Test correct results for form::control() and type submit.
- *
- * @return void
- */
- public function testFormControlSubmit()
- {
- $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormControls method
- *
- * Test correct results from Form::controls().
- *
- * @return void
- */
- public function testFormControlsLegendFieldset()
- {
- $this->Form->create($this->article);
- $result = $this->Form->allControls([], ['legend' => 'The Legend']);
- $expected = [
- '<fieldset',
- '<legend',
- 'The Legend',
- '/legend',
- '*/fieldset',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
- $this->assertContains('<legend>Field of Dreams</legend>', $result);
- $this->assertContains('<fieldset>', $result);
- $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
- $this->assertNotContains('<legend>', $result);
- $this->assertNotContains('<fieldset>', $result);
- $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
- $this->assertNotContains('<legend>', $result);
- $this->assertNotContains('<fieldset>', $result);
- $this->Form->create($this->article);
- $this->Form->request->params['prefix'] = 'admin';
- $this->Form->request->params['action'] = 'admin_edit';
- $this->Form->request->params['controller'] = 'articles';
- $result = $this->Form->allControls();
- $expected = [
- '<fieldset',
- '<legend',
- 'New Article',
- '/legend',
- '*/fieldset',
- ];
- $this->assertHtml($expected, $result);
- $this->Form->create($this->article);
- $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
- $expected = [
- '<fieldset',
- '<legend',
- 'The Legend',
- '/legend',
- '*/fieldset',
- ];
- $this->assertHtml($expected, $result);
- $this->Form->create($this->article);
- $result = $this->Form->allControls([], [
- 'fieldset' => [
- 'class' => 'some-class some-other-class',
- 'disabled' => true,
- 'data-param' => 'a-param'
- ],
- 'legend' => 'The Legend'
- ]);
- $expected = [
- '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
- '<legend',
- 'The Legend',
- '/legend',
- '*/fieldset',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormControls method
- *
- * Test the controls() method.
- *
- * @return void
- */
- public function testFormControls()
- {
- $this->Form->create($this->article);
- $result = $this->Form->allControls();
- $expected = [
- '<fieldset',
- '<legend', 'New Article', '/legend',
- 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
- ['div' => ['class' => 'input select required']],
- '*/div',
- ['div' => ['class' => 'input text required']],
- '*/div',
- ['div' => ['class' => 'input text']],
- '*/div',
- ['div' => ['class' => 'input text']],
- '*/div',
- '/fieldset',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->allControls([
- 'published' => ['type' => 'boolean']
- ]);
- $expected = [
- '<fieldset',
- '<legend', 'New Article', '/legend',
- 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
- ['div' => ['class' => 'input select required']],
- '*/div',
- ['div' => ['class' => 'input text required']],
- '*/div',
- ['div' => ['class' => 'input text']],
- '*/div',
- ['div' => ['class' => 'input boolean']],
- '*/div',
- '/fieldset',
- ];
- $this->assertHtml($expected, $result);
- $this->Form->create($this->article);
- $result = $this->Form->allControls([], ['legend' => 'Hello']);
- $expected = [
- 'fieldset' => [],
- 'legend' => [],
- 'Hello',
- '/legend',
- 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
- ['div' => ['class' => 'input select required']],
- '*/div',
- ['div' => ['class' => 'input text required']],
- '*/div',
- ['div' => ['class' => 'input text']],
- '*/div',
- ['div' => ['class' => 'input text']],
- '*/div',
- '/fieldset'
- ];
- $this->assertHtml($expected, $result);
- $this->Form->create(false);
- $expected = [
- 'fieldset' => [],
- ['div' => ['class' => 'input text']],
- 'label' => ['for' => 'foo'],
- 'Foo',
- '/label',
- 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
- '*/div',
- '/fieldset'
- ];
- $result = $this->Form->allControls(
- ['foo' => ['type' => 'text']],
- ['legend' => false]
- );
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormControlsBlacklist method
- *
- * @return void
- */
- public function testFormControlsBlacklist()
- {
- $this->Form->create($this->article);
- $result = $this->Form->allControls([
- 'id' => false
- ]);
- $expected = [
- '<fieldset',
- '<legend', 'New Article', '/legend',
- ['div' => ['class' => 'input select required']],
- '*/div',
- ['div' => ['class' => 'input text required']],
- '*/div',
- ['div' => ['class' => 'input text']],
- '*/div',
- ['div' => ['class' => 'input text']],
- '*/div',
- '/fieldset',
- ];
- $this->assertHtml($expected, $result);
- $this->Form->create($this->article);
- $result = $this->Form->allControls([
- 'id' => []
- ]);
- $expected = [
- '<fieldset',
- '<legend', 'New Article', '/legend',
- 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
- ['div' => ['class' => 'input select required']],
- '*/div',
- ['div' => ['class' => 'input text required']],
- '*/div',
- ['div' => ['class' => 'input text']],
- '*/div',
- ['div' => ['class' => 'input text']],
- '*/div',
- '/fieldset',
- ];
- $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
- }
- /**
- * testSelectAsCheckbox method
- *
- * Test multi-select widget with checkbox formatting.
- *
- * @return void
- */
- public function testSelectAsCheckbox()
- {
- $result = $this->Form->select(
- 'Model.multi_field',
- ['first', 'second', 'third'],
- ['multiple' => 'checkbox', 'value' => [0, 1]]
- );
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
- ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
- 'first',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
- ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
- 'second',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-2']],
- ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
- 'third',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->select(
- 'Model.multi_field',
- ['1/2' => 'half'],
- ['multiple' => 'checkbox']
- );
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-1-2']],
- ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
- 'half',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testLabel method
- *
- * Test label generation.
- *
- * @return void
- */
- public function testLabel()
- {
- $result = $this->Form->label('Person.name');
- $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
- $this->assertHtml($expected, $result);
- $result = $this->Form->label('Person.name');
- $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
- $this->assertHtml($expected, $result);
- $result = $this->Form->label('Person.first_name');
- $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
- $this->assertHtml($expected, $result);
- $result = $this->Form->label('Person.first_name', 'Your first name');
- $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
- $this->assertHtml($expected, $result);
- $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
- $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
- $this->assertHtml($expected, $result);
- $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
- $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
- $this->assertHtml($expected, $result);
- $result = $this->Form->label('Person.first_name', '');
- $expected = ['label' => ['for' => 'person-first-name'], '/label'];
- $this->assertHtml($expected, $result);
- $result = $this->Form->label('Person.2.name', '');
- $expected = ['label' => ['for' => 'person-2-name'], '/label'];
- $this->assertHtml($expected, $result);
- }
- /**
- * testLabelContainControl method
- *
- * Test that label() can accept an input with the correct template vars.
- *
- * @return void
- */
- public function testLabelContainControl()
- {
- $this->Form->setTemplates([
- 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
- ]);
- $result = $this->Form->label('Person.accept_terms', 'Accept', [
- 'input' => '<input type="checkbox" name="accept_tos"/>'
- ]);
- $expected = [
- 'label' => ['for' => 'person-accept-terms'],
- 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
- 'Accept',
- '/label',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testTextbox method
- *
- * Test textbox element generation.
- *
- * @return void
- */
- public function testTextbox()
- {
- $result = $this->Form->text('Model.field');
- $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
- $this->assertHtml($expected, $result);
- $result = $this->Form->text('Model.field', ['type' => 'password']);
- $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
- $this->assertHtml($expected, $result);
- $result = $this->Form->text('Model.field', ['id' => 'theID']);
- $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
- $this->assertHtml($expected, $result);
- }
- /**
- * testTextBoxDataAndError method
- *
- * Test that text() hooks up with request data and error fields.
- *
- * @return void
- */
- public function testTextBoxDataAndError()
- {
- $this->article['errors'] = [
- 'Contact' => ['text' => 'wrong']
- ];
- $this->Form->create($this->article);
- $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
- $result = $this->Form->text('Model.text');
- $expected = [
- 'input' => [
- 'type' => 'text',
- 'name' => 'Model[text]',
- 'value' => 'test <strong>HTML</strong> values',
- ]
- ];
- $this->assertHtml($expected, $result);
- $this->Form->request->data['Contact']['text'] = 'test';
- $result = $this->Form->text('Contact.text', ['id' => 'theID']);
- $expected = [
- 'input' => [
- 'type' => 'text',
- 'name' => 'Contact[text]',
- 'value' => 'test',
- 'id' => 'theID',
- 'class' => 'form-error'
- ]
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testDefaultValue method
- *
- * Test default value setting.
- *
- * @return void
- */
- public function testTextDefaultValue()
- {
- $this->Form->request->data['Model']['field'] = 'test';
- $result = $this->Form->text('Model.field', ['default' => 'default value']);
- $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
- $this->assertHtml($expected, $result);
- unset($this->Form->request->data['Model']['field']);
- $result = $this->Form->text('Model.field', ['default' => 'default value']);
- $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
- $this->assertHtml($expected, $result);
- $Articles = TableRegistry::get('Articles');
- $title = $Articles->getSchema()->getColumn('title');
- $Articles->getSchema()->addColumn(
- 'title',
- ['default' => 'default title'] + $title
- );
- $entity = $Articles->newEntity();
- $this->Form->create($entity);
- // Get default value from schema
- $result = $this->Form->text('title');
- $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
- $this->assertHtml($expected, $result);
- // Don't get value from schema
- $result = $this->Form->text('title', ['schemaDefault' => false]);
- $expected = ['input' => ['type' => 'text', 'name' => 'title']];
- $this->assertHtml($expected, $result);
- // Custom default value overrides default value from schema
- $result = $this->Form->text('title', ['default' => 'override default']);
- $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
- $this->assertHtml($expected, $result);
- // Default value from schema is used only for new entities.
- $entity->isNew(false);
- $result = $this->Form->text('title');
- $expected = ['input' => ['type' => 'text', 'name' => 'title']];
- $this->assertHtml($expected, $result);
- }
- /**
- * testError method
- *
- * Test field error generation.
- *
- * @return void
- */
- public function testError()
- {
- $this->article['errors'] = [
- 'Article' => ['field' => 'email']
- ];
- $this->Form->create($this->article);
- $result = $this->Form->error('Article.field');
- $expected = [
- ['div' => ['class' => 'error-message']],
- 'email',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
- $expected = [
- ['div' => ['class' => 'error-message']],
- '<strong>Badness!</strong>',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
- $expected = [
- ['div' => ['class' => 'error-message']],
- '<strong', 'Badness!', '/strong',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testErrorRuleName method
- *
- * Test error translation can use rule names for translating.
- *
- * @return void
- */
- public function testErrorRuleName()
- {
- $this->article['errors'] = [
- 'Article' => [
- 'field' => ['email' => 'Your email was not good']
- ]
- ];
- $this->Form->create($this->article);
- $result = $this->Form->error('Article.field');
- $expected = [
- ['div' => ['class' => 'error-message']],
- 'Your email was not good',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
- $expected = [
- ['div' => ['class' => 'error-message']],
- 'Email in use',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
- $expected = [
- ['div' => ['class' => 'error-message']],
- 'Email in use',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->error('Article.field', [
- 'email' => 'Key is preferred',
- 'Your email was not good' => 'Email in use'
- ]);
- $expected = [
- ['div' => ['class' => 'error-message']],
- 'Key is preferred',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testErrorMessages method
- *
- * Test error with nested lists.
- *
- * @return void
- */
- public function testErrorMessages()
- {
- $this->article['errors'] = [
- 'Article' => ['field' => 'email']
- ];
- $this->Form->create($this->article);
- $result = $this->Form->error('Article.field', [
- 'email' => 'No good!'
- ]);
- $expected = [
- 'div' => ['class' => 'error-message'],
- 'No good!',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testErrorMultipleMessages method
- *
- * Test error() with multiple messages.
- *
- * @return void
- */
- public function testErrorMultipleMessages()
- {
- $this->article['errors'] = [
- 'field' => ['notBlank', 'email', 'Something else']
- ];
- $this->Form->create($this->article);
- $result = $this->Form->error('field', [
- 'notBlank' => 'Cannot be empty',
- 'email' => 'No good!'
- ]);
- $expected = [
- 'div' => ['class' => 'error-message'],
- 'ul' => [],
- '<li', 'Cannot be empty', '/li',
- '<li', 'No good!', '/li',
- '<li', 'Something else', '/li',
- '/ul',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testPassword method
- *
- * Test password element generation.
- *
- * @return void
- */
- public function testPassword()
- {
- $this->article['errors'] = [
- 'Contact' => [
- 'passwd' => 1
- ]
- ];
- $this->Form->create($this->article);
- $result = $this->Form->password('Contact.field');
- $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
- $this->assertHtml($expected, $result);
- $this->Form->request->data['Contact']['passwd'] = 'test';
- $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
- $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
- $this->assertHtml($expected, $result);
- }
- /**
- * testRadio method
- *
- * Test radio element set generation.
- *
- * @return void
- */
- public function testRadio()
- {
- $result = $this->Form->radio('Model.field', ['option A']);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
- 'label' => ['for' => 'model-field-0'],
- ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
- 'option A',
- '/label'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->radio('Model.field', new Collection(['option A']));
- $this->assertHtml($expected, $result);
- $result = $this->Form->radio('Model.field', ['option A', 'option B']);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
- ['label' => ['for' => 'model-field-0']],
- ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
- 'option A',
- '/label',
- ['label' => ['for' => 'model-field-1']],
- ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
- 'option B',
- '/label',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->radio(
- 'Employee.gender',
- ['male' => 'Male', 'female' => 'Female'],
- ['form' => 'my-form']
- );
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
- ['label' => ['for' => 'employee-gender-male']],
- ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
- 'Male',
- '/label',
- ['label' => ['for' => 'employee-gender-female']],
- ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
- 'Female',
- '/label',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
- ['label' => ['for' => 'model-custom-0']],
- ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
- 'option A',
- '/label',
- ['label' => ['for' => 'model-custom-1']],
- ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
- 'option B',
- '/label',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->radio(
- 'Employee.gender',
- [
- ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
- ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
- ]
- );
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
- ['label' => ['for' => 'employee-gender-male']],
- ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
- 'id' => 'employee-gender-male', 'style' => 'width:20px']],
- 'Male',
- '/label',
- ['label' => ['for' => 'employee-gender-female']],
- ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
- 'id' => 'employee-gender-female', 'style' => 'width:20px']],
- 'Female',
- '/label',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test radio with complex options and empty disabled data.
- *
- * @return void
- */
- public function testRadioComplexDisabled()
- {
- $options = [
- ['value' => 'r', 'text' => 'red'],
- ['value' => 'b', 'text' => 'blue'],
- ];
- $attrs = ['disabled' => []];
- $result = $this->Form->radio('Model.field', $options, $attrs);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
- ['label' => ['for' => 'model-field-r']],
- ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
- 'red',
- '/label',
- ['label' => ['for' => 'model-field-b']],
- ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
- 'blue',
- '/label',
- ];
- $this->assertHtml($expected, $result);
- $attrs = ['disabled' => ['r']];
- $result = $this->Form->radio('Model.field', $options, $attrs);
- $this->assertContains('disabled="disabled"', $result);
- }
- /**
- * testRadioDefaultValue method
- *
- * Test default value setting on radio() method.
- *
- * @return void
- */
- public function testRadioDefaultValue()
- {
- $Articles = TableRegistry::get('Articles');
- $title = $Articles->getSchema()->getColumn('title');
- $Articles->getSchema()->addColumn(
- 'title',
- ['default' => '1'] + $title
- );
- $this->Form->create($Articles->newEntity());
- $result = $this->Form->radio('title', ['option A', 'option B']);
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
- ['label' => ['for' => 'title-0']],
- ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
- 'option A',
- '/label',
- ['label' => ['for' => 'title-1']],
- ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
- 'option B',
- '/label',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test setting a hiddenField value on radio buttons.
- *
- * @return void
- */
- public function testRadioHiddenFieldValue()
- {
- $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
- 'label' => ['for' => 'title-0'],
- ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
- 'option A',
- '/label',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlRadio method
- *
- * Test that input works with radio types.
- *
- * @return void
- */
- public function testControlRadio()
- {
- $result = $this->Form->control('test', [
- 'type' => 'radio',
- 'options' => ['A', 'B'],
- ]);
- $expected = [
- ['div' => ['class' => 'input radio']],
- '<label',
- 'Test',
- '/label',
- ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
- ['label' => ['for' => 'test-0']],
- ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
- 'A',
- '/label',
- ['label' => ['for' => 'test-1']],
- ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
- 'B',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('test', [
- 'type' => 'radio',
- 'options' => ['A', 'B'],
- 'value' => '0'
- ]);
- $expected = [
- ['div' => ['class' => 'input radio']],
- '<label',
- 'Test',
- '/label',
- ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
- ['label' => ['for' => 'test-0']],
- ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
- 'A',
- '/label',
- ['label' => ['for' => 'test-1']],
- ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
- 'B',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('test', [
- 'type' => 'radio',
- 'options' => ['A', 'B'],
- 'label' => false
- ]);
- $expected = [
- ['div' => ['class' => 'input radio']],
- ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
- ['label' => ['for' => 'test-0']],
- ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
- 'A',
- '/label',
- ['label' => ['for' => 'test-1']],
- ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
- 'B',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testRadioNoLabel method
- *
- * Test that radio() works with label = false.
- *
- * @return void
- */
- public function testRadioNoLabel()
- {
- $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
- ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
- ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testRadioControlInsideLabel method
- *
- * Test generating radio input inside label ala twitter bootstrap.
- *
- * @return void
- */
- public function testRadioControlInsideLabel()
- {
- $this->Form->setTemplates([
- 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
- 'radioWrapper' => '{{label}}'
- ]);
- $result = $this->Form->radio('Model.field', ['option A', 'option B']);
- //@codingStandardsIgnoreStart
- $expected = [
- ['input' => [
- 'type' => 'hidden',
- 'name' => 'Model[field]',
- 'value' => ''
- ]],
- ['label' => ['for' => 'model-field-0']],
- ['input' => [
- 'type' => 'radio',
- 'name' => 'Model[field]',
- 'value' => '0',
- 'id' => 'model-field-0'
- ]],
- 'option A',
- '/label',
- ['label' => ['for' => 'model-field-1']],
- ['input' => [
- 'type' => 'radio',
- 'name' => 'Model[field]',
- 'value' => '1',
- 'id' => 'model-field-1'
- ]],
- 'option B',
- '/label',
- ];
- //@codingStandardsIgnoreEnd
- $this->assertHtml($expected, $result);
- }
- /**
- * testRadioHiddenControlDisabling method
- *
- * Test disabling the hidden input for radio buttons.
- *
- * @return void
- */
- public function testRadioHiddenControlDisabling()
- {
- $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
- $expected = [
- 'label' => ['for' => 'model-1-field-0'],
- 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
- 'option A',
- '/label'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testRadioOutOfRange method
- *
- * Test radio element set generation.
- *
- * @return void
- */
- public function testRadioOutOfRange()
- {
- $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
- 'label' => ['for' => 'model-field-v'],
- ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
- 'value',
- '/label'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testSelect method
- *
- * Test select element generation.
- *
- * @return void
- */
- public function testSelect()
- {
- $result = $this->Form->select('Model.field', []);
- $expected = [
- 'select' => ['name' => 'Model[field]'],
- '/select'
- ];
- $this->assertHtml($expected, $result);
- $this->Form->request->data = ['Model' => ['field' => 'value']];
- $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
- $expected = [
- 'select' => ['name' => 'Model[field]'],
- ['option' => ['value' => 'value', 'selected' => 'selected']],
- 'good',
- '/option',
- ['option' => ['value' => 'other']],
- 'bad',
- '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
- $this->assertHtml($expected, $result);
- $this->Form->request->data = [];
- $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
- $expected = [
- 'select' => ['name' => 'Model[field]'],
- ['option' => ['value' => 'value']],
- 'good',
- '/option',
- ['option' => ['value' => 'other']],
- 'bad',
- '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- $options = [
- ['value' => 'first', 'text' => 'First'],
- ['value' => 'first', 'text' => 'Another First'],
- ];
- $result = $this->Form->select(
- 'Model.field',
- $options,
- ['escape' => false, 'empty' => false]
- );
- $expected = [
- 'select' => ['name' => 'Model[field]'],
- ['option' => ['value' => 'first']],
- 'First',
- '/option',
- ['option' => ['value' => 'first']],
- 'Another First',
- '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- $this->Form->request->data = ['Model' => ['contact_id' => 228]];
- $result = $this->Form->select(
- 'Model.contact_id',
- ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
- ['escape' => false, 'empty' => 'pick something']
- );
- $expected = [
- 'select' => ['name' => 'Model[contact_id]'],
- ['option' => ['value' => '']], 'pick something', '/option',
- ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
- ['option' => ['value' => '228-1']], '228-1 value', '/option',
- ['option' => ['value' => '228-2']], '228-2 value', '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- $this->Form->request->data['Model']['field'] = 0;
- $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
- $expected = [
- 'select' => ['name' => 'Model[field]'],
- ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
- ['option' => ['value' => '1']], 'Yes', '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testSelectEscapeHtml method
- *
- * Test that select() escapes HTML.
- *
- * @return void
- */
- public function testSelectEscapeHtml()
- {
- $result = $this->Form->select(
- 'Model.field',
- ['first' => 'first "html" <chars>', 'second' => 'value'],
- ['empty' => false]
- );
- $expected = [
- 'select' => ['name' => 'Model[field]'],
- ['option' => ['value' => 'first']],
- 'first "html" <chars>',
- '/option',
- ['option' => ['value' => 'second']],
- 'value',
- '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->select(
- 'Model.field',
- ['first' => 'first "html" <chars>', 'second' => 'value'],
- ['escape' => false, 'empty' => false]
- );
- $expected = [
- 'select' => ['name' => 'Model[field]'],
- ['option' => ['value' => 'first']],
- 'first "html" <chars>',
- '/option',
- ['option' => ['value' => 'second']],
- 'value',
- '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testSelectRequired method
- *
- * Test select() with required and disabled attributes.
- *
- * @return void
- */
- public function testSelectRequired()
- {
- $this->article['required'] = [
- 'user_id' => true
- ];
- $this->Form->create($this->article);
- $result = $this->Form->select('user_id', ['option A']);
- $expected = [
- 'select' => [
- 'name' => 'user_id',
- 'required' => 'required'
- ],
- ['option' => ['value' => '0']], 'option A', '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
- $expected = [
- 'select' => [
- 'name' => 'user_id',
- 'disabled' => 'disabled'
- ],
- ['option' => ['value' => '0']], 'option A', '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testNestedSelect method
- *
- * Test select element generation with optgroups.
- *
- * @return void
- */
- public function testNestedSelect()
- {
- $result = $this->Form->select(
- 'Model.field',
- [1 => 'One', 2 => 'Two', 'Three' => [
- 3 => 'Three', 4 => 'Four', 5 => 'Five'
- ]],
- ['empty' => false]
- );
- $expected = [
- 'select' => ['name' => 'Model[field]'],
- ['option' => ['value' => 1]],
- 'One',
- '/option',
- ['option' => ['value' => 2]],
- 'Two',
- '/option',
- ['optgroup' => ['label' => 'Three']],
- ['option' => ['value' => 3]],
- 'Three',
- '/option',
- ['option' => ['value' => 4]],
- 'Four',
- '/option',
- ['option' => ['value' => 5]],
- 'Five',
- '/option',
- '/optgroup',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testSelectMultiple method
- *
- * Test generation of multiple select elements.
- *
- * @return void
- */
- public function testSelectMultiple()
- {
- $options = ['first', 'second', 'third'];
- $result = $this->Form->select(
- 'Model.multi_field',
- $options,
- ['form' => 'my-form', 'multiple' => true]
- );
- $expected = [
- 'input' => [
- 'type' => 'hidden',
- 'name' => 'Model[multi_field]',
- 'value' => '',
- 'form' => 'my-form',
- ],
- 'select' => [
- 'name' => 'Model[multi_field][]',
- 'multiple' => 'multiple',
- 'form' => 'my-form',
- ],
- ['option' => ['value' => '0']],
- 'first',
- '/option',
- ['option' => ['value' => '1']],
- 'second',
- '/option',
- ['option' => ['value' => '2']],
- 'third',
- '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->select(
- 'Model.multi_field',
- $options,
- ['multiple' => 'multiple', 'form' => 'my-form']
- );
- $this->assertHtml($expected, $result);
- }
- /**
- * testCheckboxZeroValue method
- *
- * Test that a checkbox can have 0 for the value and 1 for the hidden input.
- *
- * @return void
- */
- public function testCheckboxZeroValue()
- {
- $result = $this->Form->control('User.get_spam', [
- 'type' => 'checkbox',
- 'value' => '0',
- 'hiddenField' => '1',
- ]);
- $expected = [
- 'div' => ['class' => 'input checkbox'],
- 'label' => ['for' => 'user-get-spam'],
- ['input' => [
- 'type' => 'hidden', 'name' => 'User[get_spam]',
- 'value' => '1'
- ]],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'User[get_spam]',
- 'value' => '0', 'id' => 'user-get-spam'
- ]],
- 'Get Spam',
- '/label',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testHabtmSelectBox method
- *
- * Test generation of habtm select boxes.
- *
- * @return void
- */
- public function testHabtmSelectBox()
- {
- $this->loadFixtures('Articles');
- $options = [
- 1 => 'blue',
- 2 => 'red',
- 3 => 'green'
- ];
- $tags = [
- new Entity(['id' => 1, 'name' => 'blue']),
- new Entity(['id' => 3, 'name' => 'green'])
- ];
- $article = new Article(['tags' => $tags]);
- $this->Form->create($article);
- $result = $this->Form->control('tags._ids', ['options' => $options]);
- $expected = [
- 'div' => ['class' => 'input select'],
- 'label' => ['for' => 'tags-ids'],
- 'Tags',
- '/label',
- 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
- 'select' => [
- 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
- 'multiple' => 'multiple'
- ],
- ['option' => ['value' => '1', 'selected' => 'selected']],
- 'blue',
- '/option',
- ['option' => ['value' => '2']],
- 'red',
- '/option',
- ['option' => ['value' => '3', 'selected' => 'selected']],
- 'green',
- '/option',
- '/select',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- // make sure only 50 is selected, and not 50f5c0cf
- $options = [
- '1' => 'blue',
- '50f5c0cf' => 'red',
- '50' => 'green'
- ];
- $tags = [
- new Entity(['id' => 1, 'name' => 'blue']),
- new Entity(['id' => 50, 'name' => 'green'])
- ];
- $article = new Article(['tags' => $tags]);
- $this->Form->create($article);
- $result = $this->Form->control('tags._ids', ['options' => $options]);
- $expected = [
- 'div' => ['class' => 'input select'],
- 'label' => ['for' => 'tags-ids'],
- 'Tags',
- '/label',
- 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
- 'select' => [
- 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
- 'multiple' => 'multiple'
- ],
- ['option' => ['value' => '1', 'selected' => 'selected']],
- 'blue',
- '/option',
- ['option' => ['value' => '50f5c0cf']],
- 'red',
- '/option',
- ['option' => ['value' => '50', 'selected' => 'selected']],
- 'green',
- '/option',
- '/select',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $spacecraft = [
- 1 => 'Orion',
- 2 => 'Helios'
- ];
- $this->View->viewVars['spacecraft'] = $spacecraft;
- $this->Form->create();
- $result = $this->Form->control('spacecraft._ids');
- $expected = [
- 'div' => ['class' => 'input select'],
- 'label' => ['for' => 'spacecraft-ids'],
- 'Spacecraft',
- '/label',
- 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
- 'select' => [
- 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
- 'multiple' => 'multiple'
- ],
- ['option' => ['value' => '1']],
- 'Orion',
- '/option',
- ['option' => ['value' => '2']],
- 'Helios',
- '/option',
- '/select',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testErrorsForBelongsToManySelect method
- *
- * Tests that errors for belongsToMany select fields are being
- * picked up properly.
- *
- * @return void
- */
- public function testErrorsForBelongsToManySelect()
- {
- $spacecraft = [
- 1 => 'Orion',
- 2 => 'Helios'
- ];
- $this->View->viewVars['spacecraft'] = $spacecraft;
- $article = new Article();
- $article->setError('spacecraft', ['Invalid']);
- $this->Form->create($article);
- $result = $this->Form->control('spacecraft._ids');
- $expected = [
- ['div' => ['class' => 'input select error']],
- 'label' => ['for' => 'spacecraft-ids'],
- 'Spacecraft',
- '/label',
- 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
- 'select' => [
- 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
- 'multiple' => 'multiple'
- ],
- ['option' => ['value' => '1']],
- 'Orion',
- '/option',
- ['option' => ['value' => '2']],
- 'Helios',
- '/option',
- '/select',
- ['div' => ['class' => 'error-message']],
- 'Invalid',
- '/div',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testSelectMultipleCheckboxes method
- *
- * Test generation of multi select elements in checkbox format.
- *
- * @return void
- */
- public function testSelectMultipleCheckboxes()
- {
- $result = $this->Form->select(
- 'Model.multi_field',
- ['first', 'second', 'third'],
- ['multiple' => 'checkbox']
- );
- $expected = [
- 'input' => [
- 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
- ],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-0']],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
- 'value' => '0', 'id' => 'model-multi-field-0'
- ]],
- 'first',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-1']],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
- 'value' => '1', 'id' => 'model-multi-field-1'
- ]],
- 'second',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-2']],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
- 'value' => '2', 'id' => 'model-multi-field-2'
- ]],
- 'third',
- '/label',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->select(
- 'Model.multi_field',
- ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
- ['multiple' => 'checkbox']
- );
- $expected = [
- 'input' => [
- 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
- ],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-a+']],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
- 'value' => 'a+', 'id' => 'model-multi-field-a+'
- ]],
- 'first',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-a++']],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
- 'value' => 'a++', 'id' => 'model-multi-field-a++'
- ]],
- 'second',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-a+++']],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
- 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
- ]],
- 'third',
- '/label',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->select(
- 'Model.multi_field',
- ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
- ['multiple' => 'checkbox']
- );
- $expected = [
- 'input' => [
- 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
- ],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-a-b']],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
- 'value' => 'a>b', 'id' => 'model-multi-field-a-b'
- ]],
- 'first',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-a-b1']],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
- 'value' => 'a<b', 'id' => 'model-multi-field-a-b1'
- ]],
- 'second',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-a-b2']],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
- 'value' => 'a"b', 'id' => 'model-multi-field-a-b2'
- ]],
- 'third',
- '/label',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testSelectMultipleCheckboxRequestData method
- *
- * Ensure that multiCheckbox reads from the request data.
- *
- * @return void
- */
- public function testSelectMultipleCheckboxRequestData()
- {
- $this->Form->request->data = ['Model' => ['tags' => [1]]];
- $result = $this->Form->select(
- 'Model.tags',
- ['1' => 'first', 'Array' => 'Array'],
- ['multiple' => 'checkbox']
- );
- $expected = [
- 'input' => [
- 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
- ],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'Model[tags][]',
- 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
- ]],
- 'first',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-tags-array']],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'Model[tags][]',
- 'value' => 'Array', 'id' => 'model-tags-array'
- ]],
- 'Array',
- '/label',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testSelectMultipleCheckboxSecurity method
- *
- * Checks the security hash array generated for multiple-input checkbox elements.
- *
- * @return void
- */
- public function testSelectMultipleCheckboxSecurity()
- {
- $this->Form->request->params['_Token'] = 'testKey';
- $this->assertEquals([], $this->Form->fields);
- $this->Form->select(
- 'Model.multi_field',
- ['1' => 'first', '2' => 'second', '3' => 'third'],
- ['multiple' => 'checkbox']
- );
- $this->assertEquals(['Model.multi_field'], $this->Form->fields);
- $result = $this->Form->secure($this->Form->fields);
- $key = '8af36fb34e6f2ef8ba0eb473bb4365ec232f3fe5%3A';
- $this->assertContains('"' . $key . '"', $result);
- }
- /**
- * testSelectMultipleSecureWithNoOptions method
- *
- * Multiple select elements should always be secured as they always participate
- * in the POST data.
- *
- * @return void
- */
- public function testSelectMultipleSecureWithNoOptions()
- {
- $this->assertEquals([], $this->Form->fields);
- $this->Form->select(
- 'Model.select',
- [],
- ['multiple' => true]
- );
- $this->assertEquals(['Model.select'], $this->Form->fields);
- }
- /**
- * testSelectNoSecureWithNoOptions method
- *
- * When a select box has no options it should not be added to the fields list
- * as it always fail post validation.
- *
- * @return void
- */
- public function testSelectNoSecureWithNoOptions()
- {
- $this->Form->request->params['_Token'] = 'testkey';
- $this->assertEquals([], $this->Form->fields);
- $this->Form->select(
- 'Model.select',
- []
- );
- $this->assertEquals([], $this->Form->fields);
- $this->Form->select(
- 'Model.user_id',
- [],
- ['empty' => true]
- );
- $this->assertEquals(['Model.user_id'], $this->Form->fields);
- }
- /**
- * testControlMultipleCheckboxes method
- *
- * Test control() resulting in multi select elements being generated.
- *
- * @return void
- */
- public function testControlMultipleCheckboxes()
- {
- $result = $this->Form->control('Model.multi_field', [
- 'options' => ['first', 'second', 'third'],
- 'multiple' => 'checkbox'
- ]);
- $expected = [
- ['div' => ['class' => 'input select']],
- ['label' => ['for' => 'model-multi-field']],
- 'Multi Field',
- '/label',
- 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-0']],
- ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
- 'first',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-1']],
- ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
- 'second',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-2']],
- ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
- 'third',
- '/label',
- '/div',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Model.multi_field', [
- 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
- 'multiple' => 'checkbox'
- ]);
- $expected = [
- ['div' => ['class' => 'input select']],
- ['label' => ['for' => 'model-multi-field']],
- 'Multi Field',
- '/label',
- 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-a']],
- ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
- 'first',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-b']],
- ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
- 'second',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-c']],
- ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
- 'third',
- '/label',
- '/div',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testSelectHiddenFieldOmission method
- *
- * Test that select() with 'hiddenField' => false omits the hidden field.
- *
- * @return void
- */
- public function testSelectHiddenFieldOmission()
- {
- $result = $this->Form->select(
- 'Model.multi_field',
- ['first', 'second'],
- ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
- );
- $this->assertNotContains('type="hidden"', $result);
- }
- /**
- * testSelectCheckboxMultipleOverrideName method
- *
- * Test that select() with multiple = checkbox works with overriding name attribute.
- *
- * @return void
- */
- public function testSelectCheckboxMultipleOverrideName()
- {
- $result = $this->Form->select('category', ['1', '2'], [
- 'multiple' => 'checkbox',
- 'name' => 'fish',
- ]);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'fish-0']],
- ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
- '1',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'fish-1']],
- ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
- '2',
- '/label',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->multiCheckbox(
- 'category',
- new Collection(['1', '2']),
- ['name' => 'fish']
- );
- $this->assertHtml($expected, $result);
- $result = $this->Form->multiCheckbox('category', ['1', '2'], [
- 'name' => 'fish',
- ]);
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlMultiCheckbox method
- *
- * Test that control() works with multicheckbox.
- *
- * @return void
- */
- public function testControlMultiCheckbox()
- {
- $result = $this->Form->control('category', [
- 'type' => 'multicheckbox',
- 'options' => ['1', '2'],
- ]);
- $expected = [
- ['div' => ['class' => 'input multicheckbox']],
- '<label',
- 'Category',
- '/label',
- 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'category-0']],
- ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
- '1',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'category-1']],
- ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
- '2',
- '/label',
- '/div',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testCheckbox method
- *
- * Test generation of checkboxes.
- *
- * @return void
- */
- public function testCheckbox()
- {
- $result = $this->Form->checkbox('Model.field');
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
- ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->checkbox('Model.field', [
- 'id' => 'theID',
- 'value' => 'myvalue',
- 'form' => 'my-form',
- ]);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'Model[field]',
- 'value' => 'myvalue', 'id' => 'theID',
- 'form' => 'my-form',
- ]]
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testCheckboxDefaultValue method
- *
- * Test default value setting on checkbox() method.
- *
- * @return void
- */
- public function testCheckboxDefaultValue()
- {
- $this->Form->request->data['Model']['field'] = false;
- $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
- $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
- $this->assertHtml($expected, $result);
- unset($this->Form->request->data['Model']['field']);
- $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
- $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
- $this->assertHtml($expected, $result);
- $this->Form->request->data['Model']['field'] = true;
- $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
- $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
- $this->assertHtml($expected, $result);
- unset($this->Form->request->data['Model']['field']);
- $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
- $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
- $this->assertHtml($expected, $result);
- $Articles = TableRegistry::get('Articles');
- $Articles->getSchema()->addColumn(
- 'published',
- ['type' => 'boolean', 'null' => false, 'default' => true]
- );
- $this->Form->create($Articles->newEntity());
- $result = $this->Form->checkbox('published', ['hiddenField' => false]);
- $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
- $this->assertHtml($expected, $result);
- }
- /**
- * testCheckboxCheckedAndError method
- *
- * Test checkbox being checked or having errors.
- *
- * @return void
- */
- public function testCheckboxCheckedAndError()
- {
- $this->article['errors'] = [
- 'published' => true
- ];
- $this->Form->request->data['published'] = 'myvalue';
- $this->Form->create($this->article);
- $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
- $expected = [
- 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'published',
- 'value' => 'myvalue',
- 'id' => 'theID',
- 'checked' => 'checked',
- 'class' => 'form-error'
- ]]
- ];
- $this->assertHtml($expected, $result);
- $this->Form->request->data['published'] = '';
- $result = $this->Form->checkbox('published');
- $expected = [
- 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
- ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testCheckboxCustomNameAttribute method
- *
- * Test checkbox() with a custom name attribute.
- *
- * @return void
- */
- public function testCheckboxCustomNameAttribute()
- {
- $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
- ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testCheckboxHiddenField method
- *
- * Test that the hidden input for checkboxes can be omitted or set to a
- * specific value.
- *
- * @return void
- */
- public function testCheckboxHiddenField()
- {
- $result = $this->Form->checkbox('UserForm.something', [
- 'hiddenField' => false
- ]);
- $expected = [
- 'input' => [
- 'type' => 'checkbox',
- 'name' => 'UserForm[something]',
- 'value' => '1'
- ],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->checkbox('UserForm.something', [
- 'value' => 'Y',
- 'hiddenField' => 'N',
- ]);
- $expected = [
- ['input' => [
- 'type' => 'hidden', 'name' => 'UserForm[something]',
- 'value' => 'N'
- ]],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'UserForm[something]',
- 'value' => 'Y'
- ]],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testTime method
- *
- * Test the time type.
- *
- * @return void
- */
- public function testTime()
- {
- $result = $this->Form->time('start_time', [
- 'timeFormat' => 12,
- 'interval' => 5,
- 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
- ]);
- $this->assertContains('<option value="04" selected="selected">4</option>', $result);
- $this->assertContains('<option value="30" selected="selected">30</option>', $result);
- $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
- $this->assertNotContains('year', $result);
- $this->assertNotContains('month', $result);
- $this->assertNotContains('day', $result);
- $result = $this->Form->time('start_time', [
- 'timeFormat' => 12,
- 'interval' => 5,
- 'value' => '2014-03-08 16:30:00'
- ]);
- $this->assertContains('<option value="04" selected="selected">4</option>', $result);
- $this->assertContains('<option value="30" selected="selected">30</option>', $result);
- $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
- $this->assertNotContains('year', $result);
- $this->assertNotContains('month', $result);
- $this->assertNotContains('day', $result);
- }
- /**
- * testTimeFormat24NoMeridian method
- *
- * Ensure that timeFormat=24 has no merdian.
- *
- * @return void.
- */
- public function testTimeFormat24NoMeridian()
- {
- $result = $this->Form->time('start_time', [
- 'timeFormat' => 24,
- 'interval' => 5,
- 'value' => '2014-03-08 16:30:00'
- ]);
- $this->assertContains('<option value="16" selected="selected">16</option>', $result);
- $this->assertContains('<option value="30" selected="selected">30</option>', $result);
- $this->assertNotContains('meridian', $result);
- $this->assertNotContains('pm', $result);
- $this->assertNotContains('year', $result);
- $this->assertNotContains('month', $result);
- $this->assertNotContains('day', $result);
- }
- /**
- * testDate method
- *
- * Test the date type.
- *
- * @return void
- */
- public function testDate()
- {
- $result = $this->Form->date('start_day', [
- 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
- ]);
- $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
- $this->assertContains('<option value="03" selected="selected">March</option>', $result);
- $this->assertContains('<option value="08" selected="selected">8</option>', $result);
- $this->assertNotContains('hour', $result);
- $this->assertNotContains('minute', $result);
- $this->assertNotContains('second', $result);
- $this->assertNotContains('meridian', $result);
- }
- /**
- * testDateTime method
- *
- * Test generation of date/time select elements.
- *
- * @return void
- */
- public function testDateTime()
- {
- extract($this->dateRegex);
- $result = $this->Form->dateTime('Contact.date', ['default' => true]);
- $now = strtotime('now');
- $expected = [
- ['select' => ['name' => 'Contact[date][year]']],
- ['option' => ['value' => '']],
- '/option',
- $yearsRegex,
- ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
- date('Y', $now),
- '/option',
- '*/select',
- ['select' => ['name' => 'Contact[date][month]']],
- ['option' => ['value' => '']],
- '/option',
- $monthsRegex,
- ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
- date('F', $now),
- '/option',
- '*/select',
- ['select' => ['name' => 'Contact[date][day]']],
- ['option' => ['value' => '']],
- '/option',
- $daysRegex,
- ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
- date('j', $now),
- '/option',
- '*/select',
- ['select' => ['name' => 'Contact[date][hour]']],
- ['option' => ['value' => '']],
- '/option',
- $hoursRegex,
- ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
- date('G', $now),
- '/option',
- '*/select',
- ['select' => ['name' => 'Contact[date][minute]']],
- ['option' => ['value' => '']],
- '/option',
- $minutesRegex,
- ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
- date('i', $now),
- '/option',
- '*/select',
- ];
- $this->assertHtml($expected, $result);
- // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
- $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
- $now = strtotime('now');
- $expected = [
- ['select' => ['name' => 'Contact[date][year]']],
- $yearsRegex,
- ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
- date('Y', $now),
- '/option',
- '*/select',
- ['select' => ['name' => 'Contact[date][month]']],
- $monthsRegex,
- ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
- date('F', $now),
- '/option',
- '*/select',
- ['select' => ['name' => 'Contact[date][day]']],
- $daysRegex,
- ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
- date('j', $now),
- '/option',
- '*/select',
- ['select' => ['name' => 'Contact[date][hour]']],
- $hoursRegex,
- ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
- date('G', $now),
- '/option',
- '*/select',
- ['select' => ['name' => 'Contact[date][minute]']],
- $minutesRegex,
- ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
- date('i', $now),
- '/option',
- '*/select',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testDateTimeSecured method
- *
- * Test that datetime fields are added to protected fields list.
- *
- * @return void
- */
- public function testDateTimeSecured()
- {
- $this->Form->request->params['_Token'] = ['unlockedFields' => []];
- $this->Form->dateTime('Contact.date');
- $expected = [
- 'Contact.date.year',
- 'Contact.date.month',
- 'Contact.date.day',
- 'Contact.date.hour',
- 'Contact.date.minute',
- ];
- $this->assertEquals($expected, $this->Form->fields);
- $this->Form->fields = [];
- $this->Form->date('Contact.published');
- $expected = [
- 'Contact.published.year',
- 'Contact.published.month',
- 'Contact.published.day',
- ];
- $this->assertEquals($expected, $this->Form->fields);
- }
- /**
- * testDateTimeSecuredDisabled method
- *
- * Test that datetime fields are added to protected fields list.
- *
- * @return void
- */
- public function testDateTimeSecuredDisabled()
- {
- $this->Form->request->params['_Token'] = ['unlockedFields' => []];
- $this->Form->dateTime('Contact.date', ['secure' => false]);
- $expected = [];
- $this->assertEquals($expected, $this->Form->fields);
- $this->Form->fields = [];
- $this->Form->date('Contact.published', ['secure' => false]);
- $expected = [];
- $this->assertEquals($expected, $this->Form->fields);
- }
- /**
- * testDatetimeEmpty method
- *
- * Test empty defaulting to true for datetime.
- *
- * @return void
- */
- public function testDatetimeEmpty()
- {
- extract($this->dateRegex);
- $result = $this->Form->dateTime('Contact.date', [
- 'timeFormat' => 12,
- 'empty' => true,
- 'default' => true
- ]);
- $expected = [
- ['select' => ['name' => 'Contact[date][year]']],
- $yearsRegex,
- ['option' => ['value' => '']],
- '/option',
- '*/select',
- ['select' => ['name' => 'Contact[date][month]']],
- $monthsRegex,
- ['option' => ['value' => '']],
- '/option',
- '*/select',
- ['select' => ['name' => 'Contact[date][day]']],
- $daysRegex,
- ['option' => ['value' => '']],
- '/option',
- '*/select',
- ['select' => ['name' => 'Contact[date][hour]']],
- $hoursRegex,
- ['option' => ['value' => '']],
- '/option',
- '*/select',
- ['select' => ['name' => 'Contact[date][minute]']],
- $minutesRegex,
- ['option' => ['value' => '']],
- '/option',
- '*/select',
- ['select' => ['name' => 'Contact[date][meridian]']],
- $meridianRegex,
- ['option' => ['value' => '']],
- '/option',
- '*/select'
- ];
- $this->assertHtml($expected, $result);
- $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
- }
- /**
- * testDatetimeMinuteInterval method
- *
- * Test datetime with interval option.
- *
- * @return void
- */
- public function testDatetimeMinuteInterval()
- {
- extract($this->dateRegex);
- $result = $this->Form->dateTime('Contact.date', [
- 'interval' => 5,
- 'value' => ''
- ]);
- $expected = [
- ['select' => ['name' => 'Contact[date][year]']],
- $yearsRegex,
- ['option' => ['selected' => 'selected', 'value' => '']],
- '/option',
- '*/select',
- ['select' => ['name' => 'Contact[date][month]']],
- $monthsRegex,
- ['option' => ['selected' => 'selected', 'value' => '']],
- '/option',
- '*/select',
- ['select' => ['name' => 'Contact[date][day]']],
- $daysRegex,
- ['option' => ['selected' => 'selected', 'value' => '']],
- '/option',
- '*/select',
- ['select' => ['name' => 'Contact[date][hour]']],
- $hoursRegex,
- ['option' => ['selected' => 'selected', 'value' => '']],
- '/option',
- '*/select',
- ['select' => ['name' => 'Contact[date][minute]']],
- $minutesRegex,
- ['option' => ['selected' => 'selected', 'value' => '']],
- '/option',
- ['option' => ['value' => '00']],
- '00',
- '/option',
- ['option' => ['value' => '05']],
- '05',
- '/option',
- ['option' => ['value' => '10']],
- '10',
- '/option',
- '*/select',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testDateTimeRounding method
- *
- * Test dateTime with rounding.
- *
- * @return void
- */
- public function testDateTimeRounding()
- {
- $this->Form->request->data['Contact'] = [
- 'date' => [
- 'day' => '13',
- 'month' => '12',
- 'year' => '2010',
- 'hour' => '04',
- 'minute' => '19',
- 'meridian' => 'AM'
- ]
- ];
- $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
- $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
- $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
- $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
- $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
- $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
- }
- /**
- * testDatetimeWithDefault method
- *
- * Test that datetime() and default values work.
- *
- * @return void
- */
- public function testDatetimeWithDefault()
- {
- $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
- $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
- $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
- $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
- $result = $this->Form->dateTime('Contact.updated', [
- 'default' => '2009-06-01 11:15:30'
- ]);
- $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
- $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
- $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
- }
- /**
- * testDateTimeAllZeros method
- *
- * Test datetime() with all zeros.
- *
- * @return void
- */
- public function testDateTimeAllZeros()
- {
- $result = $this->Form->dateTime('Contact.date', [
- 'timeFormat' => false,
- 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
- 'value' => '0000-00-00'
- ]);
- $this->assertRegExp('/<option value="">-<\/option>/', $result);
- $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
- }
- /**
- * testDateTimeEmptyAsArray method
- *
- * @return void
- */
- public function testDateTimeEmptyAsArray()
- {
- $result = $this->Form->dateTime('Contact.date', [
- 'empty' => [
- 'day' => 'DAY',
- 'month' => 'MONTH',
- 'year' => 'YEAR',
- 'hour' => 'HOUR',
- 'minute' => 'MINUTE',
- 'meridian' => false
- ],
- 'default' => true
- ]);
- $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
- $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
- $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
- $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
- $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
- $this->assertNotRegExp('/<option value=""><\/option>/', $result);
- $result = $this->Form->dateTime('Contact.date', [
- 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
- 'default' => true
- ]);
- $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
- $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
- $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
- }
- /**
- * testFormDateTimeMulti method
- *
- * Test multiple datetime element generation.
- *
- * @return void
- */
- public function testFormDateTimeMulti()
- {
- extract($this->dateRegex);
- $result = $this->Form->dateTime('Contact.1.updated');
- $this->assertContains('Contact[1][updated][month]', $result);
- $this->assertContains('Contact[1][updated][day]', $result);
- $this->assertContains('Contact[1][updated][year]', $result);
- $this->assertContains('Contact[1][updated][hour]', $result);
- $this->assertContains('Contact[1][updated][minute]', $result);
- }
- /**
- * testDateTimeLabelIdMatchesFirstControl method
- *
- * When changing the date format, the label should always focus the first select box when
- * clicked.
- *
- * @return void
- */
- public function testDateTimeLabelIdMatchesFirstControl()
- {
- $result = $this->Form->control('Model.date', ['type' => 'date']);
- $this->assertContains('<label>Date</label>', $result);
- $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
- $this->assertContains('<label>Date</label>', $result);
- $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
- $this->assertContains('<label>Date</label>', $result);
- }
- /**
- * testDateTimeSecondOptions method
- *
- * Test datetime second=true.
- *
- * @return void
- */
- public function testDateTimeSecondOptions()
- {
- $result = $this->Form->dateTime('updated', ['second' => true]);
- $this->assertContains('updated[second]', $result, 'Should have seconds');
- $result = $this->Form->dateTime('updated', ['second' => []]);
- $this->assertContains('updated[second]', $result, 'Should have seconds');
- $result = $this->Form->dateTime('updated', ['second' => null]);
- $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
- $result = $this->Form->dateTime('updated', ['second' => false]);
- $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
- }
- /**
- * testMonth method
- *
- * Test generation of a month input.
- *
- * @return void
- */
- public function testMonth()
- {
- $result = $this->Form->month('Model.field', ['value' => '']);
- $expected = [
- ['select' => ['name' => 'Model[field][month]']],
- ['option' => ['value' => '', 'selected' => 'selected']],
- '/option',
- ['option' => ['value' => '01']],
- date('F', strtotime('2008-01-01 00:00:00')),
- '/option',
- ['option' => ['value' => '02']],
- date('F', strtotime('2008-02-01 00:00:00')),
- '/option',
- '*/select',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
- $expected = [
- ['select' => ['name' => 'Model[field][month]']],
- ['option' => ['selected' => 'selected', 'value' => '']],
- '/option',
- ['option' => ['value' => '01']],
- date('F', strtotime('2008-01-01 00:00:00')),
- '/option',
- ['option' => ['value' => '02']],
- date('F', strtotime('2008-02-01 00:00:00')),
- '/option',
- '*/select',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
- $expected = [
- ['select' => ['name' => 'Model[field][month]']],
- ['option' => ['selected' => 'selected', 'value' => '']],
- '/option',
- ['option' => ['value' => '01']],
- '1',
- '/option',
- ['option' => ['value' => '02']],
- '2',
- '/option',
- '*/select',
- ];
- $this->assertHtml($expected, $result);
- $monthNames = [
- '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
- '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
- ];
- $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
- $expected = [
- ['select' => ['name' => 'Model[field][month]']],
- ['option' => ['value' => '']],
- '/option',
- ['option' => ['value' => '01', 'selected' => 'selected']],
- 'Jan',
- '/option',
- ['option' => ['value' => '02']],
- 'Feb',
- '/option',
- '*/select',
- ];
- $this->assertHtml($expected, $result);
- $this->Form->request->data['Project']['release'] = '2050-02-10';
- $result = $this->Form->month('Project.release');
- $expected = [
- ['select' => ['name' => 'Project[release][month]']],
- ['option' => ['value' => '']],
- '/option',
- ['option' => ['value' => '01']],
- 'January',
- '/option',
- ['option' => ['value' => '02', 'selected' => 'selected']],
- 'February',
- '/option',
- '*/select',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->month('Contact.published', [
- 'empty' => 'Published on',
- ]);
- $this->assertContains('Published on', $result);
- }
- /**
- * testDay method
- *
- * Test generation of a day input.
- *
- * @return void
- */
- public function testDay()
- {
- extract($this->dateRegex);
- $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
- $expected = [
- ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
- ['option' => ['selected' => 'selected', 'value' => '']],
- '/option',
- ['option' => ['value' => '01']],
- '1',
- '/option',
- ['option' => ['value' => '02']],
- '2',
- '/option',
- $daysRegex,
- '/select',
- ];
- $this->assertHtml($expected, $result);
- $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
- $result = $this->Form->day('Model.field');
- $expected = [
- ['select' => ['name' => 'Model[field][day]']],
- ['option' => ['value' => '']],
- '/option',
- ['option' => ['value' => '01']],
- '1',
- '/option',
- ['option' => ['value' => '02']],
- '2',
- '/option',
- $daysRegex,
- ['option' => ['value' => '10', 'selected' => 'selected']],
- '10',
- '/option',
- $daysRegex,
- '/select',
- ];
- $this->assertHtml($expected, $result);
- $this->Form->request->data['Model']['field'] = '';
- $result = $this->Form->day('Model.field', ['value' => '10']);
- $expected = [
- ['select' => ['name' => 'Model[field][day]']],
- ['option' => ['value' => '']],
- '/option',
- ['option' => ['value' => '01']],
- '1',
- '/option',
- ['option' => ['value' => '02']],
- '2',
- '/option',
- $daysRegex,
- ['option' => ['value' => '10', 'selected' => 'selected']],
- '10',
- '/option',
- $daysRegex,
- '/select',
- ];
- $this->assertHtml($expected, $result);
- $this->Form->request->data['Project']['release'] = '2050-10-10';
- $result = $this->Form->day('Project.release');
- $expected = [
- ['select' => ['name' => 'Project[release][day]']],
- ['option' => ['value' => '']],
- '/option',
- ['option' => ['value' => '01']],
- '1',
- '/option',
- ['option' => ['value' => '02']],
- '2',
- '/option',
- $daysRegex,
- ['option' => ['value' => '10', 'selected' => 'selected']],
- '10',
- '/option',
- $daysRegex,
- '/select',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->day('Contact.published', [
- 'empty' => 'Published on',
- ]);
- $this->assertContains('Published on', $result);
- }
- /**
- * testMinute method
- *
- * Test generation of a minute input.
- *
- * @return void
- */
- public function testMinute()
- {
- extract($this->dateRegex);
- $result = $this->Form->minute('Model.field', ['value' => '']);
- $expected = [
- ['select' => ['name' => 'Model[field][minute]']],
- ['option' => ['selected' => 'selected', 'value' => '']],
- '/option',
- ['option' => ['value' => '00']],
- '00',
- '/option',
- ['option' => ['value' => '01']],
- '01',
- '/option',
- ['option' => ['value' => '02']],
- '02',
- '/option',
- $minutesRegex,
- '/select',
- ];
- $this->assertHtml($expected, $result);
- $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
- $result = $this->Form->minute('Model.field');
- $expected = [
- ['select' => ['name' => 'Model[field][minute]']],
- ['option' => ['value' => '']],
- '/option',
- ['option' => ['value' => '00']],
- '00',
- '/option',
- ['option' => ['value' => '01']],
- '01',
- '/option',
- ['option' => ['value' => '02']],
- '02',
- '/option',
- $minutesRegex,
- ['option' => ['value' => '12', 'selected' => 'selected']],
- '12',
- '/option',
- $minutesRegex,
- '/select',
- ];
- $this->assertHtml($expected, $result);
- $this->Form->request->data['Model']['field'] = '';
- $result = $this->Form->minute('Model.field', ['interval' => 5]);
- $expected = [
- ['select' => ['name' => 'Model[field][minute]']],
- ['option' => ['selected' => 'selected', 'value' => '']],
- '/option',
- ['option' => ['value' => '00']],
- '00',
- '/option',
- ['option' => ['value' => '05']],
- '05',
- '/option',
- ['option' => ['value' => '10']],
- '10',
- '/option',
- $minutesRegex,
- '/select',
- ];
- $this->assertHtml($expected, $result);
- $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
- $result = $this->Form->minute('Model.field', ['interval' => 5]);
- $expected = [
- ['select' => ['name' => 'Model[field][minute]']],
- ['option' => ['value' => '']],
- '/option',
- ['option' => ['value' => '00']],
- '00',
- '/option',
- ['option' => ['value' => '05']],
- '05',
- '/option',
- ['option' => ['value' => '10', 'selected' => 'selected']],
- '10',
- '/option',
- $minutesRegex,
- '/select',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testMeridian method
- *
- * Test generating an input for the meridian.
- *
- * @return void
- */
- public function testMeridian()
- {
- extract($this->dateRegex);
- $now = new \DateTime();
- $result = $this->Form->meridian('Model.field', ['value' => 'am']);
- $expected = [
- ['select' => ['name' => 'Model[field][meridian]']],
- ['option' => ['value' => '']],
- '/option',
- $meridianRegex,
- ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
- $now->format('a'),
- '/option',
- '*/select'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testHour method
- *
- * Test generation of an hour input.
- *
- * @return void
- */
- public function testHour()
- {
- extract($this->dateRegex);
- $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
- $expected = [
- ['select' => ['name' => 'Model[field][hour]']],
- ['option' => ['selected' => 'selected', 'value' => '']],
- '/option',
- ['option' => ['value' => '01']],
- '1',
- '/option',
- ['option' => ['value' => '02']],
- '2',
- '/option',
- $hoursRegex,
- '/select',
- ];
- $this->assertHtml($expected, $result);
- $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
- $result = $this->Form->hour('Model.field', ['format' => 12]);
- $expected = [
- ['select' => ['name' => 'Model[field][hour]']],
- ['option' => ['value' => '']],
- '/option',
- ['option' => ['value' => '01']],
- '1',
- '/option',
- ['option' => ['value' => '02']],
- '2',
- '/option',
- $hoursRegex,
- ['option' => ['value' => '12', 'selected' => 'selected']],
- '12',
- '/option',
- '/select',
- ];
- $this->assertHtml($expected, $result);
- $this->Form->request->data['Model']['field'] = '';
- $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
- $this->assertContains('<option value="23" selected="selected">23</option>', $result);
- $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
- $this->assertContains('<option value="11" selected="selected">11</option>', $result);
- $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
- $result = $this->Form->hour('Model.field', ['format' => 24]);
- $expected = [
- ['select' => ['name' => 'Model[field][hour]']],
- ['option' => ['value' => '']],
- '/option',
- ['option' => ['value' => '00', 'selected' => 'selected']],
- '0',
- '/option',
- ['option' => ['value' => '01']],
- '1',
- '/option',
- ['option' => ['value' => '02']],
- '2',
- '/option',
- $hoursRegex,
- '/select',
- ];
- $this->assertHtml($expected, $result);
- unset($this->Form->request->data['Model']['field']);
- $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
- $thisHour = date('H');
- $optValue = date('G');
- $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
- $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
- $result = $this->Form->hour('Model.field', ['format' => 24]);
- $expected = [
- ['select' => ['name' => 'Model[field][hour]']],
- ['option' => ['value' => '']],
- '/option',
- ['option' => ['value' => '00']],
- '0',
- '/option',
- ['option' => ['value' => '01', 'selected' => 'selected']],
- '1',
- '/option',
- ['option' => ['value' => '02']],
- '2',
- '/option',
- $hoursRegex,
- '/select',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testYear method
- *
- * Test generation of a year input.
- *
- * @return void
- */
- public function testYear()
- {
- $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
- $expected = [
- ['select' => ['name' => 'Model[field][year]']],
- ['option' => ['selected' => 'selected', 'value' => '']],
- '/option',
- ['option' => ['value' => '2007']],
- '2007',
- '/option',
- ['option' => ['value' => '2006']],
- '2006',
- '/option',
- '/select',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->year('Model.field', [
- 'value' => '',
- 'minYear' => 2006,
- 'maxYear' => 2007,
- 'orderYear' => 'asc'
- ]);
- $expected = [
- ['select' => ['name' => 'Model[field][year]']],
- ['option' => ['selected' => 'selected', 'value' => '']],
- '/option',
- ['option' => ['value' => '2006']],
- '2006',
- '/option',
- ['option' => ['value' => '2007']],
- '2007',
- '/option',
- '/select',
- ];
- $this->assertHtml($expected, $result);
- $this->Form->request->data['Contact']['published'] = '2006-10-10';
- $result = $this->Form->year('Contact.published', [
- 'empty' => false,
- 'minYear' => 2006,
- 'maxYear' => 2007,
- ]);
- $expected = [
- ['select' => ['name' => 'Contact[published][year]']],
- ['option' => ['value' => '2007']],
- '2007',
- '/option',
- ['option' => ['value' => '2006', 'selected' => 'selected']],
- '2006',
- '/option',
- '/select',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->year('Contact.published', [
- 'empty' => 'Published on',
- ]);
- $this->assertContains('Published on', $result);
- }
- /**
- * testControlDatetimePreEpoch method
- *
- * Test minYear being prior to the unix epoch.
- *
- * @return void
- */
- public function testControlDatetimePreEpoch()
- {
- $start = date('Y') - 80;
- $end = date('Y') - 18;
- $result = $this->Form->control('birth_year', [
- 'type' => 'date',
- 'label' => 'Birth Year',
- 'minYear' => $start,
- 'maxYear' => $end,
- 'month' => false,
- 'day' => false,
- ]);
- $this->assertContains('value="' . $start . '">' . $start, $result);
- $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
- $this->assertNotContains('value="00">00', $result);
- }
- /**
- * testYearAutoExpandRange method
- *
- * @return void
- */
- public function testYearAutoExpandRange()
- {
- $this->Form->request->data['User']['birthday'] = '1930-10-10';
- $result = $this->Form->year('User.birthday');
- preg_match_all('/<option value="([\d]+)"/', $result, $matches);
- $result = $matches[1];
- $expected = range(date('Y') + 5, 1930);
- $this->assertEquals($expected, $result);
- $this->Form->request->data['Project']['release'] = '2050-10-10';
- $result = $this->Form->year('Project.release');
- preg_match_all('/<option value="([\d]+)"/', $result, $matches);
- $result = $matches[1];
- $expected = range(2050, date('Y') - 5);
- $this->assertEquals($expected, $result);
- $this->Form->request->data['Project']['release'] = '1881-10-10';
- $result = $this->Form->year('Project.release', [
- 'minYear' => 1890,
- 'maxYear' => 1900
- ]);
- preg_match_all('/<option value="([\d]+)"/', $result, $matches);
- $result = $matches[1];
- $expected = range(1900, 1881);
- $this->assertEquals($expected, $result);
- }
- /**
- * testControlDate method
- *
- * Test that control() accepts the type of date and passes options in.
- *
- * @return void
- */
- public function testControlDate()
- {
- $this->Form->request->data = [
- 'month_year' => ['month' => date('m')],
- ];
- $this->Form->create($this->article);
- $result = $this->Form->control('month_year', [
- 'label' => false,
- 'type' => 'date',
- 'minYear' => 2006,
- 'maxYear' => 2008
- ]);
- $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
- $this->assertNotContains('value="2008" selected="selected"', $result);
- }
- /**
- * testControlLabelFalse method
- *
- * Test the label option being set to false.
- *
- * @return void
- */
- public function testControlLabelFalse()
- {
- $this->Form->create($this->article);
- $result = $this->Form->control('title', ['label' => false]);
- $expected = [
- 'div' => ['class' => 'input text required'],
- 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlDateMaxYear method
- *
- * Let's say we want to only allow users born from 2006 to 2008 to register
- * This being the first signup page, we still don't have any data.
- *
- * @return void
- */
- public function testControlDateMaxYear()
- {
- $this->Form->request->data = [];
- $this->Form->create($this->article);
- $result = $this->Form->control('birthday', [
- 'label' => false,
- 'type' => 'date',
- 'minYear' => 2006,
- 'maxYear' => 2008,
- 'default' => true
- ]);
- $this->assertContains('value="2008" selected="selected"', $result);
- $this->assertContains('value="2006"', $result);
- $this->assertNotContains('value="2005"', $result);
- $this->assertNotContains('value="2009"', $result);
- }
- /**
- * testTextArea method
- *
- * Test generation of a textarea input.
- *
- * @return void
- */
- public function testTextArea()
- {
- $this->Form->request->data = ['field' => 'some test data'];
- $result = $this->Form->textarea('field');
- $expected = [
- 'textarea' => ['name' => 'field', 'rows' => 5],
- 'some test data',
- '/textarea',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->textarea('user.bio');
- $expected = [
- 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
- '/textarea',
- ];
- $this->assertHtml($expected, $result);
- $this->Form->request->data = ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'];
- $result = $this->Form->textarea('field');
- $expected = [
- 'textarea' => ['name' => 'field', 'rows' => 5],
- htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
- '/textarea',
- ];
- $this->assertHtml($expected, $result);
- $this->Form->request->data = [
- 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
- ];
- $result = $this->Form->textarea('Model.field', ['escape' => false]);
- $expected = [
- 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
- 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
- '/textarea',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->textarea('0.OtherModel.field');
- $expected = [
- 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
- '/textarea'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testTextAreaWithStupidCharacters method
- *
- * Test text area with non-ascii characters.
- *
- * @return void
- */
- public function testTextAreaWithStupidCharacters()
- {
- $result = $this->Form->textarea('Post.content', [
- 'value' => 'GREAT®',
- 'rows' => '15',
- 'cols' => '75'
- ]);
- $expected = [
- 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
- 'GREAT®',
- '/textarea',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testTextAreaMaxLength method
- *
- * Test textareas maxlength read from schema.
- *
- * @return void
- */
- public function testTextAreaMaxLength()
- {
- $this->Form->create([
- 'schema' => [
- 'stuff' => ['type' => 'string', 'length' => 10],
- ]
- ]);
- $result = $this->Form->control('other', ['type' => 'textarea']);
- $expected = [
- 'div' => ['class' => 'input textarea'],
- 'label' => ['for' => 'other'],
- 'Other',
- '/label',
- 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
- '/textarea',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('stuff', ['type' => 'textarea']);
- $expected = [
- 'div' => ['class' => 'input textarea'],
- 'label' => ['for' => 'stuff'],
- 'Stuff',
- '/label',
- 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
- '/textarea',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testHiddenField method
- *
- * Test generation of a hidden input.
- *
- * @return void
- */
- public function testHiddenField()
- {
- $this->article['errors'] = [
- 'field' => true
- ];
- $this->Form->request->data['field'] = 'test';
- $this->Form->create($this->article);
- $result = $this->Form->hidden('field', ['id' => 'theID']);
- $expected = [
- 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
- $this->assertHtml($expected, $result);
- $result = $this->Form->hidden('field', ['value' => 'my value']);
- $expected = [
- 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFileUploadField method
- *
- * Test generation of a file upload input.
- *
- * @return void
- */
- public function testFileUploadField()
- {
- $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
- $result = $this->Form->file('Model.upload');
- $this->assertHtml($expected, $result);
- $this->Form->request->data['Model']['upload'] = [
- 'name' => '', 'type' => '', 'tmp_name' => '',
- 'error' => 4, 'size' => 0
- ];
- $result = $this->Form->file('Model.upload');
- $this->assertHtml($expected, $result);
- $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
- $result = $this->Form->file('Model.upload');
- $this->assertHtml($expected, $result);
- }
- /**
- * testFileUploadOnOtherModel method
- *
- * Test File upload input on a model not used in create().
- *
- * @return void
- */
- public function testFileUploadOnOtherModel()
- {
- $this->Form->create($this->article, ['type' => 'file']);
- $result = $this->Form->file('ValidateProfile.city');
- $expected = [
- 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testButton method
- *
- * Test generation of a form button.
- *
- * @return void
- */
- public function testButton()
- {
- $result = $this->Form->button('Hi');
- $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
- $this->assertHtml($expected, $result);
- $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
- $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
- $this->assertHtml($expected, $result);
- $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
- $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
- $this->assertHtml($expected, $result);
- $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
- $expected = ['button' => ['type' => 'reset'], '<Clear Form>', '/button'];
- $this->assertHtml($expected, $result);
- $result = $this->Form->button('No type', ['type' => false]);
- $expected = ['button' => [], 'No type', '/button'];
- $this->assertHtml($expected, $result);
- $result = $this->Form->button('Upload Text', [
- 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
- 'escape' => false
- ]);
- $this->assertNotRegExp('/\&039/', $result);
- }
- /**
- * testButtonUnlockedByDefault method
- *
- * Test that button() makes unlocked fields by default.
- *
- * @return void
- */
- public function testButtonUnlockedByDefault()
- {
- $this->Form->request->params['_csrfToken'] = 'secured';
- $this->Form->button('Save', ['name' => 'save']);
- $this->Form->button('Clear');
- $result = $this->Form->unlockField();
- $this->assertEquals(['save'], $result);
- }
- /**
- * Test generation of a form button with confirm message.
- *
- * @return void
- */
- public function testButtonWithConfirm()
- {
- $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
- $expected = ['button' => ['type' => 'submit', 'onclick' => 'if (confirm("Confirm me!")) { return true; } return false;'], 'Hi', '/button'];
- $this->assertHtml($expected, $result);
- }
- /**
- * testPostButton method
- *
- * @return void
- */
- public function testPostButton()
- {
- $result = $this->Form->postButton('Hi', '/controller/action');
- $expected = [
- 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
- 'div' => ['style' => 'display:none;'],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/div',
- 'button' => ['type' => 'submit'],
- 'Hi',
- '/button',
- '/form'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
- $this->assertNotFalse(strpos($result, '<input type="hidden" name="extra" value="value"'));
- }
- /**
- * testPostButtonMethodType method
- *
- * @return void
- */
- public function testPostButtonMethodType()
- {
- $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
- $expected = [
- 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
- 'div' => ['style' => 'display:none;'],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
- '/div',
- 'button' => ['type' => 'submit'],
- 'Hi',
- '/button',
- '/form'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testPostButtonFormOptions method
- *
- * @return void
- */
- public function testPostButtonFormOptions()
- {
- $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
- $expected = [
- 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
- 'div' => ['style' => 'display:none;'],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/div',
- 'button' => ['type' => 'submit'],
- 'Hi',
- '/button',
- '/form'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testPostButtonNestedData method
- *
- * Test using postButton with N dimensional data.
- *
- * @return void
- */
- public function testPostButtonNestedData()
- {
- $data = [
- 'one' => [
- 'two' => [
- 3, 4, 5
- ]
- ]
- ];
- $result = $this->Form->postButton('Send', '/', ['data' => $data]);
- $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
- $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
- $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
- }
- /**
- * testSecurePostButton method
- *
- * Test that postButton adds _Token fields.
- *
- * @return void
- */
- public function testSecurePostButton()
- {
- $this->Form->request->params['_csrfToken'] = 'testkey';
- $this->Form->request->params['_Token'] = ['unlockedFields' => []];
- $result = $this->Form->postButton('Delete', '/posts/delete/1');
- $tokenDebug = urlencode(json_encode([
- '/posts/delete/1',
- [],
- []
- ]));
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
- ],
- ['div' => ['style' => 'display:none;']],
- ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
- ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
- '/div',
- 'button' => ['type' => 'submit'],
- 'Delete',
- '/button',
- ['div' => ['style' => 'display:none;']],
- ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
- ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[debug]',
- 'value' => $tokenDebug,
- 'autocomplete' => 'off',
- ]],
- '/div',
- '/form',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testPostLink method
- *
- * @return void
- */
- public function testPostLink()
- {
- $result = $this->Form->postLink('Delete', '/posts/delete/1');
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/posts/delete/1',
- 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
- ],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/form',
- 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
- 'Delete',
- '/a'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/posts/delete/1',
- 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
- ],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
- '/form',
- 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
- 'Delete',
- '/a'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->postLink(
- 'Delete',
- '/posts/delete/1',
- ['target' => '_blank', 'class' => 'btn btn-danger']
- );
- $expected = [
- 'form' => [
- 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
- 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
- ],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/form',
- 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
- 'Delete',
- '/a'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testPostLinkWithConfirm method
- *
- * Test the confirm option for postLink().
- *
- * @return void
- */
- public function testPostLinkWithConfirm()
- {
- $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/posts/delete/1',
- 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
- ],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/form',
- 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\("Confirm\?"\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
- 'Delete',
- '/a'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->postLink(
- 'Delete',
- '/posts/delete/1',
- ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
- );
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/posts/delete/1',
- 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
- ],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/form',
- 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\("'Confirm'\\\\nthis \\\"deletion\\\"\?"\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
- 'Delete',
- '/a'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testPostLinkWithQuery method
- *
- * Test postLink() with query string args.
- *
- * @return void
- */
- public function testPostLinkWithQuery()
- {
- $result = $this->Form->postLink(
- 'Delete',
- ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
- );
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/posts/delete/1?a=b&c=d',
- 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
- ],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/form',
- 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
- 'Delete',
- '/a'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testPostLinkWithData method
- *
- * Test postLink with additional data.
- *
- * @return void
- */
- public function testPostLinkWithData()
- {
- $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
- $this->assertContains('<input type="hidden" name="id" value="1"', $result);
- $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
- $this->Form->create($entity);
- $this->Form->end();
- $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
- $this->assertContains(
- '<input type="hidden" name="name" value="show"',
- $result,
- 'should not contain entity data.'
- );
- }
- /**
- * testPostLinkSecurityHash method
- *
- * Test that security hashes for postLink include the url.
- *
- * @return void
- */
- public function testPostLinkSecurityHash()
- {
- $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
- $hash .= '%3Aid';
- $this->Form->request->params['_Token']['key'] = 'test';
- $result = $this->Form->postLink(
- 'Delete',
- '/posts/delete/1',
- ['data' => ['id' => 1]]
- );
- $tokenDebug = urlencode(json_encode([
- '/posts/delete/1',
- [
- 'id' => 1
- ],
- []
- ]));
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/posts/delete/1',
- 'name', 'style' => 'display:none;'
- ],
- ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
- ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
- 'div' => ['style' => 'display:none;'],
- ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
- ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[debug]',
- 'value' => $tokenDebug,
- 'autocomplete' => 'off',
- ]],
- '/div',
- '/form',
- 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
- 'Delete',
- '/a'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testPostLinkSecurityHashBlockMode method
- *
- * Test that postLink doesn't modify the fields in the containing form.
- *
- * postLink() calls inside open forms should not modify the field list
- * for the form.
- *
- * @return void
- */
- public function testPostLinkSecurityHashBlockMode()
- {
- $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
- $hash .= '%3A';
- $this->Form->request->params['_Token']['key'] = 'test';
- $this->Form->create('Post', ['url' => ['action' => 'add']]);
- $this->Form->control('title');
- $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
- $result = $this->View->fetch('postLink');
- $this->assertEquals(['title'], $this->Form->fields);
- $this->assertContains($hash, $result, 'Should contain the correct hash.');
- $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
- }
- /**
- * testPostLinkSecurityHashNoDebugMode method
- *
- * Test that security does not include debug token if debug is false.
- *
- * @return void
- */
- public function testPostLinkSecurityHashNoDebugMode()
- {
- Configure::write('debug', false);
- $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
- $hash .= '%3Aid';
- $this->Form->request->params['_Token']['key'] = 'test';
- $result = $this->Form->postLink(
- 'Delete',
- '/posts/delete/1',
- ['data' => ['id' => 1]]
- );
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/posts/delete/1',
- 'name', 'style' => 'display:none;'
- ],
- ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
- ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
- 'div' => ['style' => 'display:none;'],
- ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
- ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
- '/div',
- '/form',
- 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
- 'Delete',
- '/a'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testPostLinkNestedData method
- *
- * Test using postLink with N dimensional data.
- *
- * @return void
- */
- public function testPostLinkNestedData()
- {
- $data = [
- 'one' => [
- 'two' => [
- 3, 4, 5
- ]
- ]
- ];
- $result = $this->Form->postLink('Send', '/', ['data' => $data]);
- $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
- $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
- $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
- }
- /**
- * testPostLinkAfterGetForm method
- *
- * Test creating postLinks after a GET form.
- *
- * @return void
- */
- public function testPostLinkAfterGetForm()
- {
- $this->Form->request->params['_csrfToken'] = 'testkey';
- $this->Form->request->params['_Token'] = 'val';
- $this->Form->create($this->article, ['type' => 'get']);
- $this->Form->end();
- $result = $this->Form->postLink('Delete', '/posts/delete/1');
- $tokenDebug = urlencode(json_encode([
- '/posts/delete/1',
- [],
- []
- ]));
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/posts/delete/1',
- 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
- ],
- ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
- ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
- 'div' => ['style' => 'display:none;'],
- ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
- ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
- ['input' => [
- 'type' => 'hidden', 'name' => '_Token[debug]',
- 'value' => $tokenDebug,
- 'autocomplete' => 'off'
- ]],
- '/div',
- '/form',
- 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
- 'Delete',
- '/a'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testPostLinkFormBuffer method
- *
- * Test that postLink adds form tags to view block.
- *
- * @return void
- */
- public function testPostLinkFormBuffer()
- {
- $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
- $expected = [
- 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
- 'Delete',
- '/a'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->View->fetch('postLink');
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/posts/delete/1',
- 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
- ],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/form'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->postLink(
- 'Delete',
- '/posts/delete/2',
- ['block' => true, 'method' => 'DELETE']
- );
- $expected = [
- 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
- 'Delete',
- '/a'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->View->fetch('postLink');
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/posts/delete/1',
- 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
- ],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/form',
- [
- 'form' => [
- 'method' => 'post', 'action' => '/posts/delete/2',
- 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
- ],
- ],
- ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
- '/form'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
- $expected = [
- 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
- 'Delete',
- '/a'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->View->fetch('foobar');
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/posts/delete/1',
- 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
- ],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/form'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testSubmitButton method
- *
- * @return void
- */
- public function testSubmitButton()
- {
- $result = $this->Form->submit('');
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'submit', 'value' => ''],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->submit('Test Submit');
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->submit('Next >');
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'submit', 'value' => 'Next >'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->submit('Next >', ['escape' => false]);
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'submit', 'value' => 'Next >'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->submit('Reset!', ['type' => 'reset']);
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'reset', 'value' => 'Reset!'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testSubmitImage method
- *
- * Test image submit types.
- *
- * @return void
- */
- public function testSubmitImage()
- {
- $result = $this->Form->submit('http://example.com/cake.power.gif');
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->submit('/relative/cake.power.gif');
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->submit('cake.power.gif');
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->submit('Not.an.image');
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testSubmitUnlockedByDefault method
- *
- * Submit buttons should be unlocked by default as there could be multiples, and only one will
- * be submitted at a time.
- *
- * @return void
- */
- public function testSubmitUnlockedByDefault()
- {
- $this->Form->request->params['_Token'] = 'secured';
- $this->Form->submit('Go go');
- $this->Form->submit('Save', ['name' => 'save']);
- $result = $this->Form->unlockField();
- $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
- }
- /**
- * testSubmitImageTimestamp method
- *
- * Test submit image with timestamps.
- *
- * @return void
- */
- public function testSubmitImageTimestamp()
- {
- Configure::write('Asset.timestamp', 'force');
- $result = $this->Form->submit('cake.power.gif');
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testDateTimeWithGetForms method
- *
- * Test that datetime() works with GET style forms.
- *
- * @return void
- */
- public function testDateTimeWithGetForms()
- {
- extract($this->dateRegex);
- $this->Form->create($this->article, ['type' => 'get']);
- $result = $this->Form->datetime('created');
- $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
- $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
- $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
- $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
- $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
- }
- /**
- * testForMagicControlNonExistingNorValidated method
- *
- * @return void
- */
- public function testForMagicControlNonExistingNorValidated()
- {
- $this->Form->create($this->article);
- $this->Form->setTemplates(['inputContainer' => '{{content}}']);
- $result = $this->Form->control('non_existing_nor_validated');
- $expected = [
- 'label' => ['for' => 'non-existing-nor-validated'],
- 'Non Existing Nor Validated',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => 'non_existing_nor_validated',
- 'id' => 'non-existing-nor-validated'
- ]
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('non_existing_nor_validated', [
- 'val' => 'my value'
- ]);
- $expected = [
- 'label' => ['for' => 'non-existing-nor-validated'],
- 'Non Existing Nor Validated',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => 'non_existing_nor_validated',
- 'value' => 'my value', 'id' => 'non-existing-nor-validated'
- ]
- ];
- $this->assertHtml($expected, $result);
- $this->Form->request->data = ['non_existing_nor_validated' => 'CakePHP magic'];
- $result = $this->Form->control('non_existing_nor_validated');
- $expected = [
- 'label' => ['for' => 'non-existing-nor-validated'],
- 'Non Existing Nor Validated',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => 'non_existing_nor_validated',
- 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
- ]
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormMagicControlLabel method
- *
- * @return void
- */
- public function testFormMagicControlLabel()
- {
- TableRegistry::get('Contacts', [
- 'className' => __NAMESPACE__ . '\ContactsTable'
- ]);
- $this->Form->create([], ['context' => ['table' => 'Contacts']]);
- $this->Form->setTemplates(['inputContainer' => '{{content}}']);
- $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
- $expected = [
- 'label' => ['for' => 'contacts-name'],
- 'My label',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => 'Contacts[name]',
- 'id' => 'contacts-name', 'maxlength' => '255'
- ]
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('name', [
- 'label' => ['class' => 'mandatory']
- ]);
- $expected = [
- 'label' => ['for' => 'name', 'class' => 'mandatory'],
- 'Name',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => 'name',
- 'id' => 'name', 'maxlength' => '255'
- ]
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('name', [
- 'div' => false,
- 'label' => ['class' => 'mandatory', 'text' => 'My label']
- ]);
- $expected = [
- 'label' => ['for' => 'name', 'class' => 'mandatory'],
- 'My label',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => 'name',
- 'id' => 'name', 'maxlength' => '255'
- ]
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Contact.name', [
- 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
- ]);
- $expected = [
- 'label' => ['for' => 'my_id'],
- 'Name',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => 'Contact[name]',
- 'id' => 'my_id', 'maxlength' => '255'
- ]
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('1.id');
- $expected = ['input' => [
- 'type' => 'hidden', 'name' => '1[id]',
- 'id' => '1-id'
- ]];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('1.name');
- $expected = [
- 'label' => ['for' => '1-name'],
- 'Name',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => '1[name]',
- 'id' => '1-name', 'maxlength' => '255'
- ]
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormEnd method
- *
- * @return void
- */
- public function testFormEnd()
- {
- $this->assertEquals('</form>', $this->Form->end());
- }
- /**
- * testMultiRecordForm method
- *
- * Test the generation of fields for a multi record form.
- *
- * @return void
- */
- public function testMultiRecordForm()
- {
- $this->loadFixtures('Articles', 'Comments');
- $articles = TableRegistry::get('Articles');
- $articles->hasMany('Comments');
- $comment = new Entity(['comment' => 'Value']);
- $article = new Article(['comments' => [$comment]]);
- $this->Form->create([$article]);
- $result = $this->Form->control('0.comments.1.comment');
- //@codingStandardsIgnoreStart
- $expected = [
- 'div' => ['class' => 'input textarea'],
- 'label' => ['for' => '0-comments-1-comment'],
- 'Comment',
- '/label',
- 'textarea' => [
- 'name',
- 'id' => '0-comments-1-comment',
- 'rows' => 5
- ],
- '/textarea',
- '/div'
- ];
- //@codingStandardsIgnoreEnd
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('0.comments.0.comment');
- //@codingStandardsIgnoreStart
- $expected = [
- 'div' => ['class' => 'input textarea'],
- 'label' => ['for' => '0-comments-0-comment'],
- 'Comment',
- '/label',
- 'textarea' => [
- 'name',
- 'id' => '0-comments-0-comment',
- 'rows' => 5
- ],
- 'Value',
- '/textarea',
- '/div'
- ];
- //@codingStandardsIgnoreEnd
- $this->assertHtml($expected, $result);
- $comment->setError('comment', ['Not valid']);
- $result = $this->Form->control('0.comments.0.comment');
- //@codingStandardsIgnoreStart
- $expected = [
- 'div' => ['class' => 'input textarea error'],
- 'label' => ['for' => '0-comments-0-comment'],
- 'Comment',
- '/label',
- 'textarea' => [
- 'name',
- 'class' => 'form-error',
- 'id' => '0-comments-0-comment',
- 'rows' => 5
- ],
- 'Value',
- '/textarea',
- ['div' => ['class' => 'error-message']],
- 'Not valid',
- '/div',
- '/div'
- ];
- //@codingStandardsIgnoreEnd
- $this->assertHtml($expected, $result);
- TableRegistry::get('Comments')
- ->getValidator('default')
- ->allowEmpty('comment', false);
- $result = $this->Form->control('0.comments.1.comment');
- //@codingStandardsIgnoreStart
- $expected = [
- 'div' => ['class' => 'input textarea required'],
- 'label' => ['for' => '0-comments-1-comment'],
- 'Comment',
- '/label',
- 'textarea' => [
- 'name',
- 'required' => 'required',
- 'id' => '0-comments-1-comment',
- 'rows' => 5
- ],
- '/textarea',
- '/div'
- ];
- //@codingStandardsIgnoreEnd
- $this->assertHtml($expected, $result);
- }
- /**
- * testHtml5Controls method
- *
- * Test that some html5 inputs + FormHelper::__call() work.
- *
- * @return void
- */
- public function testHtml5Controls()
- {
- $result = $this->Form->email('User.email');
- $expected = [
- 'input' => ['type' => 'email', 'name' => 'User[email]']
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->search('User.query');
- $expected = [
- 'input' => ['type' => 'search', 'name' => 'User[query]']
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->search('User.query', ['value' => 'test']);
- $expected = [
- 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
- $expected = [
- 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testHtml5ControlWithControl method
- *
- * Test accessing html5 inputs through control().
- *
- * @return void
- */
- public function testHtml5ControlWithControl()
- {
- $this->Form->create();
- $this->Form->setTemplates(['inputContainer' => '{{content}}']);
- $result = $this->Form->control('website', [
- 'type' => 'url',
- 'val' => 'http://domain.tld',
- 'label' => false
- ]);
- $expected = [
- 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testHtml5ControlException method
- *
- * Test errors when field name is missing.
- *
- * @return void
- */
- public function testHtml5ControlException()
- {
- $this->expectException(\Cake\Core\Exception\Exception::class);
- $this->Form->email();
- }
- /**
- * testRequiredAttribute method
- *
- * Tests that formhelper sets required attributes.
- *
- * @return void
- */
- public function testRequiredAttribute()
- {
- $this->article['required'] = [
- 'title' => true,
- 'body' => false,
- ];
- $this->Form->create($this->article);
- $result = $this->Form->control('title');
- $expected = [
- 'div' => ['class' => 'input text required'],
- 'label' => ['for' => 'title'],
- 'Title',
- '/label',
- 'input' => [
- 'type' => 'text',
- 'name' => 'title',
- 'id' => 'title',
- 'required' => 'required',
- ],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('title', ['required' => false]);
- $this->assertNotContains('required', $result);
- $result = $this->Form->control('body');
- $expected = [
- 'div' => ['class' => 'input text'],
- 'label' => ['for' => 'body'],
- 'Body',
- '/label',
- 'input' => [
- 'type' => 'text',
- 'name' => 'body',
- 'id' => 'body',
- ],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('body', ['required' => true]);
- $this->assertContains('required', $result);
- }
- /**
- * testControlsNotNested method
- *
- * Tests that it is possible to put inputs outside of the label.
- *
- * @return void
- */
- public function testControlsNotNested()
- {
- $this->Form->setTemplates([
- 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
- 'formGroup' => '{{input}}{{label}}',
- ]);
- $result = $this->Form->control('foo', ['type' => 'checkbox']);
- $expected = [
- 'div' => ['class' => 'input checkbox'],
- ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
- ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
- 'label' => ['for' => 'foo'],
- 'Foo',
- '/label',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
- $expected = [
- 'div' => ['class' => 'input checkbox'],
- ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
- ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('confirm', [
- 'type' => 'radio',
- 'options' => ['Y' => 'Yes', 'N' => 'No']
- ]);
- $expected = [
- 'div' => ['class' => 'input radio'],
- ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
- ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
- ['label' => ['for' => 'confirm-y']],
- 'Yes',
- '/label',
- ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
- ['label' => ['for' => 'confirm-n']],
- 'No',
- '/label',
- '<label',
- 'Confirm',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->select('category', ['1', '2'], [
- 'multiple' => 'checkbox',
- 'name' => 'fish',
- ]);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
- ['div' => ['class' => 'checkbox']],
- ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
- ['label' => ['for' => 'fish-0']],
- '1',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
- ['label' => ['for' => 'fish-1']],
- '2',
- '/label',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlContainerTemplates method
- *
- * Test that *Container templates are used by input.
- *
- * @return void
- */
- public function testControlContainerTemplates()
- {
- $this->Form->setTemplates([
- 'checkboxContainer' => '<div class="check">{{content}}</div>',
- 'radioContainer' => '<div class="rad">{{content}}</div>',
- 'radioContainerError' => '<div class="rad err">{{content}}</div>',
- 'datetimeContainer' => '<div class="dt">{{content}}</div>',
- ]);
- $this->article['errors'] = [
- 'Article' => ['published' => 'error message']
- ];
- $this->Form->create($this->article);
- $result = $this->Form->control('accept', [
- 'type' => 'checkbox'
- ]);
- $expected = [
- 'div' => ['class' => 'check'],
- ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
- 'label' => ['for' => 'accept'],
- ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
- 'Accept',
- '/label',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('accept', [
- 'type' => 'radio',
- 'options' => ['Y', 'N']
- ]);
- $this->assertContains('<div class="rad">', $result);
- $result = $this->Form->control('Article.published', [
- 'type' => 'radio',
- 'options' => ['Y', 'N']
- ]);
- $this->assertContains('<div class="rad err">', $result);
- $result = $this->Form->control('Article.created', [
- 'type' => 'datetime',
- ]);
- $this->assertContains('<div class="dt">', $result);
- }
- /**
- * testFormGroupTemplates method
- *
- * Test that *Container templates are used by input.
- *
- * @return void
- */
- public function testFormGroupTemplates()
- {
- $this->Form->setTemplates([
- 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
- ]);
- $this->Form->create($this->article);
- $result = $this->Form->control('accept', [
- 'type' => 'radio',
- 'options' => ['Y', 'N']
- ]);
- $this->assertContains('<div class="radio">', $result);
- }
- /**
- * testResetTemplates method
- *
- * Test resetting templates.
- *
- * @return void
- */
- public function testResetTemplates()
- {
- $this->Form->setTemplates(['input' => '<input/>']);
- $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
- $this->Form->resetTemplates();
- $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
- }
- /**
- * testContext method
- *
- * Test the context method.
- *
- * @return void
- */
- public function testContext()
- {
- $result = $this->Form->context();
- $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
- $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
- $this->assertSame($mock, $this->Form->context($mock));
- $this->assertSame($mock, $this->Form->context());
- }
- /**
- * testAutoDomId method
- *
- * @return void
- */
- public function testAutoDomId()
- {
- $result = $this->Form->text('field', ['id' => true]);
- $expected = [
- 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
- ];
- $this->assertHtml($expected, $result);
- // Ensure id => doesn't cause problem when multiple inputs are generated.
- $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
- ['label' => ['for' => 'field-0']],
- ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
- 'option A',
- '/label',
- ['label' => ['for' => 'field-1']],
- ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
- 'option B',
- '/label',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->select(
- 'multi_field',
- ['first', 'second'],
- ['multiple' => 'checkbox', 'id' => true]
- );
- $expected = [
- 'input' => [
- 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
- ],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'multi-field-0']],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'multi_field[]',
- 'value' => '0', 'id' => 'multi-field-0'
- ]],
- 'first',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'multi-field-1']],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'multi_field[]',
- 'value' => '1', 'id' => 'multi-field-1'
- ]],
- 'second',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test the basic setters and getters for value sources
- *
- * @return void
- */
- public function testFormValueSourcesSettersGetters()
- {
- $expected = ['context'];
- $result = $this->Form->getValueSources();
- $this->assertEquals($expected, $result);
- $expected = null;
- $result = $this->Form->getSourceValue('id');
- $this->assertEquals($expected, $result);
- $expected = ['query', 'data', 'context'];
- $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
- $result = $this->Form->getValueSources();
- $this->assertEquals($expected, $result);
- $this->Form->request->data['id'] = '1';
- $this->Form->request->query['id'] = '2';
- $this->Form->setValueSources(['context']);
- $expected = '1';
- $result = $this->Form->getSourceValue('id');
- $this->assertEquals($expected, $result);
- $this->Form->setValueSources('query');
- $expected = ['query'];
- $result = $this->Form->getValueSources();
- $this->assertEquals($expected, $result);
- $expected = '2';
- $result = $this->Form->getSourceValue('id');
- $this->assertEquals($expected, $result);
- $this->Form->setValueSources(['data']);
- $expected = '1';
- $result = $this->Form->getSourceValue('id');
- $this->assertEquals($expected, $result);
- $this->Form->setValueSources(['query', 'data']);
- $expected = '2';
- $result = $this->Form->getSourceValue('id');
- $this->assertEquals($expected, $result);
- }
- /**
- * Tests the different input rendering values based on sources values switching
- *
- * @return void
- */
- public function testFormValueSourcesSingleSwitchRendering()
- {
- $this->loadFixtures('Articles');
- $articles = TableRegistry::get('Articles');
- $article = new Article();
- $articles->patchEntity($article, ['id' => '3']);
- $this->Form->create($article);
- $this->Form->setValueSources(['context']);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
- ];
- $this->assertHtml($expected, $result);
- $this->Form->request->query['id'] = '5';
- $this->Form->setValueSources(['query']);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
- ];
- $this->assertHtml($expected, $result);
- $this->Form->request->query['id'] = '5a';
- $this->Form->request->data['id'] = '5b';
- $this->Form->setValueSources(['context']);
- $this->Form->create($article);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
- ];
- $this->assertHtml($expected, $result);
- $this->Form->setValueSources(['data']);
- $this->Form->create($article);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
- ];
- $this->assertHtml($expected, $result);
- $this->Form->setValueSources(['query']);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Tests the different input rendering values based on sources values switching while supplying
- * an entity (base context) and multiple sources (such as data, query)
- *
- * @return void
- */
- public function testFormValueSourcesListSwitchRendering()
- {
- $this->loadFixtures('Articles');
- $articles = TableRegistry::get('Articles');
- $article = new Article();
- $articles->patchEntity($article, ['id' => '3']);
- $this->Form->request = $this->Form->request->withQueryParams(['id' => '9']);
- $this->Form->create($article);
- $this->Form->setValueSources(['context', 'query']);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
- ];
- $this->assertHtml($expected, $result);
- $this->Form->setValueSources(['query', 'context']);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
- ];
- $this->assertHtml($expected, $result);
- $this->Form->setValueSources(['data', 'query', 'context']);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
- ];
- $this->assertHtml($expected, $result);
- $this->Form->request = $this->Form->request
- ->withData('id', '8')
- ->withQueryParams(['id' => '9']);
- $this->Form->setValueSources(['data', 'query', 'context']);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test the different form input renderings based on values sources switchings through form options
- *
- * @return void
- */
- public function testFormValueSourcesSwitchViaOptionsRendering()
- {
- $this->loadFixtures('Articles');
- $articles = TableRegistry::get('Articles');
- $article = new Article();
- $articles->patchEntity($article, ['id' => '3']);
- $this->Form->request->data['id'] = '4';
- $this->Form->request->query['id'] = '5';
- $this->Form->create($article, ['valueSources' => 'query']);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->getSourceValue('id');
- $this->assertEquals('5', $result);
- $this->Form->setValueSources(['context']);
- $this->Form->create($article, ['valueSources' => 'query']);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->getSourceValue('id');
- $this->assertEquals('5', $result);
- $this->Form->setValueSources(['query']);
- $this->Form->create($article, ['valueSources' => 'data']);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->getSourceValue('id');
- $this->assertEquals('4', $result);
- $this->Form->setValueSources(['query']);
- $this->Form->create($article, ['valueSources' => ['context', 'data']]);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->getSourceValue('id');
- $this->assertEquals('4', $result);
- }
- /**
- * Test the different form input renderings based on values sources switchings through form options
- *
- * @return void
- */
- public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
- {
- $this->loadFixtures('Articles');
- $articles = TableRegistry::get('Articles');
- $article = new Article();
- $articles->patchEntity($article, ['id' => '3']);
- $this->Form->request->data['id'] = '10';
- $this->Form->request->query['id'] = '11';
- $this->Form->setValueSources(['context'])
- ->create($article, ['valueSources' => ['query', 'data']]);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->getSourceValue('id');
- $this->assertEquals('11', $result);
- unset($this->Form->request->query['id']);
- $this->Form->setValueSources(['context'])
- ->create($article, ['valueSources' => ['query', 'data']]);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->getSourceValue('id');
- $this->assertEquals('10', $result);
- }
- /**
- * Test the different form values sources resetting through From::end();
- *
- * @return void
- */
- public function testFormValueSourcesResetViaEnd()
- {
- $expected = ['context'];
- $result = $this->Form->getValueSources();
- $this->assertEquals($expected, $result);
- $expected = ['query', 'context', 'data'];
- $this->Form->setValueSources(['query', 'context', 'data']);
- $result = $this->Form->getValueSources();
- $this->assertEquals($expected, $result);
- $this->Form->create();
- $result = $this->Form->getValueSources();
- $this->assertEquals($expected, $result);
- $this->Form->end();
- $result = $this->Form->getValueSources();
- $this->assertEquals(['context'], $result);
- }
- /**
- * Test sources values defaults handling
- *
- * @return void
- */
- public function testFormValueSourcesDefaults()
- {
- $this->Form->request->query['password'] = 'open Sesame';
- $this->Form->create();
- $result = $this->Form->password('password');
- $expected = ['input' => ['type' => 'password', 'name' => 'password']];
- $this->assertHtml($expected, $result);
- $result = $this->Form->password('password', ['default' => 'helloworld']);
- $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
- $this->assertHtml($expected, $result);
- $this->Form->setValueSources('query');
- $result = $this->Form->password('password', ['default' => 'helloworld']);
- $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
- $this->assertHtml($expected, $result);
- $this->Form->setValueSources('data');
- $result = $this->Form->password('password', ['default' => 'helloworld']);
- $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test sources values schema defaults handling
- *
- * @return void
- */
- public function testSourcesValueDoesntExistPassThrough()
- {
- $this->Form->request = $this->Form->request->withQueryParams(['category' => 'sesame-cookies']);
- $articles = TableRegistry::get('Articles');
- $entity = $articles->newEntity();
- $this->Form->create($entity);
- $this->Form->setValueSources(['query', 'context']);
- $result = $this->Form->getSourceValue('category');
- $this->assertEquals('sesame-cookies', $result);
- $this->Form->setValueSources(['context', 'query']);
- $result = $this->Form->getSourceValue('category');
- $this->assertEquals('sesame-cookies', $result);
- }
- /**
- * testNestedLabelInput method
- *
- * Test the `nestedInput` parameter
- *
- * @group deprecated
- * @return void
- */
- public function testNestedLabelInput()
- {
- $this->deprecated(function () {
- $result = $this->Form->input('foo', ['nestedInput' => true]);
- $expected = [
- 'div' => ['class' => 'input text'],
- 'label' => ['for' => 'foo'],
- ['input' => [
- 'type' => 'text',
- 'name' => 'foo',
- 'id' => 'foo'
- ]],
- 'Foo',
- '/label',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- });
- }
- /**
- * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
- *
- * This test makes sure `false` excludes the label from the render
- *
- * @return void
- */
- public function testControlLabelManipulationDisableLabels()
- {
- $result = $this->Form->control('test', [
- 'type' => 'radio',
- 'options' => ['A', 'B'],
- 'labelOptions' => false
- ]);
- $expected = [
- ['div' => ['class' => 'input radio']],
- '<label',
- 'Test',
- '/label',
- ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
- ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
- ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('checkbox1', [
- 'label' => 'My checkboxes',
- 'multiple' => 'checkbox',
- 'type' => 'select',
- 'options' => [
- ['text' => 'First Checkbox', 'value' => 1],
- ['text' => 'Second Checkbox', 'value' => 2]
- ],
- 'labelOptions' => false
- ]);
- $expected = [
- ['div' => ['class' => 'input select']],
- ['label' => ['for' => 'checkbox1']],
- 'My checkboxes',
- '/label',
- 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
- ['div' => ['class' => 'checkbox']],
- ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
- '/div',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
- *
- * This test checks rendering of class (as string and array) also makes sure 'selected' is
- * added to the class if checked.
- *
- * Also checks to make sure any custom attributes are rendered correctly
- *
- * @return void
- */
- public function testControlLabelManipulationRadios()
- {
- $result = $this->Form->control('test', [
- 'type' => 'radio',
- 'options' => ['A', 'B'],
- 'labelOptions' => ['class' => 'custom-class']
- ]);
- $expected = [
- ['div' => ['class' => 'input radio']],
- '<label',
- 'Test',
- '/label',
- ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
- ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
- ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
- 'A',
- '/label',
- ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
- ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
- 'B',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('test', [
- 'type' => 'radio',
- 'options' => ['A', 'B'],
- 'value' => 1,
- 'labelOptions' => ['class' => 'custom-class']
- ]);
- $expected = [
- ['div' => ['class' => 'input radio']],
- '<label',
- 'Test',
- '/label',
- ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
- ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
- ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
- 'A',
- '/label',
- ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
- ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
- 'B',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('test', [
- 'type' => 'radio',
- 'options' => ['A', 'B'],
- 'value' => 1,
- 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']]
- ]);
- $expected = [
- ['div' => ['class' => 'input radio']],
- '<label',
- 'Test',
- '/label',
- ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
- ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
- ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
- 'A',
- '/label',
- ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
- ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
- 'B',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->radio('test', ['A', 'B'], [
- 'label' => [
- 'class' => ['custom-class', 'another-class'],
- 'data-name' => 'bob'
- ],
- 'value' => 1
- ]);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
- ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
- ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
- 'A',
- '/label',
- ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
- ['input' => [
- 'type' => 'radio',
- 'name' => 'test',
- 'value' => '1',
- 'id' => 'test-1',
- 'checked' => 'checked'
- ]],
- 'B',
- '/label',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
- *
- * This test checks rendering of class (as string and array) also makes sure 'selected' is
- * added to the class if checked.
- *
- * Also checks to make sure any custom attributes are rendered correctly
- *
- * @return void
- */
- public function testControlLabelManipulationCheckboxes()
- {
- $result = $this->Form->control('checkbox1', [
- 'label' => 'My checkboxes',
- 'multiple' => 'checkbox',
- 'type' => 'select',
- 'options' => [
- ['text' => 'First Checkbox', 'value' => 1],
- ['text' => 'Second Checkbox', 'value' => 2]
- ],
- 'labelOptions' => ['class' => 'custom-class'],
- 'value' => ['1']
- ]);
- $expected = [
- ['div' => ['class' => 'input select']],
- ['label' => ['for' => 'checkbox1']],
- 'My checkboxes',
- '/label',
- 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
- ['div' => ['class' => 'checkbox']],
- ['label' => [
- 'class' => 'custom-class selected',
- 'for' => 'checkbox1-1'
- ]],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'checkbox1[]',
- 'value' => '1',
- 'id' => 'checkbox1-1',
- 'checked' => 'checked'
- ]],
- 'First Checkbox',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => [
- 'class' => 'custom-class',
- 'for' => 'checkbox1-2'
- ]],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'checkbox1[]',
- 'value' => '2',
- 'id' => 'checkbox1-2'
- ]],
- 'Second Checkbox',
- '/label',
- '/div',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('checkbox1', [
- 'label' => 'My checkboxes',
- 'multiple' => 'checkbox',
- 'type' => 'select',
- 'options' => [
- ['text' => 'First Checkbox', 'value' => 1],
- ['text' => 'Second Checkbox', 'value' => 2]
- ],
- 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
- 'value' => ['1']
- ]);
- $expected = [
- ['div' => ['class' => 'input select']],
- ['label' => ['for' => 'checkbox1']],
- 'My checkboxes',
- '/label',
- 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
- ['div' => ['class' => 'checkbox']],
- ['label' => [
- 'class' => 'custom-class another-class selected',
- 'data-name' => 'bob',
- 'for' => 'checkbox1-1'
- ]],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'checkbox1[]',
- 'value' => '1',
- 'id' => 'checkbox1-1',
- 'checked' => 'checked'
- ]],
- 'First Checkbox',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => [
- 'class' => 'custom-class another-class',
- 'data-name' => 'bob',
- 'for' => 'checkbox1-2'
- ]],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'checkbox1[]',
- 'value' => '2',
- 'id' => 'checkbox1-2'
- ]],
- 'Second Checkbox',
- '/label',
- '/div',
- '/div'
- ];
- $this->assertHtml($expected, $result);
- }
- }
|