1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474: 475: 476: 477: 478: 479: 480: 481: 482: 483: 484: 485: 486: 487: 488: 489: 490: 491: 492: 493: 494: 495: 496: 497: 498: 499: 500: 501: 502: 503: 504: 505: 506: 507: 508: 509: 510: 511: 512: 513: 514: 515: 516: 517: 518: 519: 520: 521: 522: 523: 524: 525: 526: 527: 528: 529: 530: 531: 532: 533: 534: 535: 536: 537: 538: 539: 540: 541: 542: 543: 544: 545: 546: 547: 548: 549: 550: 551: 552: 553: 554: 555: 556: 557: 558: 559: 560: 561: 562: 563: 564: 565: 566: 567: 568: 569: 570: 571: 572: 573: 574: 575: 576: 577: 578: 579: 580: 581: 582: 583: 584: 585: 586: 587: 588: 589: 590: 591: 592: 593: 594: 595: 596: 597: 598: 599: 600: 601: 602: 603: 604: 605: 606: 607: 608: 609: 610: 611: 612: 613: 614: 615: 616: 617: 618: 619: 620: 621: 622: 623: 624: 625:
<?php
namespace Nethgui\Test\Unit\Nethgui\System;
class ValidatorTest extends \PHPUnit_Framework_TestCase
{
protected $object;
private $platform;
protected function setUp()
{
$this->platform = $this->getMock('\Nethgui\System\PlatformInterface', array(
'getDateFormat',
'exec',
'getIdentityAdapter',
'getMapAdapter',
'getDatabase',
'getTableAdapter',
'signalEvent',
'createValidator',
'getDetachedProcess',
'getDetachedProcesses',
'runEvents',
));
$this->platform
->expects($this->any())
->method('getDateFormat')
->will($this->returnValue('YYYY-mm-dd'));
$this->object = new \Nethgui\System\Validator($this->platform);
}
public function testOrValidator()
{
$v1 = new \Nethgui\System\Validator($this->platform);
$v2 = new \Nethgui\System\Validator($this->platform);
$v1->equalTo(1);
$v2->equalTo(2);
$o = $this->object->orValidator($v1, $v2);
$this->assertSame($this->object, $o);
$this->assertTrue($this->object->evaluate(1));
$this->assertTrue($this->object->evaluate(2));
$this->assertFalse($this->object->evaluate(0));
$this->assertFalse($this->object->evaluate(3));
}
public function testMemberOf1()
{
$o = $this->object->memberOf('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h');
$this->assertSame($this->object, $o);
$this->assertTrue($this->object->evaluate('a'));
$this->assertTrue($this->object->evaluate('h'));
$this->assertTrue($this->object->evaluate('d'));
$this->assertFalse($this->object->evaluate('z'));
}
public function testMemberOf2()
{
$o = $this->object->memberOf(array('a', 'b', 'c'));
$this->assertSame($this->object, $o);
$this->assertTrue($this->object->evaluate('a'));
$this->assertFalse($this->object->evaluate('z'));
}
public function testRegexpSuccess()
{
$o = $this->object->regexp('/[0-9]+/');
$this->assertSame($this->object, $o);
$this->assertTrue($this->object->evaluate('12345'));
}
public function testRegexpFail()
{
$o = $this->object->regexp('/[0-9]+/');
$this->assertSame($this->object, $o);
$this->assertFalse($this->object->evaluate('aaaaa'));
}
public function testNotEmpty()
{
$o = $this->object->notEmpty();
$this->assertSame($this->object, $o);
$this->assertFalse($this->object->evaluate(''));
}
public function testEmpty()
{
$o = $this->object->isEmpty();
$this->assertSame($this->object, $o);
$this->assertTrue($this->object->evaluate(''));
$this->assertTrue($this->object->evaluate(FALSE));
$this->assertTrue($this->object->evaluate(NULL));
$this->assertTrue($this->object->evaluate(array()));
$this->assertTrue($this->object->evaluate('0'));
$this->assertFalse($this->object->evaluate('1'));
}
public function testForceResultTrue()
{
$o = $this->object->forceResult(TRUE)->notEmpty();
$this->assertSame($this->object, $o);
$this->assertTrue($this->object->evaluate(''));
}
public function testForceResultFalse()
{
$o = $this->object->notEmpty()->forceResult(FALSE);
$this->assertSame($this->object, $o);
$this->assertFalse($this->object->evaluate('x'));
}
public function testIpV4Address()
{
$o = $this->object->ipV4Address();
$this->assertSame($this->object, $o);
$this->assertTrue($this->object->evaluate('1.1.1.1'));
$this->assertTrue($this->object->evaluate('0.0.0.0'));
$this->assertFalse($this->object->evaluate(''));
$this->assertFalse($this->object->evaluate('a.b.c.d'));
$this->assertFalse($this->object->evaluate('192.168.5.002'));
$this->assertFalse($this->object->evaluate('192.168.005.003'));
$this->assertTrue($this->object->evaluate('0.10.20.30'));
}
public function testIpV4Netmask()
{
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
public function testIpV6Address()
{
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
public function testIpV6Netmask()
{
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
public function testUsernameValid()
{
$o = $this->object->username();
$this->assertSame($this->object, $o);
$this->assertTrue($this->object->evaluate('v123alid-user_name'));
}
public function testUsernameInvalid()
{
$o = $this->object->username();
$this->assertSame($this->object, $o);
$invalidUsernames = array(
'invalidUserName',
'0invalidusername',
'in*valid',
str_repeat('x', 256),
);
foreach ($invalidUsernames as $username) {
$this->assertFalse($this->object->evaluate($username), "Invalid user name: $username");
}
}
public function testCollectionValidatorNotEmptyMembers()
{
$v = new \Nethgui\System\Validator($this->platform);
$v->notEmpty();
$cv = $this->object->collectionValidator($v);
$this->assertSame($this->object, $cv);
$o = new \ArrayObject(array('a', 'b', 'c'));
$this->assertTrue($this->object->evaluate(array('a', 'b', 'c')));
$this->assertTrue($this->object->evaluate($o));
$this->assertTrue($this->object->evaluate(array()));
$this->assertTrue($this->object->evaluate($o->getIterator()));
$this->assertFalse($this->object->evaluate(array('a', '', 'c')));
$this->assertFalse($this->object->evaluate(new \ArrayObject(array('a', 'b', ''))));
$this->assertFalse($this->object->evaluate(2));
$this->assertFalse($this->object->evaluate(TRUE));
$this->assertFalse($this->object->evaluate(1.2));
}
public function testInteger()
{
$o = $this->object->integer();
$this->assertSame($this->object, $o);
$this->assertTrue($this->object->evaluate('123'));
$this->assertFalse($this->object->evaluate('123.0'));
$this->assertFalse($this->object->evaluate('123.1'));
$this->assertFalse($this->object->evaluate('a'));
$this->assertFalse($this->object->evaluate(' 1'));
$this->assertFalse($this->object->evaluate('01'));
$this->assertFalse($this->object->evaluate('1 '));
$this->assertTrue($this->object->evaluate('-123'));
}
public function testPositive()
{
$o = $this->object->positive();
$this->assertSame($this->object, $o);
$this->assertTrue($this->object->evaluate(1.1));
$this->assertTrue($this->object->evaluate('1.1'));
$this->assertFalse($this->object->evaluate('0'));
$this->assertFalse($this->object->evaluate(FALSE));
$this->assertFalse($this->object->evaluate(-1));
$this->assertFalse($this->object->evaluate('a'));
$this->assertFalse($this->object->evaluate(' 1.1'));
$this->assertFalse($this->object->evaluate('1 '));
}
public function testNegative()
{
$o = $this->object->negative();
$this->assertSame($this->object, $o);
$this->assertTrue($this->object->evaluate('-1.2'));
$this->assertTrue($this->object->evaluate(-1));
$this->assertFalse($this->object->evaluate(1.1));
$this->assertFalse($this->object->evaluate('1.1'));
$this->assertFalse($this->object->evaluate('0'));
}
public function testGreatThan()
{
$o = $this->object->greatThan('100');
$this->assertSame($this->object, $o);
$this->assertTrue($this->object->evaluate('101'));
$this->assertFalse($this->object->evaluate('100'));
$this->assertFalse($this->object->evaluate('99'));
}
public function testLessThan()
{
$o = $this->object->lessThan('100');
$this->assertSame($this->object, $o);
$this->assertTrue($this->object->evaluate('99'));
$this->assertFalse($this->object->evaluate('100'));
$this->assertFalse($this->object->evaluate('101'));
}
public function testEqualTo()
{
$o = $this->object->equalTo('100');
$this->assertSame($this->object, $o);
$this->assertTrue($this->object->evaluate('100'));
$this->assertFalse($this->object->evaluate('101'));
}
public function testStringEqualTo()
{
$o = $this->object->equalTo('0');
$this->assertSame($this->object, $o);
$this->assertTrue($this->object->evaluate('0'));
$this->assertFalse($this->object->evaluate('00'));
}
public function testMinLength()
{
$o = $this->object->minLength(3);
$this->assertSame($this->object, $o);
$this->assertFalse($this->object->evaluate(''));
$this->assertFalse($this->object->evaluate('AA'));
$this->assertTrue($this->object->evaluate('AAA'));
$this->assertTrue($this->object->evaluate('AAAA'));
$this->setExpectedException('InvalidArgumentException');
$this->object->evaluate(array('a'));
}
public function testMaxLength()
{
$o = $this->object->maxLength(3);
$this->assertSame($this->object, $o);
$this->assertTrue($this->object->evaluate(''));
$this->assertTrue($this->object->evaluate('AA'));
$this->assertTrue($this->object->evaluate('AAA'));
$this->assertFalse($this->object->evaluate('AAAA'));
$this->setExpectedException('InvalidArgumentException');
$this->object->evaluate(10);
}
public function testHostname()
{
$o = $this->object->hostname();
$this->assertSame($this->object, $o);
$this->assertTrue($this->object->evaluate('www.Nethesis.It'));
$this->assertTrue($this->object->evaluate('A'));
$this->assertFalse($this->object->evaluate('www.micro$oft.com'));
$this->assertFalse($this->object->evaluate('-ww.fail.com'));
$this->assertFalse($this->object->evaluate('www._fail.com'));
$this->assertFalse($this->object->evaluate('www.fail.-'));
$this->assertFalse($this->object->evaluate(''));
$this->assertFalse($this->object->evaluate(str_repeat('w', 65) . '.example.com'));
$this->assertFalse($this->object->evaluate('www.' . str_repeat('.aaa', 100)));
}
public function testHostnameFqdn()
{
$o = $this->object->hostname(1);
$this->assertSame($this->object, $o);
$this->assertFalse($this->object->evaluate('host123'));
$this->assertFalse($this->object->evaluate('davidep1'));
$this->assertTrue($this->object->evaluate('host.domain'));
$this->assertTrue($this->object->evaluate('host.domain.co.uk'));
$this->assertTrue($this->object->evaluate('xn--bcher-kva.ch'));
$this->assertTrue($this->object->evaluate('bcher--kva.ch'));
}
public function testHostnameSimple()
{
$o = $this->object->hostname(0, 0);
$this->assertSame($this->object, $o);
$this->assertTrue($this->object->evaluate('host123'));
$this->assertFalse($this->object->evaluate('host.domain'));
}
public function testHostnameEx()
{
$this->object->hostname(15, 7);
}
public function testFailureInfo1()
{
$o = $this->object->minLength(3);
$this->assertSame($this->object, $o);
$this->assertFalse($this->object->evaluate('hi'));
$failureInfo = $this->object->getFailureInfo();
$this->assertInternalType('array', $failureInfo);
$this->assertEquals(1, count($failureInfo));
$this->assertArrayHasKey(0, $failureInfo);
$this->assertInternalType('array', $failureInfo[0]);
$this->assertArrayHasKey(0, $failureInfo[0]);
$this->assertArrayHasKey(1, $failureInfo[0]);
$this->assertInternalType('array', $failureInfo[0][1]);
$this->assertEquals(1, count($failureInfo[0][1]));
}
public function testDateSmallEndian()
{
$o = $this->object->date('dd/mm/YYYY');
$this->assertSame($this->object, $o);
$this->assertTrue($this->object->evaluate('31/12/1999'));
$this->assertTrue($this->object->evaluate('1/1/1999'));
$this->assertFalse($this->object->evaluate(''));
$this->assertFalse($this->object->evaluate('12-31-1999'));
$this->assertFalse($this->object->evaluate('1999-31-12'));
$this->assertFalse($this->object->evaluate('0/0/0'));
$this->assertFalse($this->object->evaluate('29-02-1999'));
$this->assertFalse($this->object->evaluate('29/02/1999'));
}
public function testDateMiddleEndian()
{
$o = $this->object->date('mm-dd-YYYY');
$this->assertSame($this->object, $o);
$this->assertTrue($this->object->evaluate('12-31-1999'));
$this->assertTrue($this->object->evaluate('1-1-1999'));
$this->assertFalse($this->object->evaluate(''));
$this->assertFalse($this->object->evaluate('31/12/1999'));
$this->assertFalse($this->object->evaluate('1999-31-12'));
$this->assertFalse($this->object->evaluate('0-0-0'));
$this->assertFalse($this->object->evaluate('02-29-1999'));
$this->assertFalse($this->object->evaluate('02/29/1999'));
}
public function testDateBigEndian()
{
$o = $this->object->date('YYYY-mm-dd');
$this->assertSame($this->object, $o);
$this->assertTrue($this->object->evaluate('1999-12-31'));
$this->assertFalse($this->object->evaluate('1999-31-12'));
}
public function testDateDefault()
{
$o = $this->object->date();
$this->assertSame($this->object, $o);
$this->assertTrue($this->object->evaluate('1999-12-31'));
}
public function testDateUnknownFormat()
{
$this->setExpectedException('InvalidArgumentException');
$this->object->date('mm.dd.yyyy');
$this->object->evaluate('1999-12-31');
}
public function testTime()
{
$o = $this->object->time();
$this->assertSame($this->object, $o);
$this->assertTrue($this->object->evaluate('00:00'));
$this->assertTrue($this->object->evaluate('23:59'));
$this->assertFalse($this->object->evaluate('24:00'));
$this->assertFalse($this->object->evaluate('1:0'));
}
public function testPlatform1()
{
$o = $this->object->platform('test', 'a1', 'a2');
$this->assertSame($this->object, $o);
$processMockSuccess = $this->getMock('\Nethgui\System\ProcessInterface', array(
'getOutput',
'getOutputArray',
'readOutput',
'getExitCode',
'addArgument',
'exec',
'kill',
'setIdentifier',
'getIdentifier',
'readExecutionState',
'getTimes',
'isDisposed',
'dispose')
);
$processMockSuccess->expects($this->any())
->method('getExitCode')
->will($this->returnValue(0));
$processMockSuccess->expects($this->any())
->method('getOutput')
->will($this->returnValue(''));
$this->platform
->expects($this->once())
->method('exec')
->with('/usr/bin/sudo /sbin/e-smith/validate ${@}', array('test', 'a1', 'a2', 'value1'))
->will($this->returnValue($processMockSuccess));
$this->assertTrue($this->object->evaluate('value1'));
}
public function testPlatform2()
{
$o = $this->object->platform('test', 'a1', 'a2');
$this->assertSame($this->object, $o);
$processMockFail = $this->getMockBuilder('\Nethgui\System\ProcessInterface')
->getMock();
$processMockFail->expects($this->any())
->method('getExitStatus')
->will($this->returnValue(1));
$processMockFail->expects($this->any())
->method('getOutput')
->will($this->returnValue("Invalid value\nExiting..."));
$this->platform
->expects($this->once())
->method('exec')
->with('/usr/bin/sudo /sbin/e-smith/validate ${@}', array('test', 'a1', 'a2', 'value2'))
->will($this->returnValue($processMockFail));
$this->assertFalse($this->object->evaluate('value2'));
}
public function testEmailValid()
{
$o = $this->object->email();
$this->assertSame($this->object, $o);
$eval = $this->object->evaluate('my_valid.e-m4il@domain.tld');
$failureInfo = $this->object->getFailureInfo();
$this->assertTrue($eval, 'Validation failed. Reason: ' . ($eval === FALSE ? $failureInfo[0][0] : ''));
}
public function testEmailInvalid()
{
$o = $this->object->email();
$this->assertSame($this->object, $o);
$invalidEmails = array(
array('invalidUserName', 'valid_email,generic_failure'),
array('@domain.tld', 'valid_email,generic_failure'),
array('.invalidusername@domain.tld', 'valid_email,generic_failure'),
array('in(valid)@domain.tld', 'valid_email,generic_failure'),
array('in..valid@domain.tld', 'valid_email,generic_failure'),
array('invalid.@domain.tld', 'valid_email,generic_failure'),
array(str_repeat('x', 65) . '@domain.tld', 'valid_email,generic_failure'),
array(str_repeat('x', 244) . '@domain.tld', 'valid_email,generic_failure'),
array('my.email@.domain', 'valid_email,generic_failure'),
);
foreach ($invalidEmails as $test) {
$this->assertFalse($this->object->evaluate($test[0]), "Invalid email address: " . $test[0]);
$this->assertEquals(array(array($test[1], array())), $this->object->getFailureInfo(), 'Testing ' . $test[0]);
}
}
public function testCidrBlock()
{
$o = $this->object->cidrBlock();
$this->assertSame($this->object, $o);
$this->assertTrue($o->evaluate('12.13.14.15/24'));
$this->assertFalse($o->evaluate('12.13.14.15'));
$this->assertFalse($o->evaluate('12.13.14.15/'));
$this->assertFalse($o->evaluate('12.13.14.15/2aaa'));
$this->assertFalse($o->evaluate('12.13.14.15/q'));
$this->assertFalse($o->evaluate('1/1'));
$this->assertFalse($o->evaluate('12.13.14.300/12'));
}
public function testBug3044()
{
$o = $this->object->integer()->greatThan(0)->lessThan(256);
$this->assertTrue($o->evaluate('1'));
$this->assertTrue($o->evaluate('255'));
$this->assertTrue($o->evaluate('32'));
$this->assertFalse($o->evaluate('01'));
$this->assertFalse($o->evaluate(' 1'));
$this->assertFalse($o->evaluate('255 '));
}
}