1<?php
2
3class Test {
4    var $fails = array();
5    var $warnings = array();
6    var $name = "";
7
8    static $third_party_paths = array(
9        '/include/JSON.php',
10        '/include/htmLawed.php',
11        '/include/PasswordHash.php',
12        '/include/pear/',
13        '/include/Spyc.php',
14        '/setup/cli/stage/',
15        '/include/plugins/',
16        '/include/h2o/',
17        '/include/mpdf/',
18        '/js/select2.min.js',
19        '/js/redactor.min.js',
20        '/js/jquery-ui-1.12.1.custom.min.js',
21        '/js/fabric.min.js',
22
23        # Includes in the core-plugins project
24        '/lib/',
25    );
26
27    function __construct() {
28        assert_options(ASSERT_CALLBACK, array($this, 'fail'));
29        error_reporting(E_ALL & ~E_WARNING);
30    }
31
32    function setup() {
33    }
34
35    function teardown() {
36    }
37
38    static function ignore3rdparty() {
39        return true;
40    }
41
42    static function getAllScripts($pattern='*.php', $root=false, $excludes=true) {
43        $root = $root ?: get_osticket_root_path();
44        $excludes = $excludes ?: static::ignore3rdparty();
45        $scripts = array();
46        foreach (glob_recursive("$root/$pattern") as $s) {
47            $found = false;
48            if ($excludes) {
49                foreach (self::$third_party_paths as $p) {
50                    if (strpos($s, $p) !== false) {
51                        $found = true;
52                        break;
53                    }
54                }
55            }
56            if (!$found)
57                $scripts[] = $s;
58        }
59        return $scripts;
60    }
61
62    function fail($script, $line, $message) {
63        $this->fails[] = array(get_class($this), $script, $line, $message);
64        fputs(STDOUT, 'F');
65    }
66
67    function pass() {
68        fputs(STDOUT, ".");
69    }
70
71    function warn($message) {
72        $this->warnings[] = array(get_class($this), '', '', 'WARNING: '.$message);
73        fputs(STDOUT, 'w');
74    }
75
76    function assert($expr, $message=false) {
77        if ($expr)
78            $this->pass();
79        else
80            $this->fail('', '', $message ?: 'Test case failed');
81    }
82
83    function assertEqual($a, $b, $message=false) {
84        if (!$message)
85            $message = "Assertion: {$a} != {$b}";
86        return $this->assert($a == $b, $message);
87    }
88
89    function assertNotEqual($a, $b, $message=false) {
90        if (!$message)
91            $message = "Assertion: {$a} == {$b}";
92        return $this->assert($a != $b, $message);
93    }
94
95    function run() {
96        $rc = new ReflectionClass(get_class($this));
97        foreach ($rc->getMethods() as $m) {
98            if (stripos($m->name, 'test') === 0) {
99                $this->setup();
100                @call_user_func(array($this, $m->name));
101                $this->teardown();
102            }
103        }
104    }
105
106    function line_number_for_offset($file, $offset) {
107
108        if (is_file($file))
109            $content = file_get_contents($file, false, null, 0, $offset);
110        else
111            $content = @substr($file, 0, $offset);
112
113        return count(explode("\n", $content));
114    }
115}
116
117if (!function_exists('glob_recursive')) {
118    # Check PHP syntax across all php files
119    function glob_recursive($pattern, $flags = 0) {
120        $files = glob($pattern, $flags);
121        foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
122            $files = array_merge($files,
123                glob_recursive($dir.'/'.basename($pattern), $flags));
124        }
125        return $files;
126    }
127}
128
129if (!function_exists('get_osticket_root_path')) {
130    function get_osticket_root_path() {
131        # Hop up to the root folder
132        $start = dirname(__file__);
133        for (;;) {
134            if (file_exists($start . '/main.inc.php')) break;
135            $start .= '/..';
136        }
137        return realpath($start);
138    }
139}
140?>
141