1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Unit tests for xhprof.
19 *
20 * @package   core
21 * @copyright 2019 Brendan Heywood <brendan@catalyst-au.net>
22 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5)
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27/**
28 * Unit tests for the xhprof class.
29 *
30 * @copyright 2019 Brendan Heywood <brendan@catalyst-au.net>
31 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 */
33class core_xhprof_testcase extends advanced_testcase {
34
35    /**
36     * Data provider for string matches
37     *
38     * @return  array
39     */
40    public function profiling_string_matches_provider() {
41        return [
42            ['/index.php',              '/index.php',           true],
43            ['/some/dir/index.php',     '/index.php',           false],
44            ['/course/view.php',        '/course/view.php',     true],
45            ['/view.php',               '/course/view.php',     false],
46            ['/mod/forum',              '/mod/forum/*',         false],
47            ['/mod/forum/',             '/mod/forum/*',         true],
48            ['/mod/forum/index.php',    '/mod/forum/*',         true],
49            ['/mod/forum/foo.php',      '/mod/forum/*',         true],
50            ['/mod/forum/view.php',     '/mod/*/view.php',      true],
51            ['/mod/one/two/view.php',   '/mod/*/view.php',      true],
52            ['/view.php',               '*/view.php',           true],
53            ['/mod/one/two/view.php',   '*/view.php',           true],
54            ['/foo.php',                '/foo.php,/bar.php',    true],
55            ['/bar.php',                '/foo.php,/bar.php',    true],
56            ['/foo/bar.php',            "/foo.php,/bar.php",    false],
57            ['/foo/bar.php',            "/foo.php,*/bar.php",   true],
58            ['/foo/bar.php',            "/foo*.php,/bar.php",   true],
59            ['/foo.php',                "/foo.php\n/bar.php",   true],
60            ['/bar.php',                "/foo.php\n/bar.php",   true],
61            ['/foo/bar.php',            "/foo.php\n/bar.php",   false],
62            ['/foo/bar.php',            "/foo.php\n*/bar.php",  true],
63            ['/foo/bar.php',            "/foo*.php\n/bar.php",  true],
64        ];
65    }
66
67    /**
68     * Test the matching syntax
69     *
70     * @dataProvider profiling_string_matches_provider
71     * @param   string $string
72     * @param   string $patterns
73     * @param   bool   $expected
74     */
75    public function test_profiling_string_matches($string, $patterns, $expected) {
76
77        global $CFG;
78        require_once($CFG->libdir . '/xhprof/xhprof_moodle.php');
79
80        $result = profiling_string_matches($string, $patterns);
81        $this->assertSame($result, $expected);
82    }
83
84}
85
86