. * * @package LibreNMS * @link http://librenms.org * @copyright 2016 Tony Murray * @author Tony Murray */ namespace LibreNMS\Tests; class CommonFunctionsTest extends \PHPUnit_Framework_TestCase { public function testStrContains() { $data = 'This is a test. Just Testing.'; $this->assertTrue(str_contains($data, 'Just')); $this->assertFalse(str_contains($data, 'just')); $this->assertTrue(str_contains($data, 'juSt', true)); $this->assertFalse(str_contains($data, 'nope', true)); $this->assertTrue(str_contains($data, array('not', 'this', 'This'))); $this->assertFalse(str_contains($data, array('not', 'this'))); $this->assertTrue(str_contains($data, array('not', 'thIs'), true)); $this->assertFalse(str_contains($data, array('not', 'anything'), true)); } public function testStartsWith() { $data = 'This is a test. Just Testing that.'; $this->assertTrue(starts_with($data, 'This')); $this->assertFalse(starts_with($data, 'this')); $this->assertTrue(starts_with($data, 'thIs', true)); $this->assertFalse(starts_with($data, 'test', true)); $this->assertTrue(starts_with($data, array('this', 'Test', 'This'))); $this->assertFalse(starts_with($data, array('this', 'Test'))); $this->assertTrue(starts_with($data, array('Test', 'no', 'thiS'), true)); $this->assertFalse(starts_with($data, array('just', 'Test'), true)); } public function testEndsWith() { $data = 'This is a test. Just Testing'; $this->assertTrue(ends_with($data, 'Testing')); $this->assertFalse(ends_with($data, 'testing')); $this->assertTrue(ends_with($data, 'testIng', true)); $this->assertFalse(ends_with($data, 'test', true)); $this->assertTrue(ends_with($data, array('this', 'Testing', 'This'))); $this->assertFalse(ends_with($data, array('this', 'Test'))); $this->assertTrue(ends_with($data, array('this', 'tesTing', 'no'), true)); $this->assertFalse(ends_with($data, array('this', 'Test'), true)); } public function testRrdDescriptions() { $data = 'Toner, S/N:CR_UM-16021314488.'; $this->assertEquals('Toner, S/N CR_UM-16021314488.', safedescr($data)); } public function testSetNull() { $this->assertNull(set_null('BAD-DATA')); $this->assertEquals(0, set_null(0)); $this->assertEquals(25, set_null(25)); $this->assertEquals(-25, set_null(-25)); $this->assertEquals(99, set_null(' ', 99)); $this->assertNull(set_null(-25, null, 0)); $this->assertEquals(2, set_null(2, 0, 2)); } public function testDisplay() { $this->assertEquals('<html>string</html>', display('string')); $this->assertEquals('<script>alert("test")</script>', display('')); $tmp_config = array( 'HTML.Allowed' => 'b,iframe,i,ul,li,h1,h2,h3,h4,br,p', 'HTML.Trusted' => true, 'HTML.SafeIframe' => true, ); $this->assertEquals('Bold', display('Bold', $tmp_config)); $this->assertEquals('', display('', $tmp_config)); } public function testStringToClass() { $this->assertSame('LibreNMS\OS\Os', str_to_class('OS', 'LibreNMS\\OS\\')); $this->assertSame('SpacesName', str_to_class('spaces name')); $this->assertSame('DashName', str_to_class('dash-name')); $this->assertSame('UnderscoreName', str_to_class('underscore_name')); $this->assertSame('LibreNMS\\AllOfThemName', str_to_class('all OF-thEm_NaMe', 'LibreNMS\\')); } public function testIsValidHostname() { $this->assertTrue(is_valid_hostname('a'), 'a'); $this->assertTrue(is_valid_hostname('a.'), 'a.'); $this->assertTrue(is_valid_hostname('0'), '0'); $this->assertTrue(is_valid_hostname('a.b'), 'a.b'); $this->assertTrue(is_valid_hostname('localhost'), 'localhost'); $this->assertTrue(is_valid_hostname('google.com'), 'google.com'); $this->assertTrue(is_valid_hostname('news.google.co.uk'), 'news.google.co.uk'); $this->assertTrue(is_valid_hostname('xn--fsqu00a.xn--0zwm56d'), 'xn--fsqu00a.xn--0zwm56d'); $this->assertFalse(is_valid_hostname('goo gle.com'), 'goo gle.com'); $this->assertFalse(is_valid_hostname('google..com'), 'google..com'); $this->assertFalse(is_valid_hostname('google.com '), 'google.com '); $this->assertFalse(is_valid_hostname('google-.com'), 'google-.com'); $this->assertFalse(is_valid_hostname('.google.com'), '.google.com'); $this->assertFalse(is_valid_hostname('assertFalse(is_valid_hostname('alert('), 'alert('); $this->assertFalse(is_valid_hostname('.'), '.'); $this->assertFalse(is_valid_hostname('..'), '..'); $this->assertFalse(is_valid_hostname(' '), 'Just a space'); $this->assertFalse(is_valid_hostname('-'), '-'); $this->assertFalse(is_valid_hostname(''), 'Empty string'); } }