| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2011 Polytechnique.org * |
| 4 | * http://opensource.polytechnique.org/ * |
| 5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the Free Software * |
| 18 | * Foundation, Inc., * |
| 19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | require_once dirname(__FILE__) . '/../include/test.inc.php'; |
| 23 | |
| 24 | class TestPlatal extends Platal |
| 25 | { |
| 26 | public function force_login(PlPage $page) { |
| 27 | throw new Exception('Force login called in a test'); |
| 28 | } |
| 29 | } |
| 30 | class TestSession extends PlSession |
| 31 | { |
| 32 | protected function doAuth($level) { |
| 33 | throw new Exception('Not implemented test method'); |
| 34 | } |
| 35 | |
| 36 | protected function makePerms($perms, $is_admin) { |
| 37 | throw new Exception('Not implemented test method'); |
| 38 | } |
| 39 | |
| 40 | protected function startSessionAs($user, $level) { |
| 41 | throw new Exception('Not implemented test method'); |
| 42 | } |
| 43 | |
| 44 | public function loggedLevel() { |
| 45 | return AUTH_MDP; |
| 46 | } |
| 47 | |
| 48 | public function startAvailableAuth() { |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | public function sureLevel() { |
| 53 | return AUTH_MDP; |
| 54 | } |
| 55 | |
| 56 | public function tokenAuth($login, $token) { |
| 57 | throw new Exception('Not implemented test method'); |
| 58 | } |
| 59 | } |
| 60 | define('PL_GLOBALS_CLASS', 'PlGlobals'); |
| 61 | define('PL_SESSION_CLASS', 'TestSession'); |
| 62 | define('PL_PAGE_CLASS', 'PlPage'); |
| 63 | define('PL_LOGGER_CLASS', 'PlLogger'); |
| 64 | new TestPlatal(); |
| 65 | |
| 66 | class TestPage extends PlPage |
| 67 | { |
| 68 | public function run() { |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | class EngineTest extends PlTestCase |
| 73 | { |
| 74 | public static function blahCallback(PlPage $page) |
| 75 | { |
| 76 | return 'blah'; |
| 77 | } |
| 78 | |
| 79 | public static function blihCallback(PlPage $page) |
| 80 | { |
| 81 | $args = func_get_args(); |
| 82 | array_shift($args); |
| 83 | return 'blih-' . implode('-', $args); |
| 84 | } |
| 85 | |
| 86 | public static function fooCallback(PlPage $page, $arg1 = null) |
| 87 | { |
| 88 | if (is_null($arg1)) { |
| 89 | return 'foo'; |
| 90 | } else { |
| 91 | return 'foo-' . $arg1; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | public static function barCallback(PlPage $page, $arg1 = null, $arg2 = null) |
| 96 | { |
| 97 | if (is_null($arg1)) { |
| 98 | return 'bar'; |
| 99 | } else if (is_null($arg2)) { |
| 100 | return 'bar-' . $arg1; |
| 101 | } else { |
| 102 | return 'bar-' . $arg1 . '-' . $arg2; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | |
| 107 | public static function hookProvider() |
| 108 | { |
| 109 | return array( |
| 110 | array(PL_WIKI, new PlWikiHook(), array('a')), |
| 111 | array(PL_WIKI, new PlWikiHook(), array('a', 'b')), |
| 112 | array(PL_WIKI, new PlWikiHook(), array('a', 'b', 'c')), |
| 113 | |
| 114 | array('blah', new PlStdHook(array('EngineTest', 'blahCallback')), array('a')), |
| 115 | array('blah', new PlStdHook(array('EngineTest', 'blahCallback')), array('a', 'b')), |
| 116 | array('blah', new PlStdHook(array('EngineTest', 'blahCallback')), array('a', 'b', 'c')), |
| 117 | |
| 118 | array('blih-', new PlStdHook(array('EngineTest', 'blihCallback')), array('a')), |
| 119 | array('blih-b', new PlStdHook(array('EngineTest', 'blihCallback')), array('a', 'b')), |
| 120 | array('blih-b-c', new PlStdHook(array('EngineTest', 'blihCallback')), array('a', 'b', 'c')), |
| 121 | |
| 122 | array('foo', new PlStdHook(array('EngineTest', 'fooCallback')), array('a')), |
| 123 | array('foo-b', new PlStdHook(array('EngineTest', 'fooCallback')), array('a', 'b')), |
| 124 | array('foo-b', new PlStdHook(array('EngineTest', 'fooCallback')), array('a', 'b', 'c')), |
| 125 | |
| 126 | array('bar', new PlStdHook(array('EngineTest', 'barCallback')), array('a')), |
| 127 | array('bar-b', new PlStdHook(array('EngineTest', 'barCallback')), array('a', 'b')), |
| 128 | array('bar-b-c', new PlStdHook(array('EngineTest', 'barCallback')), array('a', 'b', 'c')), |
| 129 | ); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * @dataProvider hookProvider |
| 134 | */ |
| 135 | public function testHook($res, $hook, $args) |
| 136 | { |
| 137 | $page = new TestPage(); |
| 138 | $this->assertEquals($res, $hook->call($page, $args)); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | public static function dispatchProvider() |
| 143 | { |
| 144 | return array( |
| 145 | array('blih-', 'test', 'test'), |
| 146 | array('blih-', 'test', 'test/'), |
| 147 | array('blih-machin', 'test', 'test/machin'), |
| 148 | array('blih-machin-truc', 'test', 'test/machin/truc'), |
| 149 | |
| 150 | array('blih-hiboo', 'test', 'test/hiboo'), |
| 151 | array('foo', 'test/coucou', 'test/coucou'), |
| 152 | array('foo-', 'test/coucou', 'test/coucou/'), |
| 153 | array('foo-blah', 'test/coucou', 'test/coucou/blah'), |
| 154 | array('foo-blah', 'test/coucou', 'test/coucou/blah/truc') |
| 155 | ); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * @dataProvider dispatchProvider |
| 160 | */ |
| 161 | public function testDispatch($res, $expmatched, $path) |
| 162 | { |
| 163 | $tree = new PlHookTree(); |
| 164 | $tree->addChildren(array( |
| 165 | 'test' => new PlStdHook(array('EngineTest', 'blihCallback')), |
| 166 | 'test1' => new PlStdHook(array('EngineTest', 'blahCallback')), |
| 167 | 'tes' => new PlStdHook(array('EngineTest', 'blahCallback')), |
| 168 | 'test/coucou' => new PlStdHook(array('EngineTest', 'fooCallback')), |
| 169 | 'test/hook' => new PlStdHook(array('EngineTest', 'barCallback')) |
| 170 | )); |
| 171 | |
| 172 | $page = new TestPage(); |
| 173 | $p = explode('/', $path); |
| 174 | list($hook, $matched, $remain, $aliased) = $tree->findChild($p); |
| 175 | $matched = join('/', $matched); |
| 176 | $this->assertEquals($expmatched, $matched); |
| 177 | array_unshift($remain, $matched); |
| 178 | $this->assertEquals($res, $hook->call($page, $remain)); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8: |
| 183 | ?> |