Commit b35e480f authored by Chris Graham's avatar Chris Graham
Browse files

Fixed MANTIS-4651 (Problem with escaping in wildcard matches)

parent 87dbb999
Loading
Loading
Loading
Loading
+47 −0
Original line number Diff line number Diff line
<?php /*

 Composr
 Copyright (c) ocProducts, 2004-2016

 See text/EN/licence.txt for full licencing information.

*/

/**
 * @license    http://opensource.org/licenses/cpal_1.0 Common Public Attribution License
 * @copyright  ocProducts Ltd
 * @package    testing_platform
 */

/**
 * Composr test case class (unit testing).
 */
class simulated_wildcard_match_test_set extends cms_test_case
{
    public function testWildcards()
    {
        // Full cover
        $this->assertTrue(simulated_wildcard_match('Test sentence', 'Test', false));
        $this->assertTrue(!simulated_wildcard_match('Test sentence', 'Test', true));

        // Normal syntax
        $this->assertTrue(simulated_wildcard_match('Test sentence', 'Test*', true));
        $this->assertTrue(!simulated_wildcard_match('Test sentence', 'X', true));
        $this->assertTrue(!simulated_wildcard_match('Test sentence', '*X*', true));
        $this->assertTrue(!simulated_wildcard_match('Test sentence', '?X?', true));
        $this->assertTrue(simulated_wildcard_match('Test sentence', '*sentence', true));
        $this->assertTrue(simulated_wildcard_match('Test sentence', '???? sentence', true));

        // SQL syntax
        $this->assertTrue(simulated_wildcard_match('Test sentence', 'Test%', true));
        $this->assertTrue(!simulated_wildcard_match('Test sentence', 'X', true));
        $this->assertTrue(!simulated_wildcard_match('Test sentence', '%X%', true));
        $this->assertTrue(!simulated_wildcard_match('Test sentence', '_X_', true));
        $this->assertTrue(simulated_wildcard_match('Test sentence', '%sentence', true));
        $this->assertTrue(simulated_wildcard_match('Test sentence', '____ sentence', true));

        // Complexity
        $this->assertTrue(simulated_wildcard_match('Test sentence', 'Test \\sentence', false)); // Because we strip "\" from patterns
        $this->assertTrue(!simulated_wildcard_match('Test sentence', 'Test Xsentence', false));
    }
}
+2 −0
Original line number Diff line number Diff line
@@ -1719,6 +1719,8 @@ function __param($array, $name, $default, $integer = false, $posted = false)
 */
function simulated_wildcard_match($context, $word, $full_cover = false)
{
    $word = str_replace('\\', '', $word); // Needed for our escaping manipulation to be robust

    $rexp = str_replace('%', '.*', str_replace('_', '.', str_replace('\\?', '.', str_replace('\\*', '.*', preg_quote($word)))));
    if ($full_cover) {
        $rexp = '^' . $rexp . '$';
+2 −0
Original line number Diff line number Diff line
@@ -962,6 +962,8 @@ function require_javascript($css)
 */
function simulated_wildcard_match($context, $word, $full_cover = false)
{
    $word = str_replace('\\', '', $word); // Needed for our escaping manipulation to be robust

    $rexp = str_replace('%', '.*', str_replace('_', '.', str_replace('\\?', '.', str_replace('\\*', '.*', preg_quote($word)))));
    if ($full_cover) {
        $rexp = '^' . $rexp . '$';
+1 −0
Original line number Diff line number Diff line
@@ -542,6 +542,7 @@ class Hook_addon_registry_testing_platform
            '_tests/tests/unit_tests/shopping.php',
            '_tests/tests/unit_tests/standard_dir_files.php',
            '_tests/tests/unit_tests/tempcode.php',
            '_tests/tests/unit_tests/simulated_wildcard_match.php',
            '_tests/tests/unit_tests/httpauth.php',
            '_tests/tests/unit_tests/template_previews.php',
            '_tests/tests/unit_tests/ticket_type.php',