diff --git a/includes/common.php b/includes/common.php index 83ff349cac..f2ab56b9a2 100644 --- a/includes/common.php +++ b/includes/common.php @@ -1375,33 +1375,24 @@ function ResolveGlues($tables, $target, $x = 0, $hist = array(), $last = array() /** * Determine if a given string contains a given substring. * - * @param string $haystack - * @param string|array $needles + * @param string $haystack + * @param string|array $needles + * @param bool $case_insensitive * @return bool */ -function str_contains($haystack, $needles) +function str_contains($haystack, $needles, $case_insensitive = false) { - foreach ((array) $needles as $needle) { - if ($needle != '' && strpos($haystack, $needle) !== false) { - return true; + if ($case_insensitive) { + foreach ((array) $needles as $needle) { + if ($needle != '' && stripos($haystack, $needle) !== false) { + return true; + } } - } - return false; -} - -/** - * Determine if a given string contains a given substring. - * Case Insensitive. - * - * @param string $haystack - * @param string|array $needles - * @return bool - */ -function str_icontains($haystack, $needles) -{ - foreach ((array) $needles as $needle) { - if ($needle != '' && stripos($haystack, $needle) !== false) { - return true; + } else { + foreach ((array) $needles as $needle) { + if ($needle != '' && strpos($haystack, $needle) !== false) { + return true; + } } } return false; @@ -1410,34 +1401,25 @@ function str_icontains($haystack, $needles) /** * Determine if a given string ends with a given substring. * - * @param string $haystack - * @param string|array $needles + * @param string $haystack + * @param string|array $needles + * @param bool $case_insensitive * @return bool */ -function ends_with($haystack, $needles) +function ends_with($haystack, $needles, $case_insensitive = false) { - foreach ((array)$needles as $needle) { - if ((string)$needle === substr($haystack, -strlen($needle))) { - return true; + if ($case_insensitive) { + $lower_haystack = strtolower($haystack); + foreach ((array)$needles as $needle) { + if (strtolower($needle) === substr($lower_haystack, -strlen($needle))) { + return true; + } } - } - return false; -} - -/** - * Determine if a given string ends with a given substring. - * Case insensitive. - * - * @param string $haystack - * @param string|array $needles - * @return bool - */ -function iends_with($haystack, $needles) -{ - $lower_haystack = strtolower($haystack); - foreach ((array)$needles as $needle) { - if (strtolower($needle) === substr($lower_haystack, -strlen($needle))) { - return true; + } else { + foreach ((array)$needles as $needle) { + if ((string)$needle === substr($haystack, -strlen($needle))) { + return true; + } } } return false; @@ -1448,31 +1430,22 @@ function iends_with($haystack, $needles) * * @param string $haystack * @param string|array $needles + * @param bool $case_insensitive * @return bool */ -function starts_with($haystack, $needles) +function starts_with($haystack, $needles, $case_insensitive = false) { - foreach ((array)$needles as $needle) { - if ($needle != '' && strpos($haystack, $needle) === 0) { - return true; - } - } - return false; -} - -/** - * Determine if a given string starts with a given substring. - * Case insensitive. - * - * @param string $haystack - * @param string|array $needles - * @return bool - */ -function istarts_with($haystack, $needles) -{ - foreach ((array)$needles as $needle) { - if ($needle != '' && stripos($haystack, $needle) === 0) { - return true; + if ($case_insensitive) { + foreach ((array)$needles as $needle) { + if ($needle != '' && stripos($haystack, $needle) === 0) { + return true; + } + } + } else { + foreach ((array)$needles as $needle) { + if ($needle != '' && strpos($haystack, $needle) === 0) { + return true; + } } } return false; diff --git a/tests/CommonFunctionsTest.php b/tests/CommonFunctionsTest.php new file mode 100644 index 0000000000..99a82e7e30 --- /dev/null +++ b/tests/CommonFunctionsTest.php @@ -0,0 +1,80 @@ +. + * + * @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)); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 58b9e29526..baef847897 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -30,5 +30,6 @@ require_once $install_dir . '/includes/defaults.inc.php'; $classLoader->registerDir($install_dir . '/tests', 'LibreNMS\Tests'); +require_once $install_dir . '/includes/common.php'; require_once $install_dir . '/includes/rrdtool.inc.php'; require_once $install_dir . '/includes/syslog.php';