ValidationValidatorTest.php 309 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178
  1. <?php
  2. namespace Illuminate\Tests\Validation;
  3. use DateTime;
  4. use DateTimeImmutable;
  5. use Egulias\EmailValidator\Validation\NoRFCWarningsValidation;
  6. use Illuminate\Container\Container;
  7. use Illuminate\Contracts\Auth\Authenticatable;
  8. use Illuminate\Contracts\Auth\Guard;
  9. use Illuminate\Contracts\Hashing\Hasher;
  10. use Illuminate\Contracts\Translation\Translator as TranslatorContract;
  11. use Illuminate\Contracts\Validation\DataAwareRule;
  12. use Illuminate\Contracts\Validation\ImplicitRule;
  13. use Illuminate\Contracts\Validation\Rule;
  14. use Illuminate\Contracts\Validation\ValidatorAwareRule;
  15. use Illuminate\Database\Eloquent\Model;
  16. use Illuminate\Support\Arr;
  17. use Illuminate\Support\Carbon;
  18. use Illuminate\Translation\ArrayLoader;
  19. use Illuminate\Translation\Translator;
  20. use Illuminate\Validation\DatabasePresenceVerifierInterface;
  21. use Illuminate\Validation\Rules\Exists;
  22. use Illuminate\Validation\Rules\Unique;
  23. use Illuminate\Validation\ValidationData;
  24. use Illuminate\Validation\ValidationException;
  25. use Illuminate\Validation\Validator;
  26. use InvalidArgumentException;
  27. use Mockery as m;
  28. use PHPUnit\Framework\TestCase;
  29. use stdClass;
  30. use Symfony\Component\HttpFoundation\File\File;
  31. use Symfony\Component\HttpFoundation\File\UploadedFile;
  32. class ValidationValidatorTest extends TestCase
  33. {
  34. protected function tearDown(): void
  35. {
  36. Carbon::setTestNow();
  37. m::close();
  38. }
  39. public function testNestedErrorMessagesAreRetrievedFromLocalArray()
  40. {
  41. $trans = $this->getIlluminateArrayTranslator();
  42. $v = new Validator($trans, [
  43. 'users' => [
  44. [
  45. 'name' => 'Taylor Otwell',
  46. 'posts' => [
  47. [
  48. 'name' => '',
  49. ],
  50. ],
  51. ],
  52. ],
  53. ], [
  54. 'users.*.name' => ['required'],
  55. 'users.*.posts.*.name' => ['required'],
  56. ], [
  57. 'users.*.name.required' => 'user name is required',
  58. 'users.*.posts.*.name.required' => 'post name is required',
  59. ]);
  60. $this->assertFalse($v->passes());
  61. $this->assertSame('post name is required', $v->errors()->all()[0]);
  62. }
  63. public function testSometimesWorksOnNestedArrays()
  64. {
  65. $trans = $this->getIlluminateArrayTranslator();
  66. $v = new Validator($trans, ['foo' => ['bar' => ['baz' => '']]], ['foo.bar.baz' => 'sometimes|required']);
  67. $this->assertFalse($v->passes());
  68. $this->assertEquals(['foo.bar.baz' => ['Required' => []]], $v->failed());
  69. $trans = $this->getIlluminateArrayTranslator();
  70. $v = new Validator($trans, ['foo' => ['bar' => ['baz' => 'nonEmpty']]], ['foo.bar.baz' => 'sometimes|required']);
  71. $this->assertTrue($v->passes());
  72. }
  73. public function testAfterCallbacksAreCalledWithValidatorInstance()
  74. {
  75. $trans = $this->getIlluminateArrayTranslator();
  76. $v = new Validator($trans, ['foo' => 'bar', 'baz' => 'boom'], ['foo' => 'Same:baz']);
  77. $v->setContainer(new Container);
  78. $v->after(function ($validator) {
  79. $_SERVER['__validator.after.test'] = true;
  80. // For asserting we can actually work with the instance
  81. $validator->errors()->add('bar', 'foo');
  82. });
  83. $this->assertFalse($v->passes());
  84. $this->assertTrue($_SERVER['__validator.after.test']);
  85. $this->assertTrue($v->errors()->has('bar'));
  86. unset($_SERVER['__validator.after.test']);
  87. }
  88. public function testSometimesWorksOnArrays()
  89. {
  90. $trans = $this->getIlluminateArrayTranslator();
  91. $v = new Validator($trans, ['foo' => ['bar', 'baz', 'moo']], ['foo' => 'sometimes|required|between:5,10']);
  92. $this->assertFalse($v->passes());
  93. $this->assertNotEmpty($v->failed());
  94. $trans = $this->getIlluminateArrayTranslator();
  95. $v = new Validator($trans, ['foo' => ['bar', 'baz', 'moo', 'pew', 'boom']], ['foo' => 'sometimes|required|between:5,10']);
  96. $this->assertTrue($v->passes());
  97. }
  98. public function testValidateThrowsOnFail()
  99. {
  100. $this->expectException(ValidationException::class);
  101. $trans = $this->getIlluminateArrayTranslator();
  102. $v = new Validator($trans, ['foo' => 'bar'], ['baz' => 'required']);
  103. $v->validate();
  104. }
  105. public function testValidateDoesntThrowOnPass()
  106. {
  107. $trans = $this->getIlluminateArrayTranslator();
  108. $v = new Validator($trans, ['foo' => 'bar'], ['foo' => 'required']);
  109. $this->assertSame(['foo' => 'bar'], $v->validate());
  110. }
  111. public function testHasFailedValidationRules()
  112. {
  113. $trans = $this->getIlluminateArrayTranslator();
  114. $v = new Validator($trans, ['foo' => 'bar', 'baz' => 'boom'], ['foo' => 'Same:baz']);
  115. $this->assertFalse($v->passes());
  116. $this->assertEquals(['foo' => ['Same' => ['baz']]], $v->failed());
  117. }
  118. public function testFailingOnce()
  119. {
  120. $trans = $this->getIlluminateArrayTranslator();
  121. $v = new Validator($trans, ['foo' => 'bar', 'baz' => 'boom'], ['foo' => 'Bail|Same:baz|In:qux']);
  122. $this->assertFalse($v->passes());
  123. $this->assertEquals(['foo' => ['Same' => ['baz']]], $v->failed());
  124. }
  125. public function testHasNotFailedValidationRules()
  126. {
  127. $trans = $this->getTranslator();
  128. $trans->shouldReceive('get')->never();
  129. $v = new Validator($trans, ['foo' => 'taylor'], ['name' => 'Confirmed']);
  130. $this->assertTrue($v->passes());
  131. $this->assertEmpty($v->failed());
  132. }
  133. public function testSometimesCanSkipRequiredRules()
  134. {
  135. $trans = $this->getTranslator();
  136. $trans->shouldReceive('get')->never();
  137. $v = new Validator($trans, [], ['name' => 'sometimes|required']);
  138. $this->assertTrue($v->passes());
  139. $this->assertEmpty($v->failed());
  140. }
  141. public function testInValidatableRulesReturnsValid()
  142. {
  143. $trans = $this->getTranslator();
  144. $trans->shouldReceive('get')->never();
  145. $v = new Validator($trans, ['foo' => 'taylor'], ['name' => 'Confirmed']);
  146. $this->assertTrue($v->passes());
  147. }
  148. public function testValidateEmptyStringsAlwaysPasses()
  149. {
  150. $trans = $this->getIlluminateArrayTranslator();
  151. $v = new Validator($trans, ['x' => ''], ['x' => 'size:10|array|integer|min:5']);
  152. $this->assertTrue($v->passes());
  153. }
  154. public function testEmptyExistingAttributesAreValidated()
  155. {
  156. $trans = $this->getIlluminateArrayTranslator();
  157. $v = new Validator($trans, ['x' => ''], ['x' => 'array']);
  158. $this->assertTrue($v->passes());
  159. $v = new Validator($trans, ['x' => []], ['x' => 'boolean']);
  160. $this->assertTrue($v->fails());
  161. $v = new Validator($trans, ['x' => []], ['x' => 'numeric']);
  162. $this->assertTrue($v->fails());
  163. $v = new Validator($trans, ['x' => []], ['x' => 'integer']);
  164. $this->assertTrue($v->fails());
  165. $v = new Validator($trans, ['x' => []], ['x' => 'string']);
  166. $this->assertTrue($v->fails());
  167. $v = new Validator($trans, [], ['x' => 'string', 'y' => 'numeric', 'z' => 'integer', 'a' => 'boolean', 'b' => 'array']);
  168. $this->assertTrue($v->passes());
  169. }
  170. public function testNullable()
  171. {
  172. $trans = $this->getIlluminateArrayTranslator();
  173. $v = new Validator($trans, [
  174. 'x' => null, 'y' => null, 'z' => null, 'a' => null, 'b' => null,
  175. ], [
  176. 'x' => 'string|nullable', 'y' => 'integer|nullable', 'z' => 'numeric|nullable', 'a' => 'array|nullable', 'b' => 'bool|nullable',
  177. ]);
  178. $this->assertTrue($v->passes());
  179. $v = new Validator($trans, [
  180. 'x' => null, 'y' => null, 'z' => null, 'a' => null, 'b' => null,
  181. ], [
  182. 'x' => 'string', 'y' => 'integer', 'z' => 'numeric', 'a' => 'array', 'b' => 'bool',
  183. ]);
  184. $this->assertTrue($v->fails());
  185. $this->assertSame('validation.string', $v->messages()->get('x')[0]);
  186. $this->assertSame('validation.integer', $v->messages()->get('y')[0]);
  187. $this->assertSame('validation.numeric', $v->messages()->get('z')[0]);
  188. $this->assertSame('validation.array', $v->messages()->get('a')[0]);
  189. $this->assertSame('validation.boolean', $v->messages()->get('b')[0]);
  190. }
  191. public function testNullableMakesNoDifferenceIfImplicitRuleExists()
  192. {
  193. $trans = $this->getIlluminateArrayTranslator();
  194. $v = new Validator($trans, [
  195. 'x' => null, 'y' => null,
  196. ], [
  197. 'x' => 'nullable|required_with:y|integer',
  198. 'y' => 'nullable|required_with:x|integer',
  199. ]);
  200. $this->assertTrue($v->passes());
  201. $v = new Validator($trans, [
  202. 'x' => 'value', 'y' => null,
  203. ], [
  204. 'x' => 'nullable|required_with:y|integer',
  205. 'y' => 'nullable|required_with:x|integer',
  206. ]);
  207. $this->assertTrue($v->fails());
  208. $this->assertSame('validation.integer', $v->messages()->get('x')[0]);
  209. $v = new Validator($trans, [
  210. 'x' => 123, 'y' => null,
  211. ], [
  212. 'x' => 'nullable|required_with:y|integer',
  213. 'y' => 'nullable|required_with:x|integer',
  214. ]);
  215. $this->assertTrue($v->fails());
  216. $this->assertSame('validation.required_with', $v->messages()->get('y')[0]);
  217. }
  218. public function testProperLanguageLineIsSet()
  219. {
  220. $trans = $this->getIlluminateArrayTranslator();
  221. $trans->addLines(['validation.required' => 'required!'], 'en');
  222. $v = new Validator($trans, ['name' => ''], ['name' => 'Required']);
  223. $this->assertFalse($v->passes());
  224. $v->messages()->setFormat(':message');
  225. $this->assertSame('required!', $v->messages()->first('name'));
  226. }
  227. public function testCustomReplacersAreCalled()
  228. {
  229. $trans = $this->getIlluminateArrayTranslator();
  230. $trans->addLines(['validation.required' => 'foo bar'], 'en');
  231. $v = new Validator($trans, ['name' => ''], ['name' => 'Required']);
  232. $v->addReplacer('required', function ($message, $attribute, $rule, $parameters) {
  233. return str_replace('bar', 'taylor', $message);
  234. });
  235. $this->assertFalse($v->passes());
  236. $v->messages()->setFormat(':message');
  237. $this->assertSame('foo taylor', $v->messages()->first('name'));
  238. }
  239. public function testClassBasedCustomReplacers()
  240. {
  241. $trans = $this->getIlluminateArrayTranslator();
  242. $trans->addLines(['validation.foo' => 'foo!'], 'en');
  243. $v = new Validator($trans, [], ['name' => 'required']);
  244. $v->setContainer($container = m::mock(Container::class));
  245. $v->addReplacer('required', 'Foo@bar');
  246. $container->shouldReceive('make')->once()->with('Foo')->andReturn($foo = m::mock(stdClass::class));
  247. $foo->shouldReceive('bar')->once()->andReturn('replaced!');
  248. $v->passes();
  249. $v->messages()->setFormat(':message');
  250. $this->assertSame('replaced!', $v->messages()->first('name'));
  251. }
  252. public function testNestedAttributesAreReplacedInDimensions()
  253. {
  254. // Knowing that demo image.png has width = 3 and height = 2
  255. $uploadedFile = new UploadedFile(__DIR__.'/fixtures/image.png', '', null, null, true);
  256. $trans = $this->getIlluminateArrayTranslator();
  257. $trans->addLines(['validation.dimensions' => ':min_width :max_height :ratio'], 'en');
  258. $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:min_width=10,max_height=20,ratio=1']);
  259. $v->messages()->setFormat(':message');
  260. $this->assertTrue($v->fails());
  261. $this->assertSame('10 20 1', $v->messages()->first('x'));
  262. $trans = $this->getIlluminateArrayTranslator();
  263. $trans->addLines(['validation.dimensions' => ':width :height :ratio'], 'en');
  264. $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:min_width=10,max_height=20,ratio=1']);
  265. $v->messages()->setFormat(':message');
  266. $this->assertTrue($v->fails());
  267. $this->assertSame(':width :height 1', $v->messages()->first('x'));
  268. }
  269. public function testAttributeNamesAreReplaced()
  270. {
  271. $trans = $this->getIlluminateArrayTranslator();
  272. $trans->addLines(['validation.required' => ':attribute is required!'], 'en');
  273. $v = new Validator($trans, ['name' => ''], ['name' => 'Required']);
  274. $this->assertFalse($v->passes());
  275. $v->messages()->setFormat(':message');
  276. $this->assertSame('name is required!', $v->messages()->first('name'));
  277. $trans = $this->getIlluminateArrayTranslator();
  278. $trans->addLines(['validation.required' => ':attribute is required!', 'validation.attributes.name' => 'Name'], 'en');
  279. $v = new Validator($trans, ['name' => ''], ['name' => 'Required']);
  280. $this->assertFalse($v->passes());
  281. $v->messages()->setFormat(':message');
  282. $this->assertSame('Name is required!', $v->messages()->first('name'));
  283. // set customAttributes by setter
  284. $trans = $this->getIlluminateArrayTranslator();
  285. $trans->addLines(['validation.required' => ':attribute is required!'], 'en');
  286. $customAttributes = ['name' => 'Name'];
  287. $v = new Validator($trans, ['name' => ''], ['name' => 'Required']);
  288. $v->addCustomAttributes($customAttributes);
  289. $this->assertFalse($v->passes());
  290. $v->messages()->setFormat(':message');
  291. $this->assertSame('Name is required!', $v->messages()->first('name'));
  292. $trans = $this->getIlluminateArrayTranslator();
  293. $trans->addLines(['validation.required' => ':attribute is required!'], 'en');
  294. $v = new Validator($trans, ['name' => ''], ['name' => 'Required']);
  295. $v->setAttributeNames(['name' => 'Name']);
  296. $this->assertFalse($v->passes());
  297. $v->messages()->setFormat(':message');
  298. $this->assertSame('Name is required!', $v->messages()->first('name'));
  299. $trans = $this->getIlluminateArrayTranslator();
  300. $trans->addLines(['validation.required' => ':Attribute is required!'], 'en');
  301. $v = new Validator($trans, ['name' => ''], ['name' => 'Required']);
  302. $this->assertFalse($v->passes());
  303. $v->messages()->setFormat(':message');
  304. $this->assertSame('Name is required!', $v->messages()->first('name'));
  305. $trans = $this->getIlluminateArrayTranslator();
  306. $trans->addLines(['validation.required' => ':ATTRIBUTE is required!'], 'en');
  307. $v = new Validator($trans, ['name' => ''], ['name' => 'Required']);
  308. $this->assertFalse($v->passes());
  309. $v->messages()->setFormat(':message');
  310. $this->assertSame('NAME is required!', $v->messages()->first('name'));
  311. }
  312. public function testAttributeNamesAreReplacedInArrays()
  313. {
  314. $trans = $this->getIlluminateArrayTranslator();
  315. $trans->addLines(['validation.required' => ':attribute is required!'], 'en');
  316. $v = new Validator($trans, ['users' => [['country_code' => 'US'], ['country_code' => null]]], ['users.*.country_code' => 'Required']);
  317. $this->assertFalse($v->passes());
  318. $v->messages()->setFormat(':message');
  319. $this->assertSame('users.1.country_code is required!', $v->messages()->first('users.1.country_code'));
  320. $trans = $this->getIlluminateArrayTranslator();
  321. $trans->addLines([
  322. 'validation.string' => ':attribute must be a string!',
  323. 'validation.attributes.name.*' => 'Any name',
  324. ], 'en');
  325. $v = new Validator($trans, ['name' => ['Jon', 2]], ['name.*' => 'string']);
  326. $this->assertFalse($v->passes());
  327. $v->messages()->setFormat(':message');
  328. $this->assertSame('Any name must be a string!', $v->messages()->first('name.1'));
  329. $trans = $this->getIlluminateArrayTranslator();
  330. $trans->addLines(['validation.string' => ':attribute must be a string!'], 'en');
  331. $v = new Validator($trans, ['name' => ['Jon', 2]], ['name.*' => 'string']);
  332. $v->setAttributeNames(['name.*' => 'Any name']);
  333. $this->assertFalse($v->passes());
  334. $v->messages()->setFormat(':message');
  335. $this->assertSame('Any name must be a string!', $v->messages()->first('name.1'));
  336. $v = new Validator($trans, ['users' => [['name' => 'Jon'], ['name' => 2]]], ['users.*.name' => 'string']);
  337. $v->setAttributeNames(['users.*.name' => 'Any name']);
  338. $this->assertFalse($v->passes());
  339. $v->messages()->setFormat(':message');
  340. $this->assertSame('Any name must be a string!', $v->messages()->first('users.1.name'));
  341. $trans->addLines(['validation.required' => ':attribute is required!'], 'en');
  342. $v = new Validator($trans, ['title' => ['nl' => '', 'en' => 'Hello']], ['title.*' => 'required'], [], ['title.nl' => 'Titel', 'title.en' => 'Title']);
  343. $this->assertFalse($v->passes());
  344. $v->messages()->setFormat(':message');
  345. $this->assertSame('Titel is required!', $v->messages()->first('title.nl'));
  346. $trans = $this->getIlluminateArrayTranslator();
  347. $trans->addLines(['validation.required' => ':attribute is required!'], 'en');
  348. $trans->addLines(['validation.attributes' => ['names.*' => 'names']], 'en');
  349. $v = new Validator($trans, ['names' => [null, 'name']], ['names.*' => 'Required']);
  350. $v->messages()->setFormat(':message');
  351. $this->assertSame('names is required!', $v->messages()->first('names.0'));
  352. $trans = $this->getIlluminateArrayTranslator();
  353. $trans->addLines(['validation.required' => ':attribute is required!'], 'en');
  354. $trans->addLines(['validation.attributes' => ['names.*' => 'names']], 'en');
  355. $trans->addLines(['validation.attributes' => ['names.0' => 'First name']], 'en');
  356. $v = new Validator($trans, ['names' => [null, 'name']], ['names.*' => 'Required']);
  357. $v->messages()->setFormat(':message');
  358. $this->assertSame('First name is required!', $v->messages()->first('names.0'));
  359. }
  360. public function testInputIsReplaced()
  361. {
  362. $trans = $this->getIlluminateArrayTranslator();
  363. $trans->addLines(['validation.email' => ':input is not a valid email'], 'en');
  364. $v = new Validator($trans, ['email' => 'a@@s'], ['email' => 'email']);
  365. $this->assertFalse($v->passes());
  366. $v->messages()->setFormat(':message');
  367. $this->assertSame('a@@s is not a valid email', $v->messages()->first('email'));
  368. $trans = $this->getIlluminateArrayTranslator();
  369. $trans->addLines(['validation.email' => ':input is not a valid email'], 'en');
  370. $v = new Validator($trans, ['email' => null], ['email' => 'email']);
  371. $this->assertFalse($v->passes());
  372. $v->messages()->setFormat(':message');
  373. $this->assertSame('empty is not a valid email', $v->messages()->first('email'));
  374. }
  375. public function testInputIsReplacedByItsDisplayableValue()
  376. {
  377. $frameworks = [
  378. 1 => 'Laravel',
  379. 2 => 'Symfony',
  380. 3 => 'Rails',
  381. ];
  382. $trans = $this->getIlluminateArrayTranslator();
  383. $trans->addLines(['validation.framework_php' => ':input is not a valid PHP Framework'], 'en');
  384. $v = new Validator($trans, ['framework' => 3], ['framework' => 'framework_php']);
  385. $v->addExtension('framework_php', function ($attribute, $value, $parameters, $validator) {
  386. return in_array($value, [1, 2]);
  387. });
  388. $v->addCustomValues(['framework' => $frameworks]);
  389. $this->assertFalse($v->passes());
  390. $this->assertSame('Rails is not a valid PHP Framework', $v->messages()->first('framework'));
  391. }
  392. public function testDisplayableValuesAreReplaced()
  393. {
  394. // required_if:foo,bar
  395. $trans = $this->getIlluminateArrayTranslator();
  396. $trans->addLines(['validation.required_if' => 'The :attribute field is required when :other is :value.'], 'en');
  397. $trans->addLines(['validation.values.color.1' => 'red'], 'en');
  398. $v = new Validator($trans, ['color' => '1', 'bar' => ''], ['bar' => 'RequiredIf:color,1']);
  399. $this->assertFalse($v->passes());
  400. $v->messages()->setFormat(':message');
  401. $this->assertSame('The bar field is required when color is red.', $v->messages()->first('bar'));
  402. // required_if:foo,boolean
  403. $trans = $this->getIlluminateArrayTranslator();
  404. $trans->addLines(['validation.required_if' => 'The :attribute field is required when :other is :value.'], 'en');
  405. $trans->addLines(['validation.values.subscribe.false' => 'false'], 'en');
  406. $v = new Validator($trans, ['subscribe' => false, 'bar' => ''], ['bar' => 'RequiredIf:subscribe,false']);
  407. $this->assertFalse($v->passes());
  408. $v->messages()->setFormat(':message');
  409. $this->assertSame('The bar field is required when subscribe is false.', $v->messages()->first('bar'));
  410. $trans = $this->getIlluminateArrayTranslator();
  411. $trans->addLines(['validation.required_if' => 'The :attribute field is required when :other is :value.'], 'en');
  412. $trans->addLines(['validation.values.subscribe.true' => 'true'], 'en');
  413. $v = new Validator($trans, ['subscribe' => true, 'bar' => ''], ['bar' => 'RequiredIf:subscribe,true']);
  414. $this->assertFalse($v->passes());
  415. $v->messages()->setFormat(':message');
  416. $this->assertSame('The bar field is required when subscribe is true.', $v->messages()->first('bar'));
  417. // required_unless:foo,bar
  418. $trans = $this->getIlluminateArrayTranslator();
  419. $trans->addLines(['validation.required_unless' => 'The :attribute field is required unless :other is in :values.'], 'en');
  420. $trans->addLines(['validation.values.color.1' => 'red'], 'en');
  421. $v = new Validator($trans, ['color' => '2', 'bar' => ''], ['bar' => 'RequiredUnless:color,1']);
  422. $this->assertFalse($v->passes());
  423. $v->messages()->setFormat(':message');
  424. $this->assertSame('The bar field is required unless color is in red.', $v->messages()->first('bar'));
  425. // in:foo,bar,...
  426. $trans = $this->getIlluminateArrayTranslator();
  427. $trans->addLines(['validation.in' => ':attribute must be included in :values.'], 'en');
  428. $trans->addLines(['validation.values.type.5' => 'Short'], 'en');
  429. $trans->addLines(['validation.values.type.300' => 'Long'], 'en');
  430. $v = new Validator($trans, ['type' => '4'], ['type' => 'in:5,300']);
  431. $this->assertFalse($v->passes());
  432. $v->messages()->setFormat(':message');
  433. $this->assertSame('type must be included in Short, Long.', $v->messages()->first('type'));
  434. // date_equals:tomorrow
  435. $trans = $this->getIlluminateArrayTranslator();
  436. $trans->addLines(['validation.date_equals' => 'The :attribute must be a date equal to :date.'], 'en');
  437. $trans->addLines(['validation.values.date.tomorrow' => 'the day after today'], 'en');
  438. $v = new Validator($trans, ['date' => date('Y-m-d')], ['date' => 'date_equals:tomorrow']);
  439. $this->assertFalse($v->passes());
  440. $v->messages()->setFormat(':message');
  441. $this->assertSame('The date must be a date equal to the day after today.', $v->messages()->first('date'));
  442. // test addCustomValues
  443. $trans = $this->getIlluminateArrayTranslator();
  444. $trans->addLines(['validation.in' => ':attribute must be included in :values.'], 'en');
  445. $customValues = [
  446. 'type' => [
  447. '5' => 'Short',
  448. '300' => 'Long',
  449. ],
  450. ];
  451. $v = new Validator($trans, ['type' => '4'], ['type' => 'in:5,300']);
  452. $v->addCustomValues($customValues);
  453. $this->assertFalse($v->passes());
  454. $v->messages()->setFormat(':message');
  455. $this->assertSame('type must be included in Short, Long.', $v->messages()->first('type'));
  456. // set custom values by setter
  457. $trans = $this->getIlluminateArrayTranslator();
  458. $trans->addLines(['validation.in' => ':attribute must be included in :values.'], 'en');
  459. $customValues = [
  460. 'type' => [
  461. '5' => 'Short',
  462. '300' => 'Long',
  463. ],
  464. ];
  465. $v = new Validator($trans, ['type' => '4'], ['type' => 'in:5,300']);
  466. $v->setValueNames($customValues);
  467. $this->assertFalse($v->passes());
  468. $v->messages()->setFormat(':message');
  469. $this->assertSame('type must be included in Short, Long.', $v->messages()->first('type'));
  470. }
  471. public function testDisplayableAttributesAreReplacedInCustomReplacers()
  472. {
  473. $trans = $this->getIlluminateArrayTranslator();
  474. $trans->addLines(['validation.alliteration' => ':attribute needs to begin with the same letter as :other'], 'en');
  475. $trans->addLines(['validation.attributes.firstname' => 'Firstname'], 'en');
  476. $trans->addLines(['validation.attributes.lastname' => 'Lastname'], 'en');
  477. $v = new Validator($trans, ['firstname' => 'Bob', 'lastname' => 'Smith'], ['lastname' => 'alliteration:firstname']);
  478. $v->addExtension('alliteration', function ($attribute, $value, $parameters, $validator) {
  479. $other = Arr::get($validator->getData(), $parameters[0]);
  480. return $value[0] == $other[0];
  481. });
  482. $v->addReplacer('alliteration', function ($message, $attribute, $rule, $parameters, $validator) {
  483. return str_replace(':other', $validator->getDisplayableAttribute($parameters[0]), $message);
  484. });
  485. $this->assertFalse($v->passes());
  486. $v->messages()->setFormat(':message');
  487. $this->assertSame('Lastname needs to begin with the same letter as Firstname', $v->messages()->first('lastname'));
  488. $trans = $this->getIlluminateArrayTranslator();
  489. $trans->addLines(['validation.alliteration' => ':attribute needs to begin with the same letter as :other'], 'en');
  490. $customAttributes = ['firstname' => 'Firstname', 'lastname' => 'Lastname'];
  491. $v = new Validator($trans, ['firstname' => 'Bob', 'lastname' => 'Smith'], ['lastname' => 'alliteration:firstname']);
  492. $v->addCustomAttributes($customAttributes);
  493. $v->addExtension('alliteration', function ($attribute, $value, $parameters, $validator) {
  494. $other = Arr::get($validator->getData(), $parameters[0]);
  495. return $value[0] == $other[0];
  496. });
  497. $v->addReplacer('alliteration', function ($message, $attribute, $rule, $parameters, $validator) {
  498. return str_replace(':other', $validator->getDisplayableAttribute($parameters[0]), $message);
  499. });
  500. $this->assertFalse($v->passes());
  501. $v->messages()->setFormat(':message');
  502. $this->assertSame('Lastname needs to begin with the same letter as Firstname', $v->messages()->first('lastname'));
  503. $trans = $this->getIlluminateArrayTranslator();
  504. $trans->addLines(['validation.alliteration' => ':attribute needs to begin with the same letter as :other'], 'en');
  505. new Validator($trans, ['firstname' => 'Bob', 'lastname' => 'Smith'], ['lastname' => 'alliteration:firstname']);
  506. }
  507. public function testCustomValidationLinesAreRespected()
  508. {
  509. $trans = $this->getIlluminateArrayTranslator();
  510. $trans->getLoader()->addMessages('en', 'validation', [
  511. 'required' => 'required!',
  512. 'custom' => [
  513. 'name' => [
  514. 'required' => 'really required!',
  515. ],
  516. ],
  517. ]);
  518. $v = new Validator($trans, ['name' => ''], ['name' => 'Required']);
  519. $this->assertFalse($v->passes());
  520. $v->messages()->setFormat(':message');
  521. $this->assertSame('really required!', $v->messages()->first('name'));
  522. }
  523. public function testCustomValidationLinesAreRespectedWithAsterisks()
  524. {
  525. $trans = $this->getIlluminateArrayTranslator();
  526. $trans->getLoader()->addMessages('en', 'validation', [
  527. 'required' => 'required!',
  528. 'custom' => [
  529. 'name.*' => [
  530. 'required' => 'all are really required!',
  531. ],
  532. 'lang.en' => [
  533. 'required' => 'english is required!',
  534. ],
  535. ],
  536. ]);
  537. $v = new Validator($trans, ['name' => ['', ''], 'lang' => ['en' => '']], [
  538. 'name.*' => 'required|max:255',
  539. 'lang.*' => 'required|max:255',
  540. ]);
  541. $this->assertFalse($v->passes());
  542. $v->messages()->setFormat(':message');
  543. $this->assertSame('all are really required!', $v->messages()->first('name.0'));
  544. $this->assertSame('all are really required!', $v->messages()->first('name.1'));
  545. $this->assertSame('english is required!', $v->messages()->first('lang.en'));
  546. }
  547. public function testCustomException()
  548. {
  549. $trans = $this->getIlluminateArrayTranslator();
  550. $v = new Validator($trans, ['name' => ''], ['name' => 'required']);
  551. $exception = new class($v) extends ValidationException {};
  552. $v->setException($exception);
  553. try {
  554. $v->validate();
  555. } catch (ValidationException $e) {
  556. $this->assertSame($exception, $e);
  557. }
  558. }
  559. public function testCustomExceptionMustExtendValidationException()
  560. {
  561. $trans = $this->getIlluminateArrayTranslator();
  562. $v = new Validator($trans, [], []);
  563. $this->expectException(InvalidArgumentException::class);
  564. $this->expectExceptionMessage('Exception [RuntimeException] is invalid. It must extend [Illuminate\Validation\ValidationException].');
  565. $v->setException(\RuntimeException::class);
  566. }
  567. public function testValidationDotCustomDotAnythingCanBeTranslated()
  568. {
  569. $trans = $this->getIlluminateArrayTranslator();
  570. $trans->getLoader()->addMessages('en', 'validation', [
  571. 'required' => 'required!',
  572. 'custom' => [
  573. 'validation' => [
  574. 'custom.*' => [
  575. 'integer' => 'should be integer!',
  576. ],
  577. ],
  578. ],
  579. ]);
  580. $v = new Validator($trans, ['validation' => ['custom' => ['string', 'string']]], ['validation.custom.*' => 'integer']);
  581. $this->assertFalse($v->passes());
  582. $v->messages()->setFormat(':message');
  583. $this->assertSame('should be integer!', $v->messages()->first('validation.custom.0'));
  584. $this->assertSame('should be integer!', $v->messages()->first('validation.custom.1'));
  585. }
  586. public function testInlineValidationMessagesAreRespected()
  587. {
  588. $trans = $this->getIlluminateArrayTranslator();
  589. $v = new Validator($trans, ['name' => ''], ['name' => 'Required'], ['name.required' => 'require it please!']);
  590. $this->assertFalse($v->passes());
  591. $v->messages()->setFormat(':message');
  592. $this->assertSame('require it please!', $v->messages()->first('name'));
  593. $trans = $this->getIlluminateArrayTranslator();
  594. $v = new Validator($trans, ['name' => ''], ['name' => 'Required'], ['required' => 'require it please!']);
  595. $this->assertFalse($v->passes());
  596. $v->messages()->setFormat(':message');
  597. $this->assertSame('require it please!', $v->messages()->first('name'));
  598. $trans = $this->getIlluminateArrayTranslator();
  599. $v = new Validator($trans, ['name' => 'foobarba'], ['name' => 'size:9'], ['size' => ['string' => ':attribute should be of length :size']]);
  600. $this->assertFalse($v->passes());
  601. $v->messages()->setFormat(':message');
  602. $this->assertSame('name should be of length 9', $v->messages()->first('name'));
  603. }
  604. public function testInlineValidationMessagesAreRespectedWithAsterisks()
  605. {
  606. $trans = $this->getIlluminateArrayTranslator();
  607. $v = new Validator($trans, ['name' => ['', '']], ['name.*' => 'required|max:255'], ['name.*.required' => 'all must be required!']);
  608. $this->assertFalse($v->passes());
  609. $v->messages()->setFormat(':message');
  610. $this->assertSame('all must be required!', $v->messages()->first('name.0'));
  611. $this->assertSame('all must be required!', $v->messages()->first('name.1'));
  612. }
  613. public function testIfRulesAreSuccessfullyAdded()
  614. {
  615. $trans = $this->getIlluminateArrayTranslator();
  616. $v = new Validator($trans, [], ['foo' => 'Required']);
  617. // foo has required rule
  618. $this->assertTrue($v->hasRule('foo', 'Required'));
  619. // foo doesn't have array rule
  620. $this->assertFalse($v->hasRule('foo', 'Array'));
  621. // bar doesn't exists
  622. $this->assertFalse($v->hasRule('bar', 'Required'));
  623. }
  624. public function testValidateArray()
  625. {
  626. $trans = $this->getIlluminateArrayTranslator();
  627. $v = new Validator($trans, ['foo' => [1, 2, 3]], ['foo' => 'Array']);
  628. $this->assertTrue($v->passes());
  629. $v = new Validator($trans, ['foo' => new File('/tmp/foo', false)], ['foo' => 'Array']);
  630. $this->assertFalse($v->passes());
  631. }
  632. public function testValidateArrayKeys()
  633. {
  634. $trans = $this->getIlluminateArrayTranslator();
  635. $rules = ['user' => 'array:name,username'];
  636. $v = new Validator($trans, ['user' => ['name' => 'Duilio', 'username' => 'duilio']], $rules);
  637. $this->assertTrue($v->passes());
  638. // The array is valid if there's a missing key.
  639. $v = new Validator($trans, ['user' => ['name' => 'Duilio']], $rules);
  640. $this->assertTrue($v->passes());
  641. // But it's not valid if there's an unexpected key.
  642. $v = new Validator($trans, ['user' => ['name' => 'Duilio', 'username' => 'duilio', 'is_admin' => true]], $rules);
  643. $this->assertFalse($v->passes());
  644. }
  645. public function testValidateCurrentPassword()
  646. {
  647. // Fails when user is not logged in.
  648. $auth = m::mock(Guard::class);
  649. $auth->shouldReceive('guard')->andReturn($auth);
  650. $auth->shouldReceive('guest')->andReturn(true);
  651. $hasher = m::mock(Hasher::class);
  652. $container = m::mock(Container::class);
  653. $container->shouldReceive('make')->with('auth')->andReturn($auth);
  654. $container->shouldReceive('make')->with('hash')->andReturn($hasher);
  655. $trans = $this->getTranslator();
  656. $trans->shouldReceive('get')->andReturnArg(0);
  657. $v = new Validator($trans, ['password' => 'foo'], ['password' => 'current_password']);
  658. $v->setContainer($container);
  659. $this->assertFalse($v->passes());
  660. // Fails when password is incorrect.
  661. $user = m::mock(Authenticatable::class);
  662. $user->shouldReceive('getAuthPassword');
  663. $auth = m::mock(Guard::class);
  664. $auth->shouldReceive('guard')->andReturn($auth);
  665. $auth->shouldReceive('guest')->andReturn(false);
  666. $auth->shouldReceive('user')->andReturn($user);
  667. $hasher = m::mock(Hasher::class);
  668. $hasher->shouldReceive('check')->andReturn(false);
  669. $container = m::mock(Container::class);
  670. $container->shouldReceive('make')->with('auth')->andReturn($auth);
  671. $container->shouldReceive('make')->with('hash')->andReturn($hasher);
  672. $trans = $this->getTranslator();
  673. $trans->shouldReceive('get')->andReturnArg(0);
  674. $v = new Validator($trans, ['password' => 'foo'], ['password' => 'current_password']);
  675. $v->setContainer($container);
  676. $this->assertFalse($v->passes());
  677. // Succeeds when password is correct.
  678. $user = m::mock(Authenticatable::class);
  679. $user->shouldReceive('getAuthPassword');
  680. $auth = m::mock(Guard::class);
  681. $auth->shouldReceive('guard')->andReturn($auth);
  682. $auth->shouldReceive('guest')->andReturn(false);
  683. $auth->shouldReceive('user')->andReturn($user);
  684. $hasher = m::mock(Hasher::class);
  685. $hasher->shouldReceive('check')->andReturn(true);
  686. $container = m::mock(Container::class);
  687. $container->shouldReceive('make')->with('auth')->andReturn($auth);
  688. $container->shouldReceive('make')->with('hash')->andReturn($hasher);
  689. $trans = $this->getTranslator();
  690. $trans->shouldReceive('get')->andReturnArg(0);
  691. $v = new Validator($trans, ['password' => 'foo'], ['password' => 'current_password']);
  692. $v->setContainer($container);
  693. $this->assertTrue($v->passes());
  694. // We can use a specific guard.
  695. $user = m::mock(Authenticatable::class);
  696. $user->shouldReceive('getAuthPassword');
  697. $auth = m::mock(Guard::class);
  698. $auth->shouldReceive('guard')->with('custom')->andReturn($auth);
  699. $auth->shouldReceive('guest')->andReturn(false);
  700. $auth->shouldReceive('user')->andReturn($user);
  701. $hasher = m::mock(Hasher::class);
  702. $hasher->shouldReceive('check')->andReturn(true);
  703. $container = m::mock(Container::class);
  704. $container->shouldReceive('make')->with('auth')->andReturn($auth);
  705. $container->shouldReceive('make')->with('hash')->andReturn($hasher);
  706. $trans = $this->getTranslator();
  707. $trans->shouldReceive('get')->andReturnArg(0);
  708. $v = new Validator($trans, ['password' => 'foo'], ['password' => 'current_password:custom']);
  709. $v->setContainer($container);
  710. $this->assertTrue($v->passes());
  711. }
  712. public function testValidateFilled()
  713. {
  714. $trans = $this->getIlluminateArrayTranslator();
  715. $v = new Validator($trans, [], ['name' => 'filled']);
  716. $this->assertTrue($v->passes());
  717. $v = new Validator($trans, ['name' => ''], ['name' => 'filled']);
  718. $this->assertFalse($v->passes());
  719. $v = new Validator($trans, ['foo' => [['id' => 1], []]], ['foo.*.id' => 'filled']);
  720. $this->assertTrue($v->passes());
  721. $v = new Validator($trans, ['foo' => [['id' => '']]], ['foo.*.id' => 'filled']);
  722. $this->assertFalse($v->passes());
  723. $v = new Validator($trans, ['foo' => [['id' => null]]], ['foo.*.id' => 'filled']);
  724. $this->assertFalse($v->passes());
  725. }
  726. public function testValidationStopsAtFailedPresenceCheck()
  727. {
  728. $trans = $this->getIlluminateArrayTranslator();
  729. $v = new Validator($trans, ['name' => null], ['name' => 'Required|string']);
  730. $v->passes();
  731. $this->assertEquals(['validation.required'], $v->errors()->get('name'));
  732. $v = new Validator($trans, ['name' => null, 'email' => 'email'], ['name' => 'required_with:email|string']);
  733. $v->passes();
  734. $this->assertEquals(['validation.required_with'], $v->errors()->get('name'));
  735. $v = new Validator($trans, ['name' => null, 'email' => ''], ['name' => 'required_with:email|string']);
  736. $v->passes();
  737. $this->assertEquals(['validation.string'], $v->errors()->get('name'));
  738. $v = new Validator($trans, [], ['name' => 'present|string']);
  739. $v->passes();
  740. $this->assertEquals(['validation.present'], $v->errors()->get('name'));
  741. }
  742. public function testValidatePassword()
  743. {
  744. // Fails when user is not logged in.
  745. $auth = m::mock(Guard::class);
  746. $auth->shouldReceive('guard')->andReturn($auth);
  747. $auth->shouldReceive('guest')->andReturn(true);
  748. $hasher = m::mock(Hasher::class);
  749. $container = m::mock(Container::class);
  750. $container->shouldReceive('make')->with('auth')->andReturn($auth);
  751. $container->shouldReceive('make')->with('hash')->andReturn($hasher);
  752. $trans = $this->getTranslator();
  753. $trans->shouldReceive('get')->andReturnArg(0);
  754. $v = new Validator($trans, ['password' => 'foo'], ['password' => 'password']);
  755. $v->setContainer($container);
  756. $this->assertFalse($v->passes());
  757. // Fails when password is incorrect.
  758. $user = m::mock(Authenticatable::class);
  759. $user->shouldReceive('getAuthPassword');
  760. $auth = m::mock(Guard::class);
  761. $auth->shouldReceive('guard')->andReturn($auth);
  762. $auth->shouldReceive('guest')->andReturn(false);
  763. $auth->shouldReceive('user')->andReturn($user);
  764. $hasher = m::mock(Hasher::class);
  765. $hasher->shouldReceive('check')->andReturn(false);
  766. $container = m::mock(Container::class);
  767. $container->shouldReceive('make')->with('auth')->andReturn($auth);
  768. $container->shouldReceive('make')->with('hash')->andReturn($hasher);
  769. $trans = $this->getTranslator();
  770. $trans->shouldReceive('get')->andReturnArg(0);
  771. $v = new Validator($trans, ['password' => 'foo'], ['password' => 'password']);
  772. $v->setContainer($container);
  773. $this->assertFalse($v->passes());
  774. // Succeeds when password is correct.
  775. $user = m::mock(Authenticatable::class);
  776. $user->shouldReceive('getAuthPassword');
  777. $auth = m::mock(Guard::class);
  778. $auth->shouldReceive('guard')->andReturn($auth);
  779. $auth->shouldReceive('guest')->andReturn(false);
  780. $auth->shouldReceive('user')->andReturn($user);
  781. $hasher = m::mock(Hasher::class);
  782. $hasher->shouldReceive('check')->andReturn(true);
  783. $container = m::mock(Container::class);
  784. $container->shouldReceive('make')->with('auth')->andReturn($auth);
  785. $container->shouldReceive('make')->with('hash')->andReturn($hasher);
  786. $trans = $this->getTranslator();
  787. $trans->shouldReceive('get')->andReturnArg(0);
  788. $v = new Validator($trans, ['password' => 'foo'], ['password' => 'password']);
  789. $v->setContainer($container);
  790. $this->assertTrue($v->passes());
  791. // We can use a specific guard.
  792. $user = m::mock(Authenticatable::class);
  793. $user->shouldReceive('getAuthPassword');
  794. $auth = m::mock(Guard::class);
  795. $auth->shouldReceive('guard')->with('custom')->andReturn($auth);
  796. $auth->shouldReceive('guest')->andReturn(false);
  797. $auth->shouldReceive('user')->andReturn($user);
  798. $hasher = m::mock(Hasher::class);
  799. $hasher->shouldReceive('check')->andReturn(true);
  800. $container = m::mock(Container::class);
  801. $container->shouldReceive('make')->with('auth')->andReturn($auth);
  802. $container->shouldReceive('make')->with('hash')->andReturn($hasher);
  803. $trans = $this->getTranslator();
  804. $trans->shouldReceive('get')->andReturnArg(0);
  805. $v = new Validator($trans, ['password' => 'foo'], ['password' => 'password:custom']);
  806. $v->setContainer($container);
  807. $this->assertTrue($v->passes());
  808. }
  809. public function testValidatePresent()
  810. {
  811. $trans = $this->getIlluminateArrayTranslator();
  812. $v = new Validator($trans, [], ['name' => 'present']);
  813. $this->assertFalse($v->passes());
  814. $v = new Validator($trans, [], ['name' => 'present|nullable']);
  815. $this->assertFalse($v->passes());
  816. $v = new Validator($trans, ['name' => null], ['name' => 'present|nullable']);
  817. $this->assertTrue($v->passes());
  818. $v = new Validator($trans, ['name' => ''], ['name' => 'present']);
  819. $this->assertTrue($v->passes());
  820. $v = new Validator($trans, ['foo' => [['id' => 1], ['name' => 'a']]], ['foo.*.id' => 'present']);
  821. $this->assertFalse($v->passes());
  822. $v = new Validator($trans, ['foo' => [['id' => 1], []]], ['foo.*.id' => 'present']);
  823. $this->assertFalse($v->passes());
  824. $v = new Validator($trans, ['foo' => [['id' => 1], ['id' => '']]], ['foo.*.id' => 'present']);
  825. $this->assertTrue($v->passes());
  826. $v = new Validator($trans, ['foo' => [['id' => 1], ['id' => null]]], ['foo.*.id' => 'present']);
  827. $this->assertTrue($v->passes());
  828. }
  829. public function testValidateRequired()
  830. {
  831. $trans = $this->getIlluminateArrayTranslator();
  832. $v = new Validator($trans, [], ['name' => 'Required']);
  833. $this->assertFalse($v->passes());
  834. $v = new Validator($trans, ['name' => ''], ['name' => 'Required']);
  835. $this->assertFalse($v->passes());
  836. $v = new Validator($trans, ['name' => 'foo'], ['name' => 'Required']);
  837. $this->assertTrue($v->passes());
  838. $file = new File('', false);
  839. $v = new Validator($trans, ['name' => $file], ['name' => 'Required']);
  840. $this->assertFalse($v->passes());
  841. $file = new File(__FILE__, false);
  842. $v = new Validator($trans, ['name' => $file], ['name' => 'Required']);
  843. $this->assertTrue($v->passes());
  844. $file = new File(__FILE__, false);
  845. $file2 = new File(__FILE__, false);
  846. $v = new Validator($trans, ['files' => [$file, $file2]], ['files.0' => 'Required', 'files.1' => 'Required']);
  847. $this->assertTrue($v->passes());
  848. $v = new Validator($trans, ['files' => [$file, $file2]], ['files' => 'Required']);
  849. $this->assertTrue($v->passes());
  850. }
  851. public function testValidateRequiredWith()
  852. {
  853. $trans = $this->getIlluminateArrayTranslator();
  854. $v = new Validator($trans, ['first' => 'Taylor'], ['last' => 'required_with:first']);
  855. $this->assertFalse($v->passes());
  856. $v = new Validator($trans, ['first' => 'Taylor', 'last' => ''], ['last' => 'required_with:first']);
  857. $this->assertFalse($v->passes());
  858. $v = new Validator($trans, ['first' => ''], ['last' => 'required_with:first']);
  859. $this->assertTrue($v->passes());
  860. $v = new Validator($trans, [], ['last' => 'required_with:first']);
  861. $this->assertTrue($v->passes());
  862. $v = new Validator($trans, ['first' => 'Taylor', 'last' => 'Otwell'], ['last' => 'required_with:first']);
  863. $this->assertTrue($v->passes());
  864. $file = new File('', false);
  865. $v = new Validator($trans, ['file' => $file, 'foo' => ''], ['foo' => 'required_with:file']);
  866. $this->assertTrue($v->passes());
  867. $file = new File(__FILE__, false);
  868. $foo = new File(__FILE__, false);
  869. $v = new Validator($trans, ['file' => $file, 'foo' => $foo], ['foo' => 'required_with:file']);
  870. $this->assertTrue($v->passes());
  871. $file = new File(__FILE__, false);
  872. $foo = new File('', false);
  873. $v = new Validator($trans, ['file' => $file, 'foo' => $foo], ['foo' => 'required_with:file']);
  874. $this->assertFalse($v->passes());
  875. }
  876. public function testRequiredWithAll()
  877. {
  878. $trans = $this->getIlluminateArrayTranslator();
  879. $v = new Validator($trans, ['first' => 'foo'], ['last' => 'required_with_all:first,foo']);
  880. $this->assertTrue($v->passes());
  881. $v = new Validator($trans, ['first' => 'foo'], ['last' => 'required_with_all:first']);
  882. $this->assertFalse($v->passes());
  883. }
  884. public function testValidateRequiredWithout()
  885. {
  886. $trans = $this->getIlluminateArrayTranslator();
  887. $v = new Validator($trans, ['first' => 'Taylor'], ['last' => 'required_without:first']);
  888. $this->assertTrue($v->passes());
  889. $v = new Validator($trans, ['first' => 'Taylor', 'last' => ''], ['last' => 'required_without:first']);
  890. $this->assertTrue($v->passes());
  891. $v = new Validator($trans, ['first' => ''], ['last' => 'required_without:first']);
  892. $this->assertFalse($v->passes());
  893. $v = new Validator($trans, [], ['last' => 'required_without:first']);
  894. $this->assertFalse($v->passes());
  895. $v = new Validator($trans, ['first' => 'Taylor', 'last' => 'Otwell'], ['last' => 'required_without:first']);
  896. $this->assertTrue($v->passes());
  897. $v = new Validator($trans, ['last' => 'Otwell'], ['last' => 'required_without:first']);
  898. $this->assertTrue($v->passes());
  899. $file = new File('', false);
  900. $v = new Validator($trans, ['file' => $file], ['foo' => 'required_without:file']);
  901. $this->assertFalse($v->passes());
  902. $foo = new File('', false);
  903. $v = new Validator($trans, ['foo' => $foo], ['foo' => 'required_without:file']);
  904. $this->assertFalse($v->passes());
  905. $foo = new File(__FILE__, false);
  906. $v = new Validator($trans, ['foo' => $foo], ['foo' => 'required_without:file']);
  907. $this->assertTrue($v->passes());
  908. $file = new File(__FILE__, false);
  909. $foo = new File(__FILE__, false);
  910. $v = new Validator($trans, ['file' => $file, 'foo' => $foo], ['foo' => 'required_without:file']);
  911. $this->assertTrue($v->passes());
  912. $file = new File(__FILE__, false);
  913. $foo = new File('', false);
  914. $v = new Validator($trans, ['file' => $file, 'foo' => $foo], ['foo' => 'required_without:file']);
  915. $this->assertTrue($v->passes());
  916. $file = new File('', false);
  917. $foo = new File(__FILE__, false);
  918. $v = new Validator($trans, ['file' => $file, 'foo' => $foo], ['foo' => 'required_without:file']);
  919. $this->assertTrue($v->passes());
  920. $file = new File('', false);
  921. $foo = new File('', false);
  922. $v = new Validator($trans, ['file' => $file, 'foo' => $foo], ['foo' => 'required_without:file']);
  923. $this->assertFalse($v->passes());
  924. }
  925. public function testRequiredWithoutMultiple()
  926. {
  927. $trans = $this->getIlluminateArrayTranslator();
  928. $rules = [
  929. 'f1' => 'required_without:f2,f3',
  930. 'f2' => 'required_without:f1,f3',
  931. 'f3' => 'required_without:f1,f2',
  932. ];
  933. $v = new Validator($trans, [], $rules);
  934. $this->assertTrue($v->fails());
  935. $v = new Validator($trans, ['f1' => 'foo'], $rules);
  936. $this->assertTrue($v->fails());
  937. $v = new Validator($trans, ['f2' => 'foo'], $rules);
  938. $this->assertTrue($v->fails());
  939. $v = new Validator($trans, ['f3' => 'foo'], $rules);
  940. $this->assertTrue($v->fails());
  941. $v = new Validator($trans, ['f1' => 'foo', 'f2' => 'bar'], $rules);
  942. $this->assertTrue($v->passes());
  943. $v = new Validator($trans, ['f1' => 'foo', 'f3' => 'bar'], $rules);
  944. $this->assertTrue($v->passes());
  945. $v = new Validator($trans, ['f2' => 'foo', 'f3' => 'bar'], $rules);
  946. $this->assertTrue($v->passes());
  947. $v = new Validator($trans, ['f1' => 'foo', 'f2' => 'bar', 'f3' => 'baz'], $rules);
  948. $this->assertTrue($v->passes());
  949. }
  950. public function testRequiredWithoutAll()
  951. {
  952. $trans = $this->getIlluminateArrayTranslator();
  953. $rules = [
  954. 'f1' => 'required_without_all:f2,f3',
  955. 'f2' => 'required_without_all:f1,f3',
  956. 'f3' => 'required_without_all:f1,f2',
  957. ];
  958. $v = new Validator($trans, [], $rules);
  959. $this->assertTrue($v->fails());
  960. $v = new Validator($trans, ['f1' => 'foo'], $rules);
  961. $this->assertTrue($v->passes());
  962. $v = new Validator($trans, ['f2' => 'foo'], $rules);
  963. $this->assertTrue($v->passes());
  964. $v = new Validator($trans, ['f3' => 'foo'], $rules);
  965. $this->assertTrue($v->passes());
  966. $v = new Validator($trans, ['f1' => 'foo', 'f2' => 'bar'], $rules);
  967. $this->assertTrue($v->passes());
  968. $v = new Validator($trans, ['f1' => 'foo', 'f3' => 'bar'], $rules);
  969. $this->assertTrue($v->passes());
  970. $v = new Validator($trans, ['f2' => 'foo', 'f3' => 'bar'], $rules);
  971. $this->assertTrue($v->passes());
  972. $v = new Validator($trans, ['f1' => 'foo', 'f2' => 'bar', 'f3' => 'baz'], $rules);
  973. $this->assertTrue($v->passes());
  974. }
  975. public function testRequiredIf()
  976. {
  977. $trans = $this->getIlluminateArrayTranslator();
  978. $v = new Validator($trans, ['first' => 'taylor'], ['last' => 'required_if:first,taylor']);
  979. $this->assertTrue($v->fails());
  980. $trans = $this->getIlluminateArrayTranslator();
  981. $v = new Validator($trans, ['first' => 'taylor', 'last' => 'otwell'], ['last' => 'required_if:first,taylor']);
  982. $this->assertTrue($v->passes());
  983. $trans = $this->getIlluminateArrayTranslator();
  984. $v = new Validator($trans, ['first' => 'taylor', 'last' => 'otwell'], ['last' => 'required_if:first,taylor,dayle']);
  985. $this->assertTrue($v->passes());
  986. $trans = $this->getIlluminateArrayTranslator();
  987. $v = new Validator($trans, ['first' => 'dayle', 'last' => 'rees'], ['last' => 'required_if:first,taylor,dayle']);
  988. $this->assertTrue($v->passes());
  989. $trans = $this->getIlluminateArrayTranslator();
  990. $v = new Validator($trans, ['foo' => true], ['bar' => 'required_if:foo,false']);
  991. $this->assertTrue($v->passes());
  992. $trans = $this->getIlluminateArrayTranslator();
  993. $v = new Validator($trans, ['foo' => true], ['bar' => 'required_if:foo,null']);
  994. $this->assertTrue($v->passes());
  995. $trans = $this->getIlluminateArrayTranslator();
  996. $v = new Validator($trans, ['foo' => 0], ['bar' => 'required_if:foo,0']);
  997. $this->assertTrue($v->fails());
  998. $trans = $this->getIlluminateArrayTranslator();
  999. $v = new Validator($trans, ['foo' => '0'], ['bar' => 'required_if:foo,0']);
  1000. $this->assertTrue($v->fails());
  1001. $trans = $this->getIlluminateArrayTranslator();
  1002. $v = new Validator($trans, ['foo' => 1], ['bar' => 'required_if:foo,1']);
  1003. $this->assertTrue($v->fails());
  1004. $trans = $this->getIlluminateArrayTranslator();
  1005. $v = new Validator($trans, ['foo' => '1'], ['bar' => 'required_if:foo,1']);
  1006. $this->assertTrue($v->fails());
  1007. $trans = $this->getIlluminateArrayTranslator();
  1008. $v = new Validator($trans, ['foo' => true], ['bar' => 'required_if:foo,true']);
  1009. $this->assertTrue($v->fails());
  1010. $trans = $this->getIlluminateArrayTranslator();
  1011. $v = new Validator($trans, ['foo' => false], ['bar' => 'required_if:foo,false']);
  1012. $this->assertTrue($v->fails());
  1013. // error message when passed multiple values (required_if:foo,bar,baz)
  1014. $trans = $this->getIlluminateArrayTranslator();
  1015. $trans->addLines(['validation.required_if' => 'The :attribute field is required when :other is :value.'], 'en');
  1016. $v = new Validator($trans, ['first' => 'dayle', 'last' => ''], ['last' => 'RequiredIf:first,taylor,dayle']);
  1017. $this->assertFalse($v->passes());
  1018. $this->assertSame('The last field is required when first is dayle.', $v->messages()->first('last'));
  1019. $trans = $this->getIlluminateArrayTranslator();
  1020. $trans->addLines(['validation.required_if' => 'The :attribute field is required when :other is :value.'], 'en');
  1021. $v = new Validator($trans, ['foo' => 0], [
  1022. 'foo' => 'nullable|required|boolean',
  1023. 'bar' => 'required_if:foo,true',
  1024. 'baz' => 'required_if:foo,false',
  1025. ]);
  1026. $this->assertTrue($v->fails());
  1027. $this->assertCount(1, $v->messages());
  1028. $this->assertSame('The baz field is required when foo is 0.', $v->messages()->first('baz'));
  1029. $trans = $this->getIlluminateArrayTranslator();
  1030. $v = new Validator($trans, [], [
  1031. 'foo' => 'nullable|boolean',
  1032. 'baz' => 'nullable|required_if:foo,false',
  1033. ]);
  1034. $this->assertTrue($v->passes());
  1035. $trans = $this->getIlluminateArrayTranslator();
  1036. $v = new Validator($trans, ['foo' => null], [
  1037. 'foo' => 'nullable|boolean',
  1038. 'baz' => 'nullable|required_if:foo,false',
  1039. ]);
  1040. $this->assertTrue($v->passes());
  1041. $trans = $this->getIlluminateArrayTranslator();
  1042. $v = new Validator($trans, [], [
  1043. 'foo' => 'nullable|boolean',
  1044. 'baz' => 'nullable|required_if:foo,null',
  1045. ]);
  1046. $this->assertTrue($v->passes());
  1047. $trans = $this->getIlluminateArrayTranslator();
  1048. $trans->addLines(['validation.required_if' => 'The :attribute field is required when :other is :value.'], 'en');
  1049. $v = new Validator($trans, ['foo' => null], [
  1050. 'foo' => 'nullable|boolean',
  1051. 'baz' => 'nullable|required_if:foo,null',
  1052. ]);
  1053. $this->assertTrue($v->fails());
  1054. $this->assertCount(1, $v->messages());
  1055. $this->assertSame('The baz field is required when foo is empty.', $v->messages()->first('baz'));
  1056. }
  1057. public function testRequiredUnless()
  1058. {
  1059. $trans = $this->getIlluminateArrayTranslator();
  1060. $v = new Validator($trans, ['first' => 'sven'], ['last' => 'required_unless:first,taylor']);
  1061. $this->assertTrue($v->fails());
  1062. $trans = $this->getIlluminateArrayTranslator();
  1063. $v = new Validator($trans, ['first' => 'taylor'], ['last' => 'required_unless:first,taylor']);
  1064. $this->assertTrue($v->passes());
  1065. $trans = $this->getIlluminateArrayTranslator();
  1066. $v = new Validator($trans, ['first' => 'sven', 'last' => 'wittevrongel'], ['last' => 'required_unless:first,taylor']);
  1067. $this->assertTrue($v->passes());
  1068. $trans = $this->getIlluminateArrayTranslator();
  1069. $v = new Validator($trans, ['first' => 'taylor'], ['last' => 'required_unless:first,taylor,sven']);
  1070. $this->assertTrue($v->passes());
  1071. $trans = $this->getIlluminateArrayTranslator();
  1072. $v = new Validator($trans, ['first' => 'sven'], ['last' => 'required_unless:first,taylor,sven']);
  1073. $this->assertTrue($v->passes());
  1074. $trans = $this->getIlluminateArrayTranslator();
  1075. $v = new Validator($trans, ['foo' => false], ['bar' => 'required_unless:foo,false']);
  1076. $this->assertTrue($v->passes());
  1077. $trans = $this->getIlluminateArrayTranslator();
  1078. $v = new Validator($trans, ['foo' => false], ['bar' => 'required_unless:foo,true']);
  1079. $this->assertTrue($v->fails());
  1080. $trans = $this->getIlluminateArrayTranslator();
  1081. $v = new Validator($trans, ['bar' => '1'], ['bar' => 'required_unless:foo,true']);
  1082. $this->assertTrue($v->passes());
  1083. $trans = $this->getIlluminateArrayTranslator();
  1084. $v = new Validator($trans, [], ['bar' => 'required_unless:foo,true']);
  1085. $this->assertTrue($v->fails());
  1086. $trans = $this->getIlluminateArrayTranslator();
  1087. $v = new Validator($trans, [], ['bar' => 'required_unless:foo,null']);
  1088. $this->assertTrue($v->passes());
  1089. $trans = $this->getIlluminateArrayTranslator();
  1090. $v = new Validator($trans, ['foo' => true], ['bar' => 'required_unless:foo,null']);
  1091. $this->assertTrue($v->fails());
  1092. $trans = $this->getIlluminateArrayTranslator();
  1093. $v = new Validator($trans, ['foo' => '0'], ['bar' => 'required_unless:foo,0']);
  1094. $this->assertTrue($v->passes());
  1095. $trans = $this->getIlluminateArrayTranslator();
  1096. $v = new Validator($trans, ['foo' => 0], ['bar' => 'required_unless:foo,0']);
  1097. $this->assertTrue($v->passes());
  1098. $trans = $this->getIlluminateArrayTranslator();
  1099. $v = new Validator($trans, ['foo' => '1'], ['bar' => 'required_unless:foo,1']);
  1100. $this->assertTrue($v->passes());
  1101. $trans = $this->getIlluminateArrayTranslator();
  1102. $v = new Validator($trans, ['foo' => 1], ['bar' => 'required_unless:foo,1']);
  1103. $this->assertTrue($v->passes());
  1104. // error message when passed multiple values (required_unless:foo,bar,baz)
  1105. $trans = $this->getIlluminateArrayTranslator();
  1106. $trans->addLines(['validation.required_unless' => 'The :attribute field is required unless :other is in :values.'], 'en');
  1107. $v = new Validator($trans, ['first' => 'dayle', 'last' => ''], ['last' => 'RequiredUnless:first,taylor,sven']);
  1108. $this->assertFalse($v->passes());
  1109. $this->assertSame('The last field is required unless first is in taylor, sven.', $v->messages()->first('last'));
  1110. }
  1111. public function testProhibited()
  1112. {
  1113. $trans = $this->getIlluminateArrayTranslator();
  1114. $v = new Validator($trans, [], ['name' => 'prohibited']);
  1115. $this->assertTrue($v->passes());
  1116. $v = new Validator($trans, ['last' => 'bar'], ['name' => 'prohibited']);
  1117. $this->assertTrue($v->passes());
  1118. $v = new Validator($trans, ['name' => 'foo'], ['name' => 'prohibited']);
  1119. $this->assertTrue($v->fails());
  1120. $file = new File('', false);
  1121. $v = new Validator($trans, ['name' => $file], ['name' => 'prohibited']);
  1122. $this->assertTrue($v->fails());
  1123. $file = new File(__FILE__, false);
  1124. $v = new Validator($trans, ['name' => $file], ['name' => 'prohibited']);
  1125. $this->assertTrue($v->fails());
  1126. $file = new File(__FILE__, false);
  1127. $file2 = new File(__FILE__, false);
  1128. $v = new Validator($trans, ['files' => [$file, $file2]], ['files.0' => 'prohibited', 'files.1' => 'prohibited']);
  1129. $this->assertTrue($v->fails());
  1130. $v = new Validator($trans, ['files' => [$file, $file2]], ['files' => 'prohibited']);
  1131. $this->assertTrue($v->fails());
  1132. }
  1133. public function testProhibitedIf()
  1134. {
  1135. $trans = $this->getIlluminateArrayTranslator();
  1136. $v = new Validator($trans, ['first' => 'taylor', 'last' => 'otwell'], ['last' => 'prohibited_if:first,taylor']);
  1137. $this->assertTrue($v->fails());
  1138. $trans = $this->getIlluminateArrayTranslator();
  1139. $v = new Validator($trans, ['first' => 'taylor'], ['last' => 'prohibited_if:first,taylor']);
  1140. $this->assertTrue($v->passes());
  1141. $trans = $this->getIlluminateArrayTranslator();
  1142. $v = new Validator($trans, ['first' => 'taylor', 'last' => 'otwell'], ['last' => 'prohibited_if:first,taylor,jess']);
  1143. $this->assertTrue($v->fails());
  1144. $trans = $this->getIlluminateArrayTranslator();
  1145. $v = new Validator($trans, ['first' => 'taylor'], ['last' => 'prohibited_if:first,taylor,jess']);
  1146. $this->assertTrue($v->passes());
  1147. $trans = $this->getIlluminateArrayTranslator();
  1148. $v = new Validator($trans, ['foo' => true, 'bar' => 'baz'], ['bar' => 'prohibited_if:foo,false']);
  1149. $this->assertTrue($v->passes());
  1150. $trans = $this->getIlluminateArrayTranslator();
  1151. $v = new Validator($trans, ['foo' => true, 'bar' => 'baz'], ['bar' => 'prohibited_if:foo,true']);
  1152. $this->assertTrue($v->fails());
  1153. // error message when passed multiple values (prohibited_if:foo,bar,baz)
  1154. $trans = $this->getIlluminateArrayTranslator();
  1155. $trans->addLines(['validation.prohibited_if' => 'The :attribute field is prohibited when :other is :value.'], 'en');
  1156. $v = new Validator($trans, ['first' => 'jess', 'last' => 'archer'], ['last' => 'prohibited_if:first,taylor,jess']);
  1157. $this->assertFalse($v->passes());
  1158. $this->assertSame('The last field is prohibited when first is jess.', $v->messages()->first('last'));
  1159. }
  1160. public function testProhibitedUnless()
  1161. {
  1162. $trans = $this->getIlluminateArrayTranslator();
  1163. $v = new Validator($trans, ['first' => 'jess', 'last' => 'archer'], ['last' => 'prohibited_unless:first,taylor']);
  1164. $this->assertTrue($v->fails());
  1165. $trans = $this->getIlluminateArrayTranslator();
  1166. $v = new Validator($trans, ['first' => 'taylor', 'last' => 'otwell'], ['last' => 'prohibited_unless:first,taylor']);
  1167. $this->assertTrue($v->passes());
  1168. $trans = $this->getIlluminateArrayTranslator();
  1169. $v = new Validator($trans, ['first' => 'jess'], ['last' => 'prohibited_unless:first,taylor']);
  1170. $this->assertTrue($v->passes());
  1171. $trans = $this->getIlluminateArrayTranslator();
  1172. $v = new Validator($trans, ['first' => 'taylor', 'last' => 'otwell'], ['last' => 'prohibited_unless:first,taylor,jess']);
  1173. $this->assertTrue($v->passes());
  1174. $trans = $this->getIlluminateArrayTranslator();
  1175. $v = new Validator($trans, ['first' => 'jess', 'last' => 'archer'], ['last' => 'prohibited_unless:first,taylor,jess']);
  1176. $this->assertTrue($v->passes());
  1177. $trans = $this->getIlluminateArrayTranslator();
  1178. $v = new Validator($trans, ['foo' => false, 'bar' => 'baz'], ['bar' => 'prohibited_unless:foo,false']);
  1179. $this->assertTrue($v->passes());
  1180. $trans = $this->getIlluminateArrayTranslator();
  1181. $v = new Validator($trans, ['foo' => false, 'bar' => 'baz'], ['bar' => 'prohibited_unless:foo,true']);
  1182. $this->assertTrue($v->fails());
  1183. // error message when passed multiple values (prohibited_unless:foo,bar,baz)
  1184. $trans = $this->getIlluminateArrayTranslator();
  1185. $trans->addLines(['validation.prohibited_unless' => 'The :attribute field is prohibited unless :other is in :values.'], 'en');
  1186. $v = new Validator($trans, ['first' => 'tim', 'last' => 'macdonald'], ['last' => 'prohibitedUnless:first,taylor,jess']);
  1187. $this->assertFalse($v->passes());
  1188. $this->assertSame('The last field is prohibited unless first is in taylor, jess.', $v->messages()->first('last'));
  1189. }
  1190. public function testProhibits()
  1191. {
  1192. $trans = $this->getIlluminateArrayTranslator();
  1193. $v = new Validator($trans, ['email' => 'foo', 'emails' => ['foo']], ['email' => 'prohibits:emails']);
  1194. $this->assertTrue($v->fails());
  1195. $trans = $this->getIlluminateArrayTranslator();
  1196. $v = new Validator($trans, ['email' => 'foo', 'emails' => []], ['email' => 'prohibits:emails']);
  1197. $this->assertTrue($v->fails());
  1198. $trans = $this->getIlluminateArrayTranslator();
  1199. $v = new Validator($trans, ['email' => 'foo', 'emails' => ''], ['email' => 'prohibits:emails']);
  1200. $this->assertTrue($v->fails());
  1201. $trans = $this->getIlluminateArrayTranslator();
  1202. $v = new Validator($trans, ['email' => 'foo', 'emails' => null], ['email' => 'prohibits:emails']);
  1203. $this->assertTrue($v->fails());
  1204. $trans = $this->getIlluminateArrayTranslator();
  1205. $v = new Validator($trans, ['email' => 'foo', 'emails' => false], ['email' => 'prohibits:emails']);
  1206. $this->assertTrue($v->fails());
  1207. $trans = $this->getIlluminateArrayTranslator();
  1208. $v = new Validator($trans, ['email' => 'foo', 'emails' => ['foo']], ['email' => 'prohibits:email_address,emails']);
  1209. $this->assertTrue($v->fails());
  1210. $trans = $this->getIlluminateArrayTranslator();
  1211. $v = new Validator($trans, ['email' => 'foo'], ['email' => 'prohibits:emails']);
  1212. $this->assertTrue($v->passes());
  1213. $trans = $this->getIlluminateArrayTranslator();
  1214. $v = new Validator($trans, ['email' => 'foo', 'other' => 'foo'], ['email' => 'prohibits:email_address,emails']);
  1215. $this->assertTrue($v->passes());
  1216. $trans = $this->getIlluminateArrayTranslator();
  1217. $trans->addLines(['validation.prohibits' => 'The :attribute field prohibits :other being present.'], 'en');
  1218. $v = new Validator($trans, ['email' => 'foo', 'emails' => 'bar', 'email_address' => 'baz'], ['email' => 'prohibits:emails,email_address']);
  1219. $this->assertFalse($v->passes());
  1220. $this->assertSame('The email field prohibits emails / email address being present.', $v->messages()->first('email'));
  1221. $trans = $this->getIlluminateArrayTranslator();
  1222. $v = new Validator($trans, [
  1223. 'foo' => [
  1224. ['email' => 'foo', 'emails' => 'foo'],
  1225. ['emails' => 'foo'],
  1226. ],
  1227. ], ['foo.*.email' => 'prohibits:foo.*.emails']);
  1228. $this->assertFalse($v->passes());
  1229. $this->assertTrue($v->messages()->has('foo.0.email'));
  1230. $this->assertFalse($v->messages()->has('foo.1.email'));
  1231. }
  1232. public function testFailedFileUploads()
  1233. {
  1234. $trans = $this->getIlluminateArrayTranslator();
  1235. // If file is not successfully uploaded validation should fail with a
  1236. // 'uploaded' error message instead of the original rule.
  1237. $file = m::mock(UploadedFile::class);
  1238. $file->shouldReceive('isValid')->andReturn(false);
  1239. $file->shouldNotReceive('getSize');
  1240. $v = new Validator($trans, ['photo' => $file], ['photo' => 'Max:10']);
  1241. $this->assertTrue($v->fails());
  1242. $this->assertEquals(['validation.uploaded'], $v->errors()->get('photo'));
  1243. // Even "required" will not run if the file failed to upload.
  1244. $file = m::mock(UploadedFile::class);
  1245. $file->shouldReceive('isValid')->once()->andReturn(false);
  1246. $v = new Validator($trans, ['photo' => $file], ['photo' => 'required']);
  1247. $this->assertTrue($v->fails());
  1248. $this->assertEquals(['validation.uploaded'], $v->errors()->get('photo'));
  1249. // It should only fail with that rule if a validation rule implies it's
  1250. // a file. Otherwise it should fail with the regular rule.
  1251. $file = m::mock(UploadedFile::class);
  1252. $file->shouldReceive('isValid')->andReturn(false);
  1253. $v = new Validator($trans, ['photo' => $file], ['photo' => 'string']);
  1254. $this->assertTrue($v->fails());
  1255. $this->assertEquals(['validation.string'], $v->errors()->get('photo'));
  1256. // Validation shouldn't continue if a file failed to upload.
  1257. $file = m::mock(UploadedFile::class);
  1258. $file->shouldReceive('isValid')->once()->andReturn(false);
  1259. $v = new Validator($trans, ['photo' => $file], ['photo' => 'file|mimes:pdf|min:10']);
  1260. $this->assertTrue($v->fails());
  1261. $this->assertEquals(['validation.uploaded'], $v->errors()->get('photo'));
  1262. }
  1263. public function testValidateInArray()
  1264. {
  1265. $trans = $this->getIlluminateArrayTranslator();
  1266. $v = new Validator($trans, ['foo' => [1, 2, 3], 'bar' => [1, 2]], ['foo.*' => 'in_array:bar.*']);
  1267. $this->assertFalse($v->passes());
  1268. $trans = $this->getIlluminateArrayTranslator();
  1269. $v = new Validator($trans, ['foo' => [1, 2], 'bar' => [1, 2, 3]], ['foo.*' => 'in_array:bar.*']);
  1270. $this->assertTrue($v->passes());
  1271. $trans = $this->getIlluminateArrayTranslator();
  1272. $v = new Validator($trans, ['foo' => [['bar_id' => 5], ['bar_id' => 2]], 'bar' => [['id' => 1, ['id' => 2]]]], ['foo.*.bar_id' => 'in_array:bar.*.id']);
  1273. $this->assertFalse($v->passes());
  1274. $trans = $this->getIlluminateArrayTranslator();
  1275. $v = new Validator($trans, ['foo' => [['bar_id' => 1], ['bar_id' => 2]], 'bar' => [['id' => 1, ['id' => 2]]]], ['foo.*.bar_id' => 'in_array:bar.*.id']);
  1276. $this->assertTrue($v->passes());
  1277. $trans->addLines(['validation.in_array' => 'The value of :attribute does not exist in :other.'], 'en');
  1278. $v = new Validator($trans, ['foo' => [1, 2, 3], 'bar' => [1, 2]], ['foo.*' => 'in_array:bar.*']);
  1279. $this->assertSame('The value of foo.2 does not exist in bar.*.', $v->messages()->first('foo.2'));
  1280. }
  1281. public function testValidateConfirmed()
  1282. {
  1283. $trans = $this->getIlluminateArrayTranslator();
  1284. $v = new Validator($trans, ['password' => 'foo'], ['password' => 'Confirmed']);
  1285. $this->assertFalse($v->passes());
  1286. $v = new Validator($trans, ['password' => 'foo', 'password_confirmation' => 'bar'], ['password' => 'Confirmed']);
  1287. $this->assertFalse($v->passes());
  1288. $v = new Validator($trans, ['password' => 'foo', 'password_confirmation' => 'foo'], ['password' => 'Confirmed']);
  1289. $this->assertTrue($v->passes());
  1290. $v = new Validator($trans, ['password' => '1e2', 'password_confirmation' => '100'], ['password' => 'Confirmed']);
  1291. $this->assertFalse($v->passes());
  1292. }
  1293. public function testValidateSame()
  1294. {
  1295. $trans = $this->getIlluminateArrayTranslator();
  1296. $v = new Validator($trans, ['foo' => 'bar', 'baz' => 'boom'], ['foo' => 'Same:baz']);
  1297. $this->assertFalse($v->passes());
  1298. $v = new Validator($trans, ['foo' => 'bar'], ['foo' => 'Same:baz']);
  1299. $this->assertFalse($v->passes());
  1300. $v = new Validator($trans, ['foo' => 'bar', 'baz' => 'bar'], ['foo' => 'Same:baz']);
  1301. $this->assertTrue($v->passes());
  1302. $v = new Validator($trans, ['foo' => '1e2', 'baz' => '100'], ['foo' => 'Same:baz']);
  1303. $this->assertFalse($v->passes());
  1304. $v = new Validator($trans, ['foo' => null, 'baz' => null], ['foo' => 'Same:baz']);
  1305. $this->assertTrue($v->passes());
  1306. }
  1307. public function testValidateDifferent()
  1308. {
  1309. $trans = $this->getIlluminateArrayTranslator();
  1310. $v = new Validator($trans, ['foo' => 'bar', 'baz' => 'boom'], ['foo' => 'Different:baz']);
  1311. $this->assertTrue($v->passes());
  1312. $v = new Validator($trans, ['foo' => 'bar', 'baz' => null], ['foo' => 'Different:baz']);
  1313. $this->assertTrue($v->passes());
  1314. $v = new Validator($trans, ['foo' => 'bar'], ['foo' => 'Different:baz']);
  1315. $this->assertTrue($v->passes());
  1316. $v = new Validator($trans, ['foo' => 'bar', 'baz' => 'bar'], ['foo' => 'Different:baz']);
  1317. $this->assertFalse($v->passes());
  1318. $v = new Validator($trans, ['foo' => '1e2', 'baz' => '100'], ['foo' => 'Different:baz']);
  1319. $this->assertTrue($v->passes());
  1320. $v = new Validator($trans, ['foo' => 'bar', 'fuu' => 'baa', 'baz' => 'boom'], ['foo' => 'Different:fuu,baz']);
  1321. $this->assertTrue($v->passes());
  1322. $v = new Validator($trans, ['foo' => 'bar', 'baz' => 'boom'], ['foo' => 'Different:fuu,baz']);
  1323. $this->assertTrue($v->passes());
  1324. $v = new Validator($trans, ['foo' => 'bar', 'fuu' => 'bar', 'baz' => 'boom'], ['foo' => 'Different:fuu,baz']);
  1325. $this->assertFalse($v->passes());
  1326. }
  1327. public function testGreaterThan()
  1328. {
  1329. $trans = $this->getIlluminateArrayTranslator();
  1330. $v = new Validator($trans, ['lhs' => 15, 'rhs' => 10], ['lhs' => 'numeric|gt:rhs']);
  1331. $this->assertTrue($v->passes());
  1332. $v = new Validator($trans, ['lhs' => 15, 'rhs' => 'string'], ['lhs' => 'numeric|gt:rhs']);
  1333. $this->assertTrue($v->fails());
  1334. $v = new Validator($trans, ['lhs' => 15.0, 'rhs' => 10], ['lhs' => 'numeric|gt:rhs']);
  1335. $this->assertTrue($v->passes());
  1336. $v = new Validator($trans, ['lhs' => '15', 'rhs' => 10], ['lhs' => 'numeric|gt:rhs']);
  1337. $this->assertTrue($v->passes());
  1338. $v = new Validator($trans, ['lhs' => 15], ['lhs' => 'numeric|gt:rhs']);
  1339. $this->assertTrue($v->fails());
  1340. $v = new Validator($trans, ['lhs' => 15.0], ['lhs' => 'numeric|gt:10']);
  1341. $this->assertTrue($v->passes());
  1342. $v = new Validator($trans, ['lhs' => 5, 10 => 1], ['lhs' => 'numeric|gt:10']);
  1343. $this->assertTrue($v->fails());
  1344. $v = new Validator($trans, ['lhs' => '15'], ['lhs' => 'numeric|gt:10']);
  1345. $this->assertTrue($v->passes());
  1346. $v = new Validator($trans, ['lhs' => 'longer string', 'rhs' => 'string'], ['lhs' => 'gt:rhs']);
  1347. $this->assertTrue($v->passes());
  1348. $v = new Validator($trans, ['lhs' => ['string'], 'rhs' => [1, 'string']], ['lhs' => 'gt:rhs']);
  1349. $this->assertTrue($v->fails());
  1350. $fileOne = $this->getMockBuilder(File::class)->onlyMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
  1351. $fileOne->expects($this->any())->method('getSize')->willReturn(5472);
  1352. $fileTwo = $this->getMockBuilder(File::class)->onlyMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
  1353. $fileTwo->expects($this->any())->method('getSize')->willReturn(3151);
  1354. $v = new Validator($trans, ['lhs' => $fileOne, 'rhs' => $fileTwo], ['lhs' => 'gt:rhs']);
  1355. $this->assertTrue($v->passes());
  1356. $v = new Validator($trans, ['lhs' => 15], ['lhs' => 'numeric|gt:10']);
  1357. $this->assertTrue($v->passes());
  1358. }
  1359. public function testLessThan()
  1360. {
  1361. $trans = $this->getIlluminateArrayTranslator();
  1362. $v = new Validator($trans, ['lhs' => 15, 'rhs' => 10], ['lhs' => 'numeric|lt:rhs']);
  1363. $this->assertTrue($v->fails());
  1364. $v = new Validator($trans, ['lhs' => 15, 'rhs' => 'string'], ['lhs' => 'numeric|lt:rhs']);
  1365. $this->assertTrue($v->fails());
  1366. $v = new Validator($trans, ['lhs' => 15.0, 'rhs' => 10], ['lhs' => 'numeric|lt:rhs']);
  1367. $this->assertTrue($v->fails());
  1368. $v = new Validator($trans, ['lhs' => '15', 'rhs' => 10], ['lhs' => 'numeric|lt:rhs']);
  1369. $this->assertTrue($v->fails());
  1370. $v = new Validator($trans, ['lhs' => 15], ['lhs' => 'numeric|lt:rhs']);
  1371. $this->assertTrue($v->fails());
  1372. $v = new Validator($trans, ['lhs' => 15.0], ['lhs' => 'numeric|lt:10']);
  1373. $this->assertTrue($v->fails());
  1374. $v = new Validator($trans, ['lhs' => '15'], ['lhs' => 'numeric|lt:10']);
  1375. $this->assertTrue($v->fails());
  1376. $v = new Validator($trans, ['lhs' => 'longer string', 'rhs' => 'string'], ['lhs' => 'lt:rhs']);
  1377. $this->assertTrue($v->fails());
  1378. $v = new Validator($trans, ['lhs' => ['string'], 'rhs' => [1, 'string']], ['lhs' => 'lt:rhs']);
  1379. $this->assertTrue($v->passes());
  1380. $fileOne = $this->getMockBuilder(File::class)->onlyMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
  1381. $fileOne->expects($this->any())->method('getSize')->willReturn(5472);
  1382. $fileTwo = $this->getMockBuilder(File::class)->onlyMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
  1383. $fileTwo->expects($this->any())->method('getSize')->willReturn(3151);
  1384. $v = new Validator($trans, ['lhs' => $fileOne, 'rhs' => $fileTwo], ['lhs' => 'lt:rhs']);
  1385. $this->assertTrue($v->fails());
  1386. $v = new Validator($trans, ['lhs' => 15], ['lhs' => 'numeric|lt:10']);
  1387. $this->assertTrue($v->fails());
  1388. }
  1389. public function testGreaterThanOrEqual()
  1390. {
  1391. $trans = $this->getIlluminateArrayTranslator();
  1392. $v = new Validator($trans, ['lhs' => 15, 'rhs' => 15], ['lhs' => 'numeric|gte:rhs']);
  1393. $this->assertTrue($v->passes());
  1394. $v = new Validator($trans, ['lhs' => 15, 'rhs' => 'string'], ['lhs' => 'numeric|gte:rhs']);
  1395. $this->assertTrue($v->fails());
  1396. $v = new Validator($trans, ['lhs' => 15.0, 'rhs' => 15], ['lhs' => 'numeric|gte:rhs']);
  1397. $this->assertTrue($v->passes());
  1398. $v = new Validator($trans, ['lhs' => '15', 'rhs' => 15], ['lhs' => 'numeric|gte:rhs']);
  1399. $this->assertTrue($v->passes());
  1400. $v = new Validator($trans, ['lhs' => 15], ['lhs' => 'numeric|gte:rhs']);
  1401. $this->assertTrue($v->fails());
  1402. $v = new Validator($trans, ['lhs' => 15.0], ['lhs' => 'numeric|gte:15']);
  1403. $this->assertTrue($v->passes());
  1404. $v = new Validator($trans, ['lhs' => '15'], ['lhs' => 'numeric|gte:15']);
  1405. $this->assertTrue($v->passes());
  1406. $v = new Validator($trans, ['lhs' => 'longer string', 'rhs' => 'string'], ['lhs' => 'gte:rhs']);
  1407. $this->assertTrue($v->passes());
  1408. $v = new Validator($trans, ['lhs' => ['string'], 'rhs' => [1, 'string']], ['lhs' => 'gte:rhs']);
  1409. $this->assertTrue($v->fails());
  1410. $fileOne = $this->getMockBuilder(File::class)->onlyMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
  1411. $fileOne->expects($this->any())->method('getSize')->willReturn(5472);
  1412. $fileTwo = $this->getMockBuilder(File::class)->onlyMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
  1413. $fileTwo->expects($this->any())->method('getSize')->willReturn(5472);
  1414. $v = new Validator($trans, ['lhs' => $fileOne, 'rhs' => $fileTwo], ['lhs' => 'gte:rhs']);
  1415. $this->assertTrue($v->passes());
  1416. $v = new Validator($trans, ['lhs' => 15], ['lhs' => 'numeric|gte:15']);
  1417. $this->assertTrue($v->passes());
  1418. }
  1419. public function testLessThanOrEqual()
  1420. {
  1421. $trans = $this->getIlluminateArrayTranslator();
  1422. $v = new Validator($trans, ['lhs' => 15, 'rhs' => 15], ['lhs' => 'numeric|lte:rhs']);
  1423. $this->assertTrue($v->passes());
  1424. $v = new Validator($trans, ['lhs' => 15, 'rhs' => 'string'], ['lhs' => 'numeric|lte:rhs']);
  1425. $this->assertTrue($v->fails());
  1426. $v = new Validator($trans, ['lhs' => 15.0, 'rhs' => 15], ['lhs' => 'numeric|lte:rhs']);
  1427. $this->assertTrue($v->passes());
  1428. $v = new Validator($trans, ['lhs' => '15', 'rhs' => 15], ['lhs' => 'numeric|lte:rhs']);
  1429. $this->assertTrue($v->passes());
  1430. $v = new Validator($trans, ['lhs' => 15], ['lhs' => 'numeric|lte:rhs']);
  1431. $this->assertTrue($v->fails());
  1432. $v = new Validator($trans, ['lhs' => 15.0], ['lhs' => 'numeric|lte:10']);
  1433. $this->assertTrue($v->fails());
  1434. $v = new Validator($trans, ['lhs' => '15'], ['lhs' => 'numeric|lte:10']);
  1435. $this->assertTrue($v->fails());
  1436. $v = new Validator($trans, ['lhs' => 'longer string', 'rhs' => 'string'], ['lhs' => 'lte:rhs']);
  1437. $this->assertTrue($v->fails());
  1438. $v = new Validator($trans, ['lhs' => ['string'], 'rhs' => [1, 'string']], ['lhs' => 'lte:rhs']);
  1439. $this->assertTrue($v->passes());
  1440. $fileOne = $this->getMockBuilder(File::class)->onlyMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
  1441. $fileOne->expects($this->any())->method('getSize')->willReturn(5472);
  1442. $fileTwo = $this->getMockBuilder(File::class)->onlyMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
  1443. $fileTwo->expects($this->any())->method('getSize')->willReturn(5472);
  1444. $v = new Validator($trans, ['lhs' => $fileOne, 'rhs' => $fileTwo], ['lhs' => 'lte:rhs']);
  1445. $this->assertTrue($v->passes());
  1446. $v = new Validator($trans, ['lhs' => 15], ['lhs' => 'numeric|lte:10']);
  1447. $this->assertTrue($v->fails());
  1448. }
  1449. public function testValidateAccepted()
  1450. {
  1451. $trans = $this->getIlluminateArrayTranslator();
  1452. $v = new Validator($trans, ['foo' => 'no'], ['foo' => 'Accepted']);
  1453. $this->assertFalse($v->passes());
  1454. $v = new Validator($trans, ['foo' => 'off'], ['foo' => 'Accepted']);
  1455. $this->assertFalse($v->passes());
  1456. $v = new Validator($trans, ['foo' => null], ['foo' => 'Accepted']);
  1457. $this->assertFalse($v->passes());
  1458. $v = new Validator($trans, [], ['foo' => 'Accepted']);
  1459. $this->assertFalse($v->passes());
  1460. $v = new Validator($trans, ['foo' => 0], ['foo' => 'Accepted']);
  1461. $this->assertFalse($v->passes());
  1462. $v = new Validator($trans, ['foo' => '0'], ['foo' => 'Accepted']);
  1463. $this->assertFalse($v->passes());
  1464. $v = new Validator($trans, ['foo' => false], ['foo' => 'Accepted']);
  1465. $this->assertFalse($v->passes());
  1466. $v = new Validator($trans, ['foo' => 'false'], ['foo' => 'Accepted']);
  1467. $this->assertFalse($v->passes());
  1468. $v = new Validator($trans, ['foo' => 'yes'], ['foo' => 'Accepted']);
  1469. $this->assertTrue($v->passes());
  1470. $v = new Validator($trans, ['foo' => 'on'], ['foo' => 'Accepted']);
  1471. $this->assertTrue($v->passes());
  1472. $v = new Validator($trans, ['foo' => '1'], ['foo' => 'Accepted']);
  1473. $this->assertTrue($v->passes());
  1474. $v = new Validator($trans, ['foo' => 1], ['foo' => 'Accepted']);
  1475. $this->assertTrue($v->passes());
  1476. $v = new Validator($trans, ['foo' => true], ['foo' => 'Accepted']);
  1477. $this->assertTrue($v->passes());
  1478. $v = new Validator($trans, ['foo' => 'true'], ['foo' => 'Accepted']);
  1479. $this->assertTrue($v->passes());
  1480. }
  1481. public function testValidateAcceptedIf()
  1482. {
  1483. $trans = $this->getIlluminateArrayTranslator();
  1484. $v = new Validator($trans, ['foo' => 'no', 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
  1485. $this->assertFalse($v->passes());
  1486. $v = new Validator($trans, ['foo' => 'off', 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
  1487. $this->assertFalse($v->passes());
  1488. $v = new Validator($trans, ['foo' => null, 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
  1489. $this->assertFalse($v->passes());
  1490. $v = new Validator($trans, ['foo' => 0, 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
  1491. $this->assertFalse($v->passes());
  1492. $v = new Validator($trans, ['foo' => '0', 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
  1493. $this->assertFalse($v->passes());
  1494. $v = new Validator($trans, ['foo' => false, 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
  1495. $this->assertFalse($v->passes());
  1496. $v = new Validator($trans, ['foo' => 'false', 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
  1497. $this->assertFalse($v->passes());
  1498. $v = new Validator($trans, ['foo' => 'yes', 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
  1499. $this->assertTrue($v->passes());
  1500. $v = new Validator($trans, ['foo' => 'on', 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
  1501. $this->assertTrue($v->passes());
  1502. $v = new Validator($trans, ['foo' => '1', 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
  1503. $this->assertTrue($v->passes());
  1504. $v = new Validator($trans, ['foo' => 1, 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
  1505. $this->assertTrue($v->passes());
  1506. $v = new Validator($trans, ['foo' => true, 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
  1507. $this->assertTrue($v->passes());
  1508. $v = new Validator($trans, ['foo' => 'true', 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
  1509. $this->assertTrue($v->passes());
  1510. // accepted_if:bar,aaa
  1511. $trans = $this->getIlluminateArrayTranslator();
  1512. $trans->addLines(['validation.accepted_if' => 'The :attribute field must be accepted when :other is :value.'], 'en');
  1513. $v = new Validator($trans, ['foo' => 'no', 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
  1514. $this->assertFalse($v->passes());
  1515. $v->messages()->setFormat(':message');
  1516. $this->assertSame('The foo field must be accepted when bar is aaa.', $v->messages()->first('foo'));
  1517. // accepted_if:bar,aaa,...
  1518. $trans = $this->getIlluminateArrayTranslator();
  1519. $trans->addLines(['validation.accepted_if' => 'The :attribute field must be accepted when :other is :value.'], 'en');
  1520. $v = new Validator($trans, ['foo' => 'no', 'bar' => 'abc'], ['foo' => 'accepted_if:bar,aaa,bbb,abc']);
  1521. $this->assertFalse($v->passes());
  1522. $v->messages()->setFormat(':message');
  1523. $this->assertSame('The foo field must be accepted when bar is abc.', $v->messages()->first('foo'));
  1524. // accepted_if:bar,boolean
  1525. $trans = $this->getIlluminateArrayTranslator();
  1526. $trans->addLines(['validation.accepted_if' => 'The :attribute field must be accepted when :other is :value.'], 'en');
  1527. $v = new Validator($trans, ['foo' => 'no', 'bar' => false], ['foo' => 'accepted_if:bar,false']);
  1528. $this->assertFalse($v->passes());
  1529. $v->messages()->setFormat(':message');
  1530. $this->assertSame('The foo field must be accepted when bar is false.', $v->messages()->first('foo'));
  1531. $trans = $this->getIlluminateArrayTranslator();
  1532. $trans->addLines(['validation.accepted_if' => 'The :attribute field must be accepted when :other is :value.'], 'en');
  1533. $v = new Validator($trans, ['foo' => 'no', 'bar' => true], ['foo' => 'accepted_if:bar,true']);
  1534. $this->assertFalse($v->passes());
  1535. $v->messages()->setFormat(':message');
  1536. $this->assertSame('The foo field must be accepted when bar is true.', $v->messages()->first('foo'));
  1537. }
  1538. public function testValidateDeclined()
  1539. {
  1540. $trans = $this->getIlluminateArrayTranslator();
  1541. $v = new Validator($trans, ['foo' => 'yes'], ['foo' => 'Declined']);
  1542. $this->assertFalse($v->passes());
  1543. $v = new Validator($trans, ['foo' => 'on'], ['foo' => 'Declined']);
  1544. $this->assertFalse($v->passes());
  1545. $v = new Validator($trans, ['foo' => null], ['foo' => 'Declined']);
  1546. $this->assertFalse($v->passes());
  1547. $v = new Validator($trans, [], ['foo' => 'Declined']);
  1548. $this->assertFalse($v->passes());
  1549. $v = new Validator($trans, ['foo' => 1], ['foo' => 'Declined']);
  1550. $this->assertFalse($v->passes());
  1551. $v = new Validator($trans, ['foo' => '1'], ['foo' => 'Declined']);
  1552. $this->assertFalse($v->passes());
  1553. $v = new Validator($trans, ['foo' => true], ['foo' => 'Declined']);
  1554. $this->assertFalse($v->passes());
  1555. $v = new Validator($trans, ['foo' => 'true'], ['foo' => 'Declined']);
  1556. $this->assertFalse($v->passes());
  1557. $v = new Validator($trans, ['foo' => 'no'], ['foo' => 'Declined']);
  1558. $this->assertTrue($v->passes());
  1559. $v = new Validator($trans, ['foo' => 'off'], ['foo' => 'Declined']);
  1560. $this->assertTrue($v->passes());
  1561. $v = new Validator($trans, ['foo' => '0'], ['foo' => 'Declined']);
  1562. $this->assertTrue($v->passes());
  1563. $v = new Validator($trans, ['foo' => 0], ['foo' => 'Declined']);
  1564. $this->assertTrue($v->passes());
  1565. $v = new Validator($trans, ['foo' => false], ['foo' => 'Declined']);
  1566. $this->assertTrue($v->passes());
  1567. $v = new Validator($trans, ['foo' => 'false'], ['foo' => 'Declined']);
  1568. $this->assertTrue($v->passes());
  1569. }
  1570. public function testValidateDeclinedIf()
  1571. {
  1572. $trans = $this->getIlluminateArrayTranslator();
  1573. $v = new Validator($trans, ['foo' => 'yes', 'bar' => 'aaa'], ['foo' => 'declined_if:bar,aaa']);
  1574. $this->assertFalse($v->passes());
  1575. $v = new Validator($trans, ['foo' => 'on', 'bar' => 'aaa'], ['foo' => 'declined_if:bar,aaa']);
  1576. $this->assertFalse($v->passes());
  1577. $v = new Validator($trans, ['foo' => null, 'bar' => 'aaa'], ['foo' => 'declined_if:bar,aaa']);
  1578. $this->assertFalse($v->passes());
  1579. $v = new Validator($trans, ['foo' => 1, 'bar' => 'aaa'], ['foo' => 'declined_if:bar,aaa']);
  1580. $this->assertFalse($v->passes());
  1581. $v = new Validator($trans, ['foo' => '1', 'bar' => 'aaa'], ['foo' => 'declined_if:bar,aaa']);
  1582. $this->assertFalse($v->passes());
  1583. $v = new Validator($trans, ['foo' => true, 'bar' => 'aaa'], ['foo' => 'declined_if:bar,aaa']);
  1584. $this->assertFalse($v->passes());
  1585. $v = new Validator($trans, ['foo' => 'true', 'bar' => 'aaa'], ['foo' => 'declined_if:bar,aaa']);
  1586. $this->assertFalse($v->passes());
  1587. $v = new Validator($trans, ['foo' => 'no', 'bar' => 'aaa'], ['foo' => 'declined_if:bar,aaa']);
  1588. $this->assertTrue($v->passes());
  1589. $v = new Validator($trans, ['foo' => 'off', 'bar' => 'aaa'], ['foo' => 'declined_if:bar,aaa']);
  1590. $this->assertTrue($v->passes());
  1591. $v = new Validator($trans, ['foo' => 0, 'bar' => 'aaa'], ['foo' => 'declined_if:bar,aaa']);
  1592. $this->assertTrue($v->passes());
  1593. $v = new Validator($trans, ['foo' => '0', 'bar' => 'aaa'], ['foo' => 'declined_if:bar,aaa']);
  1594. $this->assertTrue($v->passes());
  1595. $v = new Validator($trans, ['foo' => false, 'bar' => 'aaa'], ['foo' => 'declined_if:bar,aaa']);
  1596. $this->assertTrue($v->passes());
  1597. $v = new Validator($trans, ['foo' => 'false', 'bar' => 'aaa'], ['foo' => 'declined_if:bar,aaa']);
  1598. $this->assertTrue($v->passes());
  1599. // declined_if:bar,aaa
  1600. $trans = $this->getIlluminateArrayTranslator();
  1601. $trans->addLines(['validation.declined_if' => 'The :attribute field must be declined when :other is :value.'], 'en');
  1602. $v = new Validator($trans, ['foo' => 'yes', 'bar' => 'aaa'], ['foo' => 'declined_if:bar,aaa']);
  1603. $this->assertFalse($v->passes());
  1604. $v->messages()->setFormat(':message');
  1605. $this->assertSame('The foo field must be declined when bar is aaa.', $v->messages()->first('foo'));
  1606. // declined_if:bar,aaa,...
  1607. $trans = $this->getIlluminateArrayTranslator();
  1608. $trans->addLines(['validation.declined_if' => 'The :attribute field must be declined when :other is :value.'], 'en');
  1609. $v = new Validator($trans, ['foo' => 'yes', 'bar' => 'abc'], ['foo' => 'declined_if:bar,aaa,bbb,abc']);
  1610. $this->assertFalse($v->passes());
  1611. $v->messages()->setFormat(':message');
  1612. $this->assertSame('The foo field must be declined when bar is abc.', $v->messages()->first('foo'));
  1613. // declined_if:bar,boolean
  1614. $trans = $this->getIlluminateArrayTranslator();
  1615. $trans->addLines(['validation.declined_if' => 'The :attribute field must be declined when :other is :value.'], 'en');
  1616. $v = new Validator($trans, ['foo' => 'yes', 'bar' => false], ['foo' => 'declined_if:bar,false']);
  1617. $this->assertFalse($v->passes());
  1618. $v->messages()->setFormat(':message');
  1619. $this->assertSame('The foo field must be declined when bar is false.', $v->messages()->first('foo'));
  1620. $trans = $this->getIlluminateArrayTranslator();
  1621. $trans->addLines(['validation.declined_if' => 'The :attribute field must be declined when :other is :value.'], 'en');
  1622. $v = new Validator($trans, ['foo' => 'yes', 'bar' => true], ['foo' => 'declined_if:bar,true']);
  1623. $this->assertFalse($v->passes());
  1624. $v->messages()->setFormat(':message');
  1625. $this->assertSame('The foo field must be declined when bar is true.', $v->messages()->first('foo'));
  1626. }
  1627. public function testValidateEndsWith()
  1628. {
  1629. $trans = $this->getIlluminateArrayTranslator();
  1630. $v = new Validator($trans, ['x' => 'hello world'], ['x' => 'ends_with:hello']);
  1631. $this->assertFalse($v->passes());
  1632. $trans = $this->getIlluminateArrayTranslator();
  1633. $v = new Validator($trans, ['x' => 'hello world'], ['x' => 'ends_with:world']);
  1634. $this->assertTrue($v->passes());
  1635. $trans = $this->getIlluminateArrayTranslator();
  1636. $v = new Validator($trans, ['x' => 'hello world'], ['x' => 'ends_with:world,hello']);
  1637. $this->assertTrue($v->passes());
  1638. $trans = $this->getIlluminateArrayTranslator();
  1639. $trans->addLines(['validation.ends_with' => 'The :attribute must end with one of the following values :values'], 'en');
  1640. $v = new Validator($trans, ['url' => 'laravel.com'], ['url' => 'ends_with:http']);
  1641. $this->assertFalse($v->passes());
  1642. $this->assertSame('The url must end with one of the following values http', $v->messages()->first('url'));
  1643. $trans = $this->getIlluminateArrayTranslator();
  1644. $trans->addLines(['validation.ends_with' => 'The :attribute must end with one of the following values :values'], 'en');
  1645. $v = new Validator($trans, ['url' => 'laravel.com'], ['url' => 'ends_with:http,https']);
  1646. $this->assertFalse($v->passes());
  1647. $this->assertSame('The url must end with one of the following values http, https', $v->messages()->first('url'));
  1648. }
  1649. public function testValidateStartsWith()
  1650. {
  1651. $trans = $this->getIlluminateArrayTranslator();
  1652. $v = new Validator($trans, ['x' => 'hello world'], ['x' => 'starts_with:hello']);
  1653. $this->assertTrue($v->passes());
  1654. $trans = $this->getIlluminateArrayTranslator();
  1655. $v = new Validator($trans, ['x' => 'hello world'], ['x' => 'starts_with:world']);
  1656. $this->assertFalse($v->passes());
  1657. $trans = $this->getIlluminateArrayTranslator();
  1658. $v = new Validator($trans, ['x' => 'hello world'], ['x' => 'starts_with:world,hello']);
  1659. $this->assertTrue($v->passes());
  1660. $trans = $this->getIlluminateArrayTranslator();
  1661. $trans->addLines(['validation.starts_with' => 'The :attribute must start with one of the following values :values'], 'en');
  1662. $v = new Validator($trans, ['url' => 'laravel.com'], ['url' => 'starts_with:http']);
  1663. $this->assertFalse($v->passes());
  1664. $this->assertSame('The url must start with one of the following values http', $v->messages()->first('url'));
  1665. $trans = $this->getIlluminateArrayTranslator();
  1666. $trans->addLines(['validation.starts_with' => 'The :attribute must start with one of the following values :values'], 'en');
  1667. $v = new Validator($trans, ['url' => 'laravel.com'], ['url' => 'starts_with:http,https']);
  1668. $this->assertFalse($v->passes());
  1669. $this->assertSame('The url must start with one of the following values http, https', $v->messages()->first('url'));
  1670. }
  1671. public function testValidateString()
  1672. {
  1673. $trans = $this->getIlluminateArrayTranslator();
  1674. $v = new Validator($trans, ['x' => 'aslsdlks'], ['x' => 'string']);
  1675. $this->assertTrue($v->passes());
  1676. $trans = $this->getIlluminateArrayTranslator();
  1677. $v = new Validator($trans, ['x' => ['blah' => 'test']], ['x' => 'string']);
  1678. $this->assertFalse($v->passes());
  1679. }
  1680. public function testValidateJson()
  1681. {
  1682. $trans = $this->getIlluminateArrayTranslator();
  1683. $v = new Validator($trans, ['foo' => 'aslksd'], ['foo' => 'json']);
  1684. $this->assertFalse($v->passes());
  1685. $trans = $this->getIlluminateArrayTranslator();
  1686. $v = new Validator($trans, ['foo' => '[]'], ['foo' => 'json']);
  1687. $this->assertTrue($v->passes());
  1688. $trans = $this->getIlluminateArrayTranslator();
  1689. $v = new Validator($trans, ['foo' => '{"name":"John","age":"34"}'], ['foo' => 'json']);
  1690. $this->assertTrue($v->passes());
  1691. $trans = $this->getIlluminateArrayTranslator();
  1692. $v = new Validator($trans, ['foo' => ['array']], ['foo' => 'json']);
  1693. $this->assertFalse($v->passes());
  1694. }
  1695. public function testValidateBoolean()
  1696. {
  1697. $trans = $this->getIlluminateArrayTranslator();
  1698. $v = new Validator($trans, ['foo' => 'no'], ['foo' => 'Boolean']);
  1699. $this->assertFalse($v->passes());
  1700. $v = new Validator($trans, ['foo' => 'yes'], ['foo' => 'Boolean']);
  1701. $this->assertFalse($v->passes());
  1702. $v = new Validator($trans, ['foo' => 'false'], ['foo' => 'Boolean']);
  1703. $this->assertFalse($v->passes());
  1704. $v = new Validator($trans, ['foo' => 'true'], ['foo' => 'Boolean']);
  1705. $this->assertFalse($v->passes());
  1706. $v = new Validator($trans, [], ['foo' => 'Boolean']);
  1707. $this->assertTrue($v->passes());
  1708. $v = new Validator($trans, ['foo' => false], ['foo' => 'Boolean']);
  1709. $this->assertTrue($v->passes());
  1710. $v = new Validator($trans, ['foo' => true], ['foo' => 'Boolean']);
  1711. $this->assertTrue($v->passes());
  1712. $v = new Validator($trans, ['foo' => '1'], ['foo' => 'Boolean']);
  1713. $this->assertTrue($v->passes());
  1714. $v = new Validator($trans, ['foo' => 1], ['foo' => 'Boolean']);
  1715. $this->assertTrue($v->passes());
  1716. $v = new Validator($trans, ['foo' => '0'], ['foo' => 'Boolean']);
  1717. $this->assertTrue($v->passes());
  1718. $v = new Validator($trans, ['foo' => 0], ['foo' => 'Boolean']);
  1719. $this->assertTrue($v->passes());
  1720. }
  1721. public function testValidateBool()
  1722. {
  1723. $trans = $this->getIlluminateArrayTranslator();
  1724. $v = new Validator($trans, ['foo' => 'no'], ['foo' => 'Bool']);
  1725. $this->assertFalse($v->passes());
  1726. $v = new Validator($trans, ['foo' => 'yes'], ['foo' => 'Bool']);
  1727. $this->assertFalse($v->passes());
  1728. $v = new Validator($trans, ['foo' => 'false'], ['foo' => 'Bool']);
  1729. $this->assertFalse($v->passes());
  1730. $v = new Validator($trans, ['foo' => 'true'], ['foo' => 'Bool']);
  1731. $this->assertFalse($v->passes());
  1732. $v = new Validator($trans, [], ['foo' => 'Bool']);
  1733. $this->assertTrue($v->passes());
  1734. $v = new Validator($trans, ['foo' => false], ['foo' => 'Bool']);
  1735. $this->assertTrue($v->passes());
  1736. $v = new Validator($trans, ['foo' => true], ['foo' => 'Bool']);
  1737. $this->assertTrue($v->passes());
  1738. $v = new Validator($trans, ['foo' => '1'], ['foo' => 'Bool']);
  1739. $this->assertTrue($v->passes());
  1740. $v = new Validator($trans, ['foo' => 1], ['foo' => 'Bool']);
  1741. $this->assertTrue($v->passes());
  1742. $v = new Validator($trans, ['foo' => '0'], ['foo' => 'Bool']);
  1743. $this->assertTrue($v->passes());
  1744. $v = new Validator($trans, ['foo' => 0], ['foo' => 'Bool']);
  1745. $this->assertTrue($v->passes());
  1746. }
  1747. public function testValidateNumeric()
  1748. {
  1749. $trans = $this->getIlluminateArrayTranslator();
  1750. $v = new Validator($trans, ['foo' => 'asdad'], ['foo' => 'Numeric']);
  1751. $this->assertFalse($v->passes());
  1752. $v = new Validator($trans, ['foo' => '1.23'], ['foo' => 'Numeric']);
  1753. $this->assertTrue($v->passes());
  1754. $v = new Validator($trans, ['foo' => '-1'], ['foo' => 'Numeric']);
  1755. $this->assertTrue($v->passes());
  1756. $v = new Validator($trans, ['foo' => '1'], ['foo' => 'Numeric']);
  1757. $this->assertTrue($v->passes());
  1758. }
  1759. public function testValidateInteger()
  1760. {
  1761. $trans = $this->getIlluminateArrayTranslator();
  1762. $v = new Validator($trans, ['foo' => 'asdad'], ['foo' => 'Integer']);
  1763. $this->assertFalse($v->passes());
  1764. $v = new Validator($trans, ['foo' => '1.23'], ['foo' => 'Integer']);
  1765. $this->assertFalse($v->passes());
  1766. $v = new Validator($trans, ['foo' => '-1'], ['foo' => 'Integer']);
  1767. $this->assertTrue($v->passes());
  1768. $v = new Validator($trans, ['foo' => '1'], ['foo' => 'Integer']);
  1769. $this->assertTrue($v->passes());
  1770. }
  1771. public function testValidateInt()
  1772. {
  1773. $trans = $this->getIlluminateArrayTranslator();
  1774. $v = new Validator($trans, ['foo' => 'asdad'], ['foo' => 'Int']);
  1775. $this->assertFalse($v->passes());
  1776. $v = new Validator($trans, ['foo' => '1.23'], ['foo' => 'Int']);
  1777. $this->assertFalse($v->passes());
  1778. $v = new Validator($trans, ['foo' => '-1'], ['foo' => 'Int']);
  1779. $this->assertTrue($v->passes());
  1780. $v = new Validator($trans, ['foo' => '1'], ['foo' => 'Int']);
  1781. $this->assertTrue($v->passes());
  1782. }
  1783. public function testValidateDigits()
  1784. {
  1785. $trans = $this->getIlluminateArrayTranslator();
  1786. $v = new Validator($trans, ['foo' => '12345'], ['foo' => 'Digits:5']);
  1787. $this->assertTrue($v->passes());
  1788. $v = new Validator($trans, ['foo' => '123'], ['foo' => 'Digits:200']);
  1789. $this->assertFalse($v->passes());
  1790. $v = new Validator($trans, ['foo' => '+2.37'], ['foo' => 'Digits:5']);
  1791. $this->assertTrue($v->fails());
  1792. $v = new Validator($trans, ['foo' => '2e7'], ['foo' => 'Digits:3']);
  1793. $this->assertTrue($v->fails());
  1794. $trans = $this->getIlluminateArrayTranslator();
  1795. $v = new Validator($trans, ['foo' => '12345'], ['foo' => 'digits_between:1,6']);
  1796. $this->assertTrue($v->passes());
  1797. $v = new Validator($trans, ['foo' => 'bar'], ['foo' => 'digits_between:1,10']);
  1798. $this->assertFalse($v->passes());
  1799. $v = new Validator($trans, ['foo' => '123'], ['foo' => 'digits_between:4,5']);
  1800. $this->assertFalse($v->passes());
  1801. $v = new Validator($trans, ['foo' => '+12.3'], ['foo' => 'digits_between:1,6']);
  1802. $this->assertFalse($v->passes());
  1803. }
  1804. public function testValidateSize()
  1805. {
  1806. $trans = $this->getIlluminateArrayTranslator();
  1807. $v = new Validator($trans, ['foo' => 'asdad'], ['foo' => 'Size:3']);
  1808. $this->assertFalse($v->passes());
  1809. $v = new Validator($trans, ['foo' => 'anc'], ['foo' => 'Size:3']);
  1810. $this->assertTrue($v->passes());
  1811. $v = new Validator($trans, ['foo' => '123'], ['foo' => 'Numeric|Size:3']);
  1812. $this->assertFalse($v->passes());
  1813. $v = new Validator($trans, ['foo' => '3'], ['foo' => 'Numeric|Size:3']);
  1814. $this->assertTrue($v->passes());
  1815. $v = new Validator($trans, ['foo' => [1, 2, 3]], ['foo' => 'Array|Size:3']);
  1816. $this->assertTrue($v->passes());
  1817. $v = new Validator($trans, ['foo' => [1, 2, 3]], ['foo' => 'Array|Size:4']);
  1818. $this->assertFalse($v->passes());
  1819. $file = $this->getMockBuilder(File::class)->onlyMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
  1820. $file->expects($this->any())->method('getSize')->willReturn(3072);
  1821. $v = new Validator($trans, ['photo' => $file], ['photo' => 'Size:3']);
  1822. $this->assertTrue($v->passes());
  1823. $file = $this->getMockBuilder(File::class)->onlyMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
  1824. $file->expects($this->any())->method('getSize')->willReturn(4072);
  1825. $v = new Validator($trans, ['photo' => $file], ['photo' => 'Size:3']);
  1826. $this->assertFalse($v->passes());
  1827. }
  1828. public function testValidateBetween()
  1829. {
  1830. $trans = $this->getIlluminateArrayTranslator();
  1831. $v = new Validator($trans, ['foo' => 'asdad'], ['foo' => 'Between:3,4']);
  1832. $this->assertFalse($v->passes());
  1833. $v = new Validator($trans, ['foo' => 'anc'], ['foo' => 'Between:3,5']);
  1834. $this->assertTrue($v->passes());
  1835. $v = new Validator($trans, ['foo' => 'ancf'], ['foo' => 'Between:3,5']);
  1836. $this->assertTrue($v->passes());
  1837. $v = new Validator($trans, ['foo' => 'ancfs'], ['foo' => 'Between:3,5']);
  1838. $this->assertTrue($v->passes());
  1839. $v = new Validator($trans, ['foo' => '123'], ['foo' => 'Numeric|Between:50,100']);
  1840. $this->assertFalse($v->passes());
  1841. $v = new Validator($trans, ['foo' => '3'], ['foo' => 'Numeric|Between:1,5']);
  1842. $this->assertTrue($v->passes());
  1843. $v = new Validator($trans, ['foo' => [1, 2, 3]], ['foo' => 'Array|Between:1,5']);
  1844. $this->assertTrue($v->passes());
  1845. $v = new Validator($trans, ['foo' => [1, 2, 3]], ['foo' => 'Array|Between:1,2']);
  1846. $this->assertFalse($v->passes());
  1847. $file = $this->getMockBuilder(File::class)->onlyMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
  1848. $file->expects($this->any())->method('getSize')->willReturn(3072);
  1849. $v = new Validator($trans, ['photo' => $file], ['photo' => 'Between:1,5']);
  1850. $this->assertTrue($v->passes());
  1851. $file = $this->getMockBuilder(File::class)->onlyMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
  1852. $file->expects($this->any())->method('getSize')->willReturn(4072);
  1853. $v = new Validator($trans, ['photo' => $file], ['photo' => 'Between:1,2']);
  1854. $this->assertFalse($v->passes());
  1855. }
  1856. public function testValidateMin()
  1857. {
  1858. $trans = $this->getIlluminateArrayTranslator();
  1859. $v = new Validator($trans, ['foo' => '3'], ['foo' => 'Min:3']);
  1860. $this->assertFalse($v->passes());
  1861. $v = new Validator($trans, ['foo' => 'anc'], ['foo' => 'Min:3']);
  1862. $this->assertTrue($v->passes());
  1863. $v = new Validator($trans, ['foo' => '2'], ['foo' => 'Numeric|Min:3']);
  1864. $this->assertFalse($v->passes());
  1865. $v = new Validator($trans, ['foo' => '5'], ['foo' => 'Numeric|Min:3']);
  1866. $this->assertTrue($v->passes());
  1867. $v = new Validator($trans, ['foo' => [1, 2, 3, 4]], ['foo' => 'Array|Min:3']);
  1868. $this->assertTrue($v->passes());
  1869. $v = new Validator($trans, ['foo' => [1, 2]], ['foo' => 'Array|Min:3']);
  1870. $this->assertFalse($v->passes());
  1871. $file = $this->getMockBuilder(File::class)->onlyMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
  1872. $file->expects($this->any())->method('getSize')->willReturn(3072);
  1873. $v = new Validator($trans, ['photo' => $file], ['photo' => 'Min:2']);
  1874. $this->assertTrue($v->passes());
  1875. $file = $this->getMockBuilder(File::class)->onlyMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
  1876. $file->expects($this->any())->method('getSize')->willReturn(4072);
  1877. $v = new Validator($trans, ['photo' => $file], ['photo' => 'Min:10']);
  1878. $this->assertFalse($v->passes());
  1879. }
  1880. public function testValidateMax()
  1881. {
  1882. $trans = $this->getIlluminateArrayTranslator();
  1883. $v = new Validator($trans, ['foo' => 'aslksd'], ['foo' => 'Max:3']);
  1884. $this->assertFalse($v->passes());
  1885. $v = new Validator($trans, ['foo' => 'anc'], ['foo' => 'Max:3']);
  1886. $this->assertTrue($v->passes());
  1887. $v = new Validator($trans, ['foo' => '211'], ['foo' => 'Numeric|Max:100']);
  1888. $this->assertFalse($v->passes());
  1889. $v = new Validator($trans, ['foo' => '22'], ['foo' => 'Numeric|Max:33']);
  1890. $this->assertTrue($v->passes());
  1891. $v = new Validator($trans, ['foo' => [1, 2, 3]], ['foo' => 'Array|Max:4']);
  1892. $this->assertTrue($v->passes());
  1893. $v = new Validator($trans, ['foo' => [1, 2, 3]], ['foo' => 'Array|Max:2']);
  1894. $this->assertFalse($v->passes());
  1895. $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['isValid', 'getSize'])->setConstructorArgs([__FILE__, basename(__FILE__)])->getMock();
  1896. $file->method('isValid')->willReturn(true);
  1897. $file->method('getSize')->willReturn(3072);
  1898. $v = new Validator($trans, ['photo' => $file], ['photo' => 'Max:10']);
  1899. $this->assertTrue($v->passes());
  1900. $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['isValid', 'getSize'])->setConstructorArgs([__FILE__, basename(__FILE__)])->getMock();
  1901. $file->method('isValid')->willReturn(true);
  1902. $file->method('getSize')->willReturn(4072);
  1903. $v = new Validator($trans, ['photo' => $file], ['photo' => 'Max:2']);
  1904. $this->assertFalse($v->passes());
  1905. $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['isValid'])->setConstructorArgs([__FILE__, basename(__FILE__)])->getMock();
  1906. $file->expects($this->any())->method('isValid')->willReturn(false);
  1907. $v = new Validator($trans, ['photo' => $file], ['photo' => 'Max:10']);
  1908. $this->assertFalse($v->passes());
  1909. }
  1910. /**
  1911. * @param mixed $input
  1912. * @param mixed $allowed
  1913. * @param bool $passes
  1914. *
  1915. * @dataProvider multipleOfDataProvider
  1916. */
  1917. public function testValidateMutlpleOf($input, $allowed, $passes)
  1918. {
  1919. $trans = $this->getIlluminateArrayTranslator();
  1920. $trans->addLines(['validation.multiple_of' => 'The :attribute must be a multiple of :value'], 'en');
  1921. $v = new Validator($trans, ['foo' => $input], ['foo' => "multiple_of:{$allowed}"]);
  1922. $this->assertSame($passes, $v->passes());
  1923. if ($v->fails()) {
  1924. $this->assertSame("The foo must be a multiple of {$allowed}", $v->messages()->first('foo'));
  1925. } else {
  1926. $this->assertSame('', $v->messages()->first('foo'));
  1927. }
  1928. }
  1929. public function multipleOfDataProvider()
  1930. {
  1931. return [
  1932. [0, 0, false], // zero (same)
  1933. [0, 10, true], // zero + integer
  1934. [10, 0, false],
  1935. [0, 10.1, true], // zero + float
  1936. [10.1, 0, false],
  1937. [0, -10, true], // zero + -integer
  1938. [-10, 0, false],
  1939. [0, -10.1, true], // zero + -float
  1940. [-10.1, 0, false],
  1941. [10, 10, true], // integer (same)
  1942. [10, 5, true], // integer + integer
  1943. [10, 4, false],
  1944. [20, 10, true],
  1945. [5, 10, false],
  1946. [10, -5, true], // integer + -integer
  1947. [10, -4, false],
  1948. [-20, 10, true],
  1949. [-5, 10, false],
  1950. [-10, -10, true], // -integer (same)
  1951. [-10, -5, true], // -integer + -integer
  1952. [-10, -4, false],
  1953. [-20, -10, true],
  1954. [-5, -10, false],
  1955. [10, 10.0, true], // integer + float (same)
  1956. [10, 5.0, true], // integer + float
  1957. [10, 4.0, false],
  1958. [20.0, 10, true],
  1959. [5.0, 10, false],
  1960. [10.0, -10.0, true], // integer + -float (same)
  1961. [10, -5.0, true], // integer + -float
  1962. [10, -4.0, false],
  1963. [-20.0, 10, true],
  1964. [-5.0, 10, false],
  1965. [10.0, -10.0, true], // -integer + float (same)
  1966. [-10, 5.0, true], // -integer + float
  1967. [-10, 4.0, false],
  1968. [20.0, -10, true],
  1969. [5.0, -10, false],
  1970. [10.5, 10.5, true], // float (same)
  1971. [10.5, 0.5, true], // float + float
  1972. [10.5, 0.3, true], // 10.5/.3 = 35, tricky for floating point division
  1973. [31.5, 10.5, true],
  1974. [31.6, 10.5, false],
  1975. [10.5, -0.5, true], // float + -float
  1976. [10.5, -0.3, true], // 10.5/.3 = 35, tricky for floating point division
  1977. [-31.5, 10.5, true],
  1978. [-31.6, 10.5, false],
  1979. [-10.5, -10.5, true], // -float (same)
  1980. [-10.5, -0.5, true], // -float + -float
  1981. [-10.5, -0.3, true], // 10.5/.3 = 35, tricky for floating point division
  1982. [-31.5, -10.5, true],
  1983. [-31.6, -10.5, false],
  1984. [2, .1, true], // fmod does this "wrong", it should be 0, but fmod(2, .1) = .1
  1985. [.75, .05, true], // fmod does this "wrong", it should be 0, but fmod(.75, .05) = .05
  1986. [.9, .3, true], // .9/.3 = 3, tricky for floating point division
  1987. ['foo', 1, false], // invalid values
  1988. [1, 'foo', false],
  1989. ['foo', 'foo', false],
  1990. [1, '', false],
  1991. [1, null, false],
  1992. ];
  1993. }
  1994. public function testProperMessagesAreReturnedForSizes()
  1995. {
  1996. $trans = $this->getIlluminateArrayTranslator();
  1997. $trans->addLines(['validation.min.numeric' => 'numeric', 'validation.size.string' => 'string', 'validation.max.file' => 'file'], 'en');
  1998. $v = new Validator($trans, ['name' => '3'], ['name' => 'Numeric|Min:5']);
  1999. $this->assertFalse($v->passes());
  2000. $v->messages()->setFormat(':message');
  2001. $this->assertSame('numeric', $v->messages()->first('name'));
  2002. $v = new Validator($trans, ['name' => 'asasdfadsfd'], ['name' => 'Size:2']);
  2003. $this->assertFalse($v->passes());
  2004. $v->messages()->setFormat(':message');
  2005. $this->assertSame('string', $v->messages()->first('name'));
  2006. $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
  2007. $file->expects($this->any())->method('getSize')->willReturn(4072);
  2008. $file->expects($this->any())->method('isValid')->willReturn(true);
  2009. $v = new Validator($trans, ['photo' => $file], ['photo' => 'Max:3']);
  2010. $this->assertFalse($v->passes());
  2011. $v->messages()->setFormat(':message');
  2012. $this->assertSame('file', $v->messages()->first('photo'));
  2013. }
  2014. public function testValidateGtPlaceHolderIsReplacedProperly()
  2015. {
  2016. $trans = $this->getIlluminateArrayTranslator();
  2017. $trans->addLines([
  2018. 'validation.gt.numeric' => ':value',
  2019. 'validation.gt.string' => ':value',
  2020. 'validation.gt.file' => ':value',
  2021. 'validation.gt.array' => ':value',
  2022. ], 'en');
  2023. $v = new Validator($trans, ['items' => '3'], ['items' => 'gt:4']);
  2024. $this->assertFalse($v->passes());
  2025. $this->assertEquals(4, $v->messages()->first('items'));
  2026. $v = new Validator($trans, ['items' => 3, 'more' => 5], ['items' => 'numeric|gt:more']);
  2027. $this->assertFalse($v->passes());
  2028. $this->assertEquals(5, $v->messages()->first('items'));
  2029. $v = new Validator($trans, ['items' => 'abc', 'more' => 'abcde'], ['items' => 'gt:more']);
  2030. $this->assertFalse($v->passes());
  2031. $this->assertEquals(5, $v->messages()->first('items'));
  2032. $v = new Validator($trans, ['max' => 10], ['min' => 'numeric', 'max' => 'numeric|gt:min'], [], ['min' => 'minimum value', 'max' => 'maximum value']);
  2033. $this->assertFalse($v->passes());
  2034. $v->messages()->setFormat(':message');
  2035. $this->assertSame('minimum value', $v->messages()->first('max'));
  2036. $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
  2037. $file->expects($this->any())->method('getSize')->willReturn(4072);
  2038. $file->expects($this->any())->method('isValid')->willReturn(true);
  2039. $biggerFile = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
  2040. $biggerFile->expects($this->any())->method('getSize')->willReturn(5120);
  2041. $biggerFile->expects($this->any())->method('isValid')->willReturn(true);
  2042. $v = new Validator($trans, ['photo' => $file, 'bigger' => $biggerFile], ['photo' => 'file|gt:bigger']);
  2043. $this->assertFalse($v->passes());
  2044. $this->assertEquals(5, $v->messages()->first('photo'));
  2045. $v = new Validator($trans, ['items' => [1, 2, 3], 'more' => [0, 1, 2, 3]], ['items' => 'gt:more']);
  2046. $this->assertFalse($v->passes());
  2047. $this->assertEquals(4, $v->messages()->first('items'));
  2048. }
  2049. public function testValidateLtPlaceHolderIsReplacedProperly()
  2050. {
  2051. $trans = $this->getIlluminateArrayTranslator();
  2052. $trans->addLines([
  2053. 'validation.lt.numeric' => ':value',
  2054. 'validation.lt.string' => ':value',
  2055. 'validation.lt.file' => ':value',
  2056. 'validation.lt.array' => ':value',
  2057. ], 'en');
  2058. $v = new Validator($trans, ['items' => '3'], ['items' => 'lt:2']);
  2059. $this->assertFalse($v->passes());
  2060. $this->assertEquals(2, $v->messages()->first('items'));
  2061. $v = new Validator($trans, ['items' => 3, 'less' => 2], ['items' => 'numeric|lt:less']);
  2062. $this->assertFalse($v->passes());
  2063. $this->assertEquals(2, $v->messages()->first('items'));
  2064. $v = new Validator($trans, ['items' => 'abc', 'less' => 'ab'], ['items' => 'lt:less']);
  2065. $this->assertFalse($v->passes());
  2066. $this->assertEquals(2, $v->messages()->first('items'));
  2067. $v = new Validator($trans, ['min' => 1], ['min' => 'numeric|lt:max', 'max' => 'numeric'], [], ['min' => 'minimum value', 'max' => 'maximum value']);
  2068. $this->assertFalse($v->passes());
  2069. $v->messages()->setFormat(':message');
  2070. $this->assertSame('maximum value', $v->messages()->first('min'));
  2071. $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
  2072. $file->expects($this->any())->method('getSize')->willReturn(4072);
  2073. $file->expects($this->any())->method('isValid')->willReturn(true);
  2074. $smallerFile = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
  2075. $smallerFile->expects($this->any())->method('getSize')->willReturn(2048);
  2076. $smallerFile->expects($this->any())->method('isValid')->willReturn(true);
  2077. $v = new Validator($trans, ['photo' => $file, 'smaller' => $smallerFile], ['photo' => 'file|lt:smaller']);
  2078. $this->assertFalse($v->passes());
  2079. $this->assertEquals(2, $v->messages()->first('photo'));
  2080. $v = new Validator($trans, ['items' => [1, 2, 3], 'less' => [0, 1]], ['items' => 'lt:less']);
  2081. $this->assertFalse($v->passes());
  2082. $this->assertEquals(2, $v->messages()->first('items'));
  2083. }
  2084. public function testValidateGtePlaceHolderIsReplacedProperly()
  2085. {
  2086. $trans = $this->getIlluminateArrayTranslator();
  2087. $trans->addLines([
  2088. 'validation.gte.numeric' => ':value',
  2089. 'validation.gte.string' => ':value',
  2090. 'validation.gte.file' => ':value',
  2091. 'validation.gte.array' => ':value',
  2092. ], 'en');
  2093. $v = new Validator($trans, ['items' => '3'], ['items' => 'gte:4']);
  2094. $this->assertFalse($v->passes());
  2095. $this->assertEquals(4, $v->messages()->first('items'));
  2096. $v = new Validator($trans, ['items' => 3, 'more' => 5], ['items' => 'numeric|gte:more']);
  2097. $this->assertFalse($v->passes());
  2098. $this->assertEquals(5, $v->messages()->first('items'));
  2099. $v = new Validator($trans, ['items' => 'abc', 'more' => 'abcde'], ['items' => 'gte:more']);
  2100. $this->assertFalse($v->passes());
  2101. $this->assertEquals(5, $v->messages()->first('items'));
  2102. $v = new Validator($trans, ['max' => 10], ['min' => 'numeric', 'max' => 'numeric|gte:min'], [], ['min' => 'minimum value', 'max' => 'maximum value']);
  2103. $this->assertFalse($v->passes());
  2104. $v->messages()->setFormat(':message');
  2105. $this->assertSame('minimum value', $v->messages()->first('max'));
  2106. $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
  2107. $file->expects($this->any())->method('getSize')->willReturn(4072);
  2108. $file->expects($this->any())->method('isValid')->willReturn(true);
  2109. $biggerFile = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
  2110. $biggerFile->expects($this->any())->method('getSize')->willReturn(5120);
  2111. $biggerFile->expects($this->any())->method('isValid')->willReturn(true);
  2112. $v = new Validator($trans, ['photo' => $file, 'bigger' => $biggerFile], ['photo' => 'file|gte:bigger']);
  2113. $this->assertFalse($v->passes());
  2114. $this->assertEquals(5, $v->messages()->first('photo'));
  2115. $v = new Validator($trans, ['items' => [1, 2, 3], 'more' => [0, 1, 2, 3]], ['items' => 'gte:more']);
  2116. $this->assertFalse($v->passes());
  2117. $this->assertEquals(4, $v->messages()->first('items'));
  2118. }
  2119. public function testValidateLtePlaceHolderIsReplacedProperly()
  2120. {
  2121. $trans = $this->getIlluminateArrayTranslator();
  2122. $trans->addLines([
  2123. 'validation.lte.numeric' => ':value',
  2124. 'validation.lte.string' => ':value',
  2125. 'validation.lte.file' => ':value',
  2126. 'validation.lte.array' => ':value',
  2127. ], 'en');
  2128. $v = new Validator($trans, ['items' => '3'], ['items' => 'lte:2']);
  2129. $this->assertFalse($v->passes());
  2130. $this->assertEquals(2, $v->messages()->first('items'));
  2131. $v = new Validator($trans, ['items' => 3, 'less' => 2], ['items' => 'numeric|lte:less']);
  2132. $this->assertFalse($v->passes());
  2133. $this->assertEquals(2, $v->messages()->first('items'));
  2134. $v = new Validator($trans, ['items' => 'abc', 'less' => 'ab'], ['items' => 'lte:less']);
  2135. $this->assertFalse($v->passes());
  2136. $this->assertEquals(2, $v->messages()->first('items'));
  2137. $v = new Validator($trans, ['min' => 1], ['min' => 'numeric|lte:max', 'max' => 'numeric'], [], ['min' => 'minimum value', 'max' => 'maximum value']);
  2138. $this->assertFalse($v->passes());
  2139. $v->messages()->setFormat(':message');
  2140. $this->assertSame('maximum value', $v->messages()->first('min'));
  2141. $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
  2142. $file->expects($this->any())->method('getSize')->willReturn(4072);
  2143. $file->expects($this->any())->method('isValid')->willReturn(true);
  2144. $smallerFile = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
  2145. $smallerFile->expects($this->any())->method('getSize')->willReturn(2048);
  2146. $smallerFile->expects($this->any())->method('isValid')->willReturn(true);
  2147. $v = new Validator($trans, ['photo' => $file, 'smaller' => $smallerFile], ['photo' => 'file|lte:smaller']);
  2148. $this->assertFalse($v->passes());
  2149. $this->assertEquals(2, $v->messages()->first('photo'));
  2150. $v = new Validator($trans, ['items' => [1, 2, 3], 'less' => [0, 1]], ['items' => 'lte:less']);
  2151. $this->assertFalse($v->passes());
  2152. $this->assertEquals(2, $v->messages()->first('items'));
  2153. }
  2154. public function testValidateIn()
  2155. {
  2156. $trans = $this->getIlluminateArrayTranslator();
  2157. $v = new Validator($trans, ['name' => 'foo'], ['name' => 'In:bar,baz']);
  2158. $this->assertFalse($v->passes());
  2159. $trans = $this->getIlluminateArrayTranslator();
  2160. $v = new Validator($trans, ['name' => 0], ['name' => 'In:bar,baz']);
  2161. $this->assertFalse($v->passes());
  2162. $v = new Validator($trans, ['name' => 'foo'], ['name' => 'In:foo,baz']);
  2163. $this->assertTrue($v->passes());
  2164. $v = new Validator($trans, ['name' => ['foo', 'bar']], ['name' => 'Array|In:foo,baz']);
  2165. $this->assertFalse($v->passes());
  2166. $v = new Validator($trans, ['name' => ['foo', 'qux']], ['name' => 'Array|In:foo,baz,qux']);
  2167. $this->assertTrue($v->passes());
  2168. $v = new Validator($trans, ['name' => ['foo,bar', 'qux']], ['name' => 'Array|In:"foo,bar",baz,qux']);
  2169. $this->assertTrue($v->passes());
  2170. $v = new Validator($trans, ['name' => 'f"o"o'], ['name' => 'In:"f""o""o",baz,qux']);
  2171. $this->assertTrue($v->passes());
  2172. $v = new Validator($trans, ['name' => "a,b\nc,d"], ['name' => "in:\"a,b\nc,d\""]);
  2173. $this->assertTrue($v->passes());
  2174. $v = new Validator($trans, ['name' => ['foo', 'bar']], ['name' => 'Alpha|In:foo,bar']);
  2175. $this->assertFalse($v->passes());
  2176. $v = new Validator($trans, ['name' => ['foo', []]], ['name' => 'Array|In:foo,bar']);
  2177. $this->assertFalse($v->passes());
  2178. }
  2179. public function testValidateNotIn()
  2180. {
  2181. $trans = $this->getIlluminateArrayTranslator();
  2182. $v = new Validator($trans, ['name' => 'foo'], ['name' => 'NotIn:bar,baz']);
  2183. $this->assertTrue($v->passes());
  2184. $v = new Validator($trans, ['name' => 'foo'], ['name' => 'NotIn:foo,baz']);
  2185. $this->assertFalse($v->passes());
  2186. }
  2187. public function testValidateDistinct()
  2188. {
  2189. $trans = $this->getIlluminateArrayTranslator();
  2190. $v = new Validator($trans, ['foo' => ['foo', 'foo']], ['foo.*' => 'distinct']);
  2191. $this->assertFalse($v->passes());
  2192. $v = new Validator($trans, ['foo' => ['à', 'À']], ['foo.*' => 'distinct:ignore_case']);
  2193. $this->assertFalse($v->passes());
  2194. $v = new Validator($trans, ['foo' => ['f/oo', 'F/OO']], ['foo.*' => 'distinct:ignore_case']);
  2195. $this->assertFalse($v->passes());
  2196. $v = new Validator($trans, ['foo' => ['1', '1']], ['foo.*' => 'distinct:ignore_case']);
  2197. $this->assertFalse($v->passes());
  2198. $v = new Validator($trans, ['foo' => ['1', '11']], ['foo.*' => 'distinct:ignore_case']);
  2199. $this->assertTrue($v->passes());
  2200. $v = new Validator($trans, ['foo' => ['foo', 'bar']], ['foo.*' => 'distinct']);
  2201. $this->assertTrue($v->passes());
  2202. $v = new Validator($trans, ['foo' => ['bar' => ['id' => 1], 'baz' => ['id' => 1]]], ['foo.*.id' => 'distinct']);
  2203. $this->assertFalse($v->passes());
  2204. $v = new Validator($trans, ['foo' => ['bar' => ['id' => 'qux'], 'baz' => ['id' => 'QUX']]], ['foo.*.id' => 'distinct']);
  2205. $this->assertTrue($v->passes());
  2206. $v = new Validator($trans, ['foo' => ['bar' => ['id' => 'qux'], 'baz' => ['id' => 'QUX']]], ['foo.*.id' => 'distinct:ignore_case']);
  2207. $this->assertFalse($v->passes());
  2208. $v = new Validator($trans, ['foo' => ['bar' => ['id' => 1], 'baz' => ['id' => 2]]], ['foo.*.id' => 'distinct']);
  2209. $this->assertTrue($v->passes());
  2210. $v = new Validator($trans, ['foo' => ['bar' => ['id' => 2], 'baz' => ['id' => 425]]], ['foo.*.id' => 'distinct:ignore_case']);
  2211. $this->assertTrue($v->passes());
  2212. $v = new Validator($trans, ['foo' => [['id' => 1, 'nested' => ['id' => 1]]]], ['foo.*.id' => 'distinct']);
  2213. $this->assertTrue($v->passes());
  2214. $v = new Validator($trans, ['foo' => [['id' => 1], ['id' => 1]]], ['foo.*.id' => 'distinct']);
  2215. $this->assertFalse($v->passes());
  2216. $v = new Validator($trans, ['foo' => [['id' => 1], ['id' => 2]]], ['foo.*.id' => 'distinct']);
  2217. $this->assertTrue($v->passes());
  2218. $v = new Validator($trans, ['cat' => [['prod' => [['id' => 1]]], ['prod' => [['id' => 1]]]]], ['cat.*.prod.*.id' => 'distinct']);
  2219. $this->assertFalse($v->passes());
  2220. $v = new Validator($trans, ['cat' => [['prod' => [['id' => 1]]], ['prod' => [['id' => 2]]]]], ['cat.*.prod.*.id' => 'distinct']);
  2221. $this->assertTrue($v->passes());
  2222. $v = new Validator($trans, ['cat' => ['sub' => [['prod' => [['id' => 1]]], ['prod' => [['id' => 2]]]]]], ['cat.sub.*.prod.*.id' => 'distinct']);
  2223. $this->assertTrue($v->passes());
  2224. $v = new Validator($trans, ['cat' => ['sub' => [['prod' => [['id' => 2]]], ['prod' => [['id' => 2]]]]]], ['cat.sub.*.prod.*.id' => 'distinct']);
  2225. $this->assertFalse($v->passes());
  2226. $v = new Validator($trans, ['foo' => ['foo', 'foo'], 'bar' => ['bar', 'baz']], ['foo.*' => 'distinct', 'bar.*' => 'distinct']);
  2227. $this->assertFalse($v->passes());
  2228. $this->assertCount(2, $v->messages());
  2229. $v = new Validator($trans, ['foo' => ['foo', 'foo'], 'bar' => ['bar', 'bar']], ['foo.*' => 'distinct', 'bar.*' => 'distinct']);
  2230. $this->assertFalse($v->passes());
  2231. $this->assertCount(4, $v->messages());
  2232. $v->setData(['foo' => ['foo', 'bar'], 'bar' => ['foo', 'bar']]);
  2233. $this->assertTrue($v->passes());
  2234. $v = new Validator($trans, ['foo' => ['foo', 'foo']], ['foo.*' => 'distinct'], ['foo.*.distinct' => 'There is a duplication!']);
  2235. $this->assertFalse($v->passes());
  2236. $v->messages()->setFormat(':message');
  2237. $this->assertSame('There is a duplication!', $v->messages()->first('foo.0'));
  2238. $this->assertSame('There is a duplication!', $v->messages()->first('foo.1'));
  2239. $v = new Validator($trans, ['foo' => ['0100', '100']], ['foo.*' => 'distinct'], ['foo.*.distinct' => 'There is a duplication!']);
  2240. $this->assertFalse($v->passes());
  2241. $v->messages()->setFormat(':message');
  2242. $this->assertSame('There is a duplication!', $v->messages()->first('foo.0'));
  2243. $this->assertSame('There is a duplication!', $v->messages()->first('foo.1'));
  2244. $v = new Validator($trans, ['foo' => ['0100', '100']], ['foo.*' => 'distinct:strict']);
  2245. $this->assertTrue($v->passes());
  2246. }
  2247. public function testValidateDistinctForTopLevelArrays()
  2248. {
  2249. $trans = $this->getIlluminateArrayTranslator();
  2250. $v = new Validator($trans, ['foo', 'foo'], ['*' => 'distinct']);
  2251. $this->assertFalse($v->passes());
  2252. $v = new Validator($trans, [['foo' => 1], ['foo' => 1]], ['*' => 'array', '*.foo' => 'distinct']);
  2253. $this->assertFalse($v->passes());
  2254. $v = new Validator($trans, [['foo' => 'a'], ['foo' => 'A']], ['*' => 'array', '*.foo' => 'distinct:ignore_case']);
  2255. $this->assertFalse($v->passes());
  2256. $v = new Validator($trans, [['foo' => [['id' => 1]]], ['foo' => [['id' => 1]]]], ['*' => 'array', '*.foo' => 'array', '*.foo.*.id' => 'distinct']);
  2257. $this->assertFalse($v->passes());
  2258. $v = new Validator($trans, ['foo', 'foo'], ['*' => 'distinct'], ['*.distinct' => 'There is a duplication!']);
  2259. $this->assertFalse($v->passes());
  2260. $v->messages()->setFormat(':message');
  2261. $this->assertSame('There is a duplication!', $v->messages()->first('0'));
  2262. $this->assertSame('There is a duplication!', $v->messages()->first('1'));
  2263. }
  2264. public function testValidateUnique()
  2265. {
  2266. $trans = $this->getIlluminateArrayTranslator();
  2267. $v = new Validator($trans, ['email' => 'foo'], ['email' => 'Unique:users']);
  2268. $mock = m::mock(DatabasePresenceVerifierInterface::class);
  2269. $mock->shouldReceive('setConnection')->once()->with(null);
  2270. $mock->shouldReceive('getCount')->once()->with('users', 'email', 'foo', null, null, [])->andReturn(0);
  2271. $v->setPresenceVerifier($mock);
  2272. $this->assertTrue($v->passes());
  2273. $v = new Validator($trans, ['email' => 'foo'], ['email' => 'Unique:connection.users']);
  2274. $mock = m::mock(DatabasePresenceVerifierInterface::class);
  2275. $mock->shouldReceive('setConnection')->once()->with('connection');
  2276. $mock->shouldReceive('getCount')->once()->with('users', 'email', 'foo', null, null, [])->andReturn(0);
  2277. $v->setPresenceVerifier($mock);
  2278. $this->assertTrue($v->passes());
  2279. $v = new Validator($trans, ['email' => 'foo'], ['email' => 'Unique:users,email_addr,1']);
  2280. $mock = m::mock(DatabasePresenceVerifierInterface::class);
  2281. $mock->shouldReceive('setConnection')->once()->with(null);
  2282. $mock->shouldReceive('getCount')->once()->with('users', 'email_addr', 'foo', '1', 'id', [])->andReturn(1);
  2283. $v->setPresenceVerifier($mock);
  2284. $this->assertFalse($v->passes());
  2285. $v = new Validator($trans, ['email' => 'foo'], ['email' => 'Unique:users,email_addr,1,id_col']);
  2286. $mock = m::mock(DatabasePresenceVerifierInterface::class);
  2287. $mock->shouldReceive('setConnection')->once()->with(null);
  2288. $mock->shouldReceive('getCount')->once()->with('users', 'email_addr', 'foo', '1', 'id_col', [])->andReturn(2);
  2289. $v->setPresenceVerifier($mock);
  2290. $this->assertFalse($v->passes());
  2291. $v = new Validator($trans, ['users' => [['id' => 1, 'email' => 'foo']]], ['users.*.email' => 'Unique:users,email,[users.*.id]']);
  2292. $mock = m::mock(DatabasePresenceVerifierInterface::class);
  2293. $mock->shouldReceive('setConnection')->once()->with(null);
  2294. $mock->shouldReceive('getCount')->once()->with('users', 'email', 'foo', '1', 'id', [])->andReturn(1);
  2295. $v->setPresenceVerifier($mock);
  2296. $this->assertFalse($v->passes());
  2297. $v = new Validator($trans, ['email' => 'foo'], ['email' => 'Unique:users,email_addr,NULL,id_col,foo,bar']);
  2298. $mock = m::mock(DatabasePresenceVerifierInterface::class);
  2299. $mock->shouldReceive('setConnection')->once()->with(null);
  2300. $mock->shouldReceive('getCount')->once()->withArgs(function () {
  2301. return func_get_args() === ['users', 'email_addr', 'foo', null, 'id_col', ['foo' => 'bar']];
  2302. })->andReturn(2);
  2303. $v->setPresenceVerifier($mock);
  2304. $this->assertFalse($v->passes());
  2305. }
  2306. public function testValidateUniqueAndExistsSendsCorrectFieldNameToDBWithArrays()
  2307. {
  2308. $trans = $this->getIlluminateArrayTranslator();
  2309. $v = new Validator($trans, [['email' => 'foo', 'type' => 'bar']], [
  2310. '*.email' => 'unique:users', '*.type' => 'exists:user_types',
  2311. ]);
  2312. $mock = m::mock(DatabasePresenceVerifierInterface::class);
  2313. $mock->shouldReceive('setConnection')->twice()->with(null);
  2314. $mock->shouldReceive('getCount')->with('users', 'email', 'foo', null, null, [])->andReturn(0);
  2315. $mock->shouldReceive('getCount')->with('user_types', 'type', 'bar', null, null, [])->andReturn(1);
  2316. $v->setPresenceVerifier($mock);
  2317. $this->assertTrue($v->passes());
  2318. $trans = $this->getIlluminateArrayTranslator();
  2319. $closure = function () {
  2320. //
  2321. };
  2322. $v = new Validator($trans, [['email' => 'foo', 'type' => 'bar']], [
  2323. '*.email' => (new Unique('users'))->where($closure),
  2324. '*.type' => (new Exists('user_types'))->where($closure),
  2325. ]);
  2326. $mock = m::mock(DatabasePresenceVerifierInterface::class);
  2327. $mock->shouldReceive('setConnection')->twice()->with(null);
  2328. $mock->shouldReceive('getCount')->with('users', 'email', 'foo', null, 'id', [$closure])->andReturn(0);
  2329. $mock->shouldReceive('getCount')->with('user_types', 'type', 'bar', null, null, [$closure])->andReturn(1);
  2330. $v->setPresenceVerifier($mock);
  2331. $this->assertTrue($v->passes());
  2332. }
  2333. public function testValidationExists()
  2334. {
  2335. $trans = $this->getIlluminateArrayTranslator();
  2336. $v = new Validator($trans, ['email' => 'foo'], ['email' => 'Exists:users']);
  2337. $mock = m::mock(DatabasePresenceVerifierInterface::class);
  2338. $mock->shouldReceive('setConnection')->once()->with(null);
  2339. $mock->shouldReceive('getCount')->once()->with('users', 'email', 'foo', null, null, [])->andReturn(1);
  2340. $v->setPresenceVerifier($mock);
  2341. $this->assertTrue($v->passes());
  2342. $trans = $this->getIlluminateArrayTranslator();
  2343. $v = new Validator($trans, ['email' => 'foo'], ['email' => 'Exists:users,email,account_id,1,name,taylor']);
  2344. $mock = m::mock(DatabasePresenceVerifierInterface::class);
  2345. $mock->shouldReceive('setConnection')->once()->with(null);
  2346. $mock->shouldReceive('getCount')->once()->with('users', 'email', 'foo', null, null, ['account_id' => 1, 'name' => 'taylor'])->andReturn(1);
  2347. $v->setPresenceVerifier($mock);
  2348. $this->assertTrue($v->passes());
  2349. $v = new Validator($trans, ['email' => 'foo'], ['email' => 'Exists:users,email_addr']);
  2350. $mock = m::mock(DatabasePresenceVerifierInterface::class);
  2351. $mock->shouldReceive('setConnection')->once()->with(null);
  2352. $mock->shouldReceive('getCount')->once()->with('users', 'email_addr', 'foo', null, null, [])->andReturn(0);
  2353. $v->setPresenceVerifier($mock);
  2354. $this->assertFalse($v->passes());
  2355. $v = new Validator($trans, ['email' => ['foo']], ['email' => 'Exists:users,email_addr']);
  2356. $mock = m::mock(DatabasePresenceVerifierInterface::class);
  2357. $mock->shouldReceive('setConnection')->once()->with(null);
  2358. $mock->shouldReceive('getMultiCount')->once()->with('users', 'email_addr', ['foo'], [])->andReturn(0);
  2359. $v->setPresenceVerifier($mock);
  2360. $this->assertFalse($v->passes());
  2361. $v = new Validator($trans, ['email' => 'foo'], ['email' => 'Exists:connection.users']);
  2362. $mock = m::mock(DatabasePresenceVerifierInterface::class);
  2363. $mock->shouldReceive('setConnection')->once()->with('connection');
  2364. $mock->shouldReceive('getCount')->once()->with('users', 'email', 'foo', null, null, [])->andReturn(1);
  2365. $v->setPresenceVerifier($mock);
  2366. $this->assertTrue($v->passes());
  2367. $v = new Validator($trans, ['email' => ['foo', 'foo']], ['email' => 'exists:users,email_addr']);
  2368. $mock = m::mock(DatabasePresenceVerifierInterface::class);
  2369. $mock->shouldReceive('setConnection')->once()->with(null);
  2370. $mock->shouldReceive('getMultiCount')->once()->with('users', 'email_addr', ['foo', 'foo'], [])->andReturn(1);
  2371. $v->setPresenceVerifier($mock);
  2372. $this->assertTrue($v->passes());
  2373. }
  2374. public function testValidationExistsIsNotCalledUnnecessarily()
  2375. {
  2376. $trans = $this->getIlluminateArrayTranslator();
  2377. $v = new Validator($trans, ['id' => 'foo'], ['id' => 'Integer|Exists:users,id']);
  2378. $mock = m::mock(DatabasePresenceVerifierInterface::class);
  2379. $mock->shouldReceive('getCount')->never();
  2380. $v->setPresenceVerifier($mock);
  2381. $this->assertFalse($v->passes());
  2382. $trans = $this->getIlluminateArrayTranslator();
  2383. $v = new Validator($trans, ['id' => '1'], ['id' => 'Integer|Exists:users,id']);
  2384. $mock = m::mock(DatabasePresenceVerifierInterface::class);
  2385. $mock->shouldReceive('setConnection')->once()->with(null);
  2386. $mock->shouldReceive('getCount')->once()->with('users', 'id', '1', null, null, [])->andReturn(1);
  2387. $v->setPresenceVerifier($mock);
  2388. $this->assertTrue($v->passes());
  2389. }
  2390. public function testValidateIp()
  2391. {
  2392. $trans = $this->getIlluminateArrayTranslator();
  2393. $v = new Validator($trans, ['ip' => 'aslsdlks'], ['ip' => 'Ip']);
  2394. $this->assertFalse($v->passes());
  2395. $v = new Validator($trans, ['ip' => '127.0.0.1'], ['ip' => 'Ip']);
  2396. $this->assertTrue($v->passes());
  2397. $v = new Validator($trans, ['ip' => '127.0.0.1'], ['ip' => 'Ipv4']);
  2398. $this->assertTrue($v->passes());
  2399. $v = new Validator($trans, ['ip' => '::1'], ['ip' => 'Ipv6']);
  2400. $this->assertTrue($v->passes());
  2401. $v = new Validator($trans, ['ip' => '127.0.0.1'], ['ip' => 'Ipv6']);
  2402. $this->assertTrue($v->fails());
  2403. $v = new Validator($trans, ['ip' => '::1'], ['ip' => 'Ipv4']);
  2404. $this->assertTrue($v->fails());
  2405. }
  2406. public function testValidateMacAddress()
  2407. {
  2408. $trans = $this->getIlluminateArrayTranslator();
  2409. $v = new Validator($trans, ['mac' => 'foo'], ['mac' => 'mac_address']);
  2410. $this->assertFalse($v->passes());
  2411. $trans = $this->getIlluminateArrayTranslator();
  2412. $v = new Validator($trans, ['mac' => '01-23-45-67-89-ab'], ['mac' => 'mac_address']);
  2413. $this->assertTrue($v->passes());
  2414. $trans = $this->getIlluminateArrayTranslator();
  2415. $v = new Validator($trans, ['mac' => '01-23-45-67-89-AB'], ['mac' => 'mac_address']);
  2416. $this->assertTrue($v->passes());
  2417. $trans = $this->getIlluminateArrayTranslator();
  2418. $v = new Validator($trans, ['mac' => '01-23-45-67-89-aB'], ['mac' => 'mac_address']);
  2419. $this->assertTrue($v->passes());
  2420. $trans = $this->getIlluminateArrayTranslator();
  2421. $v = new Validator($trans, ['mac' => '01:23:45:67:89:ab'], ['mac' => 'mac_address']);
  2422. $this->assertTrue($v->passes());
  2423. $trans = $this->getIlluminateArrayTranslator();
  2424. $v = new Validator($trans, ['mac' => '01:23:45:67:89:AB'], ['mac' => 'mac_address']);
  2425. $this->assertTrue($v->passes());
  2426. $trans = $this->getIlluminateArrayTranslator();
  2427. $v = new Validator($trans, ['mac' => '01:23:45:67:89:aB'], ['mac' => 'mac_address']);
  2428. $this->assertTrue($v->passes());
  2429. $trans = $this->getIlluminateArrayTranslator();
  2430. $v = new Validator($trans, ['mac' => '01:23:45-67:89:aB'], ['mac' => 'mac_address']);
  2431. $this->assertFalse($v->passes());
  2432. $trans = $this->getIlluminateArrayTranslator();
  2433. $v = new Validator($trans, ['mac' => 'xx:23:45:67:89:aB'], ['mac' => 'mac_address']);
  2434. $this->assertFalse($v->passes());
  2435. $trans = $this->getIlluminateArrayTranslator();
  2436. $v = new Validator($trans, ['mac' => '0123.4567.89ab'], ['mac' => 'mac_address']);
  2437. $this->assertTrue($v->passes());
  2438. }
  2439. public function testValidateEmail()
  2440. {
  2441. $trans = $this->getIlluminateArrayTranslator();
  2442. $v = new Validator($trans, ['x' => 'aslsdlks'], ['x' => 'Email']);
  2443. $this->assertFalse($v->passes());
  2444. $v = new Validator($trans, ['x' => ['not a string']], ['x' => 'Email']);
  2445. $this->assertFalse($v->passes());
  2446. $v = new Validator($trans, [
  2447. 'x' => new class
  2448. {
  2449. public function __toString()
  2450. {
  2451. return 'aslsdlks';
  2452. }
  2453. },
  2454. ], ['x' => 'Email']);
  2455. $this->assertFalse($v->passes());
  2456. $v = new Validator($trans, [
  2457. 'x' => new class
  2458. {
  2459. public function __toString()
  2460. {
  2461. return 'foo@gmail.com';
  2462. }
  2463. },
  2464. ], ['x' => 'Email']);
  2465. $this->assertTrue($v->passes());
  2466. $v = new Validator($trans, ['x' => 'foo@gmail.com'], ['x' => 'Email']);
  2467. $this->assertTrue($v->passes());
  2468. }
  2469. public function testValidateEmailWithInternationalCharacters()
  2470. {
  2471. $v = new Validator($this->getIlluminateArrayTranslator(), ['x' => 'foo@gmäil.com'], ['x' => 'email']);
  2472. $this->assertTrue($v->passes());
  2473. }
  2474. public function testValidateEmailWithStrictCheck()
  2475. {
  2476. $v = new Validator($this->getIlluminateArrayTranslator(), ['x' => 'foo@bar '], ['x' => 'email:strict']);
  2477. $this->assertFalse($v->passes());
  2478. }
  2479. public function testValidateEmailWithFilterCheck()
  2480. {
  2481. $v = new Validator($this->getIlluminateArrayTranslator(), ['x' => 'foo@bar'], ['x' => 'email:filter']);
  2482. $this->assertFalse($v->passes());
  2483. $v = new Validator($this->getIlluminateArrayTranslator(), ['x' => 'example@example.com'], ['x' => 'email:filter']);
  2484. $this->assertTrue($v->passes());
  2485. // Unicode characters are not allowed
  2486. $v = new Validator($this->getIlluminateArrayTranslator(), ['x' => 'exämple@example.com'], ['x' => 'email:filter']);
  2487. $this->assertFalse($v->passes());
  2488. $v = new Validator($this->getIlluminateArrayTranslator(), ['x' => 'exämple@exämple.com'], ['x' => 'email:filter']);
  2489. $this->assertFalse($v->passes());
  2490. }
  2491. public function testValidateEmailWithFilterUnicodeCheck()
  2492. {
  2493. $v = new Validator($this->getIlluminateArrayTranslator(), ['x' => 'foo@bar'], ['x' => 'email:filter_unicode']);
  2494. $this->assertFalse($v->passes());
  2495. $v = new Validator($this->getIlluminateArrayTranslator(), ['x' => 'example@example.com'], ['x' => 'email:filter_unicode']);
  2496. $this->assertTrue($v->passes());
  2497. // Any unicode characters are allowed only in local-part
  2498. $v = new Validator($this->getIlluminateArrayTranslator(), ['x' => 'exämple@example.com'], ['x' => 'email:filter_unicode']);
  2499. $this->assertTrue($v->passes());
  2500. $v = new Validator($this->getIlluminateArrayTranslator(), ['x' => 'exämple@exämple.com'], ['x' => 'email:filter_unicode']);
  2501. $this->assertFalse($v->passes());
  2502. }
  2503. public function testValidateEmailWithCustomClassCheck()
  2504. {
  2505. $container = m::mock(Container::class);
  2506. $container->shouldReceive('make')->with(NoRFCWarningsValidation::class)->andReturn(new NoRFCWarningsValidation);
  2507. $v = new Validator($this->getIlluminateArrayTranslator(), ['x' => 'foo@bar '], ['x' => 'email:'.NoRFCWarningsValidation::class]);
  2508. $v->setContainer($container);
  2509. $this->assertFalse($v->passes());
  2510. }
  2511. /**
  2512. * @dataProvider validUrls
  2513. */
  2514. public function testValidateUrlWithValidUrls($validUrl)
  2515. {
  2516. $trans = $this->getIlluminateArrayTranslator();
  2517. $v = new Validator($trans, ['x' => $validUrl], ['x' => 'Url']);
  2518. $this->assertTrue($v->passes());
  2519. }
  2520. /**
  2521. * @dataProvider invalidUrls
  2522. */
  2523. public function testValidateUrlWithInvalidUrls($invalidUrl)
  2524. {
  2525. $trans = $this->getIlluminateArrayTranslator();
  2526. $v = new Validator($trans, ['x' => $invalidUrl], ['x' => 'Url']);
  2527. $this->assertFalse($v->passes());
  2528. }
  2529. public function validUrls()
  2530. {
  2531. return [
  2532. ['aaa://fully.qualified.domain/path'],
  2533. ['aaas://fully.qualified.domain/path'],
  2534. ['about://fully.qualified.domain/path'],
  2535. ['acap://fully.qualified.domain/path'],
  2536. ['acct://fully.qualified.domain/path'],
  2537. ['acr://fully.qualified.domain/path'],
  2538. ['adiumxtra://fully.qualified.domain/path'],
  2539. ['afp://fully.qualified.domain/path'],
  2540. ['afs://fully.qualified.domain/path'],
  2541. ['aim://fully.qualified.domain/path'],
  2542. ['apt://fully.qualified.domain/path'],
  2543. ['attachment://fully.qualified.domain/path'],
  2544. ['aw://fully.qualified.domain/path'],
  2545. ['barion://fully.qualified.domain/path'],
  2546. ['beshare://fully.qualified.domain/path'],
  2547. ['bitcoin://fully.qualified.domain/path'],
  2548. ['blob://fully.qualified.domain/path'],
  2549. ['bolo://fully.qualified.domain/path'],
  2550. ['callto://fully.qualified.domain/path'],
  2551. ['cap://fully.qualified.domain/path'],
  2552. ['chrome://fully.qualified.domain/path'],
  2553. ['chrome-extension://fully.qualified.domain/path'],
  2554. ['cid://fully.qualified.domain/path'],
  2555. ['coap://fully.qualified.domain/path'],
  2556. ['coaps://fully.qualified.domain/path'],
  2557. ['com-eventbrite-attendee://fully.qualified.domain/path'],
  2558. ['content://fully.qualified.domain/path'],
  2559. ['crid://fully.qualified.domain/path'],
  2560. ['cvs://fully.qualified.domain/path'],
  2561. ['data://fully.qualified.domain/path'],
  2562. ['dav://fully.qualified.domain/path'],
  2563. ['dict://fully.qualified.domain/path'],
  2564. ['dlna-playcontainer://fully.qualified.domain/path'],
  2565. ['dlna-playsingle://fully.qualified.domain/path'],
  2566. ['dns://fully.qualified.domain/path'],
  2567. ['dntp://fully.qualified.domain/path'],
  2568. ['dtn://fully.qualified.domain/path'],
  2569. ['dvb://fully.qualified.domain/path'],
  2570. ['ed2k://fully.qualified.domain/path'],
  2571. ['example://fully.qualified.domain/path'],
  2572. ['facetime://fully.qualified.domain/path'],
  2573. ['fax://fully.qualified.domain/path'],
  2574. ['feed://fully.qualified.domain/path'],
  2575. ['feedready://fully.qualified.domain/path'],
  2576. ['file://fully.qualified.domain/path'],
  2577. ['filesystem://fully.qualified.domain/path'],
  2578. ['finger://fully.qualified.domain/path'],
  2579. ['fish://fully.qualified.domain/path'],
  2580. ['ftp://fully.qualified.domain/path'],
  2581. ['geo://fully.qualified.domain/path'],
  2582. ['gg://fully.qualified.domain/path'],
  2583. ['git://fully.qualified.domain/path'],
  2584. ['gizmoproject://fully.qualified.domain/path'],
  2585. ['go://fully.qualified.domain/path'],
  2586. ['gopher://fully.qualified.domain/path'],
  2587. ['gtalk://fully.qualified.domain/path'],
  2588. ['h323://fully.qualified.domain/path'],
  2589. ['ham://fully.qualified.domain/path'],
  2590. ['hcp://fully.qualified.domain/path'],
  2591. ['http://fully.qualified.domain/path'],
  2592. ['https://fully.qualified.domain/path'],
  2593. ['iax://fully.qualified.domain/path'],
  2594. ['icap://fully.qualified.domain/path'],
  2595. ['icon://fully.qualified.domain/path'],
  2596. ['im://fully.qualified.domain/path'],
  2597. ['imap://fully.qualified.domain/path'],
  2598. ['info://fully.qualified.domain/path'],
  2599. ['iotdisco://fully.qualified.domain/path'],
  2600. ['ipn://fully.qualified.domain/path'],
  2601. ['ipp://fully.qualified.domain/path'],
  2602. ['ipps://fully.qualified.domain/path'],
  2603. ['irc://fully.qualified.domain/path'],
  2604. ['irc6://fully.qualified.domain/path'],
  2605. ['ircs://fully.qualified.domain/path'],
  2606. ['iris://fully.qualified.domain/path'],
  2607. ['iris.beep://fully.qualified.domain/path'],
  2608. ['iris.lwz://fully.qualified.domain/path'],
  2609. ['iris.xpc://fully.qualified.domain/path'],
  2610. ['iris.xpcs://fully.qualified.domain/path'],
  2611. ['itms://fully.qualified.domain/path'],
  2612. ['jabber://fully.qualified.domain/path'],
  2613. ['jar://fully.qualified.domain/path'],
  2614. ['jms://fully.qualified.domain/path'],
  2615. ['keyparc://fully.qualified.domain/path'],
  2616. ['lastfm://fully.qualified.domain/path'],
  2617. ['ldap://fully.qualified.domain/path'],
  2618. ['ldaps://fully.qualified.domain/path'],
  2619. ['magnet://fully.qualified.domain/path'],
  2620. ['mailserver://fully.qualified.domain/path'],
  2621. ['mailto://fully.qualified.domain/path'],
  2622. ['maps://fully.qualified.domain/path'],
  2623. ['market://fully.qualified.domain/path'],
  2624. ['message://fully.qualified.domain/path'],
  2625. ['mid://fully.qualified.domain/path'],
  2626. ['mms://fully.qualified.domain/path'],
  2627. ['modem://fully.qualified.domain/path'],
  2628. ['ms-help://fully.qualified.domain/path'],
  2629. ['ms-settings://fully.qualified.domain/path'],
  2630. ['ms-settings-airplanemode://fully.qualified.domain/path'],
  2631. ['ms-settings-bluetooth://fully.qualified.domain/path'],
  2632. ['ms-settings-camera://fully.qualified.domain/path'],
  2633. ['ms-settings-cellular://fully.qualified.domain/path'],
  2634. ['ms-settings-cloudstorage://fully.qualified.domain/path'],
  2635. ['ms-settings-emailandaccounts://fully.qualified.domain/path'],
  2636. ['ms-settings-language://fully.qualified.domain/path'],
  2637. ['ms-settings-location://fully.qualified.domain/path'],
  2638. ['ms-settings-lock://fully.qualified.domain/path'],
  2639. ['ms-settings-nfctransactions://fully.qualified.domain/path'],
  2640. ['ms-settings-notifications://fully.qualified.domain/path'],
  2641. ['ms-settings-power://fully.qualified.domain/path'],
  2642. ['ms-settings-privacy://fully.qualified.domain/path'],
  2643. ['ms-settings-proximity://fully.qualified.domain/path'],
  2644. ['ms-settings-screenrotation://fully.qualified.domain/path'],
  2645. ['ms-settings-wifi://fully.qualified.domain/path'],
  2646. ['ms-settings-workplace://fully.qualified.domain/path'],
  2647. ['msnim://fully.qualified.domain/path'],
  2648. ['msrp://fully.qualified.domain/path'],
  2649. ['msrps://fully.qualified.domain/path'],
  2650. ['mtqp://fully.qualified.domain/path'],
  2651. ['mumble://fully.qualified.domain/path'],
  2652. ['mupdate://fully.qualified.domain/path'],
  2653. ['mvn://fully.qualified.domain/path'],
  2654. ['news://fully.qualified.domain/path'],
  2655. ['nfs://fully.qualified.domain/path'],
  2656. ['ni://fully.qualified.domain/path'],
  2657. ['nih://fully.qualified.domain/path'],
  2658. ['nntp://fully.qualified.domain/path'],
  2659. ['notes://fully.qualified.domain/path'],
  2660. ['oid://fully.qualified.domain/path'],
  2661. ['opaquelocktoken://fully.qualified.domain/path'],
  2662. ['pack://fully.qualified.domain/path'],
  2663. ['palm://fully.qualified.domain/path'],
  2664. ['paparazzi://fully.qualified.domain/path'],
  2665. ['pkcs11://fully.qualified.domain/path'],
  2666. ['platform://fully.qualified.domain/path'],
  2667. ['pop://fully.qualified.domain/path'],
  2668. ['pres://fully.qualified.domain/path'],
  2669. ['prospero://fully.qualified.domain/path'],
  2670. ['proxy://fully.qualified.domain/path'],
  2671. ['psyc://fully.qualified.domain/path'],
  2672. ['query://fully.qualified.domain/path'],
  2673. ['redis://fully.qualified.domain/path'],
  2674. ['rediss://fully.qualified.domain/path'],
  2675. ['reload://fully.qualified.domain/path'],
  2676. ['res://fully.qualified.domain/path'],
  2677. ['resource://fully.qualified.domain/path'],
  2678. ['rmi://fully.qualified.domain/path'],
  2679. ['rsync://fully.qualified.domain/path'],
  2680. ['rtmfp://fully.qualified.domain/path'],
  2681. ['rtmp://fully.qualified.domain/path'],
  2682. ['rtsp://fully.qualified.domain/path'],
  2683. ['rtsps://fully.qualified.domain/path'],
  2684. ['rtspu://fully.qualified.domain/path'],
  2685. ['s3://fully.qualified.domain/path'],
  2686. ['secondlife://fully.qualified.domain/path'],
  2687. ['service://fully.qualified.domain/path'],
  2688. ['session://fully.qualified.domain/path'],
  2689. ['sftp://fully.qualified.domain/path'],
  2690. ['sgn://fully.qualified.domain/path'],
  2691. ['shttp://fully.qualified.domain/path'],
  2692. ['sieve://fully.qualified.domain/path'],
  2693. ['sip://fully.qualified.domain/path'],
  2694. ['sips://fully.qualified.domain/path'],
  2695. ['skype://fully.qualified.domain/path'],
  2696. ['smb://fully.qualified.domain/path'],
  2697. ['sms://fully.qualified.domain/path'],
  2698. ['smtp://fully.qualified.domain/path'],
  2699. ['snews://fully.qualified.domain/path'],
  2700. ['snmp://fully.qualified.domain/path'],
  2701. ['soap.beep://fully.qualified.domain/path'],
  2702. ['soap.beeps://fully.qualified.domain/path'],
  2703. ['soldat://fully.qualified.domain/path'],
  2704. ['spotify://fully.qualified.domain/path'],
  2705. ['ssh://fully.qualified.domain/path'],
  2706. ['steam://fully.qualified.domain/path'],
  2707. ['stun://fully.qualified.domain/path'],
  2708. ['stuns://fully.qualified.domain/path'],
  2709. ['submit://fully.qualified.domain/path'],
  2710. ['svn://fully.qualified.domain/path'],
  2711. ['tag://fully.qualified.domain/path'],
  2712. ['teamspeak://fully.qualified.domain/path'],
  2713. ['tel://fully.qualified.domain/path'],
  2714. ['teliaeid://fully.qualified.domain/path'],
  2715. ['telnet://fully.qualified.domain/path'],
  2716. ['tftp://fully.qualified.domain/path'],
  2717. ['things://fully.qualified.domain/path'],
  2718. ['thismessage://fully.qualified.domain/path'],
  2719. ['tip://fully.qualified.domain/path'],
  2720. ['tn3270://fully.qualified.domain/path'],
  2721. ['turn://fully.qualified.domain/path'],
  2722. ['turns://fully.qualified.domain/path'],
  2723. ['tv://fully.qualified.domain/path'],
  2724. ['udp://fully.qualified.domain/path'],
  2725. ['unreal://fully.qualified.domain/path'],
  2726. ['urn://fully.qualified.domain/path'],
  2727. ['ut2004://fully.qualified.domain/path'],
  2728. ['vemmi://fully.qualified.domain/path'],
  2729. ['ventrilo://fully.qualified.domain/path'],
  2730. ['videotex://fully.qualified.domain/path'],
  2731. ['view-source://fully.qualified.domain/path'],
  2732. ['wais://fully.qualified.domain/path'],
  2733. ['webcal://fully.qualified.domain/path'],
  2734. ['ws://fully.qualified.domain/path'],
  2735. ['wss://fully.qualified.domain/path'],
  2736. ['wtai://fully.qualified.domain/path'],
  2737. ['wyciwyg://fully.qualified.domain/path'],
  2738. ['xcon://fully.qualified.domain/path'],
  2739. ['xcon-userid://fully.qualified.domain/path'],
  2740. ['xfire://fully.qualified.domain/path'],
  2741. ['xmlrpc.beep://fully.qualified.domain/path'],
  2742. ['xmlrpc.beeps://fully.qualified.domain/path'],
  2743. ['xmpp://fully.qualified.domain/path'],
  2744. ['xri://fully.qualified.domain/path'],
  2745. ['ymsgr://fully.qualified.domain/path'],
  2746. ['z39.50://fully.qualified.domain/path'],
  2747. ['z39.50r://fully.qualified.domain/path'],
  2748. ['z39.50s://fully.qualified.domain/path'],
  2749. ['http://a.pl'],
  2750. ['http://localhost/url.php'],
  2751. ['http://local.dev'],
  2752. ['http://google.com'],
  2753. ['http://www.google.com'],
  2754. ['http://goog_le.com'],
  2755. ['https://google.com'],
  2756. ['http://illuminate.dev'],
  2757. ['http://localhost'],
  2758. ['https://laravel.com/?'],
  2759. ['http://президент.рф/'],
  2760. ['http://스타벅스코리아.com'],
  2761. ['http://xn--d1abbgf6aiiy.xn--p1ai/'],
  2762. ['https://laravel.com?'],
  2763. ['https://laravel.com?q=1'],
  2764. ['https://laravel.com/?q=1'],
  2765. ['https://laravel.com#'],
  2766. ['https://laravel.com#fragment'],
  2767. ['https://laravel.com/#fragment'],
  2768. ['https://domain1'],
  2769. ['https://domain12/'],
  2770. ['https://domain12#fragment'],
  2771. ['https://domain1/path'],
  2772. ['https://domain.com/path/%2528failed%2526?param=1#fragment'],
  2773. ];
  2774. }
  2775. public function invalidUrls()
  2776. {
  2777. return [
  2778. ['aslsdlks'],
  2779. ['google.com'],
  2780. ['://google.com'],
  2781. ['http ://google.com'],
  2782. ['http:/google.com'],
  2783. ['http://google.com::aa'],
  2784. ['http://google.com:aa'],
  2785. ['http://127.0.0.1:aa'],
  2786. ['http://[::1'],
  2787. ['foo://bar'],
  2788. ['javascript://test%0Aalert(321)'],
  2789. ];
  2790. }
  2791. public function testValidateActiveUrl()
  2792. {
  2793. $trans = $this->getIlluminateArrayTranslator();
  2794. $v = new Validator($trans, ['x' => 'aslsdlks'], ['x' => 'active_url']);
  2795. $this->assertFalse($v->passes());
  2796. $v = new Validator($trans, ['x' => ['fdsfs', 'fdsfds']], ['x' => 'active_url']);
  2797. $this->assertFalse($v->passes());
  2798. $v = new Validator($trans, ['x' => 'http://google.com'], ['x' => 'active_url']);
  2799. $this->assertTrue($v->passes());
  2800. $v = new Validator($trans, ['x' => 'http://www.google.com'], ['x' => 'active_url']);
  2801. $this->assertTrue($v->passes());
  2802. $v = new Validator($trans, ['x' => 'http://www.google.com/about'], ['x' => 'active_url']);
  2803. $this->assertTrue($v->passes());
  2804. }
  2805. public function testValidateImage()
  2806. {
  2807. $trans = $this->getIlluminateArrayTranslator();
  2808. $uploadedFile = [__FILE__, '', null, null, true];
  2809. $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
  2810. $file->expects($this->any())->method('guessExtension')->willReturn('php');
  2811. $file->expects($this->any())->method('getClientOriginalExtension')->willReturn('php');
  2812. $v = new Validator($trans, ['x' => $file], ['x' => 'image']);
  2813. $this->assertFalse($v->passes());
  2814. $file2 = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
  2815. $file2->expects($this->any())->method('guessExtension')->willReturn('jpeg');
  2816. $file2->expects($this->any())->method('getClientOriginalExtension')->willReturn('jpeg');
  2817. $v = new Validator($trans, ['x' => $file2], ['x' => 'image']);
  2818. $this->assertTrue($v->passes());
  2819. $file2 = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
  2820. $file2->expects($this->any())->method('guessExtension')->willReturn('jpg');
  2821. $file2->expects($this->any())->method('getClientOriginalExtension')->willReturn('jpg');
  2822. $v = new Validator($trans, ['x' => $file2], ['x' => 'image']);
  2823. $this->assertTrue($v->passes());
  2824. $file2 = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
  2825. $file2->expects($this->any())->method('guessExtension')->willReturn('jpg');
  2826. $file2->expects($this->any())->method('getClientOriginalExtension')->willReturn('jpg');
  2827. $v = new Validator($trans, ['x' => $file2], ['x' => 'image']);
  2828. $this->assertTrue($v->passes());
  2829. $file3 = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
  2830. $file3->expects($this->any())->method('guessExtension')->willReturn('gif');
  2831. $file3->expects($this->any())->method('getClientOriginalExtension')->willReturn('gif');
  2832. $v = new Validator($trans, ['x' => $file3], ['x' => 'image']);
  2833. $this->assertTrue($v->passes());
  2834. $file4 = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
  2835. $file4->expects($this->any())->method('guessExtension')->willReturn('bmp');
  2836. $file4->expects($this->any())->method('getClientOriginalExtension')->willReturn('bmp');
  2837. $v = new Validator($trans, ['x' => $file4], ['x' => 'image']);
  2838. $this->assertTrue($v->passes());
  2839. $file5 = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
  2840. $file5->expects($this->any())->method('guessExtension')->willReturn('png');
  2841. $file5->expects($this->any())->method('getClientOriginalExtension')->willReturn('png');
  2842. $v = new Validator($trans, ['x' => $file5], ['x' => 'image']);
  2843. $this->assertTrue($v->passes());
  2844. $file6 = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
  2845. $file6->expects($this->any())->method('guessExtension')->willReturn('svg');
  2846. $file6->expects($this->any())->method('getClientOriginalExtension')->willReturn('svg');
  2847. $v = new Validator($trans, ['x' => $file6], ['x' => 'image']);
  2848. $this->assertTrue($v->passes());
  2849. $file7 = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
  2850. $file7->expects($this->any())->method('guessExtension')->willReturn('webp');
  2851. $file7->expects($this->any())->method('getClientOriginalExtension')->willReturn('webp');
  2852. $v = new Validator($trans, ['x' => $file7], ['x' => 'Image']);
  2853. $this->assertTrue($v->passes());
  2854. $file2 = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
  2855. $file2->expects($this->any())->method('guessExtension')->willReturn('jpg');
  2856. $file2->expects($this->any())->method('getClientOriginalExtension')->willReturn('jpg');
  2857. $v = new Validator($trans, ['x' => $file2], ['x' => 'Image']);
  2858. $this->assertTrue($v->passes());
  2859. }
  2860. public function testValidateImageDoesNotAllowPhpExtensionsOnImageMime()
  2861. {
  2862. $trans = $this->getIlluminateArrayTranslator();
  2863. $uploadedFile = [__FILE__, '', null, null, true];
  2864. $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
  2865. $file->expects($this->any())->method('guessExtension')->willReturn('jpeg');
  2866. $file->expects($this->any())->method('getClientOriginalExtension')->willReturn('php');
  2867. $v = new Validator($trans, ['x' => $file], ['x' => 'image']);
  2868. $this->assertFalse($v->passes());
  2869. }
  2870. public function testValidateImageDimensions()
  2871. {
  2872. // Knowing that demo image.png has width = 3 and height = 2
  2873. $uploadedFile = new UploadedFile(__DIR__.'/fixtures/image.png', '', null, null, true);
  2874. $trans = $this->getIlluminateArrayTranslator();
  2875. $v = new Validator($trans, ['x' => 'file'], ['x' => 'dimensions']);
  2876. $this->assertTrue($v->fails());
  2877. $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:min_width=1']);
  2878. $this->assertTrue($v->passes());
  2879. $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:min_width=5']);
  2880. $this->assertTrue($v->fails());
  2881. $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:max_width=10']);
  2882. $this->assertTrue($v->passes());
  2883. $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:max_width=1']);
  2884. $this->assertTrue($v->fails());
  2885. $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:min_height=1']);
  2886. $this->assertTrue($v->passes());
  2887. $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:min_height=5']);
  2888. $this->assertTrue($v->fails());
  2889. $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:max_height=10']);
  2890. $this->assertTrue($v->passes());
  2891. $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:max_height=1']);
  2892. $this->assertTrue($v->fails());
  2893. $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:width=3']);
  2894. $this->assertTrue($v->passes());
  2895. $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:height=2']);
  2896. $this->assertTrue($v->passes());
  2897. $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:min_height=2,ratio=3/2']);
  2898. $this->assertTrue($v->passes());
  2899. $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=1.5']);
  2900. $this->assertTrue($v->passes());
  2901. $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=1/1']);
  2902. $this->assertTrue($v->fails());
  2903. $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=1']);
  2904. $this->assertTrue($v->fails());
  2905. // Knowing that demo image2.png has width = 4 and height = 2
  2906. $uploadedFile = new UploadedFile(__DIR__.'/fixtures/image2.png', '', null, null, true);
  2907. $trans = $this->getIlluminateArrayTranslator();
  2908. // Ensure validation doesn't erroneously fail when ratio has no fractional part
  2909. $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=2/1']);
  2910. $this->assertTrue($v->passes());
  2911. // This test fails without suppressing warnings on getimagesize() due to a read error.
  2912. $emptyUploadedFile = new UploadedFile(__DIR__.'/fixtures/empty.png', '', null, null, true);
  2913. $trans = $this->getIlluminateArrayTranslator();
  2914. $v = new Validator($trans, ['x' => $emptyUploadedFile], ['x' => 'dimensions:min_width=1']);
  2915. $this->assertTrue($v->fails());
  2916. // Knowing that demo image3.png has width = 7 and height = 10
  2917. $uploadedFile = new UploadedFile(__DIR__.'/fixtures/image3.png', '', null, null, true);
  2918. $trans = $this->getIlluminateArrayTranslator();
  2919. // Ensure validation doesn't erroneously fail when ratio has no fractional part
  2920. $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=2/3']);
  2921. $this->assertTrue($v->passes());
  2922. // Ensure svg images always pass as size is irrelevant (image/svg+xml)
  2923. $svgXmlUploadedFile = new UploadedFile(__DIR__.'/fixtures/image.svg', '', 'image/svg+xml', null, true);
  2924. $trans = $this->getIlluminateArrayTranslator();
  2925. $v = new Validator($trans, ['x' => $svgXmlUploadedFile], ['x' => 'dimensions:max_width=1,max_height=1']);
  2926. $this->assertTrue($v->passes());
  2927. $svgXmlFile = new File(__DIR__.'/fixtures/image.svg', '', 'image/svg+xml', null, true);
  2928. $trans = $this->getIlluminateArrayTranslator();
  2929. $v = new Validator($trans, ['x' => $svgXmlFile], ['x' => 'dimensions:max_width=1,max_height=1']);
  2930. $this->assertTrue($v->passes());
  2931. // Ensure svg images always pass as size is irrelevant (image/svg)
  2932. $svgUploadedFile = new UploadedFile(__DIR__.'/fixtures/image2.svg', '', 'image/svg', null, true);
  2933. $trans = $this->getIlluminateArrayTranslator();
  2934. $v = new Validator($trans, ['x' => $svgUploadedFile], ['x' => 'dimensions:max_width=1,max_height=1']);
  2935. $this->assertTrue($v->passes());
  2936. $svgFile = new File(__DIR__.'/fixtures/image2.svg', '', 'image/svg', null, true);
  2937. $trans = $this->getIlluminateArrayTranslator();
  2938. $v = new Validator($trans, ['x' => $svgFile], ['x' => 'dimensions:max_width=1,max_height=1']);
  2939. $this->assertTrue($v->passes());
  2940. // Knowing that demo image4.png has width = 64 and height = 65
  2941. $uploadedFile = new UploadedFile(__DIR__.'/fixtures/image4.png', '', null, null, true);
  2942. $trans = $this->getIlluminateArrayTranslator();
  2943. // Ensure validation doesn't erroneously fail when ratio doesn't matches
  2944. $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=1']);
  2945. $this->assertFalse($v->passes());
  2946. }
  2947. public function testValidateMimetypes()
  2948. {
  2949. $trans = $this->getIlluminateArrayTranslator();
  2950. $uploadedFile = [__DIR__.'/ValidationRuleTest.php', '', null, null, true];
  2951. $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
  2952. $file->expects($this->any())->method('guessExtension')->willReturn('rtf');
  2953. $file->expects($this->any())->method('getClientOriginalExtension')->willReturn('rtf');
  2954. $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getMimeType'])->setConstructorArgs($uploadedFile)->getMock();
  2955. $file->expects($this->any())->method('getMimeType')->willReturn('text/rtf');
  2956. $v = new Validator($trans, ['x' => $file], ['x' => 'mimetypes:text/*']);
  2957. $this->assertTrue($v->passes());
  2958. $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getMimeType'])->setConstructorArgs($uploadedFile)->getMock();
  2959. $file->expects($this->any())->method('getMimeType')->willReturn('application/pdf');
  2960. $v = new Validator($trans, ['x' => $file], ['x' => 'mimetypes:text/rtf']);
  2961. $this->assertFalse($v->passes());
  2962. $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getMimeType'])->setConstructorArgs($uploadedFile)->getMock();
  2963. $file->expects($this->any())->method('getMimeType')->willReturn('image/jpeg');
  2964. $v = new Validator($trans, ['x' => $file], ['x' => 'mimetypes:image/jpeg']);
  2965. $this->assertTrue($v->passes());
  2966. }
  2967. public function testValidateMime()
  2968. {
  2969. $trans = $this->getIlluminateArrayTranslator();
  2970. $uploadedFile = [__FILE__, '', null, null, true];
  2971. $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
  2972. $file->expects($this->any())->method('guessExtension')->willReturn('pdf');
  2973. $file->expects($this->any())->method('getClientOriginalExtension')->willReturn('pdf');
  2974. $v = new Validator($trans, ['x' => $file], ['x' => 'mimes:pdf']);
  2975. $this->assertTrue($v->passes());
  2976. $file2 = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'isValid'])->setConstructorArgs($uploadedFile)->getMock();
  2977. $file2->expects($this->any())->method('guessExtension')->willReturn('pdf');
  2978. $file2->expects($this->any())->method('isValid')->willReturn(false);
  2979. $v = new Validator($trans, ['x' => $file2], ['x' => 'mimes:pdf']);
  2980. $this->assertFalse($v->passes());
  2981. $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
  2982. $file->expects($this->any())->method('guessExtension')->willReturn('jpg');
  2983. $file->expects($this->any())->method('getClientOriginalExtension')->willReturn('jpg');
  2984. $v = new Validator($trans, ['x' => $file], ['x' => 'mimes:jpeg']);
  2985. $this->assertTrue($v->passes());
  2986. $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
  2987. $file->expects($this->any())->method('guessExtension')->willReturn('jpg');
  2988. $file->expects($this->any())->method('getClientOriginalExtension')->willReturn('jpeg');
  2989. $v = new Validator($trans, ['x' => $file], ['x' => 'mimes:jpg']);
  2990. $this->assertTrue($v->passes());
  2991. }
  2992. public function testValidateMimeEnforcesPhpCheck()
  2993. {
  2994. $trans = $this->getIlluminateArrayTranslator();
  2995. $uploadedFile = [__FILE__, '', null, null, true];
  2996. $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
  2997. $file->expects($this->any())->method('guessExtension')->willReturn('pdf');
  2998. $file->expects($this->any())->method('getClientOriginalExtension')->willReturn('php');
  2999. $v = new Validator($trans, ['x' => $file], ['x' => 'mimes:pdf']);
  3000. $this->assertFalse($v->passes());
  3001. $file2 = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
  3002. $file2->expects($this->any())->method('guessExtension')->willReturn('php');
  3003. $file2->expects($this->any())->method('getClientOriginalExtension')->willReturn('php');
  3004. $v = new Validator($trans, ['x' => $file2], ['x' => 'mimes:pdf,php']);
  3005. $this->assertTrue($v->passes());
  3006. }
  3007. /**
  3008. * @requires extension fileinfo
  3009. */
  3010. public function testValidateFile()
  3011. {
  3012. $trans = $this->getIlluminateArrayTranslator();
  3013. $file = new UploadedFile(__FILE__, '', null, null, true);
  3014. $v = new Validator($trans, ['x' => '1'], ['x' => 'file']);
  3015. $this->assertTrue($v->fails());
  3016. $v = new Validator($trans, ['x' => $file], ['x' => 'file']);
  3017. $this->assertTrue($v->passes());
  3018. }
  3019. public function testEmptyRulesSkipped()
  3020. {
  3021. $trans = $this->getIlluminateArrayTranslator();
  3022. $v = new Validator($trans, ['x' => 'aslsdlks'], ['x' => ['alpha', [], '']]);
  3023. $this->assertTrue($v->passes());
  3024. $v = new Validator($trans, ['x' => 'aslsdlks'], ['x' => '|||required|']);
  3025. $this->assertTrue($v->passes());
  3026. }
  3027. public function testAlternativeFormat()
  3028. {
  3029. $trans = $this->getIlluminateArrayTranslator();
  3030. $v = new Validator($trans, ['x' => 'aslsdlks'], ['x' => ['alpha', ['min', 3], ['max', 10]]]);
  3031. $this->assertTrue($v->passes());
  3032. }
  3033. public function testValidateAlpha()
  3034. {
  3035. $trans = $this->getIlluminateArrayTranslator();
  3036. $v = new Validator($trans, ['x' => 'aslsdlks'], ['x' => 'Alpha']);
  3037. $this->assertTrue($v->passes());
  3038. $trans = $this->getIlluminateArrayTranslator();
  3039. $v = new Validator($trans, [
  3040. 'x' => 'aslsdlks
  3041. 1
  3042. 1',
  3043. ], ['x' => 'Alpha']);
  3044. $this->assertFalse($v->passes());
  3045. $v = new Validator($trans, ['x' => 'http://google.com'], ['x' => 'Alpha']);
  3046. $this->assertFalse($v->passes());
  3047. $v = new Validator($trans, ['x' => 'ユニコードを基盤技術と'], ['x' => 'Alpha']);
  3048. $this->assertTrue($v->passes());
  3049. $v = new Validator($trans, ['x' => 'ユニコード を基盤技術と'], ['x' => 'Alpha']);
  3050. $this->assertFalse($v->passes());
  3051. $v = new Validator($trans, ['x' => 'नमस्कार'], ['x' => 'Alpha']);
  3052. $this->assertTrue($v->passes());
  3053. $v = new Validator($trans, ['x' => 'आपका स्वागत है'], ['x' => 'Alpha']);
  3054. $this->assertFalse($v->passes());
  3055. $v = new Validator($trans, ['x' => 'Continuación'], ['x' => 'Alpha']);
  3056. $this->assertTrue($v->passes());
  3057. $v = new Validator($trans, ['x' => 'ofreció su dimisión'], ['x' => 'Alpha']);
  3058. $this->assertFalse($v->passes());
  3059. $v = new Validator($trans, ['x' => '❤'], ['x' => 'Alpha']);
  3060. $this->assertFalse($v->passes());
  3061. $v = new Validator($trans, ['x' => '123'], ['x' => 'Alpha']);
  3062. $this->assertFalse($v->passes());
  3063. $v = new Validator($trans, ['x' => 123], ['x' => 'Alpha']);
  3064. $this->assertFalse($v->passes());
  3065. $v = new Validator($trans, ['x' => 'abc123'], ['x' => 'Alpha']);
  3066. $this->assertFalse($v->passes());
  3067. }
  3068. public function testValidateAlphaNum()
  3069. {
  3070. $trans = $this->getIlluminateArrayTranslator();
  3071. $v = new Validator($trans, ['x' => 'asls13dlks'], ['x' => 'AlphaNum']);
  3072. $this->assertTrue($v->passes());
  3073. $v = new Validator($trans, ['x' => 'http://g232oogle.com'], ['x' => 'AlphaNum']);
  3074. $this->assertFalse($v->passes());
  3075. $v = new Validator($trans, ['x' => '१२३'], ['x' => 'AlphaNum']); // numbers in Hindi
  3076. $this->assertTrue($v->passes());
  3077. $v = new Validator($trans, ['x' => '٧٨٩'], ['x' => 'AlphaNum']); // eastern arabic numerals
  3078. $this->assertTrue($v->passes());
  3079. $v = new Validator($trans, ['x' => 'नमस्कार'], ['x' => 'AlphaNum']);
  3080. $this->assertTrue($v->passes());
  3081. }
  3082. public function testValidateAlphaDash()
  3083. {
  3084. $trans = $this->getIlluminateArrayTranslator();
  3085. $v = new Validator($trans, ['x' => 'asls1-_3dlks'], ['x' => 'AlphaDash']);
  3086. $this->assertTrue($v->passes());
  3087. $v = new Validator($trans, ['x' => 'http://-g232oogle.com'], ['x' => 'AlphaDash']);
  3088. $this->assertFalse($v->passes());
  3089. $v = new Validator($trans, ['x' => 'नमस्कार-_'], ['x' => 'AlphaDash']);
  3090. $this->assertTrue($v->passes());
  3091. $v = new Validator($trans, ['x' => '٧٨٩'], ['x' => 'AlphaDash']); // eastern arabic numerals
  3092. $this->assertTrue($v->passes());
  3093. }
  3094. public function testValidateTimezone()
  3095. {
  3096. $trans = $this->getIlluminateArrayTranslator();
  3097. $v = new Validator($trans, ['foo' => 'India'], ['foo' => 'Timezone']);
  3098. $this->assertFalse($v->passes());
  3099. $v = new Validator($trans, ['foo' => 'Cairo'], ['foo' => 'Timezone']);
  3100. $this->assertFalse($v->passes());
  3101. $v = new Validator($trans, ['foo' => 'UTC'], ['foo' => 'Timezone']);
  3102. $this->assertTrue($v->passes());
  3103. $v = new Validator($trans, ['foo' => 'Africa/Windhoek'], ['foo' => 'Timezone']);
  3104. $this->assertTrue($v->passes());
  3105. $v = new Validator($trans, ['foo' => 'africa/windhoek'], ['foo' => 'Timezone']);
  3106. $this->assertFalse($v->passes());
  3107. $v = new Validator($trans, ['foo' => 'GMT'], ['foo' => 'Timezone']);
  3108. $this->assertFalse($v->passes());
  3109. $v = new Validator($trans, ['foo' => 'GB'], ['foo' => 'Timezone']);
  3110. $this->assertFalse($v->passes());
  3111. $v = new Validator($trans, ['foo' => ['this_is_not_a_timezone']], ['foo' => 'Timezone']);
  3112. $this->assertFalse($v->passes());
  3113. }
  3114. public function testValidateRegex()
  3115. {
  3116. $trans = $this->getIlluminateArrayTranslator();
  3117. $v = new Validator($trans, ['x' => 'asdasdf'], ['x' => 'Regex:/^[a-z]+$/i']);
  3118. $this->assertTrue($v->passes());
  3119. $v = new Validator($trans, ['x' => 'aasd234fsd1'], ['x' => 'Regex:/^[a-z]+$/i']);
  3120. $this->assertFalse($v->passes());
  3121. // Ensure commas are not interpreted as parameter separators
  3122. $v = new Validator($trans, ['x' => 'a,b'], ['x' => 'Regex:/^a,b$/i']);
  3123. $this->assertTrue($v->passes());
  3124. $v = new Validator($trans, ['x' => '12'], ['x' => 'Regex:/^12$/i']);
  3125. $this->assertTrue($v->passes());
  3126. $v = new Validator($trans, ['x' => 12], ['x' => 'Regex:/^12$/i']);
  3127. $this->assertTrue($v->passes());
  3128. }
  3129. public function testValidateNotRegex()
  3130. {
  3131. $trans = $this->getIlluminateArrayTranslator();
  3132. $v = new Validator($trans, ['x' => 'foo bar'], ['x' => 'NotRegex:/[xyz]/i']);
  3133. $this->assertTrue($v->passes());
  3134. $v = new Validator($trans, ['x' => 'foo xxx bar'], ['x' => 'NotRegex:/[xyz]/i']);
  3135. $this->assertFalse($v->passes());
  3136. // Ensure commas are not interpreted as parameter separators
  3137. $v = new Validator($trans, ['x' => 'foo bar'], ['x' => 'NotRegex:/x{3,}/i']);
  3138. $this->assertTrue($v->passes());
  3139. }
  3140. public function testValidateDateAndFormat()
  3141. {
  3142. date_default_timezone_set('UTC');
  3143. $trans = $this->getIlluminateArrayTranslator();
  3144. $v = new Validator($trans, ['x' => '2000-01-01'], ['x' => 'date']);
  3145. $this->assertTrue($v->passes());
  3146. $v = new Validator($trans, ['x' => '01/01/2000'], ['x' => 'date']);
  3147. $this->assertTrue($v->passes());
  3148. $v = new Validator($trans, ['x' => '1325376000'], ['x' => 'date']);
  3149. $this->assertTrue($v->fails());
  3150. $v = new Validator($trans, ['x' => 'Not a date'], ['x' => 'date']);
  3151. $this->assertTrue($v->fails());
  3152. $v = new Validator($trans, ['x' => ['Not', 'a', 'date']], ['x' => 'date']);
  3153. $this->assertTrue($v->fails());
  3154. $v = new Validator($trans, ['x' => new DateTime], ['x' => 'date']);
  3155. $this->assertTrue($v->passes());
  3156. $v = new Validator($trans, ['x' => new DateTimeImmutable], ['x' => 'date']);
  3157. $this->assertTrue($v->passes());
  3158. $v = new Validator($trans, ['x' => '2000-01-01'], ['x' => 'date_format:Y-m-d']);
  3159. $this->assertTrue($v->passes());
  3160. $v = new Validator($trans, ['x' => '01/01/2001'], ['x' => 'date_format:Y-m-d']);
  3161. $this->assertTrue($v->fails());
  3162. $v = new Validator($trans, ['x' => '22000-01-01'], ['x' => 'date_format:Y-m-d']);
  3163. $this->assertTrue($v->fails());
  3164. $v = new Validator($trans, ['x' => '00-01-01'], ['x' => 'date_format:Y-m-d']);
  3165. $this->assertTrue($v->fails());
  3166. $v = new Validator($trans, ['x' => ['Not', 'a', 'date']], ['x' => 'date_format:Y-m-d']);
  3167. $this->assertTrue($v->fails());
  3168. // Set current machine date to 31/xx/xxxx
  3169. $v = new Validator($trans, ['x' => '2013-02'], ['x' => 'date_format:Y-m']);
  3170. $this->assertTrue($v->passes());
  3171. $v = new Validator($trans, ['x' => '2000-01-01T00:00:00Atlantic/Azores'], ['x' => 'date_format:Y-m-d\TH:i:se']);
  3172. $this->assertTrue($v->passes());
  3173. $v = new Validator($trans, ['x' => '2000-01-01T00:00:00Z'], ['x' => 'date_format:Y-m-d\TH:i:sT']);
  3174. $this->assertTrue($v->passes());
  3175. $v = new Validator($trans, ['x' => '2000-01-01T00:00:00+0000'], ['x' => 'date_format:Y-m-d\TH:i:sO']);
  3176. $this->assertTrue($v->passes());
  3177. $v = new Validator($trans, ['x' => '2000-01-01T00:00:00+00:30'], ['x' => 'date_format:Y-m-d\TH:i:sP']);
  3178. $this->assertTrue($v->passes());
  3179. $v = new Validator($trans, ['x' => '2000-01-01 17:43:59'], ['x' => 'date_format:Y-m-d H:i:s']);
  3180. $this->assertTrue($v->passes());
  3181. $v = new Validator($trans, ['x' => '2000-01-01 17:43:59'], ['x' => 'date_format:H:i:s']);
  3182. $this->assertTrue($v->fails());
  3183. $v = new Validator($trans, ['x' => '2000-01-01 17:43:59'], ['x' => 'date_format:Y-m-d H:i:s,H:i:s']);
  3184. $this->assertTrue($v->passes());
  3185. $v = new Validator($trans, ['x' => '17:43:59'], ['x' => 'date_format:Y-m-d H:i:s,H:i:s']);
  3186. $this->assertTrue($v->passes());
  3187. $v = new Validator($trans, ['x' => '17:43:59'], ['x' => 'date_format:H:i:s']);
  3188. $this->assertTrue($v->passes());
  3189. $v = new Validator($trans, ['x' => '17:43:59'], ['x' => 'date_format:H:i']);
  3190. $this->assertTrue($v->fails());
  3191. $v = new Validator($trans, ['x' => '17:43'], ['x' => 'date_format:H:i']);
  3192. $this->assertTrue($v->passes());
  3193. }
  3194. public function testDateEquals()
  3195. {
  3196. date_default_timezone_set('UTC');
  3197. $trans = $this->getIlluminateArrayTranslator();
  3198. $v = new Validator($trans, ['x' => '2000-01-01'], ['x' => 'date_equals:2000-01-01']);
  3199. $this->assertTrue($v->passes());
  3200. $v = new Validator($trans, ['x' => new Carbon('2000-01-01')], ['x' => 'date_equals:2000-01-01']);
  3201. $this->assertTrue($v->passes());
  3202. $v = new Validator($trans, ['x' => new Carbon('2000-01-01')], ['x' => 'date_equals:2001-01-01']);
  3203. $this->assertTrue($v->fails());
  3204. $v = new Validator($trans, ['start' => new DateTime('2000-01-01'), 'ends' => new DateTime('2000-01-01')], ['ends' => 'date_equals:start']);
  3205. $this->assertTrue($v->passes());
  3206. $v = new Validator($trans, ['x' => date('Y-m-d')], ['x' => 'date_equals:today']);
  3207. $this->assertTrue($v->passes());
  3208. $v = new Validator($trans, ['x' => date('Y-m-d')], ['x' => 'date_equals:yesterday']);
  3209. $this->assertTrue($v->fails());
  3210. $v = new Validator($trans, ['x' => date('Y-m-d')], ['x' => 'date_equals:tomorrow']);
  3211. $this->assertTrue($v->fails());
  3212. $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|date_equals:today']);
  3213. $this->assertTrue($v->passes());
  3214. $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|date_equals:yesterday']);
  3215. $this->assertTrue($v->fails());
  3216. $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|date_equals:tomorrow']);
  3217. $this->assertTrue($v->fails());
  3218. $v = new Validator($trans, ['x' => '2012-01-01 17:44:00'], ['x' => 'date_format:Y-m-d H:i:s|date_equals:2012-01-01 17:44:00']);
  3219. $this->assertTrue($v->passes());
  3220. $v = new Validator($trans, ['x' => '2012-01-01 17:44:00'], ['x' => 'date_format:Y-m-d H:i:s|date_equals:2012-01-01 17:43:59']);
  3221. $this->assertTrue($v->fails());
  3222. $v = new Validator($trans, ['x' => '2012-01-01 17:44:00'], ['x' => 'date_format:Y-m-d H:i:s|date_equals:2012-01-01 17:44:01']);
  3223. $this->assertTrue($v->fails());
  3224. $v = new Validator($trans, ['x' => '17:44:00'], ['x' => 'date_format:H:i:s|date_equals:17:44:00']);
  3225. $this->assertTrue($v->passes());
  3226. $v = new Validator($trans, ['x' => '17:44:00'], ['x' => 'date_format:H:i:s|date_equals:17:43:59']);
  3227. $this->assertTrue($v->fails());
  3228. $v = new Validator($trans, ['x' => '17:44:00'], ['x' => 'date_format:H:i:s|date_equals:17:44:01']);
  3229. $this->assertTrue($v->fails());
  3230. $v = new Validator($trans, ['x' => '17:44'], ['x' => 'date_format:H:i|date_equals:17:44']);
  3231. $this->assertTrue($v->passes());
  3232. $v = new Validator($trans, ['x' => '17:44'], ['x' => 'date_format:H:i|date_equals:17:43']);
  3233. $this->assertTrue($v->fails());
  3234. $v = new Validator($trans, ['x' => '17:44'], ['x' => 'date_format:H:i|date_equals:17:45']);
  3235. $this->assertTrue($v->fails());
  3236. }
  3237. public function testDateEqualsRespectsCarbonTestNowWhenParameterIsRelative()
  3238. {
  3239. date_default_timezone_set('UTC');
  3240. $trans = $this->getIlluminateArrayTranslator();
  3241. Carbon::setTestNow(new Carbon('2018-01-01'));
  3242. $v = new Validator($trans, ['x' => '2018-01-01 00:00:00'], ['x' => 'date_equals:now']);
  3243. $this->assertTrue($v->passes());
  3244. $v = new Validator($trans, ['x' => '2018-01-01'], ['x' => 'date_equals:today']);
  3245. $this->assertTrue($v->passes());
  3246. $v = new Validator($trans, ['x' => '2018-01-01'], ['x' => 'date_equals:yesterday']);
  3247. $this->assertTrue($v->fails());
  3248. $v = new Validator($trans, ['x' => '2018-01-01'], ['x' => 'date_equals:tomorrow']);
  3249. $this->assertTrue($v->fails());
  3250. $v = new Validator($trans, ['x' => '01/01/2018'], ['x' => 'date_format:d/m/Y|date_equals:today']);
  3251. $this->assertTrue($v->passes());
  3252. $v = new Validator($trans, ['x' => '01/01/2018'], ['x' => 'date_format:d/m/Y|date_equals:yesterday']);
  3253. $this->assertTrue($v->fails());
  3254. $v = new Validator($trans, ['x' => '01/01/2018'], ['x' => 'date_format:d/m/Y|date_equals:tomorrow']);
  3255. $this->assertTrue($v->fails());
  3256. $v = new Validator($trans, ['x' => new DateTime('2018-01-01')], ['x' => 'date_equals:today']);
  3257. $this->assertTrue($v->passes());
  3258. $v = new Validator($trans, ['x' => new DateTime('2018-01-01')], ['x' => 'date_equals:yesterday']);
  3259. $this->assertTrue($v->fails());
  3260. $v = new Validator($trans, ['x' => new DateTime('2018-01-01')], ['x' => 'date_equals:tomorrow']);
  3261. $this->assertTrue($v->fails());
  3262. $v = new Validator($trans, ['x' => new Carbon('2018-01-01')], ['x' => 'date_equals:today|after:yesterday|before:tomorrow']);
  3263. $this->assertTrue($v->passes());
  3264. $v = new Validator($trans, ['x' => new Carbon('2018-01-01')], ['x' => 'date_equals:yesterday']);
  3265. $this->assertTrue($v->fails());
  3266. $v = new Validator($trans, ['x' => new Carbon('2018-01-01')], ['x' => 'date_equals:tomorrow']);
  3267. $this->assertTrue($v->fails());
  3268. }
  3269. public function testBeforeAndAfter()
  3270. {
  3271. date_default_timezone_set('UTC');
  3272. $trans = $this->getIlluminateArrayTranslator();
  3273. $v = new Validator($trans, ['x' => '2000-01-01'], ['x' => 'Before:2012-01-01']);
  3274. $this->assertTrue($v->passes());
  3275. $v = new Validator($trans, ['x' => ['2000-01-01']], ['x' => 'Before:2012-01-01']);
  3276. $this->assertFalse($v->passes());
  3277. $v = new Validator($trans, ['x' => new Carbon('2000-01-01')], ['x' => 'Before:2012-01-01']);
  3278. $this->assertTrue($v->passes());
  3279. $v = new Validator($trans, ['x' => [new Carbon('2000-01-01')]], ['x' => 'Before:2012-01-01']);
  3280. $this->assertFalse($v->passes());
  3281. $v = new Validator($trans, ['x' => '2012-01-01'], ['x' => 'After:2000-01-01']);
  3282. $this->assertTrue($v->passes());
  3283. $v = new Validator($trans, ['x' => ['2012-01-01']], ['x' => 'After:2000-01-01']);
  3284. $this->assertFalse($v->passes());
  3285. $v = new Validator($trans, ['x' => new Carbon('2012-01-01')], ['x' => 'After:2000-01-01']);
  3286. $this->assertTrue($v->passes());
  3287. $v = new Validator($trans, ['x' => [new Carbon('2012-01-01')]], ['x' => 'After:2000-01-01']);
  3288. $this->assertFalse($v->passes());
  3289. $v = new Validator($trans, ['start' => '2012-01-01', 'ends' => '2013-01-01'], ['start' => 'After:2000-01-01', 'ends' => 'After:start']);
  3290. $this->assertTrue($v->passes());
  3291. $v = new Validator($trans, ['start' => '2012-01-01', 'ends' => '2000-01-01'], ['start' => 'After:2000-01-01', 'ends' => 'After:start']);
  3292. $this->assertTrue($v->fails());
  3293. $v = new Validator($trans, ['start' => '2012-01-01', 'ends' => '2013-01-01'], ['start' => 'Before:ends', 'ends' => 'After:start']);
  3294. $this->assertTrue($v->passes());
  3295. $v = new Validator($trans, ['start' => '2012-01-01', 'ends' => '2000-01-01'], ['start' => 'Before:ends', 'ends' => 'After:start']);
  3296. $this->assertTrue($v->fails());
  3297. $v = new Validator($trans, ['x' => new DateTime('2000-01-01')], ['x' => 'Before:2012-01-01']);
  3298. $this->assertTrue($v->passes());
  3299. $v = new Validator($trans, ['start' => new DateTime('2012-01-01'), 'ends' => new Carbon('2013-01-01')], ['start' => 'Before:ends', 'ends' => 'After:start']);
  3300. $this->assertTrue($v->passes());
  3301. $v = new Validator($trans, ['start' => '2012-01-01', 'ends' => new DateTime('2013-01-01')], ['start' => 'Before:ends', 'ends' => 'After:start']);
  3302. $this->assertTrue($v->passes());
  3303. $v = new Validator($trans, ['start' => new DateTime('2012-01-01'), 'ends' => new DateTime('2000-01-01')], ['start' => 'After:2000-01-01', 'ends' => 'After:start']);
  3304. $this->assertTrue($v->fails());
  3305. $v = new Validator($trans, ['start' => 'today', 'ends' => 'tomorrow'], ['start' => 'Before:ends', 'ends' => 'After:start']);
  3306. $this->assertTrue($v->passes());
  3307. $v = new Validator($trans, ['x' => '2012-01-01 17:43:59'], ['x' => 'Before:2012-01-01 17:44|After:2012-01-01 17:43:58']);
  3308. $this->assertTrue($v->passes());
  3309. $v = new Validator($trans, ['x' => '2012-01-01 17:44:01'], ['x' => 'Before:2012-01-01 17:44:02|After:2012-01-01 17:44']);
  3310. $this->assertTrue($v->passes());
  3311. $v = new Validator($trans, ['x' => '2012-01-01 17:44'], ['x' => 'Before:2012-01-01 17:44:00']);
  3312. $this->assertTrue($v->fails());
  3313. $v = new Validator($trans, ['x' => '2012-01-01 17:44'], ['x' => 'After:2012-01-01 17:44:00']);
  3314. $this->assertTrue($v->fails());
  3315. $v = new Validator($trans, ['x' => '17:43:59'], ['x' => 'Before:17:44|After:17:43:58']);
  3316. $this->assertTrue($v->passes());
  3317. $v = new Validator($trans, ['x' => '17:44:01'], ['x' => 'Before:17:44:02|After:17:44']);
  3318. $this->assertTrue($v->passes());
  3319. $v = new Validator($trans, ['x' => '17:44'], ['x' => 'Before:17:44:00']);
  3320. $this->assertTrue($v->fails());
  3321. $v = new Validator($trans, ['x' => '17:44'], ['x' => 'After:17:44:00']);
  3322. $this->assertTrue($v->fails());
  3323. }
  3324. public function testBeforeAndAfterWithFormat()
  3325. {
  3326. date_default_timezone_set('UTC');
  3327. $trans = $this->getIlluminateArrayTranslator();
  3328. $v = new Validator($trans, ['x' => '31/12/2000'], ['x' => 'before:31/02/2012']);
  3329. $this->assertTrue($v->fails());
  3330. $v = new Validator($trans, ['x' => ['31/12/2000']], ['x' => 'before:31/02/2012']);
  3331. $this->assertTrue($v->fails());
  3332. $v = new Validator($trans, ['x' => '31/12/2000'], ['x' => 'date_format:d/m/Y|before:31/12/2012']);
  3333. $this->assertTrue($v->passes());
  3334. $v = new Validator($trans, ['x' => '31/12/2012'], ['x' => 'after:31/12/2000']);
  3335. $this->assertTrue($v->fails());
  3336. $v = new Validator($trans, ['x' => ['31/12/2012']], ['x' => 'after:31/12/2000']);
  3337. $this->assertTrue($v->fails());
  3338. $v = new Validator($trans, ['x' => '31/12/2012'], ['x' => 'date_format:d/m/Y|after:31/12/2000']);
  3339. $this->assertTrue($v->passes());
  3340. $v = new Validator($trans, ['start' => '31/12/2012', 'ends' => '31/12/2013'], ['start' => 'after:01/01/2000', 'ends' => 'after:start']);
  3341. $this->assertTrue($v->fails());
  3342. $v = new Validator($trans, ['start' => '31/12/2012', 'ends' => '31/12/2013'], ['start' => 'date_format:d/m/Y|after:31/12/2000', 'ends' => 'date_format:d/m/Y|after:start']);
  3343. $this->assertTrue($v->passes());
  3344. $v = new Validator($trans, ['start' => '31/12/2012', 'ends' => '31/12/2000'], ['start' => 'after:31/12/2000', 'ends' => 'after:start']);
  3345. $this->assertTrue($v->fails());
  3346. $v = new Validator($trans, ['start' => '31/12/2012', 'ends' => '31/12/2000'], ['start' => 'date_format:d/m/Y|after:31/12/2000', 'ends' => 'date_format:d/m/Y|after:start']);
  3347. $this->assertTrue($v->fails());
  3348. $v = new Validator($trans, ['start' => '31/12/2012', 'ends' => '31/12/2013'], ['start' => 'before:ends', 'ends' => 'after:start']);
  3349. $this->assertTrue($v->fails());
  3350. $v = new Validator($trans, ['start' => '31/12/2012', 'ends' => '31/12/2013'], ['start' => 'date_format:d/m/Y|before:ends', 'ends' => 'date_format:d/m/Y|after:start']);
  3351. $this->assertTrue($v->passes());
  3352. $v = new Validator($trans, ['start' => '31/12/2012', 'ends' => '31/12/2000'], ['start' => 'before:ends', 'ends' => 'after:start']);
  3353. $this->assertTrue($v->fails());
  3354. $v = new Validator($trans, ['start' => '31/12/2012', 'ends' => '31/12/2000'], ['start' => 'date_format:d/m/Y|before:ends', 'ends' => 'date_format:d/m/Y|after:start']);
  3355. $this->assertTrue($v->fails());
  3356. $v = new Validator($trans, ['start' => 'invalid', 'ends' => 'invalid'], ['start' => 'date_format:d/m/Y|before:ends', 'ends' => 'date_format:d/m/Y|after:start']);
  3357. $this->assertTrue($v->fails());
  3358. $v = new Validator($trans, ['start' => '31/12/2012', 'ends' => null], ['start' => 'date_format:d/m/Y|before:ends', 'ends' => 'date_format:d/m/Y|after:start']);
  3359. $this->assertTrue($v->fails());
  3360. $v = new Validator($trans, ['start' => '31/12/2012', 'ends' => null], ['start' => 'date_format:d/m/Y|before:ends', 'ends' => 'nullable|date_format:d/m/Y|after:start']);
  3361. $this->assertTrue($v->passes());
  3362. $v = new Validator($trans, ['start' => '31/12/2012', 'ends' => null], ['start' => 'before:ends']);
  3363. $this->assertTrue($v->fails());
  3364. $v = new Validator($trans, ['start' => '31/12/2012', 'ends' => null], ['start' => 'before:ends', 'ends' => 'nullable']);
  3365. $this->assertTrue($v->fails());
  3366. $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|after:yesterday|before:tomorrow']);
  3367. $this->assertTrue($v->passes());
  3368. $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|after:today']);
  3369. $this->assertTrue($v->fails());
  3370. $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|before:today']);
  3371. $this->assertTrue($v->fails());
  3372. $v = new Validator($trans, ['x' => date('Y-m-d')], ['x' => 'after:yesterday|before:tomorrow']);
  3373. $this->assertTrue($v->passes());
  3374. $v = new Validator($trans, ['x' => date('Y-m-d')], ['x' => 'after:today']);
  3375. $this->assertTrue($v->fails());
  3376. $v = new Validator($trans, ['x' => date('Y-m-d')], ['x' => 'before:today']);
  3377. $this->assertTrue($v->fails());
  3378. $v = new Validator($trans, ['x' => '2012-01-01 17:44:00'], ['x' => 'date_format:Y-m-d H:i:s|before:2012-01-01 17:44:01|after:2012-01-01 17:43:59']);
  3379. $this->assertTrue($v->passes());
  3380. $v = new Validator($trans, ['x' => '2012-01-01 17:44:00'], ['x' => 'date_format:Y-m-d H:i:s|before:2012-01-01 17:44:00']);
  3381. $this->assertTrue($v->fails());
  3382. $v = new Validator($trans, ['x' => '2012-01-01 17:44:00'], ['x' => 'date_format:Y-m-d H:i:s|after:2012-01-01 17:44:00']);
  3383. $this->assertTrue($v->fails());
  3384. $v = new Validator($trans, ['x' => '17:44:00'], ['x' => 'date_format:H:i:s|before:17:44:01|after:17:43:59']);
  3385. $this->assertTrue($v->passes());
  3386. $v = new Validator($trans, ['x' => '17:44:00'], ['x' => 'date_format:H:i:s|before:17:44:00']);
  3387. $this->assertTrue($v->fails());
  3388. $v = new Validator($trans, ['x' => '17:44:00'], ['x' => 'date_format:H:i:s|after:17:44:00']);
  3389. $this->assertTrue($v->fails());
  3390. $v = new Validator($trans, ['x' => '17:44'], ['x' => 'date_format:H:i|before:17:45|after:17:43']);
  3391. $this->assertTrue($v->passes());
  3392. $v = new Validator($trans, ['x' => '17:44'], ['x' => 'date_format:H:i|before:17:44']);
  3393. $this->assertTrue($v->fails());
  3394. $v = new Validator($trans, ['x' => '17:44'], ['x' => 'date_format:H:i|after:17:44']);
  3395. $this->assertTrue($v->fails());
  3396. $v = new Validator($trans, ['x' => '2038-01-18', '2018-05-12' => '2038-01-19'], ['x' => 'date_format:Y-m-d|before:2018-05-12']);
  3397. $this->assertTrue($v->fails());
  3398. $v = new Validator($trans, ['x' => '1970-01-02', '2018-05-12' => '1970-01-01'], ['x' => 'date_format:Y-m-d|after:2018-05-12']);
  3399. $this->assertTrue($v->fails());
  3400. }
  3401. public function testWeakBeforeAndAfter()
  3402. {
  3403. date_default_timezone_set('UTC');
  3404. $trans = $this->getIlluminateArrayTranslator();
  3405. $v = new Validator($trans, ['x' => '2012-01-15'], ['x' => 'before_or_equal:2012-01-15']);
  3406. $this->assertTrue($v->passes());
  3407. $v = new Validator($trans, ['x' => '2012-01-15'], ['x' => 'before_or_equal:2012-01-16']);
  3408. $this->assertTrue($v->passes());
  3409. $v = new Validator($trans, ['x' => '2012-01-15'], ['x' => 'before_or_equal:2012-01-14']);
  3410. $this->assertTrue($v->fails());
  3411. $v = new Validator($trans, ['x' => '15/01/2012'], ['x' => 'date_format:d/m/Y|before_or_equal:15/01/2012']);
  3412. $this->assertTrue($v->passes());
  3413. $v = new Validator($trans, ['x' => '15/01/2012'], ['x' => 'date_format:d/m/Y|before_or_equal:14/01/2012']);
  3414. $this->assertTrue($v->fails());
  3415. $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|before_or_equal:today']);
  3416. $this->assertTrue($v->passes());
  3417. $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|before_or_equal:tomorrow']);
  3418. $this->assertTrue($v->passes());
  3419. $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|before_or_equal:yesterday']);
  3420. $this->assertTrue($v->fails());
  3421. $v = new Validator($trans, ['x' => '2012-01-15'], ['x' => 'after_or_equal:2012-01-15']);
  3422. $this->assertTrue($v->passes());
  3423. $v = new Validator($trans, ['x' => '2012-01-15'], ['x' => 'after_or_equal:2012-01-14']);
  3424. $this->assertTrue($v->passes());
  3425. $v = new Validator($trans, ['x' => '2012-01-15'], ['x' => 'after_or_equal:2012-01-16']);
  3426. $this->assertTrue($v->fails());
  3427. $v = new Validator($trans, ['x' => '15/01/2012'], ['x' => 'date_format:d/m/Y|after_or_equal:15/01/2012']);
  3428. $this->assertTrue($v->passes());
  3429. $v = new Validator($trans, ['x' => '15/01/2012'], ['x' => 'date_format:d/m/Y|after_or_equal:16/01/2012']);
  3430. $this->assertTrue($v->fails());
  3431. $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|after_or_equal:today']);
  3432. $this->assertTrue($v->passes());
  3433. $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|after_or_equal:yesterday']);
  3434. $this->assertTrue($v->passes());
  3435. $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|after_or_equal:tomorrow']);
  3436. $this->assertTrue($v->fails());
  3437. $v = new Validator($trans, ['x' => '2012-01-01 17:44:00'], ['x' => 'date_format:Y-m-d H:i:s|before_or_equal:2012-01-01 17:44:00|after_or_equal:2012-01-01 17:44:00']);
  3438. $this->assertTrue($v->passes());
  3439. $v = new Validator($trans, ['x' => '2012-01-01 17:44:00'], ['x' => 'date_format:Y-m-d H:i:s|before_or_equal:2012-01-01 17:43:59']);
  3440. $this->assertTrue($v->fails());
  3441. $v = new Validator($trans, ['x' => '2012-01-01 17:44:00'], ['x' => 'date_format:Y-m-d H:i:s|after_or_equal:2012-01-01 17:44:01']);
  3442. $this->assertTrue($v->fails());
  3443. $v = new Validator($trans, ['x' => '17:44:00'], ['x' => 'date_format:H:i:s|before_or_equal:17:44:00|after_or_equal:17:44:00']);
  3444. $this->assertTrue($v->passes());
  3445. $v = new Validator($trans, ['x' => '17:44:00'], ['x' => 'date_format:H:i:s|before_or_equal:17:43:59']);
  3446. $this->assertTrue($v->fails());
  3447. $v = new Validator($trans, ['x' => '17:44:00'], ['x' => 'date_format:H:i:s|after_or_equal:17:44:01']);
  3448. $this->assertTrue($v->fails());
  3449. $v = new Validator($trans, ['x' => '17:44'], ['x' => 'date_format:H:i|before_or_equal:17:44|after_or_equal:17:44']);
  3450. $this->assertTrue($v->passes());
  3451. $v = new Validator($trans, ['x' => '17:44'], ['x' => 'date_format:H:i|before_or_equal:17:43']);
  3452. $this->assertTrue($v->fails());
  3453. $v = new Validator($trans, ['x' => '17:44'], ['x' => 'date_format:H:i|after_or_equal:17:45']);
  3454. $this->assertTrue($v->fails());
  3455. $v = new Validator($trans, ['foo' => '2012-01-14', 'bar' => '2012-01-15'], ['foo' => 'before_or_equal:bar']);
  3456. $this->assertTrue($v->passes());
  3457. $v = new Validator($trans, ['foo' => '2012-01-15', 'bar' => '2012-01-15'], ['foo' => 'before_or_equal:bar']);
  3458. $this->assertTrue($v->passes());
  3459. $v = new Validator($trans, ['foo' => '2012-01-15 13:00', 'bar' => '2012-01-15 12:00'], ['foo' => 'before_or_equal:bar']);
  3460. $this->assertTrue($v->fails());
  3461. $v = new Validator($trans, ['foo' => '2012-01-15 11:00', 'bar' => null], ['foo' => 'date_format:Y-m-d H:i|before_or_equal:bar', 'bar' => 'date_format:Y-m-d H:i']);
  3462. $this->assertTrue($v->fails());
  3463. $v = new Validator($trans, ['foo' => '2012-01-15 11:00', 'bar' => null], ['foo' => 'date_format:Y-m-d H:i|before_or_equal:bar', 'bar' => 'date_format:Y-m-d H:i|nullable']);
  3464. $this->assertTrue($v->passes());
  3465. $v = new Validator($trans, ['foo' => '2012-01-15 11:00', 'bar' => null], ['foo' => 'before_or_equal:bar']);
  3466. $this->assertTrue($v->fails());
  3467. $v = new Validator($trans, ['foo' => '2012-01-15 11:00', 'bar' => null], ['foo' => 'before_or_equal:bar', 'bar' => 'nullable']);
  3468. $this->assertTrue($v->fails());
  3469. }
  3470. public function testSometimesAddingRules()
  3471. {
  3472. $trans = $this->getIlluminateArrayTranslator();
  3473. $v = new Validator($trans, ['x' => 'foo'], ['x' => 'Required']);
  3474. $v->sometimes('x', 'Confirmed', function ($i) {
  3475. return $i->x === 'foo';
  3476. });
  3477. $this->assertEquals(['x' => ['Required', 'Confirmed']], $v->getRules());
  3478. $trans = $this->getIlluminateArrayTranslator();
  3479. $v = new Validator($trans, ['x' => ''], ['y' => 'Required']);
  3480. $v->sometimes('x', 'Required', function ($i) {
  3481. return true;
  3482. });
  3483. $this->assertEquals(['x' => ['Required'], 'y' => ['Required']], $v->getRules());
  3484. $trans = $this->getIlluminateArrayTranslator();
  3485. $v = new Validator($trans, ['x' => 'foo'], ['x' => 'Required']);
  3486. $v->sometimes('x', 'Confirmed', function ($i) {
  3487. return $i->x === 'bar';
  3488. });
  3489. $this->assertEquals(['x' => ['Required']], $v->getRules());
  3490. $trans = $this->getIlluminateArrayTranslator();
  3491. $v = new Validator($trans, ['x' => 'foo'], ['x' => 'Required']);
  3492. $v->sometimes('x', 'Foo|Bar', function ($i) {
  3493. return $i->x === 'foo';
  3494. });
  3495. $this->assertEquals(['x' => ['Required', 'Foo', 'Bar']], $v->getRules());
  3496. $trans = $this->getIlluminateArrayTranslator();
  3497. $v = new Validator($trans, ['x' => 'foo'], ['x' => 'Required']);
  3498. $v->sometimes('x', ['Foo', 'Bar:Baz'], function ($i) {
  3499. return $i->x === 'foo';
  3500. });
  3501. $this->assertEquals(['x' => ['Required', 'Foo', 'Bar:Baz']], $v->getRules());
  3502. $trans = $this->getIlluminateArrayTranslator();
  3503. $v = new Validator($trans, ['foo' => [['name' => 'first', 'title' => null]]], []);
  3504. $v->sometimes('foo.*.name', 'Required|String', function ($i) {
  3505. return is_null($i['foo'][0]['title']);
  3506. });
  3507. $this->assertEquals(['foo.0.name' => ['Required', 'String']], $v->getRules());
  3508. }
  3509. public function testItemAwareSometimesAddingRules()
  3510. {
  3511. // ['users'] -> if users is not empty it must be validated as array
  3512. $trans = $this->getIlluminateArrayTranslator();
  3513. $v = new Validator($trans, ['users' => [['name' => 'Taylor'], ['name' => 'Abigail']]], ['users.*.name'=> 'required|string']);
  3514. $v->sometimes(['users'], 'array', function ($i, $item) {
  3515. return $item !== null;
  3516. });
  3517. $this->assertEquals(['users' => ['array'], 'users.0.name' => ['required', 'string'], 'users.1.name' => ['required', 'string']], $v->getRules());
  3518. // ['users'] -> if users is null no rules will be applied
  3519. $trans = $this->getIlluminateArrayTranslator();
  3520. $v = new Validator($trans, ['users' => null], ['users.*.name'=> 'required|string']);
  3521. $v->sometimes(['users'], 'array', function ($i, $item) {
  3522. return (bool) $item;
  3523. });
  3524. $this->assertEquals([], $v->getRules());
  3525. // ['company.users'] -> if users is not empty it must be validated as array
  3526. $trans = $this->getIlluminateArrayTranslator();
  3527. $v = new Validator($trans, ['company' => ['users' => [['name' => 'Taylor'], ['name' => 'Abigail']]]], ['company.users.*.name'=> 'required|string']);
  3528. $v->sometimes(['company.users'], 'array', function ($i, $item) {
  3529. return $item->users !== null;
  3530. });
  3531. $this->assertEquals(['company.users' => ['array'], 'company.users.0.name' => ['required', 'string'], 'company.users.1.name' => ['required', 'string']], $v->getRules());
  3532. // ['company.users'] -> if users is null no rules will be applied
  3533. $trans = $this->getIlluminateArrayTranslator();
  3534. $v = new Validator($trans, ['company' => ['users' => null]], ['company'=> 'required', 'company.users.*.name'=> 'required|string']);
  3535. $v->sometimes(['company.users'], 'array', function ($i, $item) {
  3536. return (bool) $item->users;
  3537. });
  3538. $this->assertEquals(['company' => ['required']], $v->getRules());
  3539. // ['company.*'] -> if users is not empty it must be validated as array
  3540. $trans = $this->getIlluminateArrayTranslator();
  3541. $v = new Validator($trans, ['company' => ['users' => [['name' => 'Taylor'], ['name' => 'Abigail']]]], ['company.users.*.name'=> 'required|string']);
  3542. $v->sometimes(['company.*'], 'array', function ($i, $item) {
  3543. return $item !== null;
  3544. });
  3545. $this->assertEquals(['company.users' => ['array'], 'company.users.0.name' => ['required', 'string'], 'company.users.1.name' => ['required', 'string']], $v->getRules());
  3546. // ['company.*'] -> if users is null no rules will be applied
  3547. $trans = $this->getIlluminateArrayTranslator();
  3548. $v = new Validator($trans, ['company' => ['users' => null]], ['company'=> 'required', 'company.users.*.name'=> 'required|string']);
  3549. $v->sometimes(['company.*'], 'array', function ($i, $item) {
  3550. return (bool) $item;
  3551. });
  3552. $this->assertEquals(['company' => ['required']], $v->getRules());
  3553. // ['users.*'] -> all nested array items in users must be validated as array
  3554. $trans = $this->getIlluminateArrayTranslator();
  3555. $v = new Validator($trans, ['users' => [['name' => 'Taylor'], ['name' => 'Abigail']]], ['users.*.name'=> 'required|string']);
  3556. $v->sometimes(['users.*'], 'array', function ($i, $item) {
  3557. return (bool) $item;
  3558. });
  3559. $this->assertEquals(['users.0' => ['array'], 'users.1' => ['array'], 'users.0.name' => ['required', 'string'], 'users.1.name' => ['required', 'string']], $v->getRules());
  3560. // ['company.users.*'] -> all nested array items in users must be validated as array
  3561. $trans = $this->getIlluminateArrayTranslator();
  3562. $v = new Validator($trans, ['company' => ['users' => [['name' => 'Taylor'], ['name' => 'Abigail']]]], ['company.users.*.name'=> 'required|string']);
  3563. $v->sometimes(['company.users.*'], 'array', function () {
  3564. return true;
  3565. });
  3566. $this->assertEquals(['company.users.0' => ['array'], 'company.users.1' => ['array'], 'company.users.0.name' => ['required', 'string'], 'company.users.1.name' => ['required', 'string']], $v->getRules());
  3567. // ['company.*.*'] -> all nested array items in users must be validated as array
  3568. $trans = $this->getIlluminateArrayTranslator();
  3569. $v = new Validator($trans, ['company' => ['users' => [['name' => 'Taylor'], ['name' => 'Abigail']]]], ['company.users.*.name'=> 'required|string']);
  3570. $v->sometimes(['company.*.*'], 'array', function ($i, $item) {
  3571. return true;
  3572. });
  3573. $this->assertEquals(['company.users.0' => ['array'], 'company.users.1' => ['array'], 'company.users.0.name' => ['required', 'string'], 'company.users.1.name' => ['required', 'string']], $v->getRules());
  3574. // ['user.profile.value'] -> multiple true cases, the item based condition does match and the optional validation is added
  3575. $trans = $this->getIlluminateArrayTranslator();
  3576. $v = new Validator($trans, ['user' => ['profile' => ['photo' => 'image.jpg', 'type' => 'email', 'value' => 'test@test.com']]], ['user.profile.*' => ['required']]);
  3577. $v->sometimes(['user.profile.value'], 'email', function ($i, $item) {
  3578. return $item->type === 'email';
  3579. });
  3580. $v->sometimes('user.profile.photo', 'mimes:jpg,bmp,png', function ($i, $item) {
  3581. return $item->photo;
  3582. });
  3583. $this->assertEquals(['user.profile.value' => ['required', 'email'], 'user.profile.photo' => ['required', 'mimes:jpg,bmp,png'], 'user.profile.type' => ['required']], $v->getRules());
  3584. // ['user.profile.value'] -> multiple true cases with middle wildcard, the item based condition does match and the optional validation is added
  3585. $trans = $this->getIlluminateArrayTranslator();
  3586. $v = new Validator($trans, ['user' => ['profile' => ['photo' => 'image.jpg', 'type' => 'email', 'value' => 'test@test.com']]], ['user.profile.*' => ['required']]);
  3587. $v->sometimes('user.*.value', 'email', function ($i, $item) {
  3588. return $item->type === 'email';
  3589. });
  3590. $v->sometimes('user.*.photo', 'mimes:jpg,bmp,png', function ($i, $item) {
  3591. return $item->photo;
  3592. });
  3593. $this->assertEquals(['user.profile.value' => ['required', 'email'], 'user.profile.photo' => ['required', 'mimes:jpg,bmp,png'], 'user.profile.type' => ['required']], $v->getRules());
  3594. // ['profiles.*.value'] -> true and false cases for the same field with middle wildcard, the item based condition does match and the optional validation is added
  3595. $trans = $this->getIlluminateArrayTranslator();
  3596. $v = new Validator($trans, ['profiles' => [['type' => 'email'], ['type' => 'string']]], ['profiles.*.value' => ['required']]);
  3597. $v->sometimes(['profiles.*.value'], 'email', function ($i, $item) {
  3598. return $item->type === 'email';
  3599. });
  3600. $v->sometimes('profiles.*.value', 'url', function ($i, $item) {
  3601. return $item->type !== 'email';
  3602. });
  3603. $this->assertEquals(['profiles.0.value' => ['required', 'email'], 'profiles.1.value' => ['required', 'url']], $v->getRules());
  3604. // ['profiles.*.value'] -> true case with middle wildcard, the item based condition does match and the optional validation is added
  3605. $trans = $this->getIlluminateArrayTranslator();
  3606. $v = new Validator($trans, ['profiles' => [['type' => 'email'], ['type' => 'string']]], ['profiles.*.value' => ['required']]);
  3607. $v->sometimes(['profiles.*.value'], 'email', function ($i, $item) {
  3608. return $item->type === 'email';
  3609. });
  3610. $this->assertEquals(['profiles.0.value' => ['required', 'email'], 'profiles.1.value' => ['required']], $v->getRules());
  3611. // ['profiles.*.value'] -> false case with middle wildcard, the item based condition does not match and the optional validation is not added
  3612. $trans = $this->getIlluminateArrayTranslator();
  3613. $v = new Validator($trans, ['profiles' => [['type' => 'string'], ['type' => 'string']]], ['profiles.*.value' => ['required']]);
  3614. $v->sometimes(['profiles.*.value'], 'email', function ($i, $item) {
  3615. return $item->type === 'email';
  3616. });
  3617. $this->assertEquals(['profiles.0.value' => ['required'], 'profiles.1.value' => ['required']], $v->getRules());
  3618. // ['users.profiles.*.value'] -> true case nested and with middle wildcard, the item based condition does match and the optional validation is added
  3619. $trans = $this->getIlluminateArrayTranslator();
  3620. $v = new Validator($trans, ['users' => ['profiles' => [['type' => 'email'], ['type' => 'string']]]], ['users.profiles.*.value' => ['required']]);
  3621. $v->sometimes(['users.profiles.*.value'], 'email', function ($i, $item) {
  3622. return $item->type === 'email';
  3623. });
  3624. $this->assertEquals(['users.profiles.0.value' => ['required', 'email'], 'users.profiles.1.value' => ['required']], $v->getRules());
  3625. // ['users.*.*.value'] -> true case nested and with double middle wildcard, the item based condition does match and the optional validation is added
  3626. $trans = $this->getIlluminateArrayTranslator();
  3627. $v = new Validator($trans, ['users' => ['profiles' => [['type' => 'email'], ['type' => 'string']]]], ['users.profiles.*.value' => ['required']]);
  3628. $v->sometimes(['users.*.*.value'], 'email', function ($i, $item) {
  3629. return $item->type === 'email';
  3630. });
  3631. $this->assertEquals(['users.profiles.0.value' => ['required', 'email'], 'users.profiles.1.value' => ['required']], $v->getRules());
  3632. // 'user.value' -> true case nested with string, the item based condition does match and the optional validation is added
  3633. $trans = $this->getIlluminateArrayTranslator();
  3634. $v = new Validator($trans, ['user' => ['name' => 'username', 'type' => 'email', 'value' => 'test@test.com']], ['user.*' => ['required']]);
  3635. $v->sometimes('user.value', 'email', function ($i, $item) {
  3636. return $item->type === 'email';
  3637. });
  3638. $this->assertEquals(['user.name' => ['required'], 'user.type' => ['required'], 'user.value' => ['required', 'email']], $v->getRules());
  3639. // 'user.value' -> standard true case with string, the INPUT based condition does match and the optional validation is added
  3640. $trans = $this->getIlluminateArrayTranslator();
  3641. $v = new Validator($trans, ['name' => 'username', 'type' => 'email', 'value' => 'test@test.com'], ['*' => ['required']]);
  3642. $v->sometimes('value', 'email', function ($i) {
  3643. return $i->type === 'email';
  3644. });
  3645. $this->assertEquals(['name' => ['required'], 'type' => ['required'], 'value' => ['required', 'email']], $v->getRules());
  3646. // ['value'] -> standard true case with array, the INPUT based condition does match and the optional validation is added
  3647. $trans = $this->getIlluminateArrayTranslator();
  3648. $v = new Validator($trans, ['name' => 'username', 'type' => 'email', 'value' => 'test@test.com'], ['*' => ['required']]);
  3649. $v->sometimes(['value'], 'email', function ($i, $item) {
  3650. return $i->type === 'email';
  3651. });
  3652. $this->assertEquals(['name' => ['required'], 'type' => ['required'], 'value' => ['required', 'email']], $v->getRules());
  3653. // ['email'] -> if value is set, it will be validated as string
  3654. $trans = $this->getIlluminateArrayTranslator();
  3655. $v = new Validator($trans, ['email' => 'test@test.com'], ['*' => ['required']]);
  3656. $v->sometimes(['email'], 'email', function ($i, $item) {
  3657. return $item;
  3658. });
  3659. $this->assertEquals(['email' => ['required', 'email']], $v->getRules());
  3660. // ['attendee.*'] -> if attendee name is set, all other fields will be required as well
  3661. $trans = $this->getIlluminateArrayTranslator();
  3662. $v = new Validator($trans, ['attendee' => ['name' => 'Taylor', 'title' => 'Creator of Laravel', 'type' => 'Developer']], ['attendee.*'=> 'string']);
  3663. $v->sometimes(['attendee.*'], 'required', function ($i, $item) {
  3664. return (bool) $item;
  3665. });
  3666. $this->assertEquals(['attendee.name' => ['string', 'required'], 'attendee.title' => ['string', 'required'], 'attendee.type' => ['string', 'required']], $v->getRules());
  3667. }
  3668. public function testValidateSometimesImplicitEachWithAsterisksBeforeAndAfter()
  3669. {
  3670. $trans = $this->getIlluminateArrayTranslator();
  3671. $v = new Validator($trans, [
  3672. 'foo' => [
  3673. ['start' => '2016-04-19', 'end' => '2017-04-19'],
  3674. ],
  3675. ], []);
  3676. $v->sometimes('foo.*.start', ['before:foo.*.end'], function () {
  3677. return true;
  3678. });
  3679. $this->assertTrue($v->passes());
  3680. $v = new Validator($trans, [
  3681. 'foo' => [
  3682. ['start' => '2016-04-19', 'end' => '2017-04-19'],
  3683. ],
  3684. ], []);
  3685. $v->sometimes('foo.*.start', 'before:foo.*.end', function () {
  3686. return true;
  3687. });
  3688. $this->assertTrue($v->passes());
  3689. $v = new Validator($trans, [
  3690. 'foo' => [
  3691. ['start' => '2016-04-19', 'end' => '2017-04-19'],
  3692. ],
  3693. ], []);
  3694. $v->sometimes('foo.*.end', ['before:foo.*.start'], function () {
  3695. return true;
  3696. });
  3697. $this->assertTrue($v->fails());
  3698. $v = new Validator($trans, [
  3699. 'foo' => [
  3700. ['start' => '2016-04-19', 'end' => '2017-04-19'],
  3701. ],
  3702. ], []);
  3703. $v->sometimes('foo.*.end', ['after:foo.*.start'], function () {
  3704. return true;
  3705. });
  3706. $this->assertTrue($v->passes());
  3707. $v = new Validator($trans, [
  3708. 'foo' => [
  3709. ['start' => '2016-04-19', 'end' => '2017-04-19'],
  3710. ],
  3711. ], []);
  3712. $v->sometimes('foo.*.start', ['after:foo.*.end'], function () {
  3713. return true;
  3714. });
  3715. $this->assertTrue($v->fails());
  3716. }
  3717. public function testCustomValidators()
  3718. {
  3719. $trans = $this->getIlluminateArrayTranslator();
  3720. $trans->addLines(['validation.foo' => 'foo!'], 'en');
  3721. $v = new Validator($trans, ['name' => 'taylor'], ['name' => 'foo']);
  3722. $v->addExtension('foo', function () {
  3723. return false;
  3724. });
  3725. $this->assertFalse($v->passes());
  3726. $v->messages()->setFormat(':message');
  3727. $this->assertSame('foo!', $v->messages()->first('name'));
  3728. $trans = $this->getIlluminateArrayTranslator();
  3729. $trans->addLines(['validation.foo_bar' => 'foo!'], 'en');
  3730. $v = new Validator($trans, ['name' => 'taylor'], ['name' => 'foo_bar']);
  3731. $v->addExtension('FooBar', function () {
  3732. return false;
  3733. });
  3734. $this->assertFalse($v->passes());
  3735. $v->messages()->setFormat(':message');
  3736. $this->assertSame('foo!', $v->messages()->first('name'));
  3737. $trans = $this->getIlluminateArrayTranslator();
  3738. $v = new Validator($trans, ['name' => 'taylor'], ['name' => 'foo_bar']);
  3739. $v->addExtension('FooBar', function () {
  3740. return false;
  3741. });
  3742. $v->setFallbackMessages(['foo_bar' => 'foo!']);
  3743. $this->assertFalse($v->passes());
  3744. $v->messages()->setFormat(':message');
  3745. $this->assertSame('foo!', $v->messages()->first('name'));
  3746. $trans = $this->getIlluminateArrayTranslator();
  3747. $v = new Validator($trans, ['name' => 'taylor'], ['name' => 'foo_bar']);
  3748. $v->addExtensions([
  3749. 'FooBar' => function () {
  3750. return false;
  3751. },
  3752. ]);
  3753. $v->setFallbackMessages(['foo_bar' => 'foo!']);
  3754. $this->assertFalse($v->passes());
  3755. $v->messages()->setFormat(':message');
  3756. $this->assertSame('foo!', $v->messages()->first('name'));
  3757. }
  3758. public function testClassBasedCustomValidators()
  3759. {
  3760. $trans = $this->getIlluminateArrayTranslator();
  3761. $trans->addLines(['validation.foo' => 'foo!'], 'en');
  3762. $v = new Validator($trans, ['name' => 'taylor'], ['name' => 'foo']);
  3763. $v->setContainer($container = m::mock(Container::class));
  3764. $v->addExtension('foo', 'Foo@bar');
  3765. $container->shouldReceive('make')->once()->with('Foo')->andReturn($foo = m::mock(stdClass::class));
  3766. $foo->shouldReceive('bar')->once()->andReturn(false);
  3767. $this->assertFalse($v->passes());
  3768. $v->messages()->setFormat(':message');
  3769. $this->assertSame('foo!', $v->messages()->first('name'));
  3770. }
  3771. public function testClassBasedCustomValidatorsUsingConventionalMethod()
  3772. {
  3773. $trans = $this->getIlluminateArrayTranslator();
  3774. $trans->addLines(['validation.foo' => 'foo!'], 'en');
  3775. $v = new Validator($trans, ['name' => 'taylor'], ['name' => 'foo']);
  3776. $v->setContainer($container = m::mock(Container::class));
  3777. $v->addExtension('foo', 'Foo');
  3778. $container->shouldReceive('make')->once()->with('Foo')->andReturn($foo = m::mock(stdClass::class));
  3779. $foo->shouldReceive('validate')->once()->andReturn(false);
  3780. $this->assertFalse($v->passes());
  3781. $v->messages()->setFormat(':message');
  3782. $this->assertSame('foo!', $v->messages()->first('name'));
  3783. }
  3784. public function testCustomImplicitValidators()
  3785. {
  3786. $trans = $this->getIlluminateArrayTranslator();
  3787. $v = new Validator($trans, [], ['implicit_rule' => 'foo']);
  3788. $v->addImplicitExtension('implicit_rule', function () {
  3789. return true;
  3790. });
  3791. $this->assertTrue($v->passes());
  3792. }
  3793. public function testCustomDependentValidators()
  3794. {
  3795. $trans = $this->getIlluminateArrayTranslator();
  3796. $v = new Validator($trans,
  3797. [
  3798. ['name' => 'Jamie', 'age' => 27],
  3799. ],
  3800. ['*.name' => 'dependent_rule:*.age']
  3801. );
  3802. $v->addDependentExtension('dependent_rule', function ($name) use ($v) {
  3803. return Arr::get($v->getData(), $name) === 'Jamie';
  3804. });
  3805. $this->assertTrue($v->passes());
  3806. }
  3807. public function testExceptionThrownOnIncorrectParameterCount()
  3808. {
  3809. $this->expectException(InvalidArgumentException::class);
  3810. $this->expectExceptionMessage('Validation rule required_if requires at least 2 parameters.');
  3811. $trans = $this->getTranslator();
  3812. $v = new Validator($trans, [], ['foo' => 'required_if:foo']);
  3813. $v->passes();
  3814. }
  3815. public function testValidateImplicitEachWithAsterisks()
  3816. {
  3817. $trans = $this->getIlluminateArrayTranslator();
  3818. $data = ['foo' => [5, 10, 15]];
  3819. // pipe rules fails
  3820. $v = new Validator($trans, $data, [
  3821. 'foo' => 'Array',
  3822. 'foo.*' => 'Numeric|Min:6|Max:16',
  3823. ]);
  3824. $this->assertFalse($v->passes());
  3825. // pipe passes
  3826. $v = new Validator($trans, $data, [
  3827. 'foo' => 'Array',
  3828. 'foo.*' => 'Numeric|Min:4|Max:16',
  3829. ]);
  3830. $this->assertTrue($v->passes());
  3831. // array rules fails
  3832. $v = new Validator($trans, $data, [
  3833. 'foo' => 'Array',
  3834. 'foo.*' => ['Numeric', 'Min:6', 'Max:16'],
  3835. ]);
  3836. $this->assertFalse($v->passes());
  3837. // array rules passes
  3838. $v = new Validator($trans, $data, [
  3839. 'foo' => 'Array',
  3840. 'foo.*' => ['Numeric', 'Min:4', 'Max:16'],
  3841. ]);
  3842. $this->assertTrue($v->passes());
  3843. // string passes
  3844. $v = new Validator($trans,
  3845. ['foo' => [['name' => 'first'], ['name' => 'second']]],
  3846. ['foo' => 'Array', 'foo.*.name' => 'Required|String']);
  3847. $this->assertTrue($v->passes());
  3848. // numeric fails
  3849. $v = new Validator($trans,
  3850. ['foo' => [['name' => 'first'], ['name' => 'second']]],
  3851. ['foo' => 'Array', 'foo.*.name' => 'Required|Numeric']);
  3852. $this->assertFalse($v->passes());
  3853. // nested array fails
  3854. $v = new Validator($trans,
  3855. ['foo' => [['name' => 'first', 'votes' => [1, 2]], ['name' => 'second', 'votes' => ['something', 2]]]],
  3856. ['foo' => 'Array', 'foo.*.name' => 'Required|String', 'foo.*.votes.*' => 'Required|Integer']);
  3857. $this->assertFalse($v->passes());
  3858. // multiple items passes
  3859. $v = new Validator($trans, ['foo' => [['name' => 'first'], ['name' => 'second']]],
  3860. ['foo' => 'Array', 'foo.*.name' => ['Required', 'String']]);
  3861. $this->assertTrue($v->passes());
  3862. // multiple items fails
  3863. $v = new Validator($trans, ['foo' => [['name' => 'first'], ['name' => 'second']]],
  3864. ['foo' => 'Array', 'foo.*.name' => ['Required', 'Numeric']]);
  3865. $this->assertFalse($v->passes());
  3866. // nested arrays fails
  3867. $v = new Validator($trans,
  3868. ['foo' => [['name' => 'first', 'votes' => [1, 2]], ['name' => 'second', 'votes' => ['something', 2]]]],
  3869. ['foo' => 'Array', 'foo.*.name' => ['Required', 'String'], 'foo.*.votes.*' => ['Required', 'Integer']]);
  3870. $this->assertFalse($v->passes());
  3871. }
  3872. public function testSometimesOnArraysInImplicitRules()
  3873. {
  3874. $trans = $this->getIlluminateArrayTranslator();
  3875. $v = new Validator($trans, [['bar' => 'baz']], ['*.foo' => 'sometimes|required|string']);
  3876. $this->assertTrue($v->passes());
  3877. // $data = ['names' => [['second' => []]]];
  3878. // $v = new Validator($trans, $data, ['names.*.second' => 'sometimes|required']);
  3879. // $this->assertFalse($v->passes());
  3880. $data = ['names' => [['second' => ['Taylor']]]];
  3881. $v = new Validator($trans, $data, ['names.*.second' => 'sometimes|required|string']);
  3882. $this->assertFalse($v->passes());
  3883. $this->assertEquals(['validation.string'], $v->errors()->get('names.0.second'));
  3884. }
  3885. public function testValidateImplicitEachWithAsterisksForRequiredNonExistingKey()
  3886. {
  3887. $trans = $this->getIlluminateArrayTranslator();
  3888. $data = ['companies' => ['spark']];
  3889. $v = new Validator($trans, $data, ['companies.*.name' => 'required']);
  3890. $this->assertFalse($v->passes());
  3891. $data = ['names' => [['second' => 'I have no first']]];
  3892. $v = new Validator($trans, $data, ['names.*.first' => 'required']);
  3893. $this->assertFalse($v->passes());
  3894. $data = [];
  3895. $v = new Validator($trans, $data, ['names.*.first' => 'required']);
  3896. $this->assertTrue($v->passes());
  3897. $data = ['names' => [['second' => 'I have no first']]];
  3898. $v = new Validator($trans, $data, ['names.*.first' => 'required']);
  3899. $this->assertFalse($v->passes());
  3900. $data = [
  3901. 'people' => [
  3902. ['cars' => [['model' => 2005], []]],
  3903. ],
  3904. ];
  3905. $v = new Validator($trans, $data, ['people.*.cars.*.model' => 'required']);
  3906. $this->assertFalse($v->passes());
  3907. $data = [
  3908. 'people' => [
  3909. ['name' => 'test', 'cars' => [['model' => 2005], ['name' => 'test2']]],
  3910. ],
  3911. ];
  3912. $v = new Validator($trans, $data, ['people.*.cars.*.model' => 'required']);
  3913. $this->assertFalse($v->passes());
  3914. $data = [
  3915. 'people' => [
  3916. ['phones' => ['iphone', 'android'], 'cars' => [['model' => 2005], ['name' => 'test2']]],
  3917. ],
  3918. ];
  3919. $v = new Validator($trans, $data, ['people.*.cars.*.model' => 'required']);
  3920. $this->assertFalse($v->passes());
  3921. $data = ['names' => [['second' => '2']]];
  3922. $v = new Validator($trans, $data, ['names.*.first' => 'sometimes|required']);
  3923. $this->assertTrue($v->passes());
  3924. $data = [
  3925. 'people' => [
  3926. ['name' => 'Jon', 'email' => 'a@b.c'],
  3927. ['name' => 'Jon'],
  3928. ],
  3929. ];
  3930. $v = new Validator($trans, $data, ['people.*.email' => 'required']);
  3931. $this->assertFalse($v->passes());
  3932. $data = [
  3933. 'people' => [
  3934. [
  3935. 'name' => 'Jon',
  3936. 'cars' => [
  3937. ['model' => 2014],
  3938. ],
  3939. ],
  3940. [
  3941. 'name' => 'Arya',
  3942. 'cars' => [
  3943. ['name' => 'test'],
  3944. ],
  3945. ],
  3946. ],
  3947. ];
  3948. $v = new Validator($trans, $data, ['people.*.cars.*.model' => 'required']);
  3949. $this->assertFalse($v->passes());
  3950. }
  3951. public function testParsingArrayKeysWithDot()
  3952. {
  3953. $trans = $this->getIlluminateArrayTranslator();
  3954. // Interpreted dot fails on empty value
  3955. $v = new Validator($trans, ['foo' => ['bar' => ''], 'foo.bar' => 'valid'], ['foo.bar' => 'required']);
  3956. $this->assertTrue($v->fails());
  3957. // Escaped dot fails on empty value
  3958. $v = new Validator($trans, ['foo' => ['bar' => 'valid'], 'foo.bar' => ''], ['foo\.bar' => 'required']);
  3959. $this->assertTrue($v->fails());
  3960. // Interpreted dot succeeds
  3961. $v = new Validator($trans, ['foo' => ['bar' => 'valid'], 'foo.bar' => 'zxc'], ['foo\.bar' => 'required']);
  3962. $this->assertFalse($v->fails());
  3963. // Interpreted dot followed by escaped dot fails on empty value
  3964. $v = new Validator($trans, ['foo' => ['bar.baz' => '']], ['foo.bar\.baz' => 'required']);
  3965. $this->assertTrue($v->fails());
  3966. // Interpreted dot followed by escaped dot fails on empty value
  3967. $v = new Validator($trans, ['foo' => [['bar.baz' => ''], ['bar.baz' => '']]], ['foo.*.bar\.baz' => 'required']);
  3968. $this->assertTrue($v->fails());
  3969. }
  3970. public function testParsingArrayKeysWithDotWhenTestingExistence()
  3971. {
  3972. $trans = $this->getIlluminateArrayTranslator();
  3973. // RequiredWith using escaped dot in a nested array
  3974. $v = new Validator($trans, ['foo' => '', 'bar' => ['foo.bar' => 'valid']], ['foo' => 'required_with:bar.foo\.bar']);
  3975. $this->assertFalse($v->passes());
  3976. // RequiredWithAll using escaped dot in a nested array
  3977. $v = new Validator($trans, ['foo' => '', 'bar' => ['foo.bar' => 'valid']], ['foo' => 'required_with_all:bar.foo\.bar']);
  3978. $this->assertFalse($v->passes());
  3979. // RequiredWithout using escaped dot in a nested array
  3980. $v = new Validator($trans, ['foo' => 'valid', 'bar' => ['foo.bar' => 'valid']], ['foo' => 'required_without:bar.foo\.bar']);
  3981. $this->assertTrue($v->passes());
  3982. // RequiredWithoutAll using escaped dot in a nested array
  3983. $v = new Validator($trans, ['foo' => 'valid', 'bar' => ['foo.bar' => 'valid']], ['foo' => 'required_without_all:bar.foo\.bar']);
  3984. $this->assertTrue($v->passes());
  3985. // Same using escaped dot in a nested array
  3986. $v = new Validator($trans, ['foo' => 'valid', 'bar' => ['foo.bar' => 'valid']], ['foo' => 'same:bar.foo\.bar']);
  3987. $this->assertTrue($v->passes());
  3988. // RequiredUnless using escaped dot in a nested array
  3989. $v = new Validator($trans, ['foo' => '', 'bar' => ['foo.bar' => 'valid']], ['foo' => 'required_unless:bar.foo\.bar,valid']);
  3990. $this->assertTrue($v->passes());
  3991. }
  3992. public function testPassingSlashVulnerability()
  3993. {
  3994. $trans = $this->getIlluminateArrayTranslator();
  3995. $v = new Validator($trans, [
  3996. 'matrix' => ['\\' => ['invalid'], '1\\' => ['invalid']],
  3997. ], [
  3998. 'matrix.*.*' => 'integer',
  3999. ]);
  4000. $this->assertTrue($v->fails());
  4001. $v = new Validator($trans, [
  4002. 'matrix' => ['\\' => [1], '1\\' => [1]],
  4003. ], [
  4004. 'matrix.*.*' => 'integer',
  4005. ]);
  4006. $this->assertTrue($v->passes());
  4007. $v = new Validator($trans, [
  4008. 'foo' => ['bar' => 'valid'], 'foo.bar' => 'invalid', 'foo->bar' => 'valid',
  4009. ], [
  4010. 'foo\.bar' => 'required|in:valid',
  4011. ]);
  4012. $this->assertTrue($v->fails());
  4013. }
  4014. public function testPlaceholdersAreReplaced()
  4015. {
  4016. $trans = $this->getIlluminateArrayTranslator();
  4017. $v = new Validator($trans, [
  4018. 'matrix' => ['\\' => ['invalid'], '1\\' => ['invalid']],
  4019. ], [
  4020. 'matrix.*.*' => 'integer',
  4021. ]);
  4022. $this->assertTrue($v->fails());
  4023. $v = new Validator($trans, [
  4024. 'matrix' => ['\\' => [1], '1\\' => [1]],
  4025. ], [
  4026. 'matrix.*.*' => 'integer',
  4027. ]);
  4028. $this->assertTrue($v->passes());
  4029. $v = new Validator($trans, [
  4030. 'foo' => ['bar' => 'valid'], 'foo.bar' => 'invalid', 'foo->bar' => 'valid',
  4031. ], [
  4032. 'foo\.bar' => 'required|in:valid',
  4033. ]);
  4034. $this->assertTrue($v->fails());
  4035. $this->assertArrayHasKey('foo.bar', $v->errors()->getMessages());
  4036. $v = new Validator($trans, [
  4037. 'foo.bar' => 'valid',
  4038. ], [
  4039. 'foo\.bar' => 'required|in:valid',
  4040. ]);
  4041. $this->assertTrue($v->passes());
  4042. $this->assertArrayHasKey('foo.bar', $v->validated());
  4043. }
  4044. public function testCoveringEmptyKeys()
  4045. {
  4046. $trans = $this->getIlluminateArrayTranslator();
  4047. $v = new Validator($trans, ['foo' => ['' => ['bar' => '']]], ['foo.*.bar' => 'required']);
  4048. $this->assertTrue($v->fails());
  4049. }
  4050. public function testImplicitEachWithAsterisksWithArrayValues()
  4051. {
  4052. $trans = $this->getIlluminateArrayTranslator();
  4053. $v = new Validator($trans, ['foo' => ['bar.baz' => '']], ['foo' => 'required']);
  4054. $this->assertEquals(['foo' => ['bar.baz' => '']], $v->validated());
  4055. }
  4056. public function testValidateNestedArrayWithCommonParentChildKey()
  4057. {
  4058. $trans = $this->getIlluminateArrayTranslator();
  4059. $data = [
  4060. 'products' => [
  4061. [
  4062. 'price' => 2,
  4063. 'options' => [
  4064. ['price' => 1],
  4065. ],
  4066. ],
  4067. [
  4068. 'price' => 2,
  4069. 'options' => [
  4070. ['price' => 0],
  4071. ],
  4072. ],
  4073. ],
  4074. ];
  4075. $v = new Validator($trans, $data, ['products.*.price' => 'numeric|min:1']);
  4076. $this->assertTrue($v->passes());
  4077. }
  4078. public function testValidateNestedArrayWithNonNumericKeys()
  4079. {
  4080. $trans = $this->getIlluminateArrayTranslator();
  4081. $data = [
  4082. 'item_amounts' => [
  4083. 'item_123' => 2,
  4084. ],
  4085. ];
  4086. $v = new Validator($trans, $data, ['item_amounts.*' => 'numeric|min:5']);
  4087. $this->assertFalse($v->passes());
  4088. }
  4089. public function testValidateImplicitEachWithAsterisksConfirmed()
  4090. {
  4091. $trans = $this->getIlluminateArrayTranslator();
  4092. // confirmed passes
  4093. $v = new Validator($trans, [
  4094. 'foo' => [
  4095. ['password' => 'foo0', 'password_confirmation' => 'foo0'],
  4096. ['password' => 'foo1', 'password_confirmation' => 'foo1'],
  4097. ],
  4098. ], ['foo.*.password' => 'confirmed']);
  4099. $this->assertTrue($v->passes());
  4100. // nested confirmed passes
  4101. $v = new Validator($trans, [
  4102. 'foo' => [
  4103. [
  4104. 'bar' => [
  4105. ['password' => 'bar0', 'password_confirmation' => 'bar0'],
  4106. ['password' => 'bar1', 'password_confirmation' => 'bar1'],
  4107. ],
  4108. ],
  4109. [
  4110. 'bar' => [
  4111. ['password' => 'bar2', 'password_confirmation' => 'bar2'],
  4112. ['password' => 'bar3', 'password_confirmation' => 'bar3'],
  4113. ],
  4114. ],
  4115. ],
  4116. ], ['foo.*.bar.*.password' => 'confirmed']);
  4117. $this->assertTrue($v->passes());
  4118. // confirmed fails
  4119. $v = new Validator($trans, [
  4120. 'foo' => [
  4121. ['password' => 'foo0', 'password_confirmation' => 'bar0'],
  4122. ['password' => 'foo1'],
  4123. ],
  4124. ], ['foo.*.password' => 'confirmed']);
  4125. $this->assertFalse($v->passes());
  4126. $this->assertTrue($v->messages()->has('foo.0.password'));
  4127. $this->assertTrue($v->messages()->has('foo.1.password'));
  4128. // nested confirmed fails
  4129. $v = new Validator($trans, [
  4130. 'foo' => [
  4131. [
  4132. 'bar' => [
  4133. ['password' => 'bar0'],
  4134. ['password' => 'bar1', 'password_confirmation' => 'bar2'],
  4135. ],
  4136. ],
  4137. ],
  4138. ], ['foo.*.bar.*.password' => 'confirmed']);
  4139. $this->assertFalse($v->passes());
  4140. $this->assertTrue($v->messages()->has('foo.0.bar.0.password'));
  4141. $this->assertTrue($v->messages()->has('foo.0.bar.1.password'));
  4142. }
  4143. public function testValidateImplicitEachWithAsterisksDifferent()
  4144. {
  4145. $trans = $this->getIlluminateArrayTranslator();
  4146. // different passes
  4147. $v = new Validator($trans, [
  4148. 'foo' => [
  4149. ['name' => 'foo', 'last' => 'bar'],
  4150. ['name' => 'bar', 'last' => 'foo'],
  4151. ],
  4152. ], ['foo.*.name' => ['different:foo.*.last']]);
  4153. $this->assertTrue($v->passes());
  4154. // nested different passes
  4155. $v = new Validator($trans, [
  4156. 'foo' => [
  4157. [
  4158. 'bar' => [
  4159. ['name' => 'foo', 'last' => 'bar'],
  4160. ['name' => 'bar', 'last' => 'foo'],
  4161. ],
  4162. ],
  4163. ],
  4164. ], ['foo.*.bar.*.name' => ['different:foo.*.bar.*.last']]);
  4165. $this->assertTrue($v->passes());
  4166. // different fails
  4167. $v = new Validator($trans, [
  4168. 'foo' => [
  4169. ['name' => 'foo', 'last' => 'foo'],
  4170. ['name' => 'bar', 'last' => 'bar'],
  4171. ],
  4172. ], ['foo.*.name' => ['different:foo.*.last']]);
  4173. $this->assertFalse($v->passes());
  4174. $this->assertTrue($v->messages()->has('foo.0.name'));
  4175. $this->assertTrue($v->messages()->has('foo.1.name'));
  4176. // nested different fails
  4177. $v = new Validator($trans, [
  4178. 'foo' => [
  4179. [
  4180. 'bar' => [
  4181. ['name' => 'foo', 'last' => 'foo'],
  4182. ['name' => 'bar', 'last' => 'bar'],
  4183. ],
  4184. ],
  4185. ],
  4186. ], ['foo.*.bar.*.name' => ['different:foo.*.bar.*.last']]);
  4187. $this->assertFalse($v->passes());
  4188. $this->assertTrue($v->messages()->has('foo.0.bar.0.name'));
  4189. $this->assertTrue($v->messages()->has('foo.0.bar.1.name'));
  4190. }
  4191. public function testValidateImplicitEachWithAsterisksSame()
  4192. {
  4193. $trans = $this->getIlluminateArrayTranslator();
  4194. // same passes
  4195. $v = new Validator($trans, [
  4196. 'foo' => [
  4197. ['name' => 'foo', 'last' => 'foo'],
  4198. ['name' => 'bar', 'last' => 'bar'],
  4199. ],
  4200. ], ['foo.*.name' => ['same:foo.*.last']]);
  4201. $this->assertTrue($v->passes());
  4202. // nested same passes
  4203. $v = new Validator($trans, [
  4204. 'foo' => [
  4205. [
  4206. 'bar' => [
  4207. ['name' => 'foo', 'last' => 'foo'],
  4208. ['name' => 'bar', 'last' => 'bar'],
  4209. ],
  4210. ],
  4211. ],
  4212. ], ['foo.*.bar.*.name' => ['same:foo.*.bar.*.last']]);
  4213. $this->assertTrue($v->passes());
  4214. // same fails
  4215. $v = new Validator($trans, [
  4216. 'foo' => [
  4217. ['name' => 'foo', 'last' => 'bar'],
  4218. ['name' => 'bar', 'last' => 'foo'],
  4219. ],
  4220. ], ['foo.*.name' => ['same:foo.*.last']]);
  4221. $this->assertFalse($v->passes());
  4222. $this->assertTrue($v->messages()->has('foo.0.name'));
  4223. $this->assertTrue($v->messages()->has('foo.1.name'));
  4224. // nested same fails
  4225. $v = new Validator($trans, [
  4226. 'foo' => [
  4227. [
  4228. 'bar' => [
  4229. ['name' => 'foo', 'last' => 'bar'],
  4230. ['name' => 'bar', 'last' => 'foo'],
  4231. ],
  4232. ],
  4233. ],
  4234. ], ['foo.*.bar.*.name' => ['same:foo.*.bar.*.last']]);
  4235. $this->assertFalse($v->passes());
  4236. $this->assertTrue($v->messages()->has('foo.0.bar.0.name'));
  4237. $this->assertTrue($v->messages()->has('foo.0.bar.1.name'));
  4238. }
  4239. public function testValidateImplicitEachWithAsterisksRequired()
  4240. {
  4241. $trans = $this->getIlluminateArrayTranslator();
  4242. // required passes
  4243. $v = new Validator($trans, [
  4244. 'foo' => [
  4245. ['name' => 'first'],
  4246. ['name' => 'second'],
  4247. ],
  4248. ], ['foo.*.name' => ['Required']]);
  4249. $this->assertTrue($v->passes());
  4250. // nested required passes
  4251. $v = new Validator($trans, [
  4252. 'foo' => [
  4253. ['name' => 'first'],
  4254. ['name' => 'second'],
  4255. ],
  4256. ], ['foo.*.name' => ['Required']]);
  4257. $this->assertTrue($v->passes());
  4258. // required fails
  4259. $v = new Validator($trans, [
  4260. 'foo' => [
  4261. ['name' => null],
  4262. ['name' => null, 'last' => 'last'],
  4263. ],
  4264. ], ['foo.*.name' => ['Required']]);
  4265. $this->assertFalse($v->passes());
  4266. $this->assertTrue($v->messages()->has('foo.0.name'));
  4267. $this->assertTrue($v->messages()->has('foo.1.name'));
  4268. // nested required fails
  4269. $v = new Validator($trans, [
  4270. 'foo' => [
  4271. [
  4272. 'bar' => [
  4273. ['name' => null],
  4274. ['name' => null],
  4275. ],
  4276. ],
  4277. ],
  4278. ], ['foo.*.bar.*.name' => ['Required']]);
  4279. $this->assertFalse($v->passes());
  4280. $this->assertTrue($v->messages()->has('foo.0.bar.0.name'));
  4281. $this->assertTrue($v->messages()->has('foo.0.bar.1.name'));
  4282. }
  4283. public function testValidateImplicitEachWithAsterisksRequiredIf()
  4284. {
  4285. $trans = $this->getIlluminateArrayTranslator();
  4286. // required_if passes
  4287. $v = new Validator($trans, [
  4288. 'foo' => [
  4289. ['name' => 'first', 'last' => 'foo'],
  4290. ['last' => 'bar'],
  4291. ],
  4292. ], ['foo.*.name' => ['Required_if:foo.*.last,foo']]);
  4293. $this->assertTrue($v->passes());
  4294. // nested required_if passes
  4295. $v = new Validator($trans, [
  4296. 'foo' => [
  4297. ['name' => 'first', 'last' => 'foo'],
  4298. ['last' => 'bar'],
  4299. ],
  4300. ], ['foo.*.name' => ['Required_if:foo.*.last,foo']]);
  4301. $this->assertTrue($v->passes());
  4302. // required_if fails
  4303. $v = new Validator($trans, [
  4304. 'foo' => [
  4305. ['name' => null, 'last' => 'foo'],
  4306. ['name' => null, 'last' => 'foo'],
  4307. ],
  4308. ], ['foo.*.name' => ['Required_if:foo.*.last,foo']]);
  4309. $this->assertFalse($v->passes());
  4310. $this->assertTrue($v->messages()->has('foo.0.name'));
  4311. $this->assertTrue($v->messages()->has('foo.1.name'));
  4312. // nested required_if fails
  4313. $v = new Validator($trans, [
  4314. 'foo' => [
  4315. [
  4316. 'bar' => [
  4317. ['name' => null, 'last' => 'foo'],
  4318. ['name' => null, 'last' => 'foo'],
  4319. ],
  4320. ],
  4321. ],
  4322. ], ['foo.*.bar.*.name' => ['Required_if:foo.*.bar.*.last,foo']]);
  4323. $this->assertFalse($v->passes());
  4324. $this->assertTrue($v->messages()->has('foo.0.bar.0.name'));
  4325. $this->assertTrue($v->messages()->has('foo.0.bar.1.name'));
  4326. }
  4327. public function testValidateImplicitEachWithAsterisksRequiredUnless()
  4328. {
  4329. $trans = $this->getIlluminateArrayTranslator();
  4330. // required_unless passes
  4331. $v = new Validator($trans, [
  4332. 'foo' => [
  4333. ['name' => null, 'last' => 'foo'],
  4334. ['name' => 'second', 'last' => 'bar'],
  4335. ],
  4336. ], ['foo.*.name' => ['Required_unless:foo.*.last,foo']]);
  4337. $this->assertTrue($v->passes());
  4338. // nested required_unless passes
  4339. $v = new Validator($trans, [
  4340. 'foo' => [
  4341. ['name' => null, 'last' => 'foo'],
  4342. ['name' => 'second', 'last' => 'foo'],
  4343. ],
  4344. ], ['foo.*.bar.*.name' => ['Required_unless:foo.*.bar.*.last,foo']]);
  4345. $this->assertTrue($v->passes());
  4346. // required_unless fails
  4347. $v = new Validator($trans, [
  4348. 'foo' => [
  4349. ['name' => null, 'last' => 'baz'],
  4350. ['name' => null, 'last' => 'bar'],
  4351. ],
  4352. ], ['foo.*.name' => ['Required_unless:foo.*.last,foo']]);
  4353. $this->assertFalse($v->passes());
  4354. $this->assertTrue($v->messages()->has('foo.0.name'));
  4355. $this->assertTrue($v->messages()->has('foo.1.name'));
  4356. // nested required_unless fails
  4357. $v = new Validator($trans, [
  4358. 'foo' => [
  4359. [
  4360. 'bar' => [
  4361. ['name' => null, 'last' => 'bar'],
  4362. ['name' => null, 'last' => 'bar'],
  4363. ],
  4364. ],
  4365. ],
  4366. ], ['foo.*.bar.*.name' => ['Required_unless:foo.*.bar.*.last,foo']]);
  4367. $this->assertFalse($v->passes());
  4368. $this->assertTrue($v->messages()->has('foo.0.bar.0.name'));
  4369. $this->assertTrue($v->messages()->has('foo.0.bar.1.name'));
  4370. }
  4371. public function testValidateImplicitEachWithAsterisksRequiredWith()
  4372. {
  4373. $trans = $this->getIlluminateArrayTranslator();
  4374. // required_with passes
  4375. $v = new Validator($trans, [
  4376. 'foo' => [
  4377. ['name' => 'first', 'last' => 'last'],
  4378. ['name' => 'second', 'last' => 'last'],
  4379. ],
  4380. ], ['foo.*.name' => ['Required_with:foo.*.last']]);
  4381. $this->assertTrue($v->passes());
  4382. // nested required_with passes
  4383. $v = new Validator($trans, [
  4384. 'foo' => [
  4385. ['name' => 'first', 'last' => 'last'],
  4386. ['name' => 'second', 'last' => 'last'],
  4387. ],
  4388. ], ['foo.*.name' => ['Required_with:foo.*.last']]);
  4389. $this->assertTrue($v->passes());
  4390. // required_with fails
  4391. $v = new Validator($trans, [
  4392. 'foo' => [
  4393. ['name' => null, 'last' => 'last'],
  4394. ['name' => null, 'last' => 'last'],
  4395. ],
  4396. ], ['foo.*.name' => ['Required_with:foo.*.last']]);
  4397. $this->assertFalse($v->passes());
  4398. $this->assertTrue($v->messages()->has('foo.0.name'));
  4399. $this->assertTrue($v->messages()->has('foo.1.name'));
  4400. $v = new Validator($trans, [
  4401. 'fields' => [
  4402. 'fr' => ['name' => '', 'content' => 'ragnar'],
  4403. 'es' => ['name' => '', 'content' => 'lagertha'],
  4404. ],
  4405. ], ['fields.*.name' => 'required_with:fields.*.content']);
  4406. $this->assertFalse($v->passes());
  4407. // nested required_with fails
  4408. $v = new Validator($trans, [
  4409. 'foo' => [
  4410. [
  4411. 'bar' => [
  4412. ['name' => null, 'last' => 'last'],
  4413. ['name' => null, 'last' => 'last'],
  4414. ],
  4415. ],
  4416. ],
  4417. ], ['foo.*.bar.*.name' => ['Required_with:foo.*.bar.*.last']]);
  4418. $this->assertFalse($v->passes());
  4419. $this->assertTrue($v->messages()->has('foo.0.bar.0.name'));
  4420. $this->assertTrue($v->messages()->has('foo.0.bar.1.name'));
  4421. }
  4422. public function testValidateImplicitEachWithAsterisksRequiredWithAll()
  4423. {
  4424. $trans = $this->getIlluminateArrayTranslator();
  4425. // required_with_all passes
  4426. $v = new Validator($trans, [
  4427. 'foo' => [
  4428. ['name' => 'first', 'last' => 'last', 'middle' => 'middle'],
  4429. ['name' => 'second', 'last' => 'last', 'middle' => 'middle'],
  4430. ],
  4431. ], ['foo.*.name' => ['Required_with_all:foo.*.last,foo.*.middle']]);
  4432. $this->assertTrue($v->passes());
  4433. // nested required_with_all passes
  4434. $v = new Validator($trans, [
  4435. 'foo' => [
  4436. ['name' => 'first', 'last' => 'last', 'middle' => 'middle'],
  4437. ['name' => 'second', 'last' => 'last', 'middle' => 'middle'],
  4438. ],
  4439. ], ['foo.*.name' => ['Required_with_all:foo.*.last,foo.*.middle']]);
  4440. $this->assertTrue($v->passes());
  4441. // required_with_all fails
  4442. $v = new Validator($trans, [
  4443. 'foo' => [
  4444. ['name' => null, 'last' => 'last', 'middle' => 'middle'],
  4445. ['name' => null, 'last' => 'last', 'middle' => 'middle'],
  4446. ],
  4447. ], ['foo.*.name' => ['Required_with_all:foo.*.last,foo.*.middle']]);
  4448. $this->assertFalse($v->passes());
  4449. $this->assertTrue($v->messages()->has('foo.0.name'));
  4450. $this->assertTrue($v->messages()->has('foo.1.name'));
  4451. // nested required_with_all fails
  4452. $v = new Validator($trans, [
  4453. 'foo' => [
  4454. [
  4455. 'bar' => [
  4456. ['name' => null, 'last' => 'last', 'middle' => 'middle'],
  4457. ['name' => null, 'last' => 'last', 'middle' => 'middle'],
  4458. ],
  4459. ],
  4460. ],
  4461. ], ['foo.*.bar.*.name' => ['Required_with_all:foo.*.bar.*.last,foo.*.bar.*.middle']]);
  4462. $this->assertFalse($v->passes());
  4463. $this->assertTrue($v->messages()->has('foo.0.bar.0.name'));
  4464. $this->assertTrue($v->messages()->has('foo.0.bar.1.name'));
  4465. }
  4466. public function testValidateImplicitEachWithAsterisksRequiredWithout()
  4467. {
  4468. $trans = $this->getIlluminateArrayTranslator();
  4469. // required_without passes
  4470. $v = new Validator($trans, [
  4471. 'foo' => [
  4472. ['name' => 'first', 'middle' => 'middle'],
  4473. ['name' => 'second', 'last' => 'last'],
  4474. ],
  4475. ], ['foo.*.name' => ['Required_without:foo.*.last,foo.*.middle']]);
  4476. $this->assertTrue($v->passes());
  4477. // nested required_without passes
  4478. $v = new Validator($trans, [
  4479. 'foo' => [
  4480. ['name' => 'first', 'middle' => 'middle'],
  4481. ['name' => 'second', 'last' => 'last'],
  4482. ],
  4483. ], ['foo.*.name' => ['Required_without:foo.*.last,foo.*.middle']]);
  4484. $this->assertTrue($v->passes());
  4485. // required_without fails
  4486. $v = new Validator($trans, [
  4487. 'foo' => [
  4488. ['name' => null, 'last' => 'last'],
  4489. ['name' => null, 'middle' => 'middle'],
  4490. ],
  4491. ], ['foo.*.name' => ['Required_without:foo.*.last,foo.*.middle']]);
  4492. $this->assertFalse($v->passes());
  4493. $this->assertTrue($v->messages()->has('foo.0.name'));
  4494. $this->assertTrue($v->messages()->has('foo.1.name'));
  4495. // nested required_without fails
  4496. $v = new Validator($trans, [
  4497. 'foo' => [
  4498. [
  4499. 'bar' => [
  4500. ['name' => null, 'last' => 'last'],
  4501. ['name' => null, 'middle' => 'middle'],
  4502. ],
  4503. ],
  4504. ],
  4505. ], ['foo.*.bar.*.name' => ['Required_without:foo.*.bar.*.last,foo.*.bar.*.middle']]);
  4506. $this->assertFalse($v->passes());
  4507. $this->assertTrue($v->messages()->has('foo.0.bar.0.name'));
  4508. $this->assertTrue($v->messages()->has('foo.0.bar.1.name'));
  4509. }
  4510. public function testValidateImplicitEachWithAsterisksRequiredWithoutAll()
  4511. {
  4512. $trans = $this->getIlluminateArrayTranslator();
  4513. // required_without_all passes
  4514. $v = new Validator($trans, [
  4515. 'foo' => [
  4516. ['name' => 'first'],
  4517. ['name' => null, 'middle' => 'middle'],
  4518. ['name' => null, 'middle' => 'middle', 'last' => 'last'],
  4519. ],
  4520. ], ['foo.*.name' => ['Required_without_all:foo.*.last,foo.*.middle']]);
  4521. $this->assertTrue($v->passes());
  4522. // required_without_all fails
  4523. // nested required_without_all passes
  4524. $v = new Validator($trans, [
  4525. 'foo' => [
  4526. ['name' => 'first'],
  4527. ['name' => null, 'middle' => 'middle'],
  4528. ['name' => null, 'middle' => 'middle', 'last' => 'last'],
  4529. ],
  4530. ], ['foo.*.name' => ['Required_without_all:foo.*.last,foo.*.middle']]);
  4531. $this->assertTrue($v->passes());
  4532. $v = new Validator($trans, [
  4533. 'foo' => [
  4534. ['name' => null, 'foo' => 'foo', 'bar' => 'bar'],
  4535. ['name' => null, 'foo' => 'foo', 'bar' => 'bar'],
  4536. ],
  4537. ], ['foo.*.name' => ['Required_without_all:foo.*.last,foo.*.middle']]);
  4538. $this->assertFalse($v->passes());
  4539. $this->assertTrue($v->messages()->has('foo.0.name'));
  4540. $this->assertTrue($v->messages()->has('foo.1.name'));
  4541. // nested required_without_all fails
  4542. $v = new Validator($trans, [
  4543. 'foo' => [
  4544. [
  4545. 'bar' => [
  4546. ['name' => null, 'foo' => 'foo', 'bar' => 'bar'],
  4547. ['name' => null, 'foo' => 'foo', 'bar' => 'bar'],
  4548. ],
  4549. ],
  4550. ],
  4551. ], ['foo.*.bar.*.name' => ['Required_without_all:foo.*.bar.*.last,foo.*.bar.*.middle']]);
  4552. $this->assertFalse($v->passes());
  4553. $this->assertTrue($v->messages()->has('foo.0.bar.0.name'));
  4554. $this->assertTrue($v->messages()->has('foo.0.bar.1.name'));
  4555. }
  4556. public function testValidateImplicitEachWithAsterisksBeforeAndAfter()
  4557. {
  4558. $trans = $this->getIlluminateArrayTranslator();
  4559. $v = new Validator($trans, [
  4560. 'foo' => [
  4561. ['start' => '2016-04-19', 'end' => '2017-04-19'],
  4562. ],
  4563. ], ['foo.*.start' => ['before:foo.*.end']]);
  4564. $this->assertTrue($v->passes());
  4565. $v = new Validator($trans, [
  4566. 'foo' => [
  4567. ['start' => '2016-04-19', 'end' => '2017-04-19'],
  4568. ],
  4569. ], ['foo.*.end' => ['before:foo.*.start']]);
  4570. $this->assertTrue($v->fails());
  4571. $v = new Validator($trans, [
  4572. 'foo' => [
  4573. ['start' => '2016-04-19', 'end' => '2017-04-19'],
  4574. ],
  4575. ], ['foo.*.end' => ['after:foo.*.start']]);
  4576. $this->assertTrue($v->passes());
  4577. $v = new Validator($trans, [
  4578. 'foo' => [
  4579. ['start' => '2016-04-19', 'end' => '2017-04-19'],
  4580. ],
  4581. ], ['foo.*.start' => ['after:foo.*.end']]);
  4582. $this->assertTrue($v->fails());
  4583. }
  4584. public function testGetLeadingExplicitAttributePath()
  4585. {
  4586. $this->assertNull(ValidationData::getLeadingExplicitAttributePath('*.email'));
  4587. $this->assertSame('foo', ValidationData::getLeadingExplicitAttributePath('foo.*'));
  4588. $this->assertSame('foo.bar', ValidationData::getLeadingExplicitAttributePath('foo.bar.*.baz'));
  4589. $this->assertSame('foo.bar.1', ValidationData::getLeadingExplicitAttributePath('foo.bar.1'));
  4590. }
  4591. public function testExtractDataFromPath()
  4592. {
  4593. $data = [['email' => 'mail'], ['email' => 'mail2']];
  4594. $this->assertEquals([['email' => 'mail'], ['email' => 'mail2']], ValidationData::extractDataFromPath(null, $data));
  4595. $data = ['cat' => ['cat1' => ['name']], ['cat2' => ['name2']]];
  4596. $this->assertEquals(['cat' => ['cat1' => ['name']]], ValidationData::extractDataFromPath('cat.cat1', $data));
  4597. $data = ['cat' => ['cat1' => ['name' => '1', 'price' => 1]], ['cat2' => ['name' => 2]]];
  4598. $this->assertEquals(['cat' => ['cat1' => ['name' => '1']]], ValidationData::extractDataFromPath('cat.cat1.name', $data));
  4599. }
  4600. public function testParsingTablesFromModels()
  4601. {
  4602. $trans = $this->getIlluminateArrayTranslator();
  4603. $v = new Validator($trans, [], []);
  4604. $implicit_no_connection = $v->parseTable(ImplicitTableModel::class);
  4605. $this->assertNull($implicit_no_connection[0]);
  4606. $this->assertSame('implicit_table_models', $implicit_no_connection[1]);
  4607. $explicit_no_connection = $v->parseTable(ExplicitTableModel::class);
  4608. $this->assertNull($explicit_no_connection[0]);
  4609. $this->assertSame('explicits', $explicit_no_connection[1]);
  4610. $explicit_model_with_prefix = $v->parseTable(ExplicitPrefixedTableModel::class);
  4611. $this->assertNull($explicit_model_with_prefix[0]);
  4612. $this->assertSame('prefix.explicits', $explicit_model_with_prefix[1]);
  4613. $explicit_table_with_connection_prefix = $v->parseTable('connection.table');
  4614. $this->assertSame('connection', $explicit_table_with_connection_prefix[0]);
  4615. $this->assertSame('table', $explicit_table_with_connection_prefix[1]);
  4616. $noneloquent_no_connection = $v->parseTable(NonEloquentModel::class);
  4617. $this->assertNull($noneloquent_no_connection[0]);
  4618. $this->assertEquals(NonEloquentModel::class, $noneloquent_no_connection[1]);
  4619. $raw_no_connection = $v->parseTable('table');
  4620. $this->assertNull($raw_no_connection[0]);
  4621. $this->assertSame('table', $raw_no_connection[1]);
  4622. $implicit_connection = $v->parseTable('connection.'.ImplicitTableModel::class);
  4623. $this->assertSame('connection', $implicit_connection[0]);
  4624. $this->assertSame('implicit_table_models', $implicit_connection[1]);
  4625. $explicit_connection = $v->parseTable('connection.'.ExplicitTableModel::class);
  4626. $this->assertSame('connection', $explicit_connection[0]);
  4627. $this->assertSame('explicits', $explicit_connection[1]);
  4628. $explicit_model_implicit_connection = $v->parseTable(ExplicitTableAndConnectionModel::class);
  4629. $this->assertSame('connection', $explicit_model_implicit_connection[0]);
  4630. $this->assertSame('explicits', $explicit_model_implicit_connection[1]);
  4631. $noneloquent_connection = $v->parseTable('connection.'.NonEloquentModel::class);
  4632. $this->assertSame('connection', $noneloquent_connection[0]);
  4633. $this->assertEquals(NonEloquentModel::class, $noneloquent_connection[1]);
  4634. $raw_connection = $v->parseTable('connection.table');
  4635. $this->assertSame('connection', $raw_connection[0]);
  4636. $this->assertSame('table', $raw_connection[1]);
  4637. }
  4638. public function testUsingSettersWithImplicitRules()
  4639. {
  4640. $trans = $this->getIlluminateArrayTranslator();
  4641. $v = new Validator($trans, ['foo' => ['a', 'b', 'c']], ['foo.*' => 'string']);
  4642. $v->setData(['foo' => ['a', 'b', 'c', 4]]);
  4643. $this->assertFalse($v->passes());
  4644. $trans = $this->getIlluminateArrayTranslator();
  4645. $v = new Validator($trans, ['foo' => ['a', 'b', 'c']], ['foo.*' => 'string']);
  4646. $v->setRules(['foo.*' => 'integer']);
  4647. $this->assertFalse($v->passes());
  4648. }
  4649. public function testInvalidMethod()
  4650. {
  4651. $trans = $this->getIlluminateArrayTranslator();
  4652. $v = new Validator($trans,
  4653. [
  4654. ['name' => 'John'],
  4655. ['name' => null],
  4656. ['name' => ''],
  4657. ],
  4658. [
  4659. '*.name' => 'required',
  4660. ]);
  4661. $this->assertEquals(
  4662. [
  4663. 1 => ['name' => null],
  4664. 2 => ['name' => ''],
  4665. ],
  4666. $v->invalid()
  4667. );
  4668. $v = new Validator($trans,
  4669. [
  4670. 'name' => '',
  4671. ],
  4672. [
  4673. 'name' => 'required',
  4674. ]);
  4675. $this->assertEquals(
  4676. [
  4677. 'name' => '',
  4678. ],
  4679. $v->invalid()
  4680. );
  4681. }
  4682. public function testValidMethod()
  4683. {
  4684. $trans = $this->getIlluminateArrayTranslator();
  4685. $v = new Validator($trans,
  4686. [
  4687. ['name' => 'John'],
  4688. ['name' => null],
  4689. ['name' => ''],
  4690. ['name' => 'Doe'],
  4691. ],
  4692. [
  4693. '*.name' => 'required',
  4694. ]);
  4695. $this->assertEquals(
  4696. [
  4697. 0 => ['name' => 'John'],
  4698. 3 => ['name' => 'Doe'],
  4699. ],
  4700. $v->valid()
  4701. );
  4702. $v = new Validator($trans,
  4703. [
  4704. 'name' => 'Carlos',
  4705. 'age' => 'unknown',
  4706. 'gender' => 'male',
  4707. ],
  4708. [
  4709. 'name' => 'required',
  4710. 'gender' => 'in:male,female',
  4711. 'age' => 'required|int',
  4712. ]);
  4713. $this->assertEquals(
  4714. [
  4715. 'name' => 'Carlos',
  4716. 'gender' => 'male',
  4717. ],
  4718. $v->valid()
  4719. );
  4720. }
  4721. public function testNestedInvalidMethod()
  4722. {
  4723. $trans = $this->getIlluminateArrayTranslator();
  4724. $v = new Validator($trans, [
  4725. 'testvalid' => 'filled',
  4726. 'testinvalid' => '',
  4727. 'records' => [
  4728. 'ABC123',
  4729. 'ABC122',
  4730. 'ABB132',
  4731. 'ADCD23',
  4732. ],
  4733. ], [
  4734. 'testvalid' => 'filled',
  4735. 'testinvalid' => 'filled',
  4736. 'records.*' => [
  4737. 'required',
  4738. 'regex:/[A-F]{3}[0-9]{3}/',
  4739. ],
  4740. ]);
  4741. $this->assertEquals(
  4742. [
  4743. 'testinvalid' => '',
  4744. 'records' => [
  4745. 3 => 'ADCD23',
  4746. ],
  4747. ],
  4748. $v->invalid()
  4749. );
  4750. }
  4751. public function testMultipleFileUploads()
  4752. {
  4753. $trans = $this->getIlluminateArrayTranslator();
  4754. $file = new File(__FILE__, false);
  4755. $file2 = new File(__FILE__, false);
  4756. $v = new Validator($trans, ['file' => [$file, $file2]], ['file.*' => 'Required|mimes:xls']);
  4757. $this->assertFalse($v->passes());
  4758. }
  4759. public function testFileUploads()
  4760. {
  4761. $trans = $this->getIlluminateArrayTranslator();
  4762. $file = new File(__FILE__, false);
  4763. $v = new Validator($trans, ['file' => $file], ['file' => 'Required|mimes:xls']);
  4764. $this->assertFalse($v->passes());
  4765. }
  4766. public function testCustomValidationObject()
  4767. {
  4768. // Test passing case...
  4769. $v = new Validator(
  4770. $this->getIlluminateArrayTranslator(),
  4771. ['name' => 'taylor'],
  4772. [
  4773. 'name' => new class implements Rule
  4774. {
  4775. public function passes($attribute, $value)
  4776. {
  4777. return $value === 'taylor';
  4778. }
  4779. public function message()
  4780. {
  4781. return ':attribute must be taylor';
  4782. }
  4783. },
  4784. ]
  4785. );
  4786. $this->assertTrue($v->passes());
  4787. // Test failing case...
  4788. $v = new Validator(
  4789. $this->getIlluminateArrayTranslator(),
  4790. ['name' => 'adam'],
  4791. [
  4792. 'name' => [
  4793. new class implements Rule
  4794. {
  4795. public function passes($attribute, $value)
  4796. {
  4797. return $value === 'taylor';
  4798. }
  4799. public function message()
  4800. {
  4801. return ':attribute must be taylor';
  4802. }
  4803. },
  4804. ],
  4805. ]
  4806. );
  4807. $this->assertTrue($v->fails());
  4808. $this->assertSame('name must be taylor', $v->errors()->all()[0]);
  4809. // Test passing case with Closure...
  4810. $v = new Validator(
  4811. $this->getIlluminateArrayTranslator(),
  4812. ['name' => 'taylor'],
  4813. [
  4814. 'name.*' => function ($attribute, $value, $fail) {
  4815. if ($value !== 'taylor') {
  4816. $fail(':attribute was '.$value.' instead of taylor');
  4817. }
  4818. },
  4819. ]
  4820. );
  4821. $this->assertTrue($v->passes());
  4822. // Test failing case with Closure...
  4823. $v = new Validator(
  4824. $this->getIlluminateArrayTranslator(),
  4825. ['name' => 'adam'],
  4826. [
  4827. 'name' => function ($attribute, $value, $fail) {
  4828. if ($value !== 'taylor') {
  4829. $fail(':attribute was '.$value.' instead of taylor');
  4830. }
  4831. },
  4832. ]
  4833. );
  4834. $this->assertTrue($v->fails());
  4835. $this->assertSame('name was adam instead of taylor', $v->errors()->all()[0]);
  4836. // Test complex failing case...
  4837. $v = new Validator(
  4838. $this->getIlluminateArrayTranslator(),
  4839. ['name' => 'taylor', 'states' => ['AR', 'TX'], 'number' => 9],
  4840. [
  4841. 'states.*' => new class implements Rule
  4842. {
  4843. public function passes($attribute, $value)
  4844. {
  4845. return in_array($value, ['AK', 'HI']);
  4846. }
  4847. public function message()
  4848. {
  4849. return ':attribute must be AR or TX';
  4850. }
  4851. },
  4852. 'name' => function ($attribute, $value, $fail) {
  4853. if ($value !== 'taylor') {
  4854. $fail(':attribute must be taylor');
  4855. }
  4856. },
  4857. 'number' => [
  4858. 'required',
  4859. 'integer',
  4860. function ($attribute, $value, $fail) {
  4861. if ($value % 4 !== 0) {
  4862. $fail(':attribute must be divisible by 4');
  4863. }
  4864. },
  4865. ],
  4866. ]
  4867. );
  4868. $this->assertFalse($v->passes());
  4869. $this->assertSame('states.0 must be AR or TX', $v->errors()->get('states.0')[0]);
  4870. $this->assertSame('states.1 must be AR or TX', $v->errors()->get('states.1')[0]);
  4871. $this->assertSame('number must be divisible by 4', $v->errors()->get('number')[0]);
  4872. // Test array of messages with failing case...
  4873. $v = new Validator(
  4874. $this->getIlluminateArrayTranslator(),
  4875. ['name' => 42],
  4876. [
  4877. 'name' => new class implements Rule
  4878. {
  4879. public function passes($attribute, $value)
  4880. {
  4881. return $value === 'taylor';
  4882. }
  4883. public function message()
  4884. {
  4885. return [':attribute must be taylor', ':attribute must be a first name'];
  4886. }
  4887. },
  4888. ]
  4889. );
  4890. $this->assertTrue($v->fails());
  4891. $this->assertSame('name must be taylor', $v->errors()->get('name')[0]);
  4892. $this->assertSame('name must be a first name', $v->errors()->get('name')[1]);
  4893. // Test array of messages with multiple rules for one attribute case...
  4894. $v = new Validator(
  4895. $this->getIlluminateArrayTranslator(),
  4896. ['name' => 42],
  4897. [
  4898. 'name' => [
  4899. new class implements Rule
  4900. {
  4901. public function passes($attribute, $value)
  4902. {
  4903. return $value === 'taylor';
  4904. }
  4905. public function message()
  4906. {
  4907. return [':attribute must be taylor', ':attribute must be a first name'];
  4908. }
  4909. }, 'string',
  4910. ],
  4911. ]
  4912. );
  4913. $this->assertTrue($v->fails());
  4914. $this->assertSame('name must be taylor', $v->errors()->get('name')[0]);
  4915. $this->assertSame('name must be a first name', $v->errors()->get('name')[1]);
  4916. $this->assertSame('validation.string', $v->errors()->get('name')[2]);
  4917. // Test access to the validator data
  4918. $v = new Validator(
  4919. $this->getIlluminateArrayTranslator(),
  4920. ['password' => 'foo', 'password_confirmation' => 'foo'],
  4921. [
  4922. 'password' => [
  4923. new class implements Rule, DataAwareRule
  4924. {
  4925. protected $data;
  4926. public function setData($data)
  4927. {
  4928. $this->data = $data;
  4929. }
  4930. public function passes($attribute, $value)
  4931. {
  4932. return $value === $this->data['password_confirmation'];
  4933. }
  4934. public function message()
  4935. {
  4936. return ['The :attribute confirmation does not match.'];
  4937. }
  4938. }, 'string',
  4939. ],
  4940. ]
  4941. );
  4942. $this->assertTrue($v->passes());
  4943. $v = new Validator(
  4944. $this->getIlluminateArrayTranslator(),
  4945. ['password' => 'foo', 'password_confirmation' => 'bar'],
  4946. [
  4947. 'password' => [
  4948. new class implements Rule, DataAwareRule
  4949. {
  4950. protected $data;
  4951. public function setData($data)
  4952. {
  4953. $this->data = $data;
  4954. }
  4955. public function passes($attribute, $value)
  4956. {
  4957. return $value === $this->data['password_confirmation'];
  4958. }
  4959. public function message()
  4960. {
  4961. return ['The :attribute confirmation does not match.'];
  4962. }
  4963. }, 'string',
  4964. ],
  4965. ]
  4966. );
  4967. $this->assertTrue($v->fails());
  4968. $this->assertSame('The password confirmation does not match.', $v->errors()->get('password')[0]);
  4969. // Test access to the validator
  4970. $v = new Validator(
  4971. $this->getIlluminateArrayTranslator(),
  4972. ['base' => 21, 'double' => 42],
  4973. [
  4974. 'base' => ['integer'],
  4975. 'double' => [
  4976. 'integer',
  4977. new class implements Rule, ValidatorAwareRule
  4978. {
  4979. protected $validator;
  4980. public function setValidator($validator)
  4981. {
  4982. $this->validator = $validator;
  4983. }
  4984. public function passes($attribute, $value)
  4985. {
  4986. if ($this->validator->errors()->hasAny(['base', $attribute])) {
  4987. return true;
  4988. }
  4989. return $value === 2 * $this->validator->getData()['base'];
  4990. }
  4991. public function message()
  4992. {
  4993. return ['The :attribute must be the double of base.'];
  4994. }
  4995. },
  4996. ],
  4997. ]
  4998. );
  4999. $this->assertTrue($v->passes());
  5000. $v = new Validator(
  5001. $this->getIlluminateArrayTranslator(),
  5002. ['base' => 21, 'double' => 10],
  5003. [
  5004. 'base' => ['integer'],
  5005. 'double' => [
  5006. 'integer',
  5007. new class implements Rule, ValidatorAwareRule
  5008. {
  5009. protected $validator;
  5010. public function setValidator($validator)
  5011. {
  5012. $this->validator = $validator;
  5013. }
  5014. public function passes($attribute, $value)
  5015. {
  5016. if ($this->validator->errors()->hasAny(['base', $attribute])) {
  5017. return true;
  5018. }
  5019. return $value === 2 * $this->validator->getData()['base'];
  5020. }
  5021. public function message()
  5022. {
  5023. return ['The :attribute must be the double of base.'];
  5024. }
  5025. },
  5026. ],
  5027. ]
  5028. );
  5029. $this->assertTrue($v->fails());
  5030. $this->assertSame('The double must be the double of base.', $v->errors()->get('double')[0]);
  5031. $v = new Validator(
  5032. $this->getIlluminateArrayTranslator(),
  5033. ['base' => 21, 'double' => 'foo'],
  5034. [
  5035. 'base' => ['integer'],
  5036. 'double' => [
  5037. 'integer',
  5038. new class implements Rule, ValidatorAwareRule
  5039. {
  5040. protected $validator;
  5041. public function setValidator($validator)
  5042. {
  5043. $this->validator = $validator;
  5044. }
  5045. public function passes($attribute, $value)
  5046. {
  5047. if ($this->validator->errors()->hasAny(['base', $attribute])) {
  5048. return true;
  5049. }
  5050. return $value === 2 * $this->validator->getData()['base'];
  5051. }
  5052. public function message()
  5053. {
  5054. return ['The :attribute must be the double of base.'];
  5055. }
  5056. },
  5057. ],
  5058. ]
  5059. );
  5060. $this->assertTrue($v->fails());
  5061. $this->assertCount(1, $v->errors()->get('double'));
  5062. $this->assertSame('validation.integer', $v->errors()->get('double')[0]);
  5063. }
  5064. public function testCustomValidationObjectWithDotKeysIsCorrectlyPassedValue()
  5065. {
  5066. $v = new Validator(
  5067. $this->getIlluminateArrayTranslator(),
  5068. ['foo' => ['foo.bar' => 'baz']],
  5069. [
  5070. 'foo' => new class implements Rule
  5071. {
  5072. public function passes($attribute, $value)
  5073. {
  5074. return $value === ['foo.bar' => 'baz'];
  5075. }
  5076. public function message()
  5077. {
  5078. return ':attribute must be baz';
  5079. }
  5080. },
  5081. ]
  5082. );
  5083. $this->assertTrue($v->passes());
  5084. // Test failed attributes contains proper entries
  5085. $v = new Validator(
  5086. $this->getIlluminateArrayTranslator(),
  5087. ['foo' => ['foo.bar' => 'baz']],
  5088. [
  5089. 'foo.foo\.bar' => new class implements Rule
  5090. {
  5091. public function passes($attribute, $value)
  5092. {
  5093. return false;
  5094. }
  5095. public function message()
  5096. {
  5097. return ':attribute must be baz';
  5098. }
  5099. },
  5100. ]
  5101. );
  5102. $this->assertFalse($v->passes());
  5103. $this->assertIsArray($v->failed()['foo.foo.bar']);
  5104. }
  5105. public function testImplicitCustomValidationObjects()
  5106. {
  5107. // Test passing case...
  5108. $v = new Validator(
  5109. $this->getIlluminateArrayTranslator(),
  5110. ['name' => ''],
  5111. [
  5112. 'name' => $rule = new class implements ImplicitRule
  5113. {
  5114. public $called = false;
  5115. public function passes($attribute, $value)
  5116. {
  5117. $this->called = true;
  5118. return true;
  5119. }
  5120. public function message()
  5121. {
  5122. return 'message';
  5123. }
  5124. },
  5125. ]
  5126. );
  5127. $this->assertTrue($v->passes());
  5128. $this->assertTrue($rule->called);
  5129. }
  5130. public function testValidateReturnsValidatedData()
  5131. {
  5132. $post = ['first' => 'john', 'preferred' => 'john', 'last' => 'doe', 'type' => 'admin'];
  5133. $v = new Validator($this->getIlluminateArrayTranslator(), $post, ['first' => 'required', 'preferred' => 'required']);
  5134. $v->sometimes('type', 'required', function () {
  5135. return false;
  5136. });
  5137. $data = $v->validate();
  5138. $this->assertEquals(['first' => 'john', 'preferred' => 'john'], $data);
  5139. }
  5140. public function testValidateReturnsValidatedDataNestedRules()
  5141. {
  5142. $post = ['nested' => ['foo' => 'bar', 'baz' => ''], 'array' => [1, 2]];
  5143. $rules = ['nested.foo' => 'required', 'array.*' => 'integer'];
  5144. $v = new Validator($this->getIlluminateArrayTranslator(), $post, $rules);
  5145. $v->sometimes('type', 'required', function () {
  5146. return false;
  5147. });
  5148. $data = $v->validate();
  5149. $this->assertEquals(['nested' => ['foo' => 'bar'], 'array' => [1, 2]], $data);
  5150. }
  5151. public function testValidateReturnsValidatedDataNestedChildRules()
  5152. {
  5153. $post = ['nested' => ['foo' => 'bar', 'with' => 'extras', 'type' => 'admin']];
  5154. $v = new Validator($this->getIlluminateArrayTranslator(), $post, ['nested.foo' => 'required']);
  5155. $v->sometimes('nested.type', 'required', function () {
  5156. return false;
  5157. });
  5158. $data = $v->validate();
  5159. $this->assertEquals(['nested' => ['foo' => 'bar']], $data);
  5160. }
  5161. public function testValidateReturnsValidatedDataNestedArrayRules()
  5162. {
  5163. $post = ['nested' => [['bar' => 'baz', 'with' => 'extras', 'type' => 'admin'], ['bar' => 'baz2', 'with' => 'extras', 'type' => 'admin']]];
  5164. $v = new Validator($this->getIlluminateArrayTranslator(), $post, ['nested.*.bar' => 'required']);
  5165. $v->sometimes('nested.*.type', 'required', function () {
  5166. return false;
  5167. });
  5168. $data = $v->validate();
  5169. $this->assertEquals(['nested' => [['bar' => 'baz'], ['bar' => 'baz2']]], $data);
  5170. }
  5171. public function testValidateAndValidatedData()
  5172. {
  5173. $post = ['first' => 'john', 'preferred' => 'john', 'last' => 'doe', 'type' => 'admin'];
  5174. $v = new Validator($this->getIlluminateArrayTranslator(), $post, ['first' => 'required', 'preferred' => 'required']);
  5175. $v->sometimes('type', 'required', function () {
  5176. return false;
  5177. });
  5178. $data = $v->validate();
  5179. $validatedData = $v->validated();
  5180. $this->assertEquals(['first' => 'john', 'preferred' => 'john'], $data);
  5181. $this->assertEquals($data, $validatedData);
  5182. }
  5183. public function testValidatedNotValidateTwiceData()
  5184. {
  5185. $post = ['first' => 'john', 'preferred' => 'john', 'last' => 'doe', 'type' => 'admin'];
  5186. $validateCount = 0;
  5187. $v = new Validator($this->getIlluminateArrayTranslator(), $post, ['first' => 'required', 'preferred' => 'required']);
  5188. $v->after(function () use (&$validateCount) {
  5189. $validateCount++;
  5190. });
  5191. $data = $v->validate();
  5192. $v->validated();
  5193. $this->assertEquals(['first' => 'john', 'preferred' => 'john'], $data);
  5194. $this->assertEquals(1, $validateCount);
  5195. }
  5196. public function testMultiplePassesCalls()
  5197. {
  5198. $trans = $this->getIlluminateArrayTranslator();
  5199. $v = new Validator($trans, [], ['foo' => 'string|required']);
  5200. $this->assertFalse($v->passes());
  5201. $this->assertFalse($v->passes());
  5202. }
  5203. /**
  5204. * @dataProvider validUuidList
  5205. */
  5206. public function testValidateWithValidUuid($uuid)
  5207. {
  5208. $trans = $this->getIlluminateArrayTranslator();
  5209. $v = new Validator($trans, ['foo' => $uuid], ['foo' => 'uuid']);
  5210. $this->assertTrue($v->passes());
  5211. }
  5212. /**
  5213. * @dataProvider invalidUuidList
  5214. */
  5215. public function testValidateWithInvalidUuid($uuid)
  5216. {
  5217. $trans = $this->getIlluminateArrayTranslator();
  5218. $v = new Validator($trans, ['foo' => $uuid], ['foo' => 'uuid']);
  5219. $this->assertFalse($v->passes());
  5220. }
  5221. public function validUuidList()
  5222. {
  5223. return [
  5224. ['a0a2a2d2-0b87-4a18-83f2-2529882be2de'],
  5225. ['145a1e72-d11d-11e8-a8d5-f2801f1b9fd1'],
  5226. ['00000000-0000-0000-0000-000000000000'],
  5227. ['e60d3f48-95d7-4d8d-aad0-856f29a27da2'],
  5228. ['ff6f8cb0-c57d-11e1-9b21-0800200c9a66'],
  5229. ['ff6f8cb0-c57d-21e1-9b21-0800200c9a66'],
  5230. ['ff6f8cb0-c57d-31e1-9b21-0800200c9a66'],
  5231. ['ff6f8cb0-c57d-41e1-9b21-0800200c9a66'],
  5232. ['ff6f8cb0-c57d-51e1-9b21-0800200c9a66'],
  5233. ['FF6F8CB0-C57D-11E1-9B21-0800200C9A66'],
  5234. ];
  5235. }
  5236. public function invalidUuidList()
  5237. {
  5238. return [
  5239. ['not a valid uuid so we can test this'],
  5240. ['zf6f8cb0-c57d-11e1-9b21-0800200c9a66'],
  5241. ['145a1e72-d11d-11e8-a8d5-f2801f1b9fd1'.PHP_EOL],
  5242. ['145a1e72-d11d-11e8-a8d5-f2801f1b9fd1 '],
  5243. [' 145a1e72-d11d-11e8-a8d5-f2801f1b9fd1'],
  5244. ['145a1e72-d11d-11e8-a8d5-f2z01f1b9fd1'],
  5245. ['3f6f8cb0-c57d-11e1-9b21-0800200c9a6'],
  5246. ['af6f8cb-c57d-11e1-9b21-0800200c9a66'],
  5247. ['af6f8cb0c57d11e19b210800200c9a66'],
  5248. ['ff6f8cb0-c57da-51e1-9b21-0800200c9a66'],
  5249. ];
  5250. }
  5251. public function providesPassingExcludeIfData()
  5252. {
  5253. return [
  5254. [
  5255. [
  5256. 'has_appointment' => ['required', 'bool'],
  5257. 'appointment_date' => ['exclude_if:has_appointment,false', 'required', 'date'],
  5258. ], [
  5259. 'has_appointment' => false,
  5260. 'appointment_date' => 'should be excluded',
  5261. ], [
  5262. 'has_appointment' => false,
  5263. ],
  5264. ],
  5265. [
  5266. [
  5267. 'cat' => ['required', 'string'],
  5268. 'mouse' => ['exclude_if:cat,Tom', 'required', 'file'],
  5269. ], [
  5270. 'cat' => 'Tom',
  5271. 'mouse' => 'should be excluded',
  5272. ], [
  5273. 'cat' => 'Tom',
  5274. ],
  5275. ],
  5276. [
  5277. [
  5278. 'has_appointment' => ['required', 'bool'],
  5279. 'appointment_date' => ['exclude_if:has_appointment,false', 'required', 'date'],
  5280. ], [
  5281. 'has_appointment' => false,
  5282. ], [
  5283. 'has_appointment' => false,
  5284. ],
  5285. ],
  5286. [
  5287. [
  5288. 'has_appointment' => ['nullable', 'bool'],
  5289. 'appointment_date' => ['exclude_if:has_appointment,null', 'required', 'date'],
  5290. ],
  5291. [
  5292. 'has_appointment' => true,
  5293. 'appointment_date' => '2021-03-08',
  5294. ],
  5295. [
  5296. 'has_appointment' => true,
  5297. 'appointment_date' => '2021-03-08',
  5298. ],
  5299. ],
  5300. [
  5301. [
  5302. 'has_appointment' => ['required', 'bool'],
  5303. 'appointment_date' => ['exclude_if:has_appointment,false', 'required', 'date'],
  5304. ], [
  5305. 'has_appointment' => true,
  5306. 'appointment_date' => '2019-12-13',
  5307. ], [
  5308. 'has_appointment' => true,
  5309. 'appointment_date' => '2019-12-13',
  5310. ],
  5311. ],
  5312. [
  5313. [
  5314. 'has_no_appointments' => ['required', 'bool'],
  5315. 'has_doctor_appointment' => ['exclude_if:has_no_appointments,true', 'required', 'bool'],
  5316. 'doctor_appointment_date' => ['exclude_if:has_no_appointments,true', 'exclude_if:has_doctor_appointment,false', 'required', 'date'],
  5317. ], [
  5318. 'has_no_appointments' => true,
  5319. 'has_doctor_appointment' => true,
  5320. 'doctor_appointment_date' => '2019-12-13',
  5321. ], [
  5322. 'has_no_appointments' => true,
  5323. ],
  5324. ],
  5325. [
  5326. [
  5327. 'has_no_appointments' => ['required', 'bool'],
  5328. 'has_doctor_appointment' => ['exclude_if:has_no_appointments,true', 'required', 'bool'],
  5329. 'doctor_appointment_date' => ['exclude_if:has_no_appointments,true', 'exclude_if:has_doctor_appointment,false', 'required', 'date'],
  5330. ], [
  5331. 'has_no_appointments' => false,
  5332. 'has_doctor_appointment' => false,
  5333. 'doctor_appointment_date' => 'should be excluded',
  5334. ], [
  5335. 'has_no_appointments' => false,
  5336. 'has_doctor_appointment' => false,
  5337. ],
  5338. ],
  5339. 'nested-01' => [
  5340. [
  5341. 'has_appointments' => ['required', 'bool'],
  5342. 'appointments.*' => ['exclude_if:has_appointments,false', 'required', 'date'],
  5343. ], [
  5344. 'has_appointments' => false,
  5345. 'appointments' => ['2019-05-15', '2020-05-15'],
  5346. ], [
  5347. 'has_appointments' => false,
  5348. ],
  5349. ],
  5350. 'nested-02' => [
  5351. [
  5352. 'has_appointments' => ['required', 'bool'],
  5353. 'appointments.*.date' => ['exclude_if:has_appointments,false', 'required', 'date'],
  5354. 'appointments.*.name' => ['exclude_if:has_appointments,false', 'required', 'string'],
  5355. ], [
  5356. 'has_appointments' => false,
  5357. 'appointments' => [
  5358. ['date' => 'should be excluded', 'name' => 'should be excluded'],
  5359. ],
  5360. ], [
  5361. 'has_appointments' => false,
  5362. ],
  5363. ],
  5364. 'nested-03' => [
  5365. [
  5366. 'has_appointments' => ['required', 'bool'],
  5367. 'appointments' => ['exclude_if:has_appointments,false', 'required', 'array'],
  5368. 'appointments.*.date' => ['required', 'date'],
  5369. 'appointments.*.name' => ['required', 'string'],
  5370. ], [
  5371. 'has_appointments' => false,
  5372. 'appointments' => [
  5373. ['date' => 'should be excluded', 'name' => 'should be excluded'],
  5374. ],
  5375. ], [
  5376. 'has_appointments' => false,
  5377. ],
  5378. ],
  5379. 'nested-04' => [
  5380. [
  5381. 'has_appointments' => ['required', 'bool'],
  5382. 'appointments.*.date' => ['required', 'date'],
  5383. 'appointments' => ['exclude_if:has_appointments,false', 'required', 'array'],
  5384. ], [
  5385. 'has_appointments' => false,
  5386. 'appointments' => [
  5387. ['date' => 'should be excluded', 'name' => 'should be excluded'],
  5388. ],
  5389. ], [
  5390. 'has_appointments' => false,
  5391. ],
  5392. ],
  5393. 'nested-05' => [
  5394. [
  5395. 'vehicles.*.type' => 'required|in:car,boat',
  5396. 'vehicles.*.wheels' => 'exclude_if:vehicles.*.type,boat|required|numeric',
  5397. ], [
  5398. 'vehicles' => [
  5399. ['type' => 'car', 'wheels' => 4],
  5400. ['type' => 'boat', 'wheels' => 'should be excluded'],
  5401. ],
  5402. ], [
  5403. 'vehicles' => [
  5404. ['type' => 'car', 'wheels' => 4],
  5405. ['type' => 'boat'],
  5406. ],
  5407. ],
  5408. ],
  5409. 'nested-06' => [
  5410. [
  5411. 'vehicles.*.type' => 'required|in:car,boat',
  5412. 'vehicles.*.wheels' => 'exclude_if:vehicles.*.type,boat|required|numeric',
  5413. ], [
  5414. 'vehicles' => [
  5415. ['type' => 'car', 'wheels' => 4],
  5416. ['type' => 'boat'],
  5417. ],
  5418. ], [
  5419. 'vehicles' => [
  5420. ['type' => 'car', 'wheels' => 4],
  5421. ['type' => 'boat'],
  5422. ],
  5423. ],
  5424. ],
  5425. 'nested-07' => [
  5426. [
  5427. 'vehicles.*.type' => 'required|in:car,boat',
  5428. 'vehicles.*.wheels' => 'exclude_if:vehicles.*.type,boat|required|array',
  5429. 'vehicles.*.wheels.*.color' => 'required|in:red,blue',
  5430. // In this bizzaro world example you can choose a custom shape for your wheels if they are red
  5431. 'vehicles.*.wheels.*.shape' => 'exclude_unless:vehicles.*.wheels.*.color,red|required|in:square,round',
  5432. ], [
  5433. 'vehicles' => [
  5434. [
  5435. 'type' => 'car', 'wheels' => [
  5436. ['color' => 'red', 'shape' => 'square'],
  5437. ['color' => 'blue', 'shape' => 'hexagon'],
  5438. ['color' => 'red', 'shape' => 'round', 'junk' => 'no rule, still present'],
  5439. ['color' => 'blue', 'shape' => 'triangle'],
  5440. ],
  5441. ],
  5442. ['type' => 'boat'],
  5443. ],
  5444. ], [
  5445. 'vehicles' => [
  5446. [
  5447. 'type' => 'car', 'wheels' => [
  5448. ['color' => 'red', 'shape' => 'square'],
  5449. ['color' => 'blue'],
  5450. ['color' => 'red', 'shape' => 'round', 'junk' => 'no rule, still present'],
  5451. ['color' => 'blue'],
  5452. ],
  5453. ],
  5454. ['type' => 'boat'],
  5455. ],
  5456. ],
  5457. ],
  5458. ];
  5459. }
  5460. /**
  5461. * @dataProvider providesPassingExcludeIfData
  5462. */
  5463. public function testExcludeIf($rules, $data, $expectedValidatedData)
  5464. {
  5465. $validator = new Validator(
  5466. $this->getIlluminateArrayTranslator(),
  5467. $data,
  5468. $rules
  5469. );
  5470. $passes = $validator->passes();
  5471. if (! $passes) {
  5472. $message = sprintf("Validation unexpectedly failed:\nRules: %s\nData: %s\nValidation error: %s",
  5473. json_encode($rules, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES),
  5474. json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES),
  5475. json_encode($validator->messages()->toArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
  5476. );
  5477. }
  5478. $this->assertTrue($passes, $message ?? '');
  5479. $this->assertSame($expectedValidatedData, $validator->validated());
  5480. }
  5481. public function providesFailingExcludeIfData()
  5482. {
  5483. return [
  5484. [
  5485. [
  5486. 'has_appointment' => ['required', 'bool'],
  5487. 'appointment_date' => ['exclude_if:has_appointment,false', 'required', 'date'],
  5488. ], [
  5489. 'has_appointment' => true,
  5490. ], [
  5491. 'appointment_date' => ['validation.required'],
  5492. ],
  5493. ],
  5494. [
  5495. [
  5496. 'cat' => ['required', 'string'],
  5497. 'mouse' => ['exclude_if:cat,Tom', 'required', 'file'],
  5498. ], [
  5499. 'cat' => 'Bob',
  5500. 'mouse' => 'not a file',
  5501. ], [
  5502. 'mouse' => ['validation.file'],
  5503. ],
  5504. ],
  5505. [
  5506. [
  5507. 'has_appointments' => ['required', 'bool'],
  5508. 'appointments' => ['exclude_if:has_appointments,false', 'required', 'array'],
  5509. 'appointments.*.date' => ['required', 'date'],
  5510. 'appointments.*.name' => ['required', 'string'],
  5511. ], [
  5512. 'has_appointments' => true,
  5513. 'appointments' => [
  5514. ['date' => 'invalid', 'name' => 'Bob'],
  5515. ['date' => '2019-05-15'],
  5516. ],
  5517. ], [
  5518. 'appointments.0.date' => ['validation.date'],
  5519. 'appointments.1.name' => ['validation.required'],
  5520. ],
  5521. ],
  5522. [
  5523. [
  5524. 'vehicles.*.price' => 'required|numeric',
  5525. 'vehicles.*.type' => 'required|in:car,boat',
  5526. 'vehicles.*.wheels' => 'exclude_if:vehicles.*.type,boat|required|numeric',
  5527. ], [
  5528. 'vehicles' => [
  5529. [
  5530. 'price' => 100,
  5531. 'type' => 'car',
  5532. ],
  5533. [
  5534. 'price' => 500,
  5535. 'type' => 'boat',
  5536. ],
  5537. ],
  5538. ], [
  5539. 'vehicles.0.wheels' => ['validation.required'],
  5540. // vehicles.1.wheels is not required, because type is not "car"
  5541. ],
  5542. ],
  5543. 'exclude-validation-error-01' => [
  5544. [
  5545. 'vehicles.*.type' => 'required|in:car,boat',
  5546. 'vehicles.*.wheels' => 'exclude_if:vehicles.*.type,boat|required|array',
  5547. 'vehicles.*.wheels.*.color' => 'required|in:red,blue',
  5548. // In this bizzaro world example you can choose a custom shape for your wheels if they are red
  5549. 'vehicles.*.wheels.*.shape' => 'exclude_unless:vehicles.*.wheels.*.color,red|required|in:square,round',
  5550. ], [
  5551. 'vehicles' => [
  5552. [
  5553. 'type' => 'car', 'wheels' => [
  5554. ['color' => 'red', 'shape' => 'square'],
  5555. ['color' => 'blue', 'shape' => 'hexagon'],
  5556. ['color' => 'red', 'shape' => 'hexagon'],
  5557. ['color' => 'blue', 'shape' => 'triangle'],
  5558. ],
  5559. ],
  5560. ['type' => 'boat', 'wheels' => 'should be excluded'],
  5561. ],
  5562. ], [
  5563. // The blue wheels are excluded and are therefor not validated against the "in:square,round" rule
  5564. 'vehicles.0.wheels.2.shape' => ['validation.in'],
  5565. ],
  5566. ],
  5567. ];
  5568. }
  5569. /**
  5570. * @dataProvider providesFailingExcludeIfData
  5571. */
  5572. public function testExcludeIfWhenValidationFails($rules, $data, $expectedMessages)
  5573. {
  5574. $validator = new Validator(
  5575. $this->getIlluminateArrayTranslator(),
  5576. $data,
  5577. $rules
  5578. );
  5579. $fails = $validator->fails();
  5580. if (! $fails) {
  5581. $message = sprintf("Validation unexpectedly passed:\nRules: %s\nData: %s",
  5582. json_encode($rules, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES),
  5583. json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
  5584. );
  5585. }
  5586. $this->assertTrue($fails, $message ?? '');
  5587. $this->assertSame($expectedMessages, $validator->messages()->toArray());
  5588. }
  5589. public function providesPassingExcludeData()
  5590. {
  5591. return [
  5592. [
  5593. [
  5594. 'has_appointment' => ['required', 'bool'],
  5595. 'appointment_date' => ['exclude'],
  5596. ], [
  5597. 'has_appointment' => false,
  5598. 'appointment_date' => 'should be excluded',
  5599. ], [
  5600. 'has_appointment' => false,
  5601. ],
  5602. ],
  5603. ];
  5604. }
  5605. /**
  5606. * @dataProvider providesPassingExcludeData
  5607. */
  5608. public function testExclude($rules, $data, $expectedValidatedData)
  5609. {
  5610. $validator = new Validator(
  5611. $this->getIlluminateArrayTranslator(),
  5612. $data,
  5613. $rules
  5614. );
  5615. $passes = $validator->passes();
  5616. if (! $passes) {
  5617. $message = sprintf("Validation unexpectedly failed:\nRules: %s\nData: %s\nValidation error: %s",
  5618. json_encode($rules, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES),
  5619. json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES),
  5620. json_encode($validator->messages()->toArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
  5621. );
  5622. }
  5623. $this->assertTrue($passes, $message ?? '');
  5624. $this->assertSame($expectedValidatedData, $validator->validated());
  5625. }
  5626. public function testExcludingArrays()
  5627. {
  5628. $validator = new Validator(
  5629. $this->getIlluminateArrayTranslator(),
  5630. ['users' => [['name' => 'Mohamed', 'location' => 'cairo']]],
  5631. ['users' => 'array', 'users.*.name' => 'string']
  5632. );
  5633. $this->assertTrue($validator->passes());
  5634. $this->assertSame(['users' => [['name' => 'Mohamed', 'location' => 'cairo']]], $validator->validated());
  5635. $validator = new Validator(
  5636. $this->getIlluminateArrayTranslator(),
  5637. ['users' => [['name' => 'Mohamed', 'location' => 'cairo']]],
  5638. ['users' => 'array', 'users.*.name' => 'string']
  5639. );
  5640. $validator->excludeUnvalidatedArrayKeys = true;
  5641. $this->assertTrue($validator->passes());
  5642. $this->assertSame(['users' => [['name' => 'Mohamed']]], $validator->validated());
  5643. $validator = new Validator(
  5644. $this->getIlluminateArrayTranslator(),
  5645. ['admin' => ['name' => 'Mohamed', 'location' => 'cairo'], 'users' => [['name' => 'Mohamed', 'location' => 'cairo']]],
  5646. ['admin' => 'array', 'admin.name' => 'string', 'users' => 'array', 'users.*.name' => 'string']
  5647. );
  5648. $validator->excludeUnvalidatedArrayKeys = true;
  5649. $this->assertTrue($validator->passes());
  5650. $this->assertSame(['admin' => ['name' => 'Mohamed'], 'users' => [['name' => 'Mohamed']]], $validator->validated());
  5651. $validator = new Validator(
  5652. $this->getIlluminateArrayTranslator(),
  5653. ['users' => [['name' => 'Mohamed', 'location' => 'cairo']]],
  5654. ['users' => 'array']
  5655. );
  5656. $validator->excludeUnvalidatedArrayKeys = true;
  5657. $this->assertTrue($validator->passes());
  5658. $this->assertSame(['users' => [['name' => 'Mohamed', 'location' => 'cairo']]], $validator->validated());
  5659. $validator = new Validator(
  5660. $this->getIlluminateArrayTranslator(),
  5661. ['users' => ['mohamed', 'zain']],
  5662. ['users' => 'array', 'users.*' => 'string']
  5663. );
  5664. $validator->excludeUnvalidatedArrayKeys = true;
  5665. $this->assertTrue($validator->passes());
  5666. $this->assertSame(['users' => ['mohamed', 'zain']], $validator->validated());
  5667. $validator = new Validator(
  5668. $this->getIlluminateArrayTranslator(),
  5669. ['users' => ['admins' => [['name' => 'mohamed', 'job' => 'dev']], 'unvalidated' => 'foobar']],
  5670. ['users' => 'array', 'users.admins' => 'array', 'users.admins.*.name' => 'string']
  5671. );
  5672. $validator->excludeUnvalidatedArrayKeys = true;
  5673. $this->assertTrue($validator->passes());
  5674. $this->assertSame(['users' => ['admins' => [['name' => 'mohamed']]]], $validator->validated());
  5675. $validator = new Validator(
  5676. $this->getIlluminateArrayTranslator(),
  5677. ['users' => [1, 2, 3]],
  5678. ['users' => 'array|max:10']
  5679. );
  5680. $validator->excludeUnvalidatedArrayKeys = true;
  5681. $this->assertTrue($validator->passes());
  5682. $this->assertSame(['users' => [1, 2, 3]], $validator->validated());
  5683. }
  5684. public function testExcludeUnless()
  5685. {
  5686. $validator = new Validator(
  5687. $this->getIlluminateArrayTranslator(),
  5688. ['cat' => 'Felix', 'mouse' => 'Jerry'],
  5689. ['cat' => 'required|string', 'mouse' => 'exclude_unless:cat,Tom|required|string']
  5690. );
  5691. $this->assertTrue($validator->passes());
  5692. $this->assertSame(['cat' => 'Felix'], $validator->validated());
  5693. $validator = new Validator(
  5694. $this->getIlluminateArrayTranslator(),
  5695. ['cat' => 'Felix'],
  5696. ['cat' => 'required|string', 'mouse' => 'exclude_unless:cat,Tom|required|string']
  5697. );
  5698. $this->assertTrue($validator->passes());
  5699. $this->assertSame(['cat' => 'Felix'], $validator->validated());
  5700. $validator = new Validator(
  5701. $this->getIlluminateArrayTranslator(),
  5702. ['cat' => 'Tom', 'mouse' => 'Jerry'],
  5703. ['cat' => 'required|string', 'mouse' => 'exclude_unless:cat,Tom|required|string']
  5704. );
  5705. $this->assertTrue($validator->passes());
  5706. $this->assertSame(['cat' => 'Tom', 'mouse' => 'Jerry'], $validator->validated());
  5707. $validator = new Validator(
  5708. $this->getIlluminateArrayTranslator(),
  5709. ['cat' => 'Tom'],
  5710. ['cat' => 'required|string', 'mouse' => 'exclude_unless:cat,Tom|required|string']
  5711. );
  5712. $this->assertTrue($validator->fails());
  5713. $this->assertSame(['mouse' => ['validation.required']], $validator->messages()->toArray());
  5714. $validator = new Validator(
  5715. $this->getIlluminateArrayTranslator(),
  5716. ['foo' => true, 'bar' => 'baz'],
  5717. ['foo' => 'nullable', 'bar' => 'exclude_unless:foo,null']
  5718. );
  5719. $this->assertTrue($validator->passes());
  5720. $this->assertSame(['foo' => true], $validator->validated());
  5721. $trans = $this->getIlluminateArrayTranslator();
  5722. $v = new Validator($trans, ['bar' => 'Hello'], ['bar' => 'exclude_unless:foo,true']);
  5723. $this->assertTrue($v->passes());
  5724. $this->assertSame([], $v->validated());
  5725. $trans = $this->getIlluminateArrayTranslator();
  5726. $v = new Validator($trans, ['bar' => 'Hello'], ['bar' => 'exclude_unless:foo,null']);
  5727. $this->assertTrue($v->passes());
  5728. $this->assertSame(['bar' => 'Hello'], $v->validated());
  5729. }
  5730. public function testExcludeWithout()
  5731. {
  5732. $validator = new Validator(
  5733. $this->getIlluminateArrayTranslator(),
  5734. ['region' => 'South'],
  5735. [
  5736. 'country' => 'exclude_without:region|nullable|required_with:region|string|min:3',
  5737. 'region' => 'exclude_without:country|nullable|required_with:country|string|min:3',
  5738. ]
  5739. );
  5740. $this->assertTrue($validator->fails());
  5741. $this->assertSame(['country' => ['validation.required_with']], $validator->messages()->toArray());
  5742. }
  5743. public function testExcludeValuesAreReallyRemoved()
  5744. {
  5745. $validator = new Validator(
  5746. $this->getIlluminateArrayTranslator(),
  5747. ['cat' => 'Tom', 'mouse' => 'Jerry'],
  5748. ['cat' => 'required|string', 'mouse' => 'exclude_if:cat,Tom|required|string']
  5749. );
  5750. $this->assertTrue($validator->passes());
  5751. $this->assertSame(['cat' => 'Tom'], $validator->validated());
  5752. $this->assertSame(['cat' => 'Tom'], $validator->valid());
  5753. $this->assertSame([], $validator->invalid());
  5754. $validator = new Validator(
  5755. $this->getIlluminateArrayTranslator(),
  5756. ['cat' => 'Tom', 'mouse' => null],
  5757. ['cat' => 'required|string', 'mouse' => 'exclude_if:cat,Felix|required|string']
  5758. );
  5759. $this->assertTrue($validator->fails());
  5760. $this->assertSame(['cat' => 'Tom'], $validator->valid());
  5761. $this->assertSame(['mouse' => null], $validator->invalid());
  5762. }
  5763. public function testValidateFailsWithAsterisksAsDataKeys()
  5764. {
  5765. $post = ['data' => [0 => ['date' => '2019-01-24'], 1 => ['date' => 'blah'], '*' => ['date' => 'blah']]];
  5766. $rules = ['data.*.date' => 'required|date'];
  5767. $validator = new Validator($this->getIlluminateArrayTranslator(), $post, $rules);
  5768. $this->assertTrue($validator->fails());
  5769. $this->assertSame(['data.1.date' => ['validation.date'], 'data.*.date' => ['validation.date']], $validator->messages()->toArray());
  5770. }
  5771. public function testFailOnFirstError()
  5772. {
  5773. $trans = $this->getIlluminateArrayTranslator();
  5774. $data = [
  5775. 'foo' => 'bar',
  5776. 'age' => 30,
  5777. ];
  5778. $rules = [
  5779. 'foo' => ['required', 'string'],
  5780. 'baz' => ['required'],
  5781. 'age' => ['required', 'min:31'],
  5782. ];
  5783. $expectedFailOnFirstErrorDisableResult = [
  5784. 'baz' => [
  5785. 'validation.required',
  5786. ],
  5787. 'age' => [
  5788. 'validation.min.string',
  5789. ],
  5790. ];
  5791. $failOnFirstErrorDisable = new Validator($trans, $data, $rules);
  5792. $this->assertFalse($failOnFirstErrorDisable->passes());
  5793. $this->assertEquals($expectedFailOnFirstErrorDisableResult, $failOnFirstErrorDisable->getMessageBag()->getMessages());
  5794. $expectedFailOnFirstErrorEnableResult = [
  5795. 'baz' => [
  5796. 'validation.required',
  5797. ],
  5798. ];
  5799. $failOnFirstErrorEnable = new Validator($trans, $data, $rules, [], []);
  5800. $failOnFirstErrorEnable->stopOnFirstFailure();
  5801. $this->assertFalse($failOnFirstErrorEnable->passes());
  5802. $this->assertEquals($expectedFailOnFirstErrorEnableResult, $failOnFirstErrorEnable->getMessageBag()->getMessages());
  5803. }
  5804. public function testArrayKeysValidationPassedWhenHasKeys()
  5805. {
  5806. $trans = $this->getIlluminateArrayTranslator();
  5807. $data = [
  5808. 'baz' => [
  5809. 'foo' => 'bar',
  5810. 'fee' => 'faa',
  5811. 'laa' => 'lee',
  5812. ],
  5813. ];
  5814. $rules = [
  5815. 'baz' => [
  5816. 'array',
  5817. 'required_array_keys:foo,fee,laa',
  5818. ],
  5819. ];
  5820. $validator = new Validator($trans, $data, $rules, [], []);
  5821. $this->assertTrue($validator->passes());
  5822. }
  5823. public function testArrayKeysValidationPassedWithPartialMatch()
  5824. {
  5825. $trans = $this->getIlluminateArrayTranslator();
  5826. $data = [
  5827. 'baz' => [
  5828. 'foo' => 'bar',
  5829. 'fee' => 'faa',
  5830. 'laa' => 'lee',
  5831. ],
  5832. ];
  5833. $rules = [
  5834. 'baz' => [
  5835. 'array',
  5836. 'required_array_keys:foo,fee',
  5837. ],
  5838. ];
  5839. $validator = new Validator($trans, $data, $rules, [], []);
  5840. $this->assertTrue($validator->passes());
  5841. }
  5842. public function testArrayKeysValidationFailsWithMissingKey()
  5843. {
  5844. $trans = $this->getIlluminateArrayTranslator();
  5845. $trans->addLines(['validation.required_array_keys' => 'The :attribute field must contain entries for :values'], 'en');
  5846. $data = [
  5847. 'baz' => [
  5848. 'foo' => 'bar',
  5849. 'fee' => 'faa',
  5850. 'laa' => 'lee',
  5851. ],
  5852. ];
  5853. $rules = [
  5854. 'baz' => [
  5855. 'array',
  5856. 'required_array_keys:foo,fee,boo,bar',
  5857. ],
  5858. ];
  5859. $validator = new Validator($trans, $data, $rules, [], []);
  5860. $this->assertFalse($validator->passes());
  5861. $this->assertSame(
  5862. 'The baz field must contain entries for foo, fee, boo, bar',
  5863. $validator->messages()->first('baz')
  5864. );
  5865. }
  5866. public function testArrayKeysValidationFailsWithNotAnArray()
  5867. {
  5868. $trans = $this->getIlluminateArrayTranslator();
  5869. $trans->addLines(['validation.required_array_keys' => 'The :attribute field must contain entries for :values'], 'en');
  5870. $data = [
  5871. 'baz' => 'no an array',
  5872. ];
  5873. $rules = [
  5874. 'baz' => [
  5875. 'required_array_keys:foo,fee,boo,bar',
  5876. ],
  5877. ];
  5878. $validator = new Validator($trans, $data, $rules, [], []);
  5879. $this->assertFalse($validator->passes());
  5880. $this->assertSame(
  5881. 'The baz field must contain entries for foo, fee, boo, bar',
  5882. $validator->messages()->first('baz')
  5883. );
  5884. }
  5885. public function testArrayKeysWithDotIntegerMin()
  5886. {
  5887. $trans = $this->getIlluminateArrayTranslator();
  5888. $data = [
  5889. 'foo.bar' => -1,
  5890. ];
  5891. $rules = [
  5892. 'foo\.bar' => 'integer|min:1',
  5893. ];
  5894. $expectedResult = [
  5895. 'foo.bar' => [
  5896. 'validation.min.numeric',
  5897. ],
  5898. ];
  5899. $validator = new Validator($trans, $data, $rules, [], []);
  5900. $this->assertEquals($expectedResult, $validator->getMessageBag()->getMessages());
  5901. }
  5902. protected function getTranslator()
  5903. {
  5904. return m::mock(TranslatorContract::class);
  5905. }
  5906. public function getIlluminateArrayTranslator()
  5907. {
  5908. return new Translator(
  5909. new ArrayLoader, 'en'
  5910. );
  5911. }
  5912. }
  5913. class ImplicitTableModel extends Model
  5914. {
  5915. protected $guarded = [];
  5916. public $timestamps = false;
  5917. }
  5918. class ExplicitTableModel extends Model
  5919. {
  5920. protected $table = 'explicits';
  5921. protected $guarded = [];
  5922. public $timestamps = false;
  5923. }
  5924. class ExplicitPrefixedTableModel extends Model
  5925. {
  5926. protected $table = 'prefix.explicits';
  5927. protected $guarded = [];
  5928. public $timestamps = false;
  5929. }
  5930. class ExplicitTableAndConnectionModel extends Model
  5931. {
  5932. protected $table = 'explicits';
  5933. protected $connection = 'connection';
  5934. protected $guarded = [];
  5935. public $timestamps = false;
  5936. }
  5937. class NonEloquentModel
  5938. {
  5939. }