File size: 6,341 Bytes
e4f4821 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
<?php
require_once __DIR__.'/../../Base.php';
use Kanboard\Core\Http\Route;
use Kanboard\Core\Http\Router;
use Kanboard\Core\Http\Request;
class RouterTest extends Base
{
public function testSanitize()
{
$dispatcher = new Router($this->container);
$this->assertEquals('PloP', $dispatcher->sanitize('PloP', 'default'));
$this->assertEquals('default', $dispatcher->sanitize('', 'default'));
$this->assertEquals('default', $dispatcher->sanitize('123-AB', 'default'));
$this->assertEquals('default', $dispatcher->sanitize('R&D', 'default'));
$this->assertEquals('Test123', $dispatcher->sanitize('Test123', 'default'));
$this->assertEquals('Test_123', $dispatcher->sanitize('Test_123', 'default'));
$this->assertEquals('userImport', $dispatcher->sanitize('userImport', 'default'));
}
public function testGetPathWithFolder()
{
$router = new Router($this->container);
$this->container['request'] = new Request($this->container, array('PHP_SELF' => '/index.php', 'REQUEST_URI' => '/a/b/c', 'REQUEST_METHOD' => 'GET'));
$this->assertEquals('a/b/c', $router->getPath());
}
public function testGetPathWithQueryString()
{
$router = new Router($this->container);
$this->container['request'] = new Request($this->container, array('PHP_SELF' => '/index.php', 'REQUEST_URI' => '/a/b/something?test=a', 'QUERY_STRING' => 'test=a', 'REQUEST_METHOD' => 'GET'));
$this->assertEquals('a/b/something', $router->getPath());
}
public function testGetPathWithSubFolderAndQueryString()
{
$router = new Router($this->container);
$this->container['request'] = new Request($this->container, array('PHP_SELF' => '/a/index.php', 'REQUEST_URI' => '/a/b/something?test=a', 'QUERY_STRING' => 'test=a', 'REQUEST_METHOD' => 'GET'));
$this->assertEquals('b/something', $router->getPath());
}
public function testDispatcherWithNoUrlRewrite()
{
$this->container['request'] = new Request($this->container, array(
'PHP_SELF' => '/kanboard/index.php',
'REQUEST_URI' => '/kanboard/?controller=FakeController&action=myAction&myvar=value1',
'QUERY_STRING' => 'controller=FakeController&action=myAction&myvar=value1',
'REQUEST_METHOD' => 'GET'
),
array(
'controller' => 'FakeController',
'action' => 'myAction',
'myvar' => 'value1',
)
);
$dispatcher = new Router($this->container);
$dispatcher->dispatch();
$this->assertEquals('FakeController', $dispatcher->getController());
$this->assertEquals('myAction', $dispatcher->getAction());
$this->assertEquals('', $dispatcher->getPlugin());
$this->assertEquals('value1', $this->container['request']->getStringParam('myvar'));
}
public function testDispatcherWithNoUrlRewriteAndPlugin()
{
$this->container['request'] = new Request($this->container, array(
'PHP_SELF' => '/kanboard/index.php',
'REQUEST_URI' => '/kanboard/?controller=FakeController&action=myAction&myvar=value1&plugin=myplugin',
'QUERY_STRING' => 'controller=FakeController&action=myAction&myvar=value1&plugin=myplugin',
'REQUEST_METHOD' => 'GET'
),
array(
'controller' => 'FakeController',
'action' => 'myAction',
'myvar' => 'value1',
'plugin' => 'myplugin',
)
);
$dispatcher = new Router($this->container);
$dispatcher->dispatch();
$this->assertEquals('FakeController', $dispatcher->getController());
$this->assertEquals('myAction', $dispatcher->getAction());
$this->assertEquals('Myplugin', $dispatcher->getPlugin());
$this->assertEquals('value1', $this->container['request']->getStringParam('myvar'));
}
public function testDispatcherWithUrlRewrite()
{
$this->container['request'] = new Request($this->container, array(
'PHP_SELF' => '/kanboard/index.php',
'REQUEST_URI' => '/kanboard/my/route/123?myvar=value1',
'QUERY_STRING' => 'myvar=value1',
'REQUEST_METHOD' => 'GET'
),
array(
'myvar' => 'value1',
)
);
$this->container['route'] = new Route($this->container);
$this->container['route']->enable();
$this->container['route']->addRoute('/my/route/:param', 'FakeController', 'myAction');
$dispatcher = new Router($this->container);
$dispatcher->dispatch();
$this->assertEquals('FakeController', $dispatcher->getController());
$this->assertEquals('myAction', $dispatcher->getAction());
$this->assertEquals('', $dispatcher->getPlugin());
$this->assertEquals('value1', $this->container['request']->getStringParam('myvar'));
$this->assertEquals('123', $this->container['request']->getStringParam('param'));
}
public function testDispatcherWithUrlRewriteWithPlugin()
{
$this->container['request'] = new Request($this->container, array(
'PHP_SELF' => '/kanboard/index.php',
'REQUEST_URI' => '/kanboard/my/plugin/route/123?myvar=value1',
'QUERY_STRING' => 'myvar=value1',
'REQUEST_METHOD' => 'GET'
),
array(
'myvar' => 'value1',
)
);
$this->container['route'] = new Route($this->container);
$this->container['route']->enable();
$this->container['route']->addRoute('/my/plugin/route/:param', 'fakeController', 'myAction', 'Myplugin');
$dispatcher = new Router($this->container);
$dispatcher->dispatch();
$this->assertEquals('FakeController', $dispatcher->getController());
$this->assertEquals('myAction', $dispatcher->getAction());
$this->assertEquals('Myplugin', $dispatcher->getPlugin());
$this->assertEquals('value1', $this->container['request']->getStringParam('myvar'));
$this->assertEquals('123', $this->container['request']->getStringParam('param'));
}
}
|