FormHelperTest.php 178 KB

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