FormHelperTest.php 161 KB

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