FormHelperTest.php 174 KB

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