FormHelperTest.php 171 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939
  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 error message display
  1439. *
  1440. * @return void
  1441. */
  1442. public function testErrorMessageDisplay() {
  1443. $this->article['errors'] = [
  1444. 'Article' => ['title' => 'error message']
  1445. ];
  1446. $this->Form->create($this->article);
  1447. $result = $this->Form->input('Article.title');
  1448. $expected = [
  1449. 'div' => ['class' => 'input text error'],
  1450. 'label' => ['for' => 'article-title'],
  1451. 'Title',
  1452. '/label',
  1453. 'input' => [
  1454. 'type' => 'text', 'name' => 'Article[title]',
  1455. 'id' => 'article-title', 'class' => 'form-error'
  1456. ],
  1457. ['div' => ['class' => 'error-message']],
  1458. 'error message',
  1459. '/div',
  1460. '/div'
  1461. ];
  1462. $this->assertTags($result, $expected);
  1463. $result = $this->Form->input('Article.title', [
  1464. 'templates' => [
  1465. 'groupContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  1466. ]
  1467. ]);
  1468. $expected = [
  1469. 'div' => ['class' => 'input text error'],
  1470. 'label' => ['for' => 'article-title'],
  1471. 'Title',
  1472. '/label',
  1473. 'input' => [
  1474. 'type' => 'text', 'name' => 'Article[title]',
  1475. 'id' => 'article-title', 'class' => 'form-error'
  1476. ],
  1477. '/div'
  1478. ];
  1479. $this->assertTags($result, $expected);
  1480. }
  1481. /**
  1482. * Test validation errors, when validation message is an empty string.
  1483. *
  1484. * @return void
  1485. */
  1486. public function testEmptyErrorValidation() {
  1487. $this->article['errors'] = [
  1488. 'Article' => ['title' => '']
  1489. ];
  1490. $this->Form->create($this->article);
  1491. $result = $this->Form->input('Article.title');
  1492. $expected = [
  1493. 'div' => ['class' => 'input text error'],
  1494. 'label' => ['for' => 'article-title'],
  1495. 'Title',
  1496. '/label',
  1497. 'input' => [
  1498. 'type' => 'text', 'name' => 'Article[title]',
  1499. 'id' => 'article-title', 'class' => 'form-error'
  1500. ],
  1501. ['div' => ['class' => 'error-message']],
  1502. [],
  1503. '/div',
  1504. '/div'
  1505. ];
  1506. $this->assertTags($result, $expected);
  1507. }
  1508. /**
  1509. * Test validation errors, when calling input() overriding validation message by an empty string.
  1510. *
  1511. * @return void
  1512. */
  1513. public function testEmptyInputErrorValidation() {
  1514. $this->article['errors'] = [
  1515. 'Article' => ['title' => 'error message']
  1516. ];
  1517. $this->Form->create($this->article);
  1518. $result = $this->Form->input('Article.title', array('error' => ''));
  1519. $expected = [
  1520. 'div' => ['class' => 'input text error'],
  1521. 'label' => ['for' => 'article-title'],
  1522. 'Title',
  1523. '/label',
  1524. 'input' => [
  1525. 'type' => 'text', 'name' => 'Article[title]',
  1526. 'id' => 'article-title', 'class' => 'form-error'
  1527. ],
  1528. ['div' => ['class' => 'error-message']],
  1529. [],
  1530. '/div',
  1531. '/div'
  1532. ];
  1533. $this->assertTags($result, $expected);
  1534. }
  1535. /**
  1536. * Tests displaying errors for nested entities
  1537. *
  1538. * @return void
  1539. */
  1540. public function testFormValidationAssociated() {
  1541. $nested = new Entity(['foo' => 'bar']);
  1542. $nested->errors('foo', ['not a valid bar']);
  1543. $entity = new Entity(['nested' => $nested]);
  1544. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  1545. $result = $this->Form->error('nested.foo');
  1546. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  1547. }
  1548. /**
  1549. * testFormValidationAssociatedSecondLevel method
  1550. *
  1551. * test form error display with associated model.
  1552. *
  1553. * @return void
  1554. */
  1555. public function testFormValidationAssociatedSecondLevel() {
  1556. $inner = new Entity(['bar' => 'baz']);
  1557. $nested = new Entity(['foo' => $inner]);
  1558. $entity = new Entity(['nested' => $nested]);
  1559. $inner->errors('bar', ['not a valid one']);
  1560. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  1561. $result = $this->Form->error('nested.foo.bar');
  1562. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  1563. }
  1564. /**
  1565. * testFormValidationMultiRecord method
  1566. *
  1567. * test form error display with multiple records.
  1568. *
  1569. * @return void
  1570. */
  1571. public function testFormValidationMultiRecord() {
  1572. $one = new Entity;
  1573. $two = new Entity;
  1574. TableRegistry::get('Contacts', [
  1575. 'className' => __NAMESPACE__ . '\ContactsTable'
  1576. ]);
  1577. $one->errors('email', ['invalid email']);
  1578. $two->errors('name', ['This is wrong']);
  1579. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  1580. $result = $this->Form->input('Contacts.0.email');
  1581. $expected = array(
  1582. 'div' => array('class' => 'input email error'),
  1583. 'label' => array('for' => 'contacts-0-email'),
  1584. 'Email',
  1585. '/label',
  1586. 'input' => array(
  1587. 'type' => 'text', 'name' => 'Contacts[0][email]', 'id' => 'contacts-0-email',
  1588. 'class' => 'form-error', 'maxlength' => 255
  1589. ),
  1590. array('div' => array('class' => 'error-message')),
  1591. 'invalid email',
  1592. '/div',
  1593. '/div'
  1594. );
  1595. $result = $this->Form->input('Contacts.1.name');
  1596. $expected = array(
  1597. 'div' => array('class' => 'input text error'),
  1598. 'label' => array('for' => 'contacts-1-name'),
  1599. 'Name',
  1600. '/label',
  1601. 'input' => array(
  1602. 'type' => 'text', 'name' => 'Contacts[1][name]', 'id' => 'contacts-1-name',
  1603. 'class' => 'form-error', 'maxlength' => 255
  1604. ),
  1605. array('div' => array('class' => 'error-message')),
  1606. 'This is wrong',
  1607. '/div',
  1608. '/div'
  1609. );
  1610. $this->assertTags($result, $expected);
  1611. }
  1612. /**
  1613. * testInput method
  1614. *
  1615. * Test various incarnations of input().
  1616. *
  1617. * @return void
  1618. */
  1619. public function testInput() {
  1620. TableRegistry::get('ValidateUsers', [
  1621. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  1622. ]);
  1623. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  1624. $result = $this->Form->input('ValidateUsers.balance');
  1625. $expected = array(
  1626. 'div' => array('class'),
  1627. 'label' => array('for'),
  1628. 'Balance',
  1629. '/label',
  1630. 'input' => array('name', 'type' => 'number', 'id', 'step'),
  1631. '/div',
  1632. );
  1633. $this->assertTags($result, $expected);
  1634. $result = $this->Form->input('ValidateUser.cost_decimal');
  1635. $expected = array(
  1636. 'div' => array('class'),
  1637. 'label' => array('for'),
  1638. 'Cost Decimal',
  1639. '/label',
  1640. 'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'),
  1641. '/div',
  1642. );
  1643. $this->assertTags($result, $expected);
  1644. }
  1645. /**
  1646. * Tests the input method and passing custom options
  1647. *
  1648. * @return void
  1649. */
  1650. public function testInputCustomization() {
  1651. TableRegistry::get('Contacts', [
  1652. 'className' => __NAMESPACE__ . '\ContactsTable'
  1653. ]);
  1654. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1655. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1656. $expected = array(
  1657. 'div' => array('class' => 'input email'),
  1658. 'label' => array('for' => 'custom'),
  1659. 'Email',
  1660. '/label',
  1661. array('input' => array(
  1662. 'type' => 'email', 'name' => 'Contact[email]',
  1663. 'id' => 'custom', 'maxlength' => 255
  1664. )),
  1665. '/div'
  1666. );
  1667. $this->assertTags($result, $expected);
  1668. $result = $this->Form->input('Contact.email', array(
  1669. 'templates' => ['groupContainer' => '<div>{{content}}</div>']
  1670. ));
  1671. $expected = array(
  1672. '<div',
  1673. 'label' => array('for' => 'contact-email'),
  1674. 'Email',
  1675. '/label',
  1676. array('input' => array(
  1677. 'type' => 'email', 'name' => 'Contact[email]',
  1678. 'id' => 'contact-email', 'maxlength' => 255
  1679. )),
  1680. '/div'
  1681. );
  1682. $this->assertTags($result, $expected);
  1683. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1684. $expected = array(
  1685. 'div' => array('class' => 'input text'),
  1686. 'label' => array('for' => 'contact-email'),
  1687. 'Email',
  1688. '/label',
  1689. array('input' => array(
  1690. 'type' => 'text', 'name' => 'Contact[email]',
  1691. 'id' => 'contact-email', 'maxlength' => '255'
  1692. )),
  1693. '/div'
  1694. );
  1695. $this->assertTags($result, $expected);
  1696. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1697. $expected = array(
  1698. 'div' => array('class' => 'input text'),
  1699. 'label' => array('for' => 'contact-5-email'),
  1700. 'Email',
  1701. '/label',
  1702. array('input' => array(
  1703. 'type' => 'text', 'name' => 'Contact[5][email]',
  1704. 'id' => 'contact-5-email', 'maxlength' => '255'
  1705. )),
  1706. '/div'
  1707. );
  1708. $this->assertTags($result, $expected);
  1709. $result = $this->Form->input('Contact.password');
  1710. $expected = array(
  1711. 'div' => array('class' => 'input password'),
  1712. 'label' => array('for' => 'contact-password'),
  1713. 'Password',
  1714. '/label',
  1715. array('input' => array(
  1716. 'type' => 'password', 'name' => 'Contact[password]',
  1717. 'id' => 'contact-password'
  1718. )),
  1719. '/div'
  1720. );
  1721. $this->assertTags($result, $expected);
  1722. $result = $this->Form->input('Contact.email', array(
  1723. 'type' => 'file', 'class' => 'textbox'
  1724. ));
  1725. $expected = array(
  1726. 'div' => array('class' => 'input file'),
  1727. 'label' => array('for' => 'contact-email'),
  1728. 'Email',
  1729. '/label',
  1730. array('input' => array(
  1731. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  1732. 'id' => 'contact-email'
  1733. )),
  1734. '/div'
  1735. );
  1736. $this->assertTags($result, $expected);
  1737. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  1738. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  1739. $result = $this->Form->input('Contact.phone');
  1740. $expected = array(
  1741. 'div' => array('class' => 'input tel'),
  1742. 'label' => array('for' => 'contact-phone'),
  1743. 'Phone',
  1744. '/label',
  1745. array('input' => array(
  1746. 'type' => 'tel', 'name' => 'Contact[phone]',
  1747. 'value' => 'Hello &amp; World &gt; weird chars',
  1748. 'id' => 'contact-phone', 'maxlength' => 255
  1749. )),
  1750. '/div'
  1751. );
  1752. $this->assertTags($result, $expected);
  1753. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1754. $this->Form->create();
  1755. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1756. $expected = array(
  1757. 'div' => array('class' => 'input text'),
  1758. 'label' => array('for' => 'myId'),
  1759. 'Field',
  1760. '/label',
  1761. 'input' => array(
  1762. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  1763. 'value' => 'My value', 'id' => 'myId'
  1764. ),
  1765. '/div'
  1766. );
  1767. $this->assertTags($result, $expected);
  1768. $this->Form->request->data = [];
  1769. $entity->errors('field', 'Badness!');
  1770. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  1771. $result = $this->Form->input('Contact.field');
  1772. $expected = array(
  1773. 'div' => array('class' => 'input text error'),
  1774. 'label' => array('for' => 'contact-field'),
  1775. 'Field',
  1776. '/label',
  1777. 'input' => array(
  1778. 'type' => 'text', 'name' => 'Contact[field]',
  1779. 'id' => 'contact-field', 'class' => 'form-error'
  1780. ),
  1781. array('div' => array('class' => 'error-message')),
  1782. 'Badness!',
  1783. '/div',
  1784. '/div'
  1785. );
  1786. $this->assertTags($result, $expected);
  1787. $result = $this->Form->input('Contact.field', array(
  1788. 'templates' => [
  1789. 'groupContainerError' => '{{content}}{{error}}',
  1790. 'error' => '<span class="error-message">{{content}}</span>'
  1791. ]
  1792. ));
  1793. $expected = array(
  1794. 'label' => array('for' => 'contact-field'),
  1795. 'Field',
  1796. '/label',
  1797. 'input' => array(
  1798. 'type' => 'text', 'name' => 'Contact[field]',
  1799. 'id' => 'contact-field', 'class' => 'form-error'
  1800. ),
  1801. array('span' => array('class' => 'error-message')),
  1802. 'Badness!',
  1803. '/span'
  1804. );
  1805. $this->assertTags($result, $expected);
  1806. $entity->errors('field', ['minLength']);
  1807. $result = $this->Form->input('Contact.field', array(
  1808. 'error' => array(
  1809. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1810. 'maxLength' => 'login too large'
  1811. )
  1812. ));
  1813. $expected = array(
  1814. 'div' => array('class' => 'input text error'),
  1815. 'label' => array('for' => 'contact-field'),
  1816. 'Field',
  1817. '/label',
  1818. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'contact-field', 'class' => 'form-error'),
  1819. array('div' => array('class' => 'error-message')),
  1820. 'Le login doit contenir au moins 2 caractères',
  1821. '/div',
  1822. '/div'
  1823. );
  1824. $this->assertTags($result, $expected);
  1825. $entity->errors('field', ['maxLength']);
  1826. $result = $this->Form->input('Contact.field', array(
  1827. 'error' => array(
  1828. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1829. 'maxLength' => 'login too large',
  1830. )
  1831. ));
  1832. $expected = array(
  1833. 'div' => array('class' => 'input text error'),
  1834. 'label' => array('for' => 'contact-field'),
  1835. 'Field',
  1836. '/label',
  1837. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'contact-field', 'class' => 'form-error'),
  1838. array('div' => array('class' => 'error-message')),
  1839. 'login too large',
  1840. '/div',
  1841. '/div'
  1842. );
  1843. $this->assertTags($result, $expected);
  1844. }
  1845. /**
  1846. * Test id prefix
  1847. *
  1848. * @return void
  1849. */
  1850. public function testCreateIdPrefix() {
  1851. $this->Form->create(false, ['idPrefix' => 'prefix']);
  1852. $result = $this->Form->input('field');
  1853. $expected = [
  1854. 'div' => ['class' => 'input text'],
  1855. 'label' => ['for' => 'prefix-field'],
  1856. 'Field',
  1857. '/label',
  1858. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  1859. '/div'
  1860. ];
  1861. $this->assertTags($result, $expected);
  1862. $result = $this->Form->input('field', ['id' => 'custom-id']);
  1863. $expected = [
  1864. 'div' => ['class' => 'input text'],
  1865. 'label' => ['for' => 'custom-id'],
  1866. 'Field',
  1867. '/label',
  1868. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  1869. '/div'
  1870. ];
  1871. $this->assertTags($result, $expected);
  1872. $result = $this->Form->radio('Model.field', ['option A']);
  1873. $expected = [
  1874. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  1875. ['input' => [
  1876. 'type' => 'radio',
  1877. 'name' => 'Model[field]',
  1878. 'value' => '0',
  1879. 'id' => 'prefix-model-field-0'
  1880. ]],
  1881. 'label' => ['for' => 'prefix-model-field-0'],
  1882. 'option A',
  1883. '/label'
  1884. ];
  1885. $this->assertTags($result, $expected);
  1886. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  1887. $expected = [
  1888. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  1889. ['input' => [
  1890. 'type' => 'radio',
  1891. 'name' => 'Model[field]',
  1892. 'value' => '0',
  1893. 'id' => 'prefix-model-field-0'
  1894. ]],
  1895. 'label' => ['for' => 'prefix-model-field-0'],
  1896. 'option A',
  1897. '/label'
  1898. ];
  1899. $this->assertTags($result, $expected);
  1900. $result = $this->Form->select(
  1901. 'Model.multi_field',
  1902. ['first'],
  1903. ['multiple' => 'checkbox']
  1904. );
  1905. $expected = [
  1906. 'input' => [
  1907. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  1908. ],
  1909. ['div' => ['class' => 'checkbox']],
  1910. ['input' => [
  1911. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  1912. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  1913. ]],
  1914. ['label' => ['for' => 'prefix-model-multi-field-0']],
  1915. 'first',
  1916. '/label',
  1917. '/div',
  1918. ];
  1919. $this->assertTags($result, $expected);
  1920. $this->Form->end();
  1921. $result = $this->Form->input('field');
  1922. $expected = [
  1923. 'div' => ['class' => 'input text'],
  1924. 'label' => ['for' => 'field'],
  1925. 'Field',
  1926. '/label',
  1927. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  1928. '/div'
  1929. ];
  1930. $this->assertTags($result, $expected);
  1931. }
  1932. /**
  1933. * Test that inputs with 0 can be created.
  1934. *
  1935. * @return void
  1936. */
  1937. public function testInputZero() {
  1938. TableRegistry::get('Contacts', [
  1939. 'className' => __NAMESPACE__ . '\ContactsTable'
  1940. ]);
  1941. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1942. $result = $this->Form->input('0');
  1943. $expected = array(
  1944. 'div' => array('class' => 'input text'),
  1945. 'label' => array('for' => '0'), '/label',
  1946. 'input' => array('type' => 'text', 'name' => '0', 'id' => '0'),
  1947. '/div'
  1948. );
  1949. $this->assertTags($result, $expected);
  1950. }
  1951. /**
  1952. * test input() with checkbox creation
  1953. *
  1954. * @return void
  1955. */
  1956. public function testInputCheckbox() {
  1957. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1958. $expected = array(
  1959. 'div' => array('class' => 'input checkbox'),
  1960. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  1961. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  1962. '/div'
  1963. );
  1964. $this->assertTags($result, $expected);
  1965. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1966. $expected = array(
  1967. 'div' => array('class' => 'input checkbox'),
  1968. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  1969. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  1970. '/div'
  1971. );
  1972. $this->assertTags($result, $expected);
  1973. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1974. $expected = array(
  1975. 'div' => array('class' => 'input checkbox'),
  1976. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  1977. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  1978. '/div'
  1979. );
  1980. $this->assertTags($result, $expected);
  1981. $result = $this->Form->input('User.disabled', array(
  1982. 'label' => 'Disabled',
  1983. 'type' => 'checkbox',
  1984. 'data-foo' => 'disabled'
  1985. ));
  1986. $expected = array(
  1987. 'div' => array('class' => 'input checkbox'),
  1988. 'input' => array('type' => 'hidden', 'name' => 'User[disabled]', 'value' => '0'),
  1989. array('input' => array(
  1990. 'type' => 'checkbox',
  1991. 'name' => 'User[disabled]',
  1992. 'value' => '1',
  1993. 'id' => 'user-disabled',
  1994. 'data-foo' => 'disabled'
  1995. )),
  1996. 'label' => array('for' => 'user-disabled'),
  1997. 'Disabled',
  1998. '/label',
  1999. '/div'
  2000. );
  2001. $this->assertTags($result, $expected);
  2002. }
  2003. /**
  2004. * Test that input() does not create wrapping div and label tag for hidden fields
  2005. *
  2006. * @return void
  2007. */
  2008. public function testInputHidden() {
  2009. TableRegistry::get('ValidateUsers', [
  2010. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2011. ]);
  2012. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2013. $result = $this->Form->input('ValidateUser.id');
  2014. $expected = array(
  2015. 'input' => array('name', 'type' => 'hidden', 'id')
  2016. );
  2017. $this->assertTags($result, $expected);
  2018. $result = $this->Form->input('ValidateUser.custom', ['type' => 'hidden']);
  2019. $expected = array(
  2020. 'input' => array('name', 'type' => 'hidden', 'id')
  2021. );
  2022. $this->assertTags($result, $expected);
  2023. }
  2024. /**
  2025. * test form->input() with datetime
  2026. *
  2027. * @return void
  2028. */
  2029. public function testInputDatetime() {
  2030. $this->Form = $this->getMock(
  2031. 'Cake\View\Helper\FormHelper',
  2032. ['datetime'],
  2033. [new View()]
  2034. );
  2035. $this->Form->expects($this->once())->method('datetime')
  2036. ->with('prueba', [
  2037. 'type' => 'datetime',
  2038. 'timeFormat' => 24,
  2039. 'minYear' => 2008,
  2040. 'maxYear' => 2011,
  2041. 'interval' => 15,
  2042. 'options' => null,
  2043. 'empty' => false,
  2044. 'id' => 'prueba',
  2045. 'required' => false,
  2046. ])
  2047. ->will($this->returnValue('This is it!'));
  2048. $result = $this->Form->input('prueba', array(
  2049. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  2050. 'maxYear' => 2011, 'interval' => 15
  2051. ));
  2052. $expected = array(
  2053. 'div' => array('class' => 'input datetime'),
  2054. 'label' => array('for' => 'prueba'),
  2055. 'Prueba',
  2056. '/label',
  2057. 'This is it!',
  2058. '/div'
  2059. );
  2060. $this->assertTags($result, $expected);
  2061. }
  2062. /**
  2063. * test form->input() with datetime with id prefix
  2064. *
  2065. * @return void
  2066. */
  2067. public function testInputDatetimeIdPrefix() {
  2068. $this->Form = $this->getMock(
  2069. 'Cake\View\Helper\FormHelper',
  2070. ['datetime'],
  2071. [new View()]
  2072. );
  2073. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2074. $this->Form->expects($this->once())->method('datetime')
  2075. ->with('prueba', [
  2076. 'type' => 'datetime',
  2077. 'timeFormat' => 24,
  2078. 'minYear' => 2008,
  2079. 'maxYear' => 2011,
  2080. 'interval' => 15,
  2081. 'options' => null,
  2082. 'empty' => false,
  2083. 'id' => 'prefix-prueba',
  2084. 'required' => false,
  2085. ])
  2086. ->will($this->returnValue('This is it!'));
  2087. $result = $this->Form->input('prueba', array(
  2088. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  2089. 'maxYear' => 2011, 'interval' => 15
  2090. ));
  2091. $expected = array(
  2092. 'div' => array('class' => 'input datetime'),
  2093. 'label' => array('for' => 'prefix-prueba'),
  2094. 'Prueba',
  2095. '/label',
  2096. 'This is it!',
  2097. '/div'
  2098. );
  2099. $this->assertTags($result, $expected);
  2100. }
  2101. /**
  2102. * Test generating checkboxes with disabled elements.
  2103. *
  2104. * @return void
  2105. */
  2106. public function testInputCheckboxWithDisabledElements() {
  2107. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2108. $result = $this->Form->input('Contact.multiple', array(
  2109. 'multiple' => 'checkbox',
  2110. 'disabled' => 'disabled',
  2111. 'options' => $options
  2112. ));
  2113. $expected = array(
  2114. array('div' => array('class' => 'input select')),
  2115. array('label' => array('for' => "contact-multiple")),
  2116. 'Multiple',
  2117. '/label',
  2118. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '')),
  2119. array('div' => array('class' => 'checkbox')),
  2120. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "contact-multiple-1")),
  2121. array('label' => array('for' => "contact-multiple-1")),
  2122. 'One',
  2123. '/label',
  2124. '/div',
  2125. array('div' => array('class' => 'checkbox')),
  2126. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "contact-multiple-2")),
  2127. array('label' => array('for' => "contact-multiple-2")),
  2128. 'Two',
  2129. '/label',
  2130. '/div',
  2131. array('div' => array('class' => 'checkbox')),
  2132. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "contact-multiple-3")),
  2133. array('label' => array('for' => "contact-multiple-3")),
  2134. 'Three',
  2135. '/label',
  2136. '/div',
  2137. '/div'
  2138. );
  2139. $this->assertTags($result, $expected);
  2140. // make sure 50 does only disable 50, and not 50f5c0cf
  2141. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2142. $disabled = array(50);
  2143. $expected = array(
  2144. array('div' => array('class' => 'input select')),
  2145. array('label' => array('for' => "contact-multiple")),
  2146. 'Multiple',
  2147. '/label',
  2148. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '')),
  2149. array('div' => array('class' => 'checkbox')),
  2150. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "contact-multiple-50")),
  2151. array('label' => array('for' => "contact-multiple-50")),
  2152. 'Fifty',
  2153. '/label',
  2154. '/div',
  2155. array('div' => array('class' => 'checkbox')),
  2156. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "contact-multiple-50f5c0cf")),
  2157. array('label' => array('for' => "contact-multiple-50f5c0cf")),
  2158. 'Stringy',
  2159. '/label',
  2160. '/div',
  2161. '/div'
  2162. );
  2163. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2164. $this->assertTags($result, $expected);
  2165. }
  2166. /**
  2167. * test input name with leading integer, ensure attributes are generated correctly.
  2168. *
  2169. * @return void
  2170. */
  2171. public function testInputWithLeadingInteger() {
  2172. $result = $this->Form->text('0.Node.title');
  2173. $expected = array(
  2174. 'input' => array('name' => '0[Node][title]', 'type' => 'text')
  2175. );
  2176. $this->assertTags($result, $expected);
  2177. }
  2178. /**
  2179. * test form->input() with select type inputs.
  2180. *
  2181. * @return void
  2182. */
  2183. public function testInputSelectType() {
  2184. $result = $this->Form->input('email', array(
  2185. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2186. );
  2187. $expected = array(
  2188. 'div' => array('class' => 'input select'),
  2189. 'label' => array('for' => 'email'),
  2190. 'Email',
  2191. '/label',
  2192. array('select' => array('name' => 'email', 'id' => 'email')),
  2193. array('option' => array('value' => '')),
  2194. '/option',
  2195. array('option' => array('value' => 'è')),
  2196. 'Firést',
  2197. '/option',
  2198. array('option' => array('value' => 'é')),
  2199. 'Secoènd',
  2200. '/option',
  2201. '/select',
  2202. '/div'
  2203. );
  2204. $this->assertTags($result, $expected);
  2205. $result = $this->Form->input('email', array(
  2206. 'options' => array('First', 'Second'), 'empty' => true)
  2207. );
  2208. $expected = array(
  2209. 'div' => array('class' => 'input select'),
  2210. 'label' => array('for' => 'email'),
  2211. 'Email',
  2212. '/label',
  2213. array('select' => array('name' => 'email', 'id' => 'email')),
  2214. array('option' => array('value' => '')),
  2215. '/option',
  2216. array('option' => array('value' => '0')),
  2217. 'First',
  2218. '/option',
  2219. array('option' => array('value' => '1')),
  2220. 'Second',
  2221. '/option',
  2222. '/select',
  2223. '/div'
  2224. );
  2225. $this->assertTags($result, $expected);
  2226. $result = $this->Form->input('email', [
  2227. 'type' => 'select',
  2228. 'options' => new \ArrayObject(['First', 'Second']),
  2229. 'empty' => true
  2230. ]);
  2231. $this->assertTags($result, $expected);
  2232. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2233. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2234. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2235. $expected = array(
  2236. 'div' => array('class' => 'input select'),
  2237. 'label' => array('for' => 'model-user-id'),
  2238. 'User',
  2239. '/label',
  2240. 'select' => array('name' => 'Model[user_id]', 'id' => 'model-user-id'),
  2241. array('option' => array('value' => '')),
  2242. '/option',
  2243. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2244. 'good',
  2245. '/option',
  2246. array('option' => array('value' => 'other')),
  2247. 'bad',
  2248. '/option',
  2249. '/select',
  2250. '/div'
  2251. );
  2252. $this->assertTags($result, $expected);
  2253. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2254. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2255. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2256. $expected = array(
  2257. 'div' => array('class' => 'input select'),
  2258. 'label' => array('for' => 'thing-user-id'),
  2259. 'User',
  2260. '/label',
  2261. 'select' => array('name' => 'Thing[user_id]', 'id' => 'thing-user-id'),
  2262. array('option' => array('value' => '')),
  2263. 'Some Empty',
  2264. '/option',
  2265. array('option' => array('value' => 'value')),
  2266. 'good',
  2267. '/option',
  2268. array('option' => array('value' => 'other')),
  2269. 'bad',
  2270. '/option',
  2271. '/select',
  2272. '/div'
  2273. );
  2274. $this->assertTags($result, $expected);
  2275. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2276. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2277. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2278. $expected = array(
  2279. 'div' => array('class' => 'input select'),
  2280. 'label' => array('for' => 'thing-user-id'),
  2281. 'User',
  2282. '/label',
  2283. 'select' => array('name' => 'Thing[user_id]', 'id' => 'thing-user-id'),
  2284. array('option' => array('value' => '')),
  2285. 'Some Empty',
  2286. '/option',
  2287. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2288. 'good',
  2289. '/option',
  2290. array('option' => array('value' => 'other')),
  2291. 'bad',
  2292. '/option',
  2293. '/select',
  2294. '/div'
  2295. );
  2296. $this->assertTags($result, $expected);
  2297. $this->Form->data = array();
  2298. $result = $this->Form->input('Publisher.id', array(
  2299. 'label' => 'Publisher',
  2300. 'type' => 'select',
  2301. 'multiple' => 'checkbox',
  2302. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2303. ));
  2304. $expected = array(
  2305. array('div' => array('class' => 'input select')),
  2306. array('label' => array('for' => 'publisher-id')),
  2307. 'Publisher',
  2308. '/label',
  2309. 'input' => array('type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''),
  2310. array('div' => array('class' => 'checkbox')),
  2311. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1')),
  2312. array('label' => array('for' => 'publisher-id-value-1')),
  2313. 'Label 1',
  2314. '/label',
  2315. '/div',
  2316. array('div' => array('class' => 'checkbox')),
  2317. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2')),
  2318. array('label' => array('for' => 'publisher-id-value-2')),
  2319. 'Label 2',
  2320. '/label',
  2321. '/div',
  2322. '/div'
  2323. );
  2324. $this->assertTags($result, $expected);
  2325. }
  2326. /**
  2327. * test that input() and a non standard primary key makes a hidden input by default.
  2328. *
  2329. * @return void
  2330. */
  2331. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2332. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  2333. $this->Form->create($this->article);
  2334. $result = $this->Form->input('title');
  2335. $expected = array(
  2336. 'input' => array('type' => 'hidden', 'name' => 'title', 'id' => 'title'),
  2337. );
  2338. $this->assertTags($result, $expected);
  2339. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  2340. $this->Form->create($this->article);
  2341. $result = $this->Form->input('title');
  2342. $expected = array(
  2343. 'input' => array('type' => 'hidden', 'name' => 'title', 'id' => 'title'),
  2344. );
  2345. $this->assertTags($result, $expected);
  2346. $result = $this->Form->input('body');
  2347. $expected = array(
  2348. 'input' => array('type' => 'hidden', 'name' => 'body', 'id' => 'body'),
  2349. );
  2350. $this->assertTags($result, $expected);
  2351. }
  2352. /**
  2353. * test that overriding the magic select type widget is possible
  2354. *
  2355. * @return void
  2356. */
  2357. public function testInputOverridingMagicSelectType() {
  2358. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2359. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2360. $expected = array(
  2361. 'div' => array('class' => 'input text'),
  2362. 'label' => array('for' => 'model-user-id'), 'User', '/label',
  2363. 'input' => array('name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'),
  2364. '/div'
  2365. );
  2366. $this->assertTags($result, $expected);
  2367. //Check that magic types still work for plural/singular vars
  2368. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2369. $result = $this->Form->input('Model.type');
  2370. $expected = array(
  2371. 'div' => array('class' => 'input select'),
  2372. 'label' => array('for' => 'model-type'), 'Type', '/label',
  2373. 'select' => array('name' => 'Model[type]', 'id' => 'model-type'),
  2374. array('option' => array('value' => 'value')), 'good', '/option',
  2375. array('option' => array('value' => 'other')), 'bad', '/option',
  2376. '/select',
  2377. '/div'
  2378. );
  2379. $this->assertTags($result, $expected);
  2380. }
  2381. /**
  2382. * Test that inferred types do not override developer input
  2383. *
  2384. * @return void
  2385. */
  2386. public function testInputMagicTypeDoesNotOverride() {
  2387. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2388. $result = $this->Form->input('Model.user', array('type' => 'checkbox'));
  2389. $expected = array(
  2390. 'div' => array('class' => 'input checkbox'),
  2391. array('input' => array(
  2392. 'type' => 'hidden',
  2393. 'name' => 'Model[user]',
  2394. 'value' => 0,
  2395. )),
  2396. array('input' => array(
  2397. 'name' => 'Model[user]',
  2398. 'type' => 'checkbox',
  2399. 'id' => 'model-user',
  2400. 'value' => 1
  2401. )),
  2402. 'label' => array('for' => 'model-user'), 'User', '/label',
  2403. '/div'
  2404. );
  2405. $this->assertTags($result, $expected);
  2406. }
  2407. /**
  2408. * Test that magic input() selects are created for type=number
  2409. *
  2410. * @return void
  2411. */
  2412. public function testInputMagicSelectForTypeNumber() {
  2413. TableRegistry::get('ValidateUsers', [
  2414. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2415. ]);
  2416. $entity = new Entity(['balance' => 1]);
  2417. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  2418. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2419. $result = $this->Form->input('ValidateUser.balance');
  2420. $expected = array(
  2421. 'div' => array('class' => 'input select'),
  2422. 'label' => array('for' => 'validateuser-balance'),
  2423. 'Balance',
  2424. '/label',
  2425. 'select' => array('name' => 'ValidateUser[balance]', 'id' => 'validateuser-balance'),
  2426. array('option' => array('value' => '0')),
  2427. 'nothing',
  2428. '/option',
  2429. array('option' => array('value' => '1', 'selected' => 'selected')),
  2430. 'some',
  2431. '/option',
  2432. array('option' => array('value' => '100')),
  2433. 'a lot',
  2434. '/option',
  2435. '/select',
  2436. '/div'
  2437. );
  2438. $this->assertTags($result, $expected);
  2439. }
  2440. /**
  2441. * Test that magic input() selects can easily be converted into radio types without error.
  2442. *
  2443. * @return void
  2444. */
  2445. public function testInputMagicSelectChangeToRadio() {
  2446. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2447. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2448. $this->assertContains('input type="radio"', $result);
  2449. }
  2450. /**
  2451. * testFormInputs method
  2452. *
  2453. * test correct results from form::inputs().
  2454. *
  2455. * @return void
  2456. */
  2457. public function testFormInputsLegendFieldset() {
  2458. $this->Form->create($this->article);
  2459. $result = $this->Form->inputs([], array('legend' => 'The Legend'));
  2460. $expected = array(
  2461. '<fieldset',
  2462. '<legend',
  2463. 'The Legend',
  2464. '/legend',
  2465. '*/fieldset',
  2466. );
  2467. $this->assertTags($result, $expected);
  2468. $result = $this->Form->inputs([], array('fieldset' => true, 'legend' => 'Field of Dreams'));
  2469. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  2470. $this->assertContains('<fieldset>', $result);
  2471. $result = $this->Form->inputs([], array('fieldset' => false, 'legend' => false));
  2472. $this->assertNotContains('<legend>', $result);
  2473. $this->assertNotContains('<fieldset>', $result);
  2474. $result = $this->Form->inputs([], array('fieldset' => false, 'legend' => 'Hello'));
  2475. $this->assertNotContains('<legend>', $result);
  2476. $this->assertNotContains('<fieldset>', $result);
  2477. $this->Form->create($this->article);
  2478. $this->Form->request->params['prefix'] = 'admin';
  2479. $this->Form->request->params['action'] = 'admin_edit';
  2480. $this->Form->request->params['controller'] = 'articles';
  2481. $result = $this->Form->inputs();
  2482. $expected = [
  2483. '<fieldset',
  2484. '<legend',
  2485. 'New Article',
  2486. '/legend',
  2487. '*/fieldset',
  2488. ];
  2489. $this->assertTags($result, $expected);
  2490. }
  2491. /**
  2492. * Test the inputs() method.
  2493. *
  2494. * @return void
  2495. */
  2496. public function testFormInputs() {
  2497. $this->Form->create($this->article);
  2498. $result = $this->Form->inputs();
  2499. $expected = array(
  2500. '<fieldset',
  2501. '<legend', 'New Article', '/legend',
  2502. 'input' => array('type' => 'hidden', 'name' => 'id', 'id' => 'id'),
  2503. array('div' => array('class' => 'input select required')),
  2504. '*/div',
  2505. array('div' => array('class' => 'input text required')),
  2506. '*/div',
  2507. array('div' => array('class' => 'input text')),
  2508. '*/div',
  2509. array('div' => array('class' => 'input text')),
  2510. '*/div',
  2511. '/fieldset',
  2512. );
  2513. $this->assertTags($result, $expected);
  2514. $result = $this->Form->inputs([
  2515. 'published' => ['type' => 'boolean']
  2516. ]);
  2517. $expected = array(
  2518. '<fieldset',
  2519. '<legend', 'New Article', '/legend',
  2520. 'input' => array('type' => 'hidden', 'name' => 'id', 'id' => 'id'),
  2521. array('div' => array('class' => 'input select required')),
  2522. '*/div',
  2523. array('div' => array('class' => 'input text required')),
  2524. '*/div',
  2525. array('div' => array('class' => 'input text')),
  2526. '*/div',
  2527. array('div' => array('class' => 'input boolean')),
  2528. '*/div',
  2529. '/fieldset',
  2530. );
  2531. $this->assertTags($result, $expected);
  2532. $this->Form->create($this->article);
  2533. $result = $this->Form->inputs([], ['legend' => 'Hello']);
  2534. $expected = array(
  2535. 'fieldset' => array(),
  2536. 'legend' => array(),
  2537. 'Hello',
  2538. '/legend',
  2539. 'input' => array('type' => 'hidden', 'name' => 'id', 'id' => 'id'),
  2540. array('div' => array('class' => 'input select required')),
  2541. '*/div',
  2542. array('div' => array('class' => 'input text required')),
  2543. '*/div',
  2544. array('div' => array('class' => 'input text')),
  2545. '*/div',
  2546. array('div' => array('class' => 'input text')),
  2547. '*/div',
  2548. '/fieldset'
  2549. );
  2550. $this->assertTags($result, $expected);
  2551. $this->Form->create(false);
  2552. $expected = array(
  2553. 'fieldset' => array(),
  2554. array('div' => array('class' => 'input text')),
  2555. 'label' => array('for' => 'foo'),
  2556. 'Foo',
  2557. '/label',
  2558. 'input' => array('type' => 'text', 'name' => 'foo', 'id' => 'foo'),
  2559. '*/div',
  2560. '/fieldset'
  2561. );
  2562. $result = $this->Form->inputs(
  2563. array('foo' => array('type' => 'text')),
  2564. array('legend' => false)
  2565. );
  2566. $this->assertTags($result, $expected);
  2567. }
  2568. /**
  2569. * testFormInputsBlacklist
  2570. *
  2571. * @return void
  2572. */
  2573. public function testFormInputsBlacklist() {
  2574. $this->Form->create($this->article);
  2575. $result = $this->Form->inputs([
  2576. 'id' => false
  2577. ]);
  2578. $expected = array(
  2579. '<fieldset',
  2580. '<legend', 'New Article', '/legend',
  2581. array('div' => array('class' => 'input select required')),
  2582. '*/div',
  2583. array('div' => array('class' => 'input text required')),
  2584. '*/div',
  2585. array('div' => array('class' => 'input text')),
  2586. '*/div',
  2587. array('div' => array('class' => 'input text')),
  2588. '*/div',
  2589. '/fieldset',
  2590. );
  2591. $this->assertTags($result, $expected);
  2592. $this->Form->create($this->article);
  2593. $result = $this->Form->inputs([
  2594. 'id' => []
  2595. ]);
  2596. $expected = array(
  2597. '<fieldset',
  2598. '<legend', 'New Article', '/legend',
  2599. 'input' => array('type' => 'hidden', 'name' => 'id', 'id' => 'id'),
  2600. array('div' => array('class' => 'input select required')),
  2601. '*/div',
  2602. array('div' => array('class' => 'input text required')),
  2603. '*/div',
  2604. array('div' => array('class' => 'input text')),
  2605. '*/div',
  2606. array('div' => array('class' => 'input text')),
  2607. '*/div',
  2608. '/fieldset',
  2609. );
  2610. $this->assertTags($result, $expected, 'A falsey value (array) should not remove the input');
  2611. }
  2612. /**
  2613. * testSelectAsCheckbox method
  2614. *
  2615. * test multi-select widget with checkbox formatting.
  2616. *
  2617. * @return void
  2618. */
  2619. public function testSelectAsCheckbox() {
  2620. $result = $this->Form->select(
  2621. 'Model.multi_field',
  2622. array('first', 'second', 'third'),
  2623. array('multiple' => 'checkbox', 'value' => array(0, 1))
  2624. );
  2625. $expected = array(
  2626. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  2627. array('div' => array('class' => 'checkbox')),
  2628. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0')),
  2629. array('label' => array('for' => 'model-multi-field-0', 'class' => 'selected')),
  2630. 'first',
  2631. '/label',
  2632. '/div',
  2633. array('div' => array('class' => 'checkbox')),
  2634. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1')),
  2635. array('label' => array('for' => 'model-multi-field-1', 'class' => 'selected')),
  2636. 'second',
  2637. '/label',
  2638. '/div',
  2639. array('div' => array('class' => 'checkbox')),
  2640. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2')),
  2641. array('label' => array('for' => 'model-multi-field-2')),
  2642. 'third',
  2643. '/label',
  2644. '/div',
  2645. );
  2646. $this->assertTags($result, $expected);
  2647. $result = $this->Form->select(
  2648. 'Model.multi_field',
  2649. array('1/2' => 'half'),
  2650. array('multiple' => 'checkbox')
  2651. );
  2652. $expected = array(
  2653. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  2654. array('div' => array('class' => 'checkbox')),
  2655. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2')),
  2656. array('label' => array('for' => 'model-multi-field-1-2')),
  2657. 'half',
  2658. '/label',
  2659. '/div',
  2660. );
  2661. $this->assertTags($result, $expected);
  2662. }
  2663. /**
  2664. * testLabel method
  2665. *
  2666. * test label generation.
  2667. *
  2668. * @return void
  2669. */
  2670. public function testLabel() {
  2671. $result = $this->Form->label('Person.name');
  2672. $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
  2673. $result = $this->Form->label('Person.name');
  2674. $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
  2675. $result = $this->Form->label('Person.first_name');
  2676. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'First Name', '/label'));
  2677. $result = $this->Form->label('Person.first_name', 'Your first name');
  2678. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'Your first name', '/label'));
  2679. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2680. $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class'), 'Your first name', '/label'));
  2681. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2682. $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2683. $result = $this->Form->label('Person.first_name', '');
  2684. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), '/label'));
  2685. $result = $this->Form->label('Person.2.name', '');
  2686. $this->assertTags($result, array('label' => array('for' => 'person-2-name'), '/label'));
  2687. }
  2688. /**
  2689. * testTextbox method
  2690. *
  2691. * test textbox element generation
  2692. *
  2693. * @return void
  2694. */
  2695. public function testTextbox() {
  2696. $result = $this->Form->text('Model.field');
  2697. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]')));
  2698. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2699. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Model[field]')));
  2700. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2701. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'theID')));
  2702. }
  2703. /**
  2704. * Test that text() hooks up with request data and error fields.
  2705. *
  2706. * @return void
  2707. */
  2708. public function testTextBoxDataAndError() {
  2709. $this->article['errors'] = [
  2710. 'Contact' => ['text' => 'wrong']
  2711. ];
  2712. $this->Form->create($this->article);
  2713. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2714. $result = $this->Form->text('Model.text');
  2715. $expected = [
  2716. 'input' => [
  2717. 'type' => 'text',
  2718. 'name' => 'Model[text]',
  2719. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  2720. ]
  2721. ];
  2722. $this->assertTags($result, $expected);
  2723. $this->Form->request->data['Contact']['text'] = 'test';
  2724. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  2725. $expected = [
  2726. 'input' => [
  2727. 'type' => 'text',
  2728. 'name' => 'Contact[text]',
  2729. 'value' => 'test',
  2730. 'id' => 'theID',
  2731. 'class' => 'form-error'
  2732. ]
  2733. ];
  2734. $this->assertTags($result, $expected);
  2735. }
  2736. /**
  2737. * testDefaultValue method
  2738. *
  2739. * Test default value setting
  2740. *
  2741. * @return void
  2742. */
  2743. public function testTextDefaultValue() {
  2744. $this->Form->request->data['Model']['field'] = 'test';
  2745. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2746. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  2747. $this->assertTags($result, $expected);
  2748. unset($this->Form->request->data['Model']['field']);
  2749. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2750. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  2751. $this->assertTags($result, $expected);
  2752. }
  2753. /**
  2754. * testError method
  2755. *
  2756. * Test field error generation
  2757. *
  2758. * @return void
  2759. */
  2760. public function testError() {
  2761. $this->article['errors'] = [
  2762. 'Article' => ['field' => 'email']
  2763. ];
  2764. $this->Form->create($this->article);
  2765. $result = $this->Form->error('Article.field');
  2766. $expected = [
  2767. ['div' => ['class' => 'error-message']],
  2768. 'email',
  2769. '/div',
  2770. ];
  2771. $this->assertTags($result, $expected);
  2772. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  2773. $expected = [
  2774. ['div' => ['class' => 'error-message']],
  2775. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  2776. '/div',
  2777. ];
  2778. $this->assertTags($result, $expected);
  2779. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  2780. $expected = [
  2781. ['div' => ['class' => 'error-message']],
  2782. '<strong', 'Badness!', '/strong',
  2783. '/div',
  2784. ];
  2785. $this->assertTags($result, $expected);
  2786. }
  2787. /**
  2788. * Test error with nested lists.
  2789. *
  2790. * @return void
  2791. */
  2792. public function testErrorMessages() {
  2793. $this->article['errors'] = [
  2794. 'Article' => ['field' => 'email']
  2795. ];
  2796. $this->Form->create($this->article);
  2797. $result = $this->Form->error('Article.field', array(
  2798. 'email' => 'No good!'
  2799. ));
  2800. $expected = array(
  2801. 'div' => array('class' => 'error-message'),
  2802. 'No good!',
  2803. '/div'
  2804. );
  2805. $this->assertTags($result, $expected);
  2806. }
  2807. /**
  2808. * test error() with multiple messages.
  2809. *
  2810. * @return void
  2811. */
  2812. public function testErrorMultipleMessages() {
  2813. $this->article['errors'] = [
  2814. 'field' => ['notEmpty', 'email', 'Something else']
  2815. ];
  2816. $this->Form->create($this->article);
  2817. $result = $this->Form->error('field', array(
  2818. 'notEmpty' => 'Cannot be empty',
  2819. 'email' => 'No good!'
  2820. ));
  2821. $expected = array(
  2822. 'div' => array('class' => 'error-message'),
  2823. 'ul' => array(),
  2824. '<li', 'Cannot be empty', '/li',
  2825. '<li', 'No good!', '/li',
  2826. '<li', 'Something else', '/li',
  2827. '/ul',
  2828. '/div'
  2829. );
  2830. $this->assertTags($result, $expected);
  2831. }
  2832. /**
  2833. * testPassword method
  2834. *
  2835. * Test password element generation
  2836. *
  2837. * @return void
  2838. */
  2839. public function testPassword() {
  2840. $this->article['errors'] = [
  2841. 'Contact' => [
  2842. 'passwd' => 1
  2843. ]
  2844. ];
  2845. $this->Form->create($this->article);
  2846. $result = $this->Form->password('Contact.field');
  2847. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[field]')));
  2848. $this->Form->request->data['Contact']['passwd'] = 'test';
  2849. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  2850. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2851. }
  2852. /**
  2853. * testRadio method
  2854. *
  2855. * Test radio element set generation
  2856. *
  2857. * @return void
  2858. */
  2859. public function testRadio() {
  2860. $result = $this->Form->radio('Model.field', array('option A'));
  2861. $expected = array(
  2862. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  2863. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  2864. 'label' => array('for' => 'model-field-0'),
  2865. 'option A',
  2866. '/label'
  2867. );
  2868. $this->assertTags($result, $expected);
  2869. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  2870. $this->assertTags($result, $expected);
  2871. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  2872. $expected = array(
  2873. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  2874. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  2875. array('label' => array('for' => 'model-field-0')),
  2876. 'option A',
  2877. '/label',
  2878. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1')),
  2879. array('label' => array('for' => 'model-field-1')),
  2880. 'option B',
  2881. '/label',
  2882. );
  2883. $this->assertTags($result, $expected);
  2884. $result = $this->Form->radio(
  2885. 'Employee.gender',
  2886. array('male' => 'Male', 'female' => 'Female'),
  2887. ['form' => 'my-form']
  2888. );
  2889. $expected = array(
  2890. 'input' => array('type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'),
  2891. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form')),
  2892. array('label' => array('for' => 'employee-gender-male')),
  2893. 'Male',
  2894. '/label',
  2895. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form')),
  2896. array('label' => array('for' => 'employee-gender-female')),
  2897. 'Female',
  2898. '/label',
  2899. );
  2900. $this->assertTags($result, $expected);
  2901. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'Model[custom]'));
  2902. $expected = array(
  2903. array('input' => array('type' => 'hidden', 'name' => 'Model[custom]', 'value' => '')),
  2904. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0')),
  2905. array('label' => array('for' => 'model-custom-0')),
  2906. 'option A',
  2907. '/label',
  2908. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1')),
  2909. array('label' => array('for' => 'model-custom-1')),
  2910. 'option B',
  2911. '/label',
  2912. );
  2913. $this->assertTags($result, $expected);
  2914. }
  2915. /**
  2916. * test generating radio input inside label ala twitter bootstrap
  2917. *
  2918. * @return void
  2919. */
  2920. public function testRadioInputInsideLabel() {
  2921. $this->Form->templates([
  2922. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  2923. 'radioContainer' => '{{label}}'
  2924. ]);
  2925. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  2926. $expected = [
  2927. ['input' => [
  2928. 'type' => 'hidden',
  2929. 'name' => 'Model[field]',
  2930. 'value' => ''
  2931. ]],
  2932. ['label' => ['for' => 'model-field-0']],
  2933. ['input' => [
  2934. 'type' => 'radio',
  2935. 'name' => 'Model[field]',
  2936. 'value' => '0',
  2937. 'id' => 'model-field-0'
  2938. ]],
  2939. 'option A',
  2940. '/label',
  2941. ['label' => ['for' => 'model-field-1']],
  2942. ['input' => [
  2943. 'type' => 'radio',
  2944. 'name' => 'Model[field]',
  2945. 'value' => '1',
  2946. 'id' => 'model-field-1'
  2947. ]],
  2948. 'option B',
  2949. '/label',
  2950. ];
  2951. $this->assertTags($result, $expected);
  2952. }
  2953. /**
  2954. * test disabling the hidden input for radio buttons
  2955. *
  2956. * @return void
  2957. */
  2958. public function testRadioHiddenInputDisabling() {
  2959. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  2960. $expected = array(
  2961. 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'),
  2962. 'label' => array('for' => 'model-1-field-0'),
  2963. 'option A',
  2964. '/label'
  2965. );
  2966. $this->assertTags($result, $expected);
  2967. }
  2968. /**
  2969. * testSelect method
  2970. *
  2971. * Test select element generation.
  2972. *
  2973. * @return void
  2974. */
  2975. public function testSelect() {
  2976. $result = $this->Form->select('Model.field', array());
  2977. $expected = array(
  2978. 'select' => array('name' => 'Model[field]'),
  2979. '/select'
  2980. );
  2981. $this->assertTags($result, $expected);
  2982. $this->Form->request->data = array('Model' => array('field' => 'value'));
  2983. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  2984. $expected = array(
  2985. 'select' => array('name' => 'Model[field]'),
  2986. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2987. 'good',
  2988. '/option',
  2989. array('option' => array('value' => 'other')),
  2990. 'bad',
  2991. '/option',
  2992. '/select'
  2993. );
  2994. $this->assertTags($result, $expected);
  2995. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  2996. $this->assertTags($result, $expected);
  2997. $this->Form->request->data = array();
  2998. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  2999. $expected = array(
  3000. 'select' => array('name' => 'Model[field]'),
  3001. array('option' => array('value' => 'value')),
  3002. 'good',
  3003. '/option',
  3004. array('option' => array('value' => 'other')),
  3005. 'bad',
  3006. '/option',
  3007. '/select'
  3008. );
  3009. $this->assertTags($result, $expected);
  3010. $options = array(
  3011. array('value' => 'first', 'text' => 'First'),
  3012. array('value' => 'first', 'text' => 'Another First'),
  3013. );
  3014. $result = $this->Form->select(
  3015. 'Model.field',
  3016. $options,
  3017. array('escape' => false, 'empty' => false)
  3018. );
  3019. $expected = array(
  3020. 'select' => array('name' => 'Model[field]'),
  3021. array('option' => array('value' => 'first')),
  3022. 'First',
  3023. '/option',
  3024. array('option' => array('value' => 'first')),
  3025. 'Another First',
  3026. '/option',
  3027. '/select'
  3028. );
  3029. $this->assertTags($result, $expected);
  3030. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3031. $result = $this->Form->select(
  3032. 'Model.contact_id',
  3033. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3034. array('escape' => false, 'empty' => 'pick something')
  3035. );
  3036. $expected = array(
  3037. 'select' => array('name' => 'Model[contact_id]'),
  3038. array('option' => array('value' => '')), 'pick something', '/option',
  3039. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3040. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3041. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3042. '/select'
  3043. );
  3044. $this->assertTags($result, $expected);
  3045. $this->Form->request->data['Model']['field'] = 0;
  3046. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3047. $expected = array(
  3048. 'select' => array('name' => 'Model[field]'),
  3049. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3050. array('option' => array('value' => '1')), 'Yes', '/option',
  3051. '/select'
  3052. );
  3053. $this->assertTags($result, $expected);
  3054. }
  3055. /**
  3056. * Test that select() escapes HTML.
  3057. *
  3058. * @return void
  3059. */
  3060. public function testSelectEscapeHtml() {
  3061. $result = $this->Form->select(
  3062. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3063. array('empty' => false)
  3064. );
  3065. $expected = array(
  3066. 'select' => array('name' => 'Model[field]'),
  3067. array('option' => array('value' => 'first')),
  3068. 'first &quot;html&quot; &lt;chars&gt;',
  3069. '/option',
  3070. array('option' => array('value' => 'second')),
  3071. 'value',
  3072. '/option',
  3073. '/select'
  3074. );
  3075. $this->assertTags($result, $expected);
  3076. $result = $this->Form->select(
  3077. 'Model.field',
  3078. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3079. array('escape' => false, 'empty' => false)
  3080. );
  3081. $expected = array(
  3082. 'select' => array('name' => 'Model[field]'),
  3083. array('option' => array('value' => 'first')),
  3084. 'first "html" <chars>',
  3085. '/option',
  3086. array('option' => array('value' => 'second')),
  3087. 'value',
  3088. '/option',
  3089. '/select'
  3090. );
  3091. $this->assertTags($result, $expected);
  3092. }
  3093. /**
  3094. * test select() with required and disabled attributes.
  3095. *
  3096. * @return void
  3097. */
  3098. public function testSelectRequired() {
  3099. $this->article['required'] = [
  3100. 'user_id' => true
  3101. ];
  3102. $this->Form->create($this->article);
  3103. $result = $this->Form->select('user_id', array('option A'));
  3104. $expected = array(
  3105. 'select' => array(
  3106. 'name' => 'user_id',
  3107. 'required' => 'required'
  3108. ),
  3109. array('option' => array('value' => '0')), 'option A', '/option',
  3110. '/select'
  3111. );
  3112. $this->assertTags($result, $expected);
  3113. $result = $this->Form->select('user_id', array('option A'), array('disabled' => true));
  3114. $expected = array(
  3115. 'select' => array(
  3116. 'name' => 'user_id',
  3117. 'disabled' => 'disabled'
  3118. ),
  3119. array('option' => array('value' => '0')), 'option A', '/option',
  3120. '/select'
  3121. );
  3122. $this->assertTags($result, $expected);
  3123. }
  3124. /**
  3125. * testNestedSelect method
  3126. *
  3127. * test select element generation with optgroups
  3128. *
  3129. * @return void
  3130. */
  3131. public function testNestedSelect() {
  3132. $result = $this->Form->select(
  3133. 'Model.field',
  3134. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3135. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3136. )), array('empty' => false)
  3137. );
  3138. $expected = array(
  3139. 'select' => array('name' => 'Model[field]'),
  3140. array('option' => array('value' => 1)),
  3141. 'One',
  3142. '/option',
  3143. array('option' => array('value' => 2)),
  3144. 'Two',
  3145. '/option',
  3146. array('optgroup' => array('label' => 'Three')),
  3147. array('option' => array('value' => 3)),
  3148. 'Three',
  3149. '/option',
  3150. array('option' => array('value' => 4)),
  3151. 'Four',
  3152. '/option',
  3153. array('option' => array('value' => 5)),
  3154. 'Five',
  3155. '/option',
  3156. '/optgroup',
  3157. '/select'
  3158. );
  3159. $this->assertTags($result, $expected);
  3160. }
  3161. /**
  3162. * testSelectMultiple method
  3163. *
  3164. * test generation of multiple select elements
  3165. *
  3166. * @return void
  3167. */
  3168. public function testSelectMultiple() {
  3169. $options = array('first', 'second', 'third');
  3170. $result = $this->Form->select(
  3171. 'Model.multi_field',
  3172. $options,
  3173. ['form' => 'my-form', 'multiple' => true]
  3174. );
  3175. $expected = array(
  3176. 'input' => array(
  3177. 'type' => 'hidden',
  3178. 'name' => 'Model[multi_field]',
  3179. 'value' => '',
  3180. 'form' => 'my-form',
  3181. ),
  3182. 'select' => array(
  3183. 'name' => 'Model[multi_field][]',
  3184. 'multiple' => 'multiple',
  3185. 'form' => 'my-form',
  3186. ),
  3187. array('option' => array('value' => '0')),
  3188. 'first',
  3189. '/option',
  3190. array('option' => array('value' => '1')),
  3191. 'second',
  3192. '/option',
  3193. array('option' => array('value' => '2')),
  3194. 'third',
  3195. '/option',
  3196. '/select'
  3197. );
  3198. $this->assertTags($result, $expected);
  3199. $result = $this->Form->select(
  3200. 'Model.multi_field',
  3201. $options,
  3202. ['multiple' => 'multiple', 'form' => 'my-form']
  3203. );
  3204. $this->assertTags($result, $expected);
  3205. }
  3206. /**
  3207. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  3208. *
  3209. * @return void
  3210. */
  3211. public function testCheckboxZeroValue() {
  3212. $result = $this->Form->input('User.get_spam', array(
  3213. 'type' => 'checkbox',
  3214. 'value' => '0',
  3215. 'hiddenField' => '1',
  3216. ));
  3217. $expected = array(
  3218. 'div' => array('class' => 'input checkbox'),
  3219. array('input' => array(
  3220. 'type' => 'hidden', 'name' => 'User[get_spam]',
  3221. 'value' => '1'
  3222. )),
  3223. array('input' => array(
  3224. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  3225. 'value' => '0', 'id' => 'user-get-spam'
  3226. )),
  3227. 'label' => array('for' => 'user-get-spam'),
  3228. 'Get Spam',
  3229. '/label',
  3230. '/div'
  3231. );
  3232. $this->assertTags($result, $expected);
  3233. }
  3234. /**
  3235. * test generation of habtm select boxes.
  3236. *
  3237. * @return void
  3238. */
  3239. public function testHabtmSelectBox() {
  3240. $this->loadFixtures('Article');
  3241. $options = array(
  3242. 1 => 'blue',
  3243. 2 => 'red',
  3244. 3 => 'green'
  3245. );
  3246. $tags = [
  3247. new Entity(['id' => 1, 'name' => 'blue']),
  3248. new Entity(['id' => 3, 'name' => 'green'])
  3249. ];
  3250. $article = new Article(['tags' => $tags]);
  3251. $this->Form->create($article);
  3252. $result = $this->Form->input('tags._ids', ['options' => $options]);
  3253. $expected = array(
  3254. 'div' => array('class' => 'input select'),
  3255. 'label' => array('for' => 'tags-ids'),
  3256. 'Tags',
  3257. '/label',
  3258. 'input' => array('type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''),
  3259. 'select' => array(
  3260. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  3261. 'multiple' => 'multiple'
  3262. ),
  3263. array('option' => array('value' => '1', 'selected' => 'selected')),
  3264. 'blue',
  3265. '/option',
  3266. array('option' => array('value' => '2')),
  3267. 'red',
  3268. '/option',
  3269. array('option' => array('value' => '3', 'selected' => 'selected')),
  3270. 'green',
  3271. '/option',
  3272. '/select',
  3273. '/div'
  3274. );
  3275. $this->assertTags($result, $expected);
  3276. // make sure only 50 is selected, and not 50f5c0cf
  3277. $options = array(
  3278. '1' => 'blue',
  3279. '50f5c0cf' => 'red',
  3280. '50' => 'green'
  3281. );
  3282. $tags = [
  3283. new Entity(['id' => 1, 'name' => 'blue']),
  3284. new Entity(['id' => 50, 'name' => 'green'])
  3285. ];
  3286. $article = new Article(['tags' => $tags]);
  3287. $this->Form->create($article);
  3288. $result = $this->Form->input('tags._ids', ['options' => $options]);
  3289. $expected = array(
  3290. 'div' => array('class' => 'input select'),
  3291. 'label' => array('for' => 'tags-ids'),
  3292. 'Tags',
  3293. '/label',
  3294. 'input' => array('type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''),
  3295. 'select' => array(
  3296. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  3297. 'multiple' => 'multiple'
  3298. ),
  3299. array('option' => array('value' => '1', 'selected' => 'selected')),
  3300. 'blue',
  3301. '/option',
  3302. array('option' => array('value' => '50f5c0cf')),
  3303. 'red',
  3304. '/option',
  3305. array('option' => array('value' => '50', 'selected' => 'selected')),
  3306. 'green',
  3307. '/option',
  3308. '/select',
  3309. '/div'
  3310. );
  3311. $this->assertTags($result, $expected);
  3312. }
  3313. /**
  3314. * test generation of multi select elements in checkbox format
  3315. *
  3316. * @return void
  3317. */
  3318. public function testSelectMultipleCheckboxes() {
  3319. $result = $this->Form->select(
  3320. 'Model.multi_field',
  3321. array('first', 'second', 'third'),
  3322. array('multiple' => 'checkbox')
  3323. );
  3324. $expected = array(
  3325. 'input' => array(
  3326. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3327. ),
  3328. array('div' => array('class' => 'checkbox')),
  3329. array('input' => array(
  3330. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3331. 'value' => '0', 'id' => 'model-multi-field-0'
  3332. )),
  3333. array('label' => array('for' => 'model-multi-field-0')),
  3334. 'first',
  3335. '/label',
  3336. '/div',
  3337. array('div' => array('class' => 'checkbox')),
  3338. array('input' => array(
  3339. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3340. 'value' => '1', 'id' => 'model-multi-field-1'
  3341. )),
  3342. array('label' => array('for' => 'model-multi-field-1')),
  3343. 'second',
  3344. '/label',
  3345. '/div',
  3346. array('div' => array('class' => 'checkbox')),
  3347. array('input' => array(
  3348. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3349. 'value' => '2', 'id' => 'model-multi-field-2'
  3350. )),
  3351. array('label' => array('for' => 'model-multi-field-2')),
  3352. 'third',
  3353. '/label',
  3354. '/div'
  3355. );
  3356. $this->assertTags($result, $expected);
  3357. $result = $this->Form->select(
  3358. 'Model.multi_field',
  3359. array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
  3360. array('multiple' => 'checkbox')
  3361. );
  3362. $expected = array(
  3363. 'input' => array(
  3364. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3365. ),
  3366. array('div' => array('class' => 'checkbox')),
  3367. array('input' => array(
  3368. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3369. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  3370. )),
  3371. array('label' => array('for' => 'model-multi-field-a+')),
  3372. 'first',
  3373. '/label',
  3374. '/div',
  3375. array('div' => array('class' => 'checkbox')),
  3376. array('input' => array(
  3377. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3378. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  3379. )),
  3380. array('label' => array('for' => 'model-multi-field-a++')),
  3381. 'second',
  3382. '/label',
  3383. '/div',
  3384. array('div' => array('class' => 'checkbox')),
  3385. array('input' => array(
  3386. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3387. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  3388. )),
  3389. array('label' => array('for' => 'model-multi-field-a+++')),
  3390. 'third',
  3391. '/label',
  3392. '/div'
  3393. );
  3394. $this->assertTags($result, $expected);
  3395. $result = $this->Form->select(
  3396. 'Model.multi_field',
  3397. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  3398. array('multiple' => 'checkbox')
  3399. );
  3400. $expected = array(
  3401. 'input' => array(
  3402. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3403. ),
  3404. array('div' => array('class' => 'checkbox')),
  3405. array('input' => array(
  3406. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3407. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  3408. )),
  3409. array('label' => array('for' => 'model-multi-field-a-b')),
  3410. 'first',
  3411. '/label',
  3412. '/div',
  3413. array('div' => array('class' => 'checkbox')),
  3414. array('input' => array(
  3415. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3416. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  3417. )),
  3418. array('label' => array('for' => 'model-multi-field-a-b1')),
  3419. 'second',
  3420. '/label',
  3421. '/div',
  3422. array('div' => array('class' => 'checkbox')),
  3423. array('input' => array(
  3424. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3425. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  3426. )),
  3427. array('label' => array('for' => 'model-multi-field-a-b2')),
  3428. 'third',
  3429. '/label',
  3430. '/div'
  3431. );
  3432. $this->assertTags($result, $expected);
  3433. }
  3434. /**
  3435. * Ensure that multiCheckbox reads from the request data.
  3436. *
  3437. * @return void
  3438. */
  3439. public function testSelectMultipleCheckboxRequestData() {
  3440. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3441. $result = $this->Form->select(
  3442. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3443. );
  3444. $expected = array(
  3445. 'input' => array(
  3446. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  3447. ),
  3448. array('div' => array('class' => 'checkbox')),
  3449. array('input' => array(
  3450. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  3451. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  3452. )),
  3453. array('label' => array('for' => 'model-tags-1', 'class' => 'selected')),
  3454. 'first',
  3455. '/label',
  3456. '/div',
  3457. array('div' => array('class' => 'checkbox')),
  3458. array('input' => array(
  3459. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  3460. 'value' => 'Array', 'id' => 'model-tags-array'
  3461. )),
  3462. array('label' => array('for' => 'model-tags-array')),
  3463. 'Array',
  3464. '/label',
  3465. '/div'
  3466. );
  3467. $this->assertTags($result, $expected);
  3468. }
  3469. /**
  3470. * Checks the security hash array generated for multiple-input checkbox elements
  3471. *
  3472. * @return void
  3473. */
  3474. public function testSelectMultipleCheckboxSecurity() {
  3475. $this->Form->request->params['_Token'] = 'testKey';
  3476. $this->assertEquals(array(), $this->Form->fields);
  3477. $result = $this->Form->select(
  3478. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3479. array('multiple' => 'checkbox')
  3480. );
  3481. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  3482. $result = $this->Form->secure($this->Form->fields);
  3483. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3484. $this->assertRegExp('/"' . $key . '"/', $result);
  3485. }
  3486. /**
  3487. * Multiple select elements should always be secured as they always participate
  3488. * in the POST data.
  3489. *
  3490. * @return void
  3491. */
  3492. public function testSelectMultipleSecureWithNoOptions() {
  3493. $this->assertEquals(array(), $this->Form->fields);
  3494. $this->Form->select(
  3495. 'Model.select',
  3496. array(),
  3497. array('multiple' => true)
  3498. );
  3499. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3500. }
  3501. /**
  3502. * When a select box has no options it should not be added to the fields list
  3503. * as it always fail post validation.
  3504. *
  3505. * @return void
  3506. */
  3507. public function testSelectNoSecureWithNoOptions() {
  3508. $this->Form->request->params['_Token'] = 'testkey';
  3509. $this->assertEquals([], $this->Form->fields);
  3510. $this->Form->select(
  3511. 'Model.select',
  3512. []
  3513. );
  3514. $this->assertEquals([], $this->Form->fields);
  3515. $this->Form->select(
  3516. 'Model.user_id',
  3517. [],
  3518. ['empty' => true]
  3519. );
  3520. $this->assertEquals(array('Model.user_id'), $this->Form->fields);
  3521. }
  3522. /**
  3523. * testInputMultipleCheckboxes method
  3524. *
  3525. * test input() resulting in multi select elements being generated.
  3526. *
  3527. * @return void
  3528. */
  3529. public function testInputMultipleCheckboxes() {
  3530. $result = $this->Form->input('Model.multi_field', array(
  3531. 'options' => array('first', 'second', 'third'),
  3532. 'multiple' => 'checkbox'
  3533. ));
  3534. $expected = array(
  3535. array('div' => array('class' => 'input select')),
  3536. array('label' => array('for' => 'model-multi-field')),
  3537. 'Multi Field',
  3538. '/label',
  3539. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3540. array('div' => array('class' => 'checkbox')),
  3541. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0')),
  3542. array('label' => array('for' => 'model-multi-field-0')),
  3543. 'first',
  3544. '/label',
  3545. '/div',
  3546. array('div' => array('class' => 'checkbox')),
  3547. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1')),
  3548. array('label' => array('for' => 'model-multi-field-1')),
  3549. 'second',
  3550. '/label',
  3551. '/div',
  3552. array('div' => array('class' => 'checkbox')),
  3553. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2')),
  3554. array('label' => array('for' => 'model-multi-field-2')),
  3555. 'third',
  3556. '/label',
  3557. '/div',
  3558. '/div'
  3559. );
  3560. $this->assertTags($result, $expected);
  3561. $result = $this->Form->input('Model.multi_field', array(
  3562. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3563. 'multiple' => 'checkbox'
  3564. ));
  3565. $expected = array(
  3566. array('div' => array('class' => 'input select')),
  3567. array('label' => array('for' => 'model-multi-field')),
  3568. 'Multi Field',
  3569. '/label',
  3570. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3571. array('div' => array('class' => 'checkbox')),
  3572. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a')),
  3573. array('label' => array('for' => 'model-multi-field-a')),
  3574. 'first',
  3575. '/label',
  3576. '/div',
  3577. array('div' => array('class' => 'checkbox')),
  3578. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b')),
  3579. array('label' => array('for' => 'model-multi-field-b')),
  3580. 'second',
  3581. '/label',
  3582. '/div',
  3583. array('div' => array('class' => 'checkbox')),
  3584. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c')),
  3585. array('label' => array('for' => 'model-multi-field-c')),
  3586. 'third',
  3587. '/label',
  3588. '/div',
  3589. '/div'
  3590. );
  3591. $this->assertTags($result, $expected);
  3592. }
  3593. /**
  3594. * testSelectHiddenFieldOmission method
  3595. *
  3596. * test that select() with 'hiddenField' => false omits the hidden field
  3597. *
  3598. * @return void
  3599. */
  3600. public function testSelectHiddenFieldOmission() {
  3601. $result = $this->Form->select('Model.multi_field',
  3602. array('first', 'second'),
  3603. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  3604. );
  3605. $this->assertNotContains('type="hidden"', $result);
  3606. }
  3607. /**
  3608. * test that select() with multiple = checkbox works with overriding name attribute.
  3609. *
  3610. * @return void
  3611. */
  3612. public function testSelectCheckboxMultipleOverrideName() {
  3613. $result = $this->Form->select('category', ['1', '2'], [
  3614. 'multiple' => 'checkbox',
  3615. 'name' => 'fish',
  3616. ]);
  3617. $expected = array(
  3618. 'input' => array('type' => 'hidden', 'name' => 'fish', 'value' => ''),
  3619. array('div' => array('class' => 'checkbox')),
  3620. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0')),
  3621. array('label' => array('for' => 'fish-0')), '1', '/label',
  3622. '/div',
  3623. array('div' => array('class' => 'checkbox')),
  3624. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1')),
  3625. array('label' => array('for' => 'fish-1')), '2', '/label',
  3626. '/div'
  3627. );
  3628. $this->assertTags($result, $expected);
  3629. $result = $this->Form->multiCheckbox(
  3630. 'category',
  3631. new Collection(['1', '2']),
  3632. ['name' => 'fish']
  3633. );
  3634. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  3635. 'name' => 'fish',
  3636. ]);
  3637. $this->assertTags($result, $expected);
  3638. }
  3639. /**
  3640. * testCheckbox method
  3641. *
  3642. * Test generation of checkboxes
  3643. *
  3644. * @return void
  3645. */
  3646. public function testCheckbox() {
  3647. $result = $this->Form->checkbox('Model.field');
  3648. $expected = array(
  3649. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  3650. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1'))
  3651. );
  3652. $this->assertTags($result, $expected);
  3653. $result = $this->Form->checkbox('Model.field', array(
  3654. 'id' => 'theID',
  3655. 'value' => 'myvalue',
  3656. 'form' => 'my-form',
  3657. ));
  3658. $expected = array(
  3659. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'),
  3660. array('input' => array(
  3661. 'type' => 'checkbox', 'name' => 'Model[field]',
  3662. 'value' => 'myvalue', 'id' => 'theID',
  3663. 'form' => 'my-form',
  3664. ))
  3665. );
  3666. $this->assertTags($result, $expected);
  3667. }
  3668. /**
  3669. * testCheckboxDefaultValue method
  3670. *
  3671. * Test default value setting on checkbox() method
  3672. *
  3673. * @return void
  3674. */
  3675. public function testCheckboxDefaultValue() {
  3676. $this->Form->request->data['Model']['field'] = false;
  3677. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3678. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1')));
  3679. unset($this->Form->request->data['Model']['field']);
  3680. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3681. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked')));
  3682. $this->Form->request->data['Model']['field'] = true;
  3683. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3684. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked')));
  3685. unset($this->Form->request->data['Model']['field']);
  3686. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3687. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1')));
  3688. }
  3689. /**
  3690. * Test checkbox being checked or having errors.
  3691. *
  3692. * @return void
  3693. */
  3694. public function testCheckboxCheckedAndError() {
  3695. $this->article['errors'] = [
  3696. 'published' => true
  3697. ];
  3698. $this->Form->request->data['published'] = 'myvalue';
  3699. $this->Form->create($this->article);
  3700. $result = $this->Form->checkbox('published', array('id' => 'theID', 'value' => 'myvalue'));
  3701. $expected = array(
  3702. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  3703. array('input' => array(
  3704. 'type' => 'checkbox',
  3705. 'name' => 'published',
  3706. 'value' => 'myvalue',
  3707. 'id' => 'theID',
  3708. 'checked' => 'checked',
  3709. 'class' => 'form-error'
  3710. ))
  3711. );
  3712. $this->assertTags($result, $expected);
  3713. $this->Form->request->data['published'] = '';
  3714. $result = $this->Form->checkbox('published');
  3715. $expected = array(
  3716. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  3717. array('input' => array('type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error'))
  3718. );
  3719. $this->assertTags($result, $expected);
  3720. }
  3721. /**
  3722. * test checkbox() with a custom name attribute
  3723. *
  3724. * @return void
  3725. */
  3726. public function testCheckboxCustomNameAttribute() {
  3727. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  3728. $expected = array(
  3729. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0'),
  3730. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1'))
  3731. );
  3732. $this->assertTags($result, $expected);
  3733. }
  3734. /**
  3735. * Test that the hidden input for checkboxes can be omitted or set to a
  3736. * specific value.
  3737. *
  3738. * @return void
  3739. */
  3740. public function testCheckboxHiddenField() {
  3741. $result = $this->Form->checkbox('UserForm.something', array(
  3742. 'hiddenField' => false
  3743. ));
  3744. $expected = array(
  3745. 'input' => array(
  3746. 'type' => 'checkbox',
  3747. 'name' => 'UserForm[something]',
  3748. 'value' => '1'
  3749. ),
  3750. );
  3751. $this->assertTags($result, $expected);
  3752. $result = $this->Form->checkbox('UserForm.something', array(
  3753. 'value' => 'Y',
  3754. 'hiddenField' => 'N',
  3755. ));
  3756. $expected = array(
  3757. array('input' => array(
  3758. 'type' => 'hidden', 'name' => 'UserForm[something]',
  3759. 'value' => 'N'
  3760. )),
  3761. array('input' => array(
  3762. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  3763. 'value' => 'Y'
  3764. )),
  3765. );
  3766. $this->assertTags($result, $expected);
  3767. }
  3768. /**
  3769. * Test the time type.
  3770. *
  3771. * @return void
  3772. */
  3773. public function testTime() {
  3774. $result = $this->Form->time('start_time', array(
  3775. 'timeFormat' => 12,
  3776. 'interval' => 5,
  3777. 'value' => array('hour' => '4', 'minute' => '30', 'meridian' => 'pm')
  3778. ));
  3779. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  3780. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  3781. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  3782. $this->assertNotContains('year', $result);
  3783. $this->assertNotContains('month', $result);
  3784. $this->assertNotContains('day', $result);
  3785. $result = $this->Form->time('start_time', array(
  3786. 'timeFormat' => 12,
  3787. 'interval' => 5,
  3788. 'value' => '2014-03-08 16:30:00'
  3789. ));
  3790. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  3791. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  3792. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  3793. $this->assertNotContains('year', $result);
  3794. $this->assertNotContains('month', $result);
  3795. $this->assertNotContains('day', $result);
  3796. }
  3797. /**
  3798. * Ensure that timeFormat=24 has no merdian.
  3799. *
  3800. * @return void.
  3801. */
  3802. public function testTimeFormat24NoMeridian() {
  3803. $result = $this->Form->time('start_time', array(
  3804. 'timeFormat' => 24,
  3805. 'interval' => 5,
  3806. 'value' => '2014-03-08 16:30:00'
  3807. ));
  3808. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  3809. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  3810. $this->assertNotContains('meridian', $result);
  3811. $this->assertNotContains('pm', $result);
  3812. $this->assertNotContains('year', $result);
  3813. $this->assertNotContains('month', $result);
  3814. $this->assertNotContains('day', $result);
  3815. }
  3816. /**
  3817. * Test the date type.
  3818. *
  3819. * @return void
  3820. */
  3821. public function testDate() {
  3822. $result = $this->Form->date('start_day', array(
  3823. 'value' => array('year' => '2014', 'month' => '03', 'day' => '08')
  3824. ));
  3825. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  3826. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  3827. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  3828. $this->assertNotContains('hour', $result);
  3829. $this->assertNotContains('minute', $result);
  3830. $this->assertNotContains('second', $result);
  3831. $this->assertNotContains('meridian', $result);
  3832. }
  3833. /**
  3834. * testDateTime method
  3835. *
  3836. * Test generation of date/time select elements
  3837. *
  3838. * @return void
  3839. */
  3840. public function testDateTime() {
  3841. extract($this->dateRegex);
  3842. $result = $this->Form->dateTime('Contact.date', array('empty' => false));
  3843. $now = strtotime('now');
  3844. $expected = array(
  3845. array('select' => array('name' => 'Contact[date][year]')),
  3846. $yearsRegex,
  3847. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  3848. date('Y', $now),
  3849. '/option',
  3850. '*/select',
  3851. array('select' => array('name' => 'Contact[date][month]')),
  3852. $monthsRegex,
  3853. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  3854. date('F', $now),
  3855. '/option',
  3856. '*/select',
  3857. array('select' => array('name' => 'Contact[date][day]')),
  3858. $daysRegex,
  3859. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  3860. date('j', $now),
  3861. '/option',
  3862. '*/select',
  3863. array('select' => array('name' => 'Contact[date][hour]')),
  3864. $hoursRegex,
  3865. array('option' => array('value' => date('H', $now), 'selected' => 'selected')),
  3866. date('G', $now),
  3867. '/option',
  3868. '*/select',
  3869. array('select' => array('name' => 'Contact[date][minute]')),
  3870. $minutesRegex,
  3871. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  3872. date('i', $now),
  3873. '/option',
  3874. '*/select',
  3875. );
  3876. $this->assertTags($result, $expected);
  3877. }
  3878. /**
  3879. * Test empty defaulting to true for datetime.
  3880. *
  3881. * @return void
  3882. */
  3883. public function testDatetimeEmpty() {
  3884. extract($this->dateRegex);
  3885. $now = strtotime('now');
  3886. $result = $this->Form->dateTime('Contact.date', array(
  3887. 'timeFormat' => 12,
  3888. 'empty' => true,
  3889. ));
  3890. $expected = array(
  3891. array('select' => array('name' => 'Contact[date][year]')),
  3892. $yearsRegex,
  3893. array('option' => array('value' => '')),
  3894. '/option',
  3895. '*/select',
  3896. array('select' => array('name' => 'Contact[date][month]')),
  3897. $monthsRegex,
  3898. array('option' => array('value' => '')),
  3899. '/option',
  3900. '*/select',
  3901. array('select' => array('name' => 'Contact[date][day]')),
  3902. $daysRegex,
  3903. array('option' => array('value' => '')),
  3904. '/option',
  3905. '*/select',
  3906. array('select' => array('name' => 'Contact[date][hour]')),
  3907. $hoursRegex,
  3908. array('option' => array('value' => '')),
  3909. '/option',
  3910. '*/select',
  3911. array('select' => array('name' => 'Contact[date][minute]')),
  3912. $minutesRegex,
  3913. array('option' => array('value' => '')),
  3914. '/option',
  3915. '*/select',
  3916. array('select' => array('name' => 'Contact[date][meridian]')),
  3917. $meridianRegex,
  3918. array('option' => array('value' => '')),
  3919. '/option',
  3920. '*/select'
  3921. );
  3922. $this->assertTags($result, $expected);
  3923. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  3924. }
  3925. /**
  3926. * Test datetime with interval option.
  3927. *
  3928. * @return void
  3929. */
  3930. public function testDatetimeMinuteInterval() {
  3931. extract($this->dateRegex);
  3932. $now = strtotime('now');
  3933. $result = $this->Form->dateTime('Contact.date', array(
  3934. 'interval' => 5,
  3935. 'value' => ''
  3936. ));
  3937. $expected = array(
  3938. array('select' => array('name' => 'Contact[date][year]')),
  3939. $yearsRegex,
  3940. array('option' => array('selected' => 'selected', 'value' => '')),
  3941. '/option',
  3942. '*/select',
  3943. array('select' => array('name' => 'Contact[date][month]')),
  3944. $monthsRegex,
  3945. array('option' => array('selected' => 'selected', 'value' => '')),
  3946. '/option',
  3947. '*/select',
  3948. array('select' => array('name' => 'Contact[date][day]')),
  3949. $daysRegex,
  3950. array('option' => array('selected' => 'selected', 'value' => '')),
  3951. '/option',
  3952. '*/select',
  3953. array('select' => array('name' => 'Contact[date][hour]')),
  3954. $hoursRegex,
  3955. array('option' => array('selected' => 'selected', 'value' => '')),
  3956. '/option',
  3957. '*/select',
  3958. array('select' => array('name' => 'Contact[date][minute]')),
  3959. $minutesRegex,
  3960. array('option' => array('selected' => 'selected', 'value' => '')),
  3961. '/option',
  3962. array('option' => array('value' => '00')),
  3963. '00',
  3964. '/option',
  3965. array('option' => array('value' => '05')),
  3966. '05',
  3967. '/option',
  3968. array('option' => array('value' => '10')),
  3969. '10',
  3970. '/option',
  3971. '*/select',
  3972. );
  3973. $this->assertTags($result, $expected);
  3974. }
  3975. /**
  3976. * Test dateTime with rounding
  3977. *
  3978. * @return void
  3979. */
  3980. public function testDateTimeRounding() {
  3981. $this->Form->request->data['Contact'] = array(
  3982. 'date' => array(
  3983. 'day' => '13',
  3984. 'month' => '12',
  3985. 'year' => '2010',
  3986. 'hour' => '04',
  3987. 'minute' => '19',
  3988. 'meridian' => 'AM'
  3989. )
  3990. );
  3991. $result = $this->Form->dateTime('Contact.date', array('interval' => 15));
  3992. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  3993. $result = $this->Form->dateTime('Contact.date', array('interval' => 15, 'round' => 'up'));
  3994. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  3995. $result = $this->Form->dateTime('Contact.date', array('interval' => 5, 'round' => 'down'));
  3996. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  3997. }
  3998. /**
  3999. * test that datetime() and default values work.
  4000. *
  4001. * @return void
  4002. */
  4003. public function testDatetimeWithDefault() {
  4004. $result = $this->Form->dateTime('Contact.updated', array('value' => '2009-06-01 11:15:30'));
  4005. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4006. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4007. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4008. $result = $this->Form->dateTime('Contact.updated', array(
  4009. 'default' => '2009-06-01 11:15:30'
  4010. ));
  4011. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4012. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4013. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4014. }
  4015. /**
  4016. * testDateTime all zeros
  4017. *
  4018. * @return void
  4019. */
  4020. public function testDateTimeAllZeros() {
  4021. $result = $this->Form->dateTime('Contact.date', array(
  4022. 'timeFormat' => false,
  4023. 'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
  4024. 'value' => '0000-00-00'
  4025. ));
  4026. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  4027. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  4028. }
  4029. /**
  4030. * testDateTimeEmptyAsArray
  4031. *
  4032. * @return void
  4033. */
  4034. public function testDateTimeEmptyAsArray() {
  4035. $result = $this->Form->dateTime('Contact.date', array(
  4036. 'empty' => array(
  4037. 'day' => 'DAY',
  4038. 'month' => 'MONTH',
  4039. 'year' => 'YEAR',
  4040. 'hour' => 'HOUR',
  4041. 'minute' => 'MINUTE',
  4042. 'meridian' => false
  4043. )
  4044. ));
  4045. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4046. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4047. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4048. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4049. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4050. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4051. $result = $this->Form->dateTime('Contact.date', array(
  4052. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4053. ));
  4054. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4055. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4056. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4057. }
  4058. /**
  4059. * testFormDateTimeMulti method
  4060. *
  4061. * test multiple datetime element generation
  4062. *
  4063. * @return void
  4064. */
  4065. public function testFormDateTimeMulti() {
  4066. extract($this->dateRegex);
  4067. $result = $this->Form->dateTime('Contact.1.updated');
  4068. $this->assertContains('Contact[1][updated][month]', $result);
  4069. $this->assertContains('Contact[1][updated][day]', $result);
  4070. $this->assertContains('Contact[1][updated][year]', $result);
  4071. $this->assertContains('Contact[1][updated][hour]', $result);
  4072. $this->assertContains('Contact[1][updated][minute]', $result);
  4073. }
  4074. /**
  4075. * When changing the date format, the label should always focus the first select box when
  4076. * clicked.
  4077. *
  4078. * @return void
  4079. */
  4080. public function testDateTimeLabelIdMatchesFirstInput() {
  4081. $this->markTestIncomplete('Need to revisit once models work again.');
  4082. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4083. $this->assertContains('label for="ModelDateMonth"', $result);
  4084. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4085. $this->assertContains('label for="ModelDateDay"', $result);
  4086. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4087. $this->assertContains('label for="ModelDateYear"', $result);
  4088. }
  4089. /**
  4090. * testMonth method
  4091. *
  4092. * @return void
  4093. */
  4094. public function testMonth() {
  4095. $result = $this->Form->month('Model.field', ['value' => '']);
  4096. $expected = array(
  4097. array('select' => array('name' => 'Model[field][month]')),
  4098. array('option' => array('value' => '', 'selected' => 'selected')),
  4099. '/option',
  4100. array('option' => array('value' => '01')),
  4101. date('F', strtotime('2008-01-01 00:00:00')),
  4102. '/option',
  4103. array('option' => array('value' => '02')),
  4104. date('F', strtotime('2008-02-01 00:00:00')),
  4105. '/option',
  4106. '*/select',
  4107. );
  4108. $this->assertTags($result, $expected);
  4109. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  4110. $expected = array(
  4111. array('select' => array('name' => 'Model[field][month]')),
  4112. array('option' => array('selected' => 'selected', 'value' => '')),
  4113. '/option',
  4114. array('option' => array('value' => '01')),
  4115. date('F', strtotime('2008-01-01 00:00:00')),
  4116. '/option',
  4117. array('option' => array('value' => '02')),
  4118. date('F', strtotime('2008-02-01 00:00:00')),
  4119. '/option',
  4120. '*/select',
  4121. );
  4122. $this->assertTags($result, $expected);
  4123. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  4124. $expected = array(
  4125. array('select' => array('name' => 'Model[field][month]')),
  4126. array('option' => array('selected' => 'selected', 'value' => '')),
  4127. '/option',
  4128. array('option' => array('value' => '01')),
  4129. '1',
  4130. '/option',
  4131. array('option' => array('value' => '02')),
  4132. '2',
  4133. '/option',
  4134. '*/select',
  4135. );
  4136. $this->assertTags($result, $expected);
  4137. $monthNames = [
  4138. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4139. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  4140. ];
  4141. $result = $this->Form->month('Model.field', array('value' => '1', 'monthNames' => $monthNames));
  4142. $expected = array(
  4143. array('select' => array('name' => 'Model[field][month]')),
  4144. array('option' => array('value' => '')),
  4145. '/option',
  4146. array('option' => array('value' => '01', 'selected' => 'selected')),
  4147. 'Jan',
  4148. '/option',
  4149. array('option' => array('value' => '02')),
  4150. 'Feb',
  4151. '/option',
  4152. '*/select',
  4153. );
  4154. $this->assertTags($result, $expected);
  4155. $this->Form->request->data['Project']['release'] = '2050-02-10';
  4156. $result = $this->Form->month('Project.release');
  4157. $expected = array(
  4158. array('select' => array('name' => 'Project[release][month]')),
  4159. array('option' => array('value' => '')),
  4160. '/option',
  4161. array('option' => array('value' => '01')),
  4162. 'January',
  4163. '/option',
  4164. array('option' => array('value' => '02', 'selected' => 'selected')),
  4165. 'February',
  4166. '/option',
  4167. '*/select',
  4168. );
  4169. $this->assertTags($result, $expected);
  4170. }
  4171. /**
  4172. * testDay method
  4173. *
  4174. * @return void
  4175. */
  4176. public function testDay() {
  4177. extract($this->dateRegex);
  4178. $result = $this->Form->day('Model.field', array('value' => false));
  4179. $expected = array(
  4180. array('select' => array('name' => 'Model[field][day]')),
  4181. array('option' => array('selected' => 'selected', 'value' => '')),
  4182. '/option',
  4183. array('option' => array('value' => '01')),
  4184. '1',
  4185. '/option',
  4186. array('option' => array('value' => '02')),
  4187. '2',
  4188. '/option',
  4189. $daysRegex,
  4190. '/select',
  4191. );
  4192. $this->assertTags($result, $expected);
  4193. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  4194. $result = $this->Form->day('Model.field');
  4195. $expected = array(
  4196. array('select' => array('name' => 'Model[field][day]')),
  4197. array('option' => array('value' => '')),
  4198. '/option',
  4199. array('option' => array('value' => '01')),
  4200. '1',
  4201. '/option',
  4202. array('option' => array('value' => '02')),
  4203. '2',
  4204. '/option',
  4205. $daysRegex,
  4206. array('option' => array('value' => '10', 'selected' => 'selected')),
  4207. '10',
  4208. '/option',
  4209. $daysRegex,
  4210. '/select',
  4211. );
  4212. $this->assertTags($result, $expected);
  4213. $this->Form->request->data['Model']['field'] = '';
  4214. $result = $this->Form->day('Model.field', array('value' => '10'));
  4215. $expected = array(
  4216. array('select' => array('name' => 'Model[field][day]')),
  4217. array('option' => array('value' => '')),
  4218. '/option',
  4219. array('option' => array('value' => '01')),
  4220. '1',
  4221. '/option',
  4222. array('option' => array('value' => '02')),
  4223. '2',
  4224. '/option',
  4225. $daysRegex,
  4226. array('option' => array('value' => '10', 'selected' => 'selected')),
  4227. '10',
  4228. '/option',
  4229. $daysRegex,
  4230. '/select',
  4231. );
  4232. $this->assertTags($result, $expected);
  4233. $this->Form->request->data['Project']['release'] = '2050-10-10';
  4234. $result = $this->Form->day('Project.release');
  4235. $expected = array(
  4236. array('select' => array('name' => 'Project[release][day]')),
  4237. array('option' => array('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. array('option' => array('value' => '10', 'selected' => 'selected')),
  4247. '10',
  4248. '/option',
  4249. $daysRegex,
  4250. '/select',
  4251. );
  4252. $this->assertTags($result, $expected);
  4253. }
  4254. /**
  4255. * testMinute method
  4256. *
  4257. * @return void
  4258. */
  4259. public function testMinute() {
  4260. extract($this->dateRegex);
  4261. $result = $this->Form->minute('Model.field', ['value' => '']);
  4262. $expected = array(
  4263. array('select' => array('name' => 'Model[field][minute]')),
  4264. array('option' => array('selected' => 'selected', 'value' => '')),
  4265. '/option',
  4266. array('option' => array('value' => '00')),
  4267. '00',
  4268. '/option',
  4269. array('option' => array('value' => '01')),
  4270. '01',
  4271. '/option',
  4272. array('option' => array('value' => '02')),
  4273. '02',
  4274. '/option',
  4275. $minutesRegex,
  4276. '/select',
  4277. );
  4278. $this->assertTags($result, $expected);
  4279. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4280. $result = $this->Form->minute('Model.field');
  4281. $expected = array(
  4282. array('select' => array('name' => 'Model[field][minute]')),
  4283. array('option' => array('value' => '')),
  4284. '/option',
  4285. array('option' => array('value' => '00')),
  4286. '00',
  4287. '/option',
  4288. array('option' => array('value' => '01')),
  4289. '01',
  4290. '/option',
  4291. array('option' => array('value' => '02')),
  4292. '02',
  4293. '/option',
  4294. $minutesRegex,
  4295. array('option' => array('value' => '12', 'selected' => 'selected')),
  4296. '12',
  4297. '/option',
  4298. $minutesRegex,
  4299. '/select',
  4300. );
  4301. $this->assertTags($result, $expected);
  4302. $this->Form->request->data['Model']['field'] = '';
  4303. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4304. $expected = array(
  4305. array('select' => array('name' => 'Model[field][minute]')),
  4306. array('option' => array('selected' => 'selected', 'value' => '')),
  4307. '/option',
  4308. array('option' => array('value' => '00')),
  4309. '00',
  4310. '/option',
  4311. array('option' => array('value' => '05')),
  4312. '05',
  4313. '/option',
  4314. array('option' => array('value' => '10')),
  4315. '10',
  4316. '/option',
  4317. $minutesRegex,
  4318. '/select',
  4319. );
  4320. $this->assertTags($result, $expected);
  4321. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  4322. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4323. $expected = array(
  4324. array('select' => array('name' => 'Model[field][minute]')),
  4325. array('option' => array('value' => '')),
  4326. '/option',
  4327. array('option' => array('value' => '00')),
  4328. '00',
  4329. '/option',
  4330. array('option' => array('value' => '05')),
  4331. '05',
  4332. '/option',
  4333. array('option' => array('value' => '10', 'selected' => 'selected')),
  4334. '10',
  4335. '/option',
  4336. $minutesRegex,
  4337. '/select',
  4338. );
  4339. $this->assertTags($result, $expected);
  4340. }
  4341. /**
  4342. * Test generating an input for the meridian.
  4343. *
  4344. * @return void
  4345. */
  4346. public function testMeridian() {
  4347. extract($this->dateRegex);
  4348. $now = time();
  4349. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  4350. $expected = [
  4351. array('select' => array('name' => 'Model[field][meridian]')),
  4352. array('option' => array('value' => '')),
  4353. '/option',
  4354. $meridianRegex,
  4355. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4356. date('a', $now),
  4357. '/option',
  4358. '*/select'
  4359. ];
  4360. $this->assertTags($result, $expected);
  4361. }
  4362. /**
  4363. * testHour method
  4364. *
  4365. * @return void
  4366. */
  4367. public function testHour() {
  4368. extract($this->dateRegex);
  4369. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  4370. $expected = array(
  4371. array('select' => array('name' => 'Model[field][hour]')),
  4372. array('option' => array('selected' => 'selected', 'value' => '')),
  4373. '/option',
  4374. array('option' => array('value' => '01')),
  4375. '1',
  4376. '/option',
  4377. array('option' => array('value' => '02')),
  4378. '2',
  4379. '/option',
  4380. $hoursRegex,
  4381. '/select',
  4382. );
  4383. $this->assertTags($result, $expected);
  4384. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4385. $result = $this->Form->hour('Model.field', ['format' => 12]);
  4386. $expected = array(
  4387. array('select' => array('name' => 'Model[field][hour]')),
  4388. array('option' => array('value' => '')),
  4389. '/option',
  4390. array('option' => array('value' => '01')),
  4391. '1',
  4392. '/option',
  4393. array('option' => array('value' => '02')),
  4394. '2',
  4395. '/option',
  4396. $hoursRegex,
  4397. array('option' => array('value' => '12', 'selected' => 'selected')),
  4398. '12',
  4399. '/option',
  4400. '/select',
  4401. );
  4402. $this->assertTags($result, $expected);
  4403. $this->Form->request->data['Model']['field'] = '';
  4404. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => '23'));
  4405. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  4406. $result = $this->Form->hour('Model.field', array('format' => 12, 'value' => '23'));
  4407. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  4408. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4409. $result = $this->Form->hour('Model.field', ['format' => 24]);
  4410. $expected = array(
  4411. array('select' => array('name' => 'Model[field][hour]')),
  4412. array('option' => array('value' => '')),
  4413. '/option',
  4414. array('option' => array('value' => '00', 'selected' => 'selected')),
  4415. '0',
  4416. '/option',
  4417. array('option' => array('value' => '01')),
  4418. '1',
  4419. '/option',
  4420. array('option' => array('value' => '02')),
  4421. '2',
  4422. '/option',
  4423. $hoursRegex,
  4424. '/select',
  4425. );
  4426. $this->assertTags($result, $expected);
  4427. unset($this->Form->request->data['Model']['field']);
  4428. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => 'now'));
  4429. $thisHour = date('H');
  4430. $optValue = date('G');
  4431. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  4432. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  4433. $result = $this->Form->hour('Model.field', ['format' => 24]);
  4434. $expected = array(
  4435. array('select' => array('name' => 'Model[field][hour]')),
  4436. array('option' => array('value' => '')),
  4437. '/option',
  4438. array('option' => array('value' => '00')),
  4439. '0',
  4440. '/option',
  4441. array('option' => array('value' => '01', 'selected' => 'selected')),
  4442. '1',
  4443. '/option',
  4444. array('option' => array('value' => '02')),
  4445. '2',
  4446. '/option',
  4447. $hoursRegex,
  4448. '/select',
  4449. );
  4450. $this->assertTags($result, $expected);
  4451. }
  4452. /**
  4453. * testYear method
  4454. *
  4455. * @return void
  4456. */
  4457. public function testYear() {
  4458. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  4459. $expected = array(
  4460. array('select' => array('name' => 'Model[field][year]')),
  4461. array('option' => array('selected' => 'selected', 'value' => '')),
  4462. '/option',
  4463. array('option' => array('value' => '2007')),
  4464. '2007',
  4465. '/option',
  4466. array('option' => array('value' => '2006')),
  4467. '2006',
  4468. '/option',
  4469. '/select',
  4470. );
  4471. $this->assertTags($result, $expected);
  4472. $result = $this->Form->year('Model.field', [
  4473. 'value' => '',
  4474. 'minYear' => 2006,
  4475. 'maxYear' => 2007,
  4476. 'orderYear' => 'asc'
  4477. ]);
  4478. $expected = array(
  4479. array('select' => array('name' => 'Model[field][year]')),
  4480. array('option' => array('selected' => 'selected', 'value' => '')),
  4481. '/option',
  4482. array('option' => array('value' => '2006')),
  4483. '2006',
  4484. '/option',
  4485. array('option' => array('value' => '2007')),
  4486. '2007',
  4487. '/option',
  4488. '/select',
  4489. );
  4490. $this->assertTags($result, $expected);
  4491. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  4492. $result = $this->Form->year('Contact.published', [
  4493. 'empty' => false,
  4494. 'minYear' => 2006,
  4495. 'maxYear' => 2007,
  4496. ]);
  4497. $expected = array(
  4498. array('select' => array('name' => 'Contact[published][year]')),
  4499. array('option' => array('value' => '2007')),
  4500. '2007',
  4501. '/option',
  4502. array('option' => array('value' => '2006', 'selected' => 'selected')),
  4503. '2006',
  4504. '/option',
  4505. '/select',
  4506. );
  4507. $this->assertTags($result, $expected);
  4508. }
  4509. /**
  4510. * testYearAutoExpandRange method
  4511. *
  4512. * @return void
  4513. */
  4514. public function testYearAutoExpandRange() {
  4515. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  4516. $result = $this->Form->year('User.birthday');
  4517. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4518. $result = $matches[1];
  4519. $expected = range(date('Y') + 5, 1930);
  4520. $this->assertEquals($expected, $result);
  4521. $this->Form->request->data['Project']['release'] = '2050-10-10';
  4522. $result = $this->Form->year('Project.release');
  4523. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4524. $result = $matches[1];
  4525. $expected = range(2050, date('Y') - 5);
  4526. $this->assertEquals($expected, $result);
  4527. $this->Form->request->data['Project']['release'] = '1881-10-10';
  4528. $result = $this->Form->year('Project.release', [
  4529. 'minYear' => 1890,
  4530. 'maxYear' => 1900
  4531. ]);
  4532. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4533. $result = $matches[1];
  4534. $expected = range(1900, 1881);
  4535. $this->assertEquals($expected, $result);
  4536. }
  4537. /**
  4538. * Test that input() accepts the type of date and passes options in.
  4539. *
  4540. * @return void
  4541. */
  4542. public function testInputDate() {
  4543. $this->Form->request->data = array(
  4544. 'month_year' => array('month' => date('m')),
  4545. );
  4546. $this->Form->create($this->article);
  4547. $result = $this->Form->input('month_year', array(
  4548. 'label' => false,
  4549. 'type' => 'date',
  4550. 'minYear' => 2006,
  4551. 'maxYear' => 2008
  4552. ));
  4553. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  4554. $this->assertNotContains('value="2008" selected="selected"', $result);
  4555. }
  4556. /**
  4557. * testInputDateMaxYear method
  4558. *
  4559. * Let's say we want to only allow users born from 2006 to 2008 to register
  4560. * This being the first singup page, we still don't have any data
  4561. *
  4562. * @return void
  4563. */
  4564. public function testInputDateMaxYear() {
  4565. $this->Form->request->data = [];
  4566. $this->Form->create($this->article);
  4567. $result = $this->Form->input('birthday', array(
  4568. 'label' => false,
  4569. 'type' => 'date',
  4570. 'minYear' => 2006,
  4571. 'maxYear' => 2008
  4572. ));
  4573. $this->assertContains('value="2008" selected="selected"', $result);
  4574. }
  4575. /**
  4576. * testTextArea method
  4577. *
  4578. * @return void
  4579. */
  4580. public function testTextArea() {
  4581. $this->Form->request->data = array('field' => 'some test data');
  4582. $result = $this->Form->textarea('field');
  4583. $expected = array(
  4584. 'textarea' => array('name' => 'field'),
  4585. 'some test data',
  4586. '/textarea',
  4587. );
  4588. $this->assertTags($result, $expected);
  4589. $result = $this->Form->textarea('user.bio');
  4590. $expected = array(
  4591. 'textarea' => array('name' => 'user[bio]'),
  4592. '/textarea',
  4593. );
  4594. $this->assertTags($result, $expected);
  4595. $this->Form->request->data = array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars');
  4596. $result = $this->Form->textarea('field');
  4597. $expected = array(
  4598. 'textarea' => array('name' => 'field'),
  4599. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  4600. '/textarea',
  4601. );
  4602. $this->assertTags($result, $expected);
  4603. $this->Form->request->data = [
  4604. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  4605. ];
  4606. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  4607. $expected = array(
  4608. 'textarea' => array('name' => 'Model[field]'),
  4609. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  4610. '/textarea',
  4611. );
  4612. $this->assertTags($result, $expected);
  4613. $result = $this->Form->textarea('0.OtherModel.field');
  4614. $expected = array(
  4615. 'textarea' => array('name' => '0[OtherModel][field]'),
  4616. '/textarea'
  4617. );
  4618. $this->assertTags($result, $expected);
  4619. }
  4620. /**
  4621. * testTextAreaWithStupidCharacters method
  4622. *
  4623. * test text area with non-ascii characters
  4624. *
  4625. * @return void
  4626. */
  4627. public function testTextAreaWithStupidCharacters() {
  4628. $result = $this->Form->textarea('Post.content', [
  4629. 'value' => "GREAT®",
  4630. 'rows' => '15',
  4631. 'cols' => '75'
  4632. ]);
  4633. $expected = [
  4634. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  4635. 'GREAT®',
  4636. '/textarea',
  4637. ];
  4638. $this->assertTags($result, $expected);
  4639. }
  4640. /**
  4641. * testHiddenField method
  4642. *
  4643. * @return void
  4644. */
  4645. public function testHiddenField() {
  4646. $this->article['errors'] = [
  4647. 'field' => true
  4648. ];
  4649. $this->Form->request->data['field'] = 'test';
  4650. $this->Form->create($this->article);
  4651. $result = $this->Form->hidden('field', array('id' => 'theID'));
  4652. $this->assertTags($result, array(
  4653. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test'))
  4654. );
  4655. $result = $this->Form->hidden('field', ['value' => 'my value']);
  4656. $expected = [
  4657. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  4658. ];
  4659. $this->assertTags($result, $expected);
  4660. }
  4661. /**
  4662. * testFileUploadField method
  4663. *
  4664. * @return void
  4665. */
  4666. public function testFileUploadField() {
  4667. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  4668. $result = $this->Form->file('Model.upload');
  4669. $this->assertTags($result, $expected);
  4670. $this->Form->request->data['Model']['upload'] = [
  4671. 'name' => '', 'type' => '', 'tmp_name' => '',
  4672. 'error' => 4, 'size' => 0
  4673. ];
  4674. $result = $this->Form->file('Model.upload');
  4675. $this->assertTags($result, $expected);
  4676. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  4677. $result = $this->Form->file('Model.upload');
  4678. $this->assertTags($result, $expected);
  4679. }
  4680. /**
  4681. * test File upload input on a model not used in create();
  4682. *
  4683. * @return void
  4684. */
  4685. public function testFileUploadOnOtherModel() {
  4686. $this->Form->create($this->article, array('type' => 'file'));
  4687. $result = $this->Form->file('ValidateProfile.city');
  4688. $expected = array(
  4689. 'input' => array('type' => 'file', 'name' => 'ValidateProfile[city]')
  4690. );
  4691. $this->assertTags($result, $expected);
  4692. }
  4693. /**
  4694. * testButton method
  4695. *
  4696. * @return void
  4697. */
  4698. public function testButton() {
  4699. $result = $this->Form->button('Hi');
  4700. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  4701. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  4702. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  4703. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  4704. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  4705. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  4706. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  4707. $result = $this->Form->button('No type', array('type' => false));
  4708. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  4709. $result = $this->Form->button('Upload Text', array(
  4710. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  4711. 'escape' => false
  4712. ));
  4713. $this->assertNotRegExp('/\&039/', $result);
  4714. }
  4715. /**
  4716. * Test that button() makes unlocked fields by default.
  4717. *
  4718. * @return void
  4719. */
  4720. public function testButtonUnlockedByDefault() {
  4721. $this->Form->request->params['_csrfToken'] = 'secured';
  4722. $this->Form->button('Save', array('name' => 'save'));
  4723. $this->Form->button('Clear');
  4724. $result = $this->Form->unlockField();
  4725. $this->assertEquals(array('save'), $result);
  4726. }
  4727. /**
  4728. * testPostButton method
  4729. *
  4730. * @return void
  4731. */
  4732. public function testPostButton() {
  4733. $result = $this->Form->postButton('Hi', '/controller/action');
  4734. $this->assertTags($result, array(
  4735. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  4736. 'div' => array('style' => 'display:none;'),
  4737. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4738. '/div',
  4739. 'button' => array('type' => 'submit'),
  4740. 'Hi',
  4741. '/button',
  4742. '/form'
  4743. ));
  4744. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  4745. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  4746. }
  4747. /**
  4748. * Test using postButton with N dimensional data.
  4749. *
  4750. * @return void
  4751. */
  4752. public function testPostButtonNestedData() {
  4753. $data = array(
  4754. 'one' => array(
  4755. 'two' => array(
  4756. 3, 4, 5
  4757. )
  4758. )
  4759. );
  4760. $result = $this->Form->postButton('Send', '/', array('data' => $data));
  4761. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  4762. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  4763. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  4764. }
  4765. /**
  4766. * Test that postButton adds _Token fields.
  4767. *
  4768. * @return void
  4769. */
  4770. public function testSecurePostButton() {
  4771. $this->Form->request->params['_csrfToken'] = 'testkey';
  4772. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  4773. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  4774. $expected = array(
  4775. 'form' => array(
  4776. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  4777. ),
  4778. array('div' => array('style' => 'display:none;')),
  4779. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  4780. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  4781. '/div',
  4782. 'button' => array('type' => 'submit'),
  4783. 'Delete',
  4784. '/button',
  4785. array('div' => array('style' => 'display:none;')),
  4786. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  4787. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  4788. '/div',
  4789. '/form',
  4790. );
  4791. $this->assertTags($result, $expected);
  4792. }
  4793. /**
  4794. * testPostLink method
  4795. *
  4796. * @return void
  4797. */
  4798. public function testPostLink() {
  4799. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  4800. $this->assertTags($result, array(
  4801. 'form' => array(
  4802. 'method' => 'post', 'action' => '/posts/delete/1',
  4803. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4804. ),
  4805. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4806. '/form',
  4807. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4808. 'Delete',
  4809. '/a'
  4810. ));
  4811. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  4812. $this->assertTags($result, array(
  4813. 'form' => array(
  4814. 'method' => 'post', 'action' => '/posts/delete/1',
  4815. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4816. ),
  4817. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  4818. '/form',
  4819. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4820. 'Delete',
  4821. '/a'
  4822. ));
  4823. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  4824. $this->assertTags($result, array(
  4825. 'form' => array(
  4826. 'method' => 'post', 'action' => '/posts/delete/1',
  4827. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4828. ),
  4829. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4830. '/form',
  4831. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  4832. 'Delete',
  4833. '/a'
  4834. ));
  4835. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  4836. $this->assertTags($result, array(
  4837. 'form' => array(
  4838. 'method' => 'post', 'action' => '/posts/delete/1',
  4839. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4840. ),
  4841. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4842. '/form',
  4843. '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;/'),
  4844. 'Delete',
  4845. '/a'
  4846. ));
  4847. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  4848. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  4849. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  4850. $this->assertTags($result, array(
  4851. 'form' => array(
  4852. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  4853. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4854. ),
  4855. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4856. '/form',
  4857. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4858. 'Delete',
  4859. '/a'
  4860. ));
  4861. $result = $this->Form->postLink(
  4862. '',
  4863. array('controller' => 'items', 'action' => 'delete', 10),
  4864. array('class' => 'btn btn-danger', 'escape' => false),
  4865. 'Confirm thing'
  4866. );
  4867. $this->assertTags($result, array(
  4868. 'form' => array(
  4869. 'method' => 'post', 'action' => '/items/delete/10',
  4870. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4871. ),
  4872. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4873. '/form',
  4874. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  4875. '/a'
  4876. ));
  4877. }
  4878. /**
  4879. * Test using postLink with N dimensional data.
  4880. *
  4881. * @return void
  4882. */
  4883. public function testPostLinkNestedData() {
  4884. $data = array(
  4885. 'one' => array(
  4886. 'two' => array(
  4887. 3, 4, 5
  4888. )
  4889. )
  4890. );
  4891. $result = $this->Form->postLink('Send', '/', array('data' => $data));
  4892. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  4893. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  4894. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  4895. }
  4896. /**
  4897. * test creating postLinks after a GET form.
  4898. *
  4899. * @return void
  4900. */
  4901. public function testPostLinkAfterGetForm() {
  4902. $this->Form->request->params['_csrfToken'] = 'testkey';
  4903. $this->Form->request->params['_Token'] = 'val';
  4904. $this->Form->create($this->article, array('type' => 'get'));
  4905. $this->Form->end();
  4906. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  4907. $this->assertTags($result, array(
  4908. 'form' => array(
  4909. 'method' => 'post', 'action' => '/posts/delete/1',
  4910. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4911. ),
  4912. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  4913. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  4914. 'div' => array('style' => 'display:none;'),
  4915. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  4916. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  4917. '/div',
  4918. '/form',
  4919. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4920. 'Delete',
  4921. '/a'
  4922. ));
  4923. }
  4924. /**
  4925. * Test that postLink adds form tags to view block
  4926. *
  4927. * @return void
  4928. */
  4929. public function testPostLinkFormBuffer() {
  4930. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => true));
  4931. $this->assertTags($result, array(
  4932. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4933. 'Delete',
  4934. '/a'
  4935. ));
  4936. $result = $this->View->fetch('postLink');
  4937. $this->assertTags($result, array(
  4938. 'form' => array(
  4939. 'method' => 'post', 'action' => '/posts/delete/1',
  4940. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4941. ),
  4942. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4943. '/form'
  4944. ));
  4945. $result = $this->Form->postLink('Delete', '/posts/delete/2',
  4946. array('block' => true, 'method' => 'DELETE')
  4947. );
  4948. $this->assertTags($result, array(
  4949. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4950. 'Delete',
  4951. '/a'
  4952. ));
  4953. $result = $this->View->fetch('postLink');
  4954. $this->assertTags($result, array(
  4955. 'form' => array(
  4956. 'method' => 'post', 'action' => '/posts/delete/1',
  4957. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4958. ),
  4959. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4960. '/form',
  4961. array(
  4962. 'form' => array(
  4963. 'method' => 'post', 'action' => '/posts/delete/2',
  4964. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4965. ),
  4966. ),
  4967. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
  4968. '/form'
  4969. ));
  4970. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
  4971. $this->assertTags($result, array(
  4972. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4973. 'Delete',
  4974. '/a'
  4975. ));
  4976. $result = $this->View->fetch('foobar');
  4977. $this->assertTags($result, array(
  4978. 'form' => array(
  4979. 'method' => 'post', 'action' => '/posts/delete/1',
  4980. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4981. ),
  4982. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4983. '/form'
  4984. ));
  4985. }
  4986. /**
  4987. * testSubmitButton method
  4988. *
  4989. * @return void
  4990. */
  4991. public function testSubmitButton() {
  4992. $result = $this->Form->submit('');
  4993. $expected = array(
  4994. 'div' => array('class' => 'submit'),
  4995. 'input' => array('type' => 'submit', 'value' => ''),
  4996. '/div'
  4997. );
  4998. $this->assertTags($result, $expected);
  4999. $result = $this->Form->submit('Test Submit');
  5000. $expected = array(
  5001. 'div' => array('class' => 'submit'),
  5002. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5003. '/div'
  5004. );
  5005. $this->assertTags($result, $expected);
  5006. $result = $this->Form->submit('Next >');
  5007. $expected = array(
  5008. 'div' => array('class' => 'submit'),
  5009. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5010. '/div'
  5011. );
  5012. $this->assertTags($result, $expected);
  5013. $result = $this->Form->submit('Next >', array('escape' => false));
  5014. $expected = array(
  5015. 'div' => array('class' => 'submit'),
  5016. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5017. '/div'
  5018. );
  5019. $this->assertTags($result, $expected);
  5020. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5021. $expected = array(
  5022. 'div' => array('class' => 'submit'),
  5023. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5024. '/div'
  5025. );
  5026. $this->assertTags($result, $expected);
  5027. }
  5028. /**
  5029. * test image submit types.
  5030. *
  5031. * @return void
  5032. */
  5033. public function testSubmitImage() {
  5034. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5035. $expected = array(
  5036. 'div' => array('class' => 'submit'),
  5037. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5038. '/div'
  5039. );
  5040. $this->assertTags($result, $expected);
  5041. $result = $this->Form->submit('/relative/cake.power.gif');
  5042. $expected = array(
  5043. 'div' => array('class' => 'submit'),
  5044. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5045. '/div'
  5046. );
  5047. $this->assertTags($result, $expected);
  5048. $result = $this->Form->submit('cake.power.gif');
  5049. $expected = array(
  5050. 'div' => array('class' => 'submit'),
  5051. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5052. '/div'
  5053. );
  5054. $this->assertTags($result, $expected);
  5055. $result = $this->Form->submit('Not.an.image');
  5056. $expected = array(
  5057. 'div' => array('class' => 'submit'),
  5058. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5059. '/div'
  5060. );
  5061. $this->assertTags($result, $expected);
  5062. }
  5063. /**
  5064. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5065. * be submitted at a time.
  5066. *
  5067. * @return void
  5068. */
  5069. public function testSubmitUnlockedByDefault() {
  5070. $this->Form->request->params['_Token'] = 'secured';
  5071. $this->Form->submit('Go go');
  5072. $this->Form->submit('Save', array('name' => 'save'));
  5073. $result = $this->Form->unlockField();
  5074. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5075. }
  5076. /**
  5077. * Test submit image with timestamps.
  5078. *
  5079. * @return void
  5080. */
  5081. public function testSubmitImageTimestamp() {
  5082. Configure::write('Asset.timestamp', 'force');
  5083. $result = $this->Form->submit('cake.power.gif');
  5084. $expected = array(
  5085. 'div' => array('class' => 'submit'),
  5086. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5087. '/div'
  5088. );
  5089. $this->assertTags($result, $expected);
  5090. }
  5091. /**
  5092. * test that datetime() works with GET style forms.
  5093. *
  5094. * @return void
  5095. */
  5096. public function testDateTimeWithGetForms() {
  5097. extract($this->dateRegex);
  5098. $this->Form->create($this->article, array('type' => 'get'));
  5099. $result = $this->Form->datetime('created');
  5100. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  5101. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  5102. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  5103. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  5104. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  5105. }
  5106. /**
  5107. * testForMagicInputNonExistingNorValidated method
  5108. *
  5109. * @return void
  5110. */
  5111. public function testForMagicInputNonExistingNorValidated() {
  5112. $result = $this->Form->create($this->article);
  5113. $this->Form->templates(['groupContainer' => '{{content}}']);
  5114. $result = $this->Form->input('non_existing_nor_validated');
  5115. $expected = array(
  5116. 'label' => array('for' => 'non-existing-nor-validated'),
  5117. 'Non Existing Nor Validated',
  5118. '/label',
  5119. 'input' => array(
  5120. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  5121. 'id' => 'non-existing-nor-validated'
  5122. )
  5123. );
  5124. $this->assertTags($result, $expected);
  5125. $result = $this->Form->input('non_existing_nor_validated', array(
  5126. 'val' => 'my value'
  5127. ));
  5128. $expected = array(
  5129. 'label' => array('for' => 'non-existing-nor-validated'),
  5130. 'Non Existing Nor Validated',
  5131. '/label',
  5132. 'input' => array(
  5133. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  5134. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  5135. )
  5136. );
  5137. $this->assertTags($result, $expected);
  5138. $this->Form->request->data = array('non_existing_nor_validated' => 'CakePHP magic');
  5139. $result = $this->Form->input('non_existing_nor_validated');
  5140. $expected = array(
  5141. 'label' => array('for' => 'non-existing-nor-validated'),
  5142. 'Non Existing Nor Validated',
  5143. '/label',
  5144. 'input' => array(
  5145. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  5146. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  5147. )
  5148. );
  5149. $this->assertTags($result, $expected);
  5150. }
  5151. /**
  5152. * testFormMagicInputLabel method
  5153. *
  5154. * @return void
  5155. */
  5156. public function testFormMagicInputLabel() {
  5157. TableRegistry::get('Contacts', [
  5158. 'className' => __NAMESPACE__ . '\ContactsTable'
  5159. ]);
  5160. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  5161. $this->Form->templates(['groupContainer' => '{{content}}']);
  5162. $result = $this->Form->input('Contacts.name', array('label' => 'My label'));
  5163. $expected = array(
  5164. 'label' => array('for' => 'contacts-name'),
  5165. 'My label',
  5166. '/label',
  5167. 'input' => array(
  5168. 'type' => 'text', 'name' => 'Contacts[name]',
  5169. 'id' => 'contacts-name', 'maxlength' => '255'
  5170. )
  5171. );
  5172. $this->assertTags($result, $expected);
  5173. $result = $this->Form->input('name', array(
  5174. 'label' => array('class' => 'mandatory')
  5175. ));
  5176. $expected = array(
  5177. 'label' => array('for' => 'name', 'class' => 'mandatory'),
  5178. 'Name',
  5179. '/label',
  5180. 'input' => array(
  5181. 'type' => 'text', 'name' => 'name',
  5182. 'id' => 'name', 'maxlength' => '255'
  5183. )
  5184. );
  5185. $this->assertTags($result, $expected);
  5186. $result = $this->Form->input('name', array(
  5187. 'div' => false,
  5188. 'label' => array('class' => 'mandatory', 'text' => 'My label')
  5189. ));
  5190. $expected = array(
  5191. 'label' => array('for' => 'name', 'class' => 'mandatory'),
  5192. 'My label',
  5193. '/label',
  5194. 'input' => array(
  5195. 'type' => 'text', 'name' => 'name',
  5196. 'id' => 'name', 'maxlength' => '255'
  5197. )
  5198. );
  5199. $this->assertTags($result, $expected);
  5200. $result = $this->Form->input('Contact.name', array(
  5201. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  5202. ));
  5203. $expected = array(
  5204. 'label' => array('for' => 'my_id'),
  5205. 'Name',
  5206. '/label',
  5207. 'input' => array(
  5208. 'type' => 'text', 'name' => 'Contact[name]',
  5209. 'id' => 'my_id', 'maxlength' => '255'
  5210. )
  5211. );
  5212. $this->assertTags($result, $expected);
  5213. $result = $this->Form->input('1.id');
  5214. $this->assertTags($result, array('input' => array(
  5215. 'type' => 'hidden', 'name' => '1[id]',
  5216. 'id' => '1-id'
  5217. )));
  5218. $result = $this->Form->input("1.name");
  5219. $expected = array(
  5220. 'label' => array('for' => '1-name'),
  5221. 'Name',
  5222. '/label',
  5223. 'input' => array(
  5224. 'type' => 'text', 'name' => '1[name]',
  5225. 'id' => '1-name', 'maxlength' => '255'
  5226. )
  5227. );
  5228. $this->assertTags($result, $expected);
  5229. }
  5230. /**
  5231. * testFormEnd method
  5232. *
  5233. * @return void
  5234. */
  5235. public function testFormEnd() {
  5236. $this->assertEquals('</form>', $this->Form->end());
  5237. }
  5238. /**
  5239. * Test the generation of fields for a multi record form.
  5240. *
  5241. * @return void
  5242. */
  5243. public function testMultiRecordForm() {
  5244. $this->loadFixtures('Article', 'Comment');
  5245. $articles = TableRegistry::get('Articles');
  5246. $articles->hasMany('Comments');
  5247. $comment = new Entity(['comment' => 'Value']);
  5248. $article = new Article(['comments' => [$comment]]);
  5249. $this->Form->create([$article]);
  5250. $result = $this->Form->input('0.comments.1.comment');
  5251. $expected = array(
  5252. 'div' => array('class' => 'input textarea'),
  5253. 'label' => array('for' => '0-comments-1-comment'),
  5254. 'Comment',
  5255. '/label',
  5256. 'textarea' => array(
  5257. 'name',
  5258. 'type',
  5259. 'id' => '0-comments-1-comment',
  5260. ),
  5261. '/textarea',
  5262. '/div'
  5263. );
  5264. $this->assertTags($result, $expected);
  5265. $result = $this->Form->input('0.comments.0.comment');
  5266. $expected = array(
  5267. 'div' => array('class' => 'input textarea'),
  5268. 'label' => array('for' => '0-comments-0-comment'),
  5269. 'Comment',
  5270. '/label',
  5271. 'textarea' => array(
  5272. 'name',
  5273. 'type',
  5274. 'id' => '0-comments-0-comment'
  5275. ),
  5276. 'Value',
  5277. '/textarea',
  5278. '/div'
  5279. );
  5280. $this->assertTags($result, $expected);
  5281. $comment->errors('comment', ['Not valid']);
  5282. $result = $this->Form->input('0.comments.0.comment');
  5283. $expected = array(
  5284. 'div' => array('class' => 'input textarea error'),
  5285. 'label' => array('for' => '0-comments-0-comment'),
  5286. 'Comment',
  5287. '/label',
  5288. 'textarea' => array(
  5289. 'name',
  5290. 'type',
  5291. 'class' => 'form-error',
  5292. 'id' => '0-comments-0-comment'
  5293. ),
  5294. 'Value',
  5295. '/textarea',
  5296. array('div' => array('class' => 'error-message')),
  5297. 'Not valid',
  5298. '/div',
  5299. '/div'
  5300. );
  5301. $this->assertTags($result, $expected);
  5302. TableRegistry::get('Comments')
  5303. ->validator('default')
  5304. ->allowEmpty('comment', false);
  5305. $result = $this->Form->input('0.comments.1.comment');
  5306. $expected = array(
  5307. 'div' => array('class' => 'input textarea required'),
  5308. 'label' => array('for' => '0-comments-1-comment'),
  5309. 'Comment',
  5310. '/label',
  5311. 'textarea' => array(
  5312. 'name',
  5313. 'type',
  5314. 'required' => 'required',
  5315. 'id' => '0-comments-1-comment'
  5316. ),
  5317. '/textarea',
  5318. '/div'
  5319. );
  5320. $this->assertTags($result, $expected);
  5321. }
  5322. /**
  5323. * test that some html5 inputs + FormHelper::__call() work
  5324. *
  5325. * @return void
  5326. */
  5327. public function testHtml5Inputs() {
  5328. $result = $this->Form->email('User.email');
  5329. $expected = array(
  5330. 'input' => array('type' => 'email', 'name' => 'User[email]')
  5331. );
  5332. $this->assertTags($result, $expected);
  5333. $result = $this->Form->search('User.query');
  5334. $expected = array(
  5335. 'input' => array('type' => 'search', 'name' => 'User[query]')
  5336. );
  5337. $this->assertTags($result, $expected);
  5338. $result = $this->Form->search('User.query', array('value' => 'test'));
  5339. $expected = array(
  5340. 'input' => array('type' => 'search', 'name' => 'User[query]', 'value' => 'test')
  5341. );
  5342. $this->assertTags($result, $expected);
  5343. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  5344. $expected = array(
  5345. 'input' => array('type' => 'text', 'name' => 'User[query]', 'value' => 'test')
  5346. );
  5347. $this->assertTags($result, $expected);
  5348. }
  5349. /**
  5350. * Test accessing html5 inputs through input().
  5351. *
  5352. * @return void
  5353. */
  5354. public function testHtml5InputWithInput() {
  5355. $this->Form->create();
  5356. $this->Form->templates(['groupContainer' => '{{content}}']);
  5357. $result = $this->Form->input('website', array(
  5358. 'type' => 'url',
  5359. 'val' => 'http://domain.tld',
  5360. 'label' => false
  5361. ));
  5362. $expected = array(
  5363. 'input' => array('type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld')
  5364. );
  5365. $this->assertTags($result, $expected);
  5366. }
  5367. /**
  5368. * Test errors when field name is missing.
  5369. *
  5370. * @expectedException \Cake\Error\Exception
  5371. * @return void
  5372. */
  5373. public function testHtml5InputException() {
  5374. $this->Form->email();
  5375. }
  5376. /**
  5377. * Tests that formhelper sets required attributes.
  5378. *
  5379. * @return void
  5380. */
  5381. public function testRequiredAttribute() {
  5382. $this->article['required'] = [
  5383. 'title' => true,
  5384. 'body' => false,
  5385. ];
  5386. $this->Form->create($this->article);
  5387. $result = $this->Form->input('title');
  5388. $expected = array(
  5389. 'div' => array('class' => 'input text required'),
  5390. 'label' => array('for' => 'title'),
  5391. 'Title',
  5392. '/label',
  5393. 'input' => array(
  5394. 'type' => 'text',
  5395. 'name' => 'title',
  5396. 'id' => 'title',
  5397. 'required' => 'required',
  5398. ),
  5399. '/div'
  5400. );
  5401. $this->assertTags($result, $expected);
  5402. $result = $this->Form->input('title', ['required' => false]);
  5403. $this->assertNotContains('required', $result);
  5404. $result = $this->Form->input('body');
  5405. $expected = array(
  5406. 'div' => array('class' => 'input text'),
  5407. 'label' => array('for' => 'body'),
  5408. 'Body',
  5409. '/label',
  5410. 'input' => array(
  5411. 'type' => 'text',
  5412. 'name' => 'body',
  5413. 'id' => 'body',
  5414. ),
  5415. '/div'
  5416. );
  5417. $this->assertTags($result, $expected);
  5418. $result = $this->Form->input('body', ['required' => true]);
  5419. $this->assertContains('required', $result);
  5420. }
  5421. /**
  5422. * Tests that it is possible to nest inputs inside labels
  5423. *
  5424. * @return void
  5425. */
  5426. public function testNestInputInLabel() {
  5427. $this->Form->templates([
  5428. 'label' => '<label{{attrs}}>{{text}}{{input}}</label>',
  5429. 'formGroup' => '{{label}}'
  5430. ]);
  5431. $result = $this->Form->input('foo');
  5432. $expected = array(
  5433. 'div' => array('class' => 'input text'),
  5434. 'label' => array('for' => 'foo'),
  5435. 'Foo',
  5436. 'input' => array('type' => 'text', 'name' => 'foo', 'id' => 'foo'),
  5437. '/label',
  5438. '/div'
  5439. );
  5440. $this->assertTags($result, $expected);
  5441. }
  5442. /**
  5443. * Test resetting templates.
  5444. *
  5445. * @return void
  5446. */
  5447. public function testResetTemplates() {
  5448. $this->Form->templates(['input' => '<input>']);
  5449. $this->assertEquals('<input>', $this->Form->templater()->get('input'));
  5450. $this->assertNull($this->Form->resetTemplates());
  5451. $this->assertNotEquals('<input>', $this->Form->templater()->get('input'));
  5452. }
  5453. }