FormHelperTest.php 168 KB

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