From 259fc6d7819257033222234bfed7619a0c5282f9 Mon Sep 17 00:00:00 2001 From: Jan Wroniszewski <jan@industreal.pl> Date: Fri, 11 Nov 2016 01:50:50 +0100 Subject: [PATCH] #10547 Bad regexp in DbHelper.php (#10688) --- core/DbHelper.php | 2 +- tests/PHPUnit/Unit/DbHelperTest.php | 61 +++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 tests/PHPUnit/Unit/DbHelperTest.php diff --git a/core/DbHelper.php b/core/DbHelper.php index 96ba2c5a1d..1687cc89e8 100644 --- a/core/DbHelper.php +++ b/core/DbHelper.php @@ -198,7 +198,7 @@ class DbHelper */ public static function isValidDbname($dbname) { - return (0 !== preg_match('/(^[a-zA-Z0-9]+([a-zA-Z_0-9.-\+]*))$/D', $dbname)); + return (0 !== preg_match('/(^[a-zA-Z0-9]+([a-zA-Z0-9\_\.\-\+]*))$/D', $dbname)); } } diff --git a/tests/PHPUnit/Unit/DbHelperTest.php b/tests/PHPUnit/Unit/DbHelperTest.php new file mode 100644 index 0000000000..fa6957d4ba --- /dev/null +++ b/tests/PHPUnit/Unit/DbHelperTest.php @@ -0,0 +1,61 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +namespace Piwik\Tests\Unit; + +use Piwik\DbHelper; + +/** + * Class DbHelperTest + * @package Piwik\Tests\Unit + * @group Core + * @group Core_Unit + */ +class DbHelperTest extends \PHPUnit_Framework_TestCase +{ + + /** + * @dataProvider getVariousDbNames + * @param string $dbName + * @param bool $expectation + */ + public function testCorrectNames($dbName, $expectation) + { + $this->assertSame(DbHelper::isValidDbname($dbName), $expectation); + } + + public function getVariousDbNames() + { + return array( + 'simpleDbName' => array( + 'dbName' => 'FirstPiwikDb', + 'expectation' => true + ), + 'containsNumbers' => array( + 'dbName' => 'FirstPiw1kDb', + 'expectation' => true + ), + 'startsWithNumber' => array( + 'dbName' => '1stPiwikDb', + 'expectation' => true + ), + 'containsAllowedSpecialCharacters' => array( + 'dbName' => 'MyPiwikDb-with.More+compleX_N4M3', + 'expectation' => true + ), + 'containsSpace' => array( + 'dbName' => '1st PiwikDb', + 'expectation' => false + ), + 'startWithNonAlphaNumericSign' => array( + 'dbName' => ';FirstPiwikDb', + 'expectation' => false + ), + ); + } +} -- GitLab