public function testMostUsedWay() { $username = '******'; $password = '******'; $dtds['username'] = new LtValidatorDtd($_POST['username'], "username", array("max_length" => 8, "mask" => "/^[a-z0-9]+\$/i", "ban" => "/f**k/"), array("mask" => "用户名只能由数字或字组成", "ban" => "用户名不能包含脏话")); $dtds['password'] = new LtValidatorDtd($_POST['password'], "password", array("max_length" => 8, "mask" => "/^[a-z0-9]+\$/i", "ban" => "/f**k/"), array("max_length" => "密码最长8位", "mask" => "密码只能由数字或字组成")); // 配置文件 $config['validator.error_messages'] = array('ban' => '%s contain banned words', 'mask' => '%s does not match the given format', 'max_length' => '%s is longer than %s', 'min_length' => '%s is shorter than %s', 'max_value' => '%s is bigger than %s', 'min_value' => '%s is smaller than %s', 'max_selected' => '%s is too much', 'min_selected' => '%s is too few', 'required' => '%s is empty', 'equal_to' => '%s is not equal to %s'); $configHandle = new LtConfig(); $configHandle->addConfig($config); $validator = new LtValidator(); $validator->configHandle = $configHandle; $validator->init(); $dtd = $dtds['username']; foreach ($dtd->rules as $ruleKey => $ruleValue) { if ($ruleValue instanceof LtConfigExpression) { eval('$_ruleValue = ' . $ruleValue->__toString()); $dtd->rules[$ruleKey] = $ruleValue; } } $error_messages = $validator->validate($dtd); $this->assertEquals(array('max_length' => 'username is longer than 8', 'mask' => '用户名只能由数字或字组成', 'ban' => '用户名不能包含脏话'), $error_messages); $dtd = $dtds['password']; foreach ($dtd->rules as $ruleKey => $ruleValue) { if ($ruleValue instanceof LtConfigExpression) { eval('$_ruleValue = ' . $ruleValue->__toString()); $dtd->rules[$ruleKey] = $ruleValue; } } $error_messages = $validator->validate($dtd); $this->assertEquals(array('max_length' => '密码最长8位', 'mask' => '密码只能由数字或字组成', 'ban' => 'password contain banned words'), $error_messages); }
public function testPathinfo() { $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1'; $_SERVER["REQUEST_URI"] = '/index.php/default2/index2/id/123456/page/12/q-%FFkey/%E7%A9%BA%20-%FF%E6%A0%BC.html'; $_SERVER['SCRIPT_NAME'] = '/index.php'; $config['router.routing_table']['default'] = array('module' => 'default', 'action' => 'index'); $config['router.routing_table']['protocol'] = 'PATH_INFO'; $configHandle = new LtConfig(); $configHandle->addConfig($config); $router = new LtRouter(); $router->configHandle = $configHandle; $router->init(); $this->assertEquals('default2', $router->module); $this->assertEquals('index2', $router->action); $this->assertEquals(array('module' => 'default2', 'action' => 'index2', 'id' => '123456', 'page' => '12', 'q-/key' => '空 -/格'), $_GET); }
public function testRewrite() { $config['router.routing_table']['protocol'] = 'REWRITE'; $configHandle = new LtConfig(); $configHandle->addConfig($config); $url = new LtUrl(); $url->configHandle = $configHandle; $url->init(); $params = array('id' => 123456, 'page' => '12', 'q-/key' => '空 -/格'); $url->baseUrl = 'http://localhost'; $link = $url->generate('goods', 'detail', $params); $this->assertEquals('http://localhost/goods-detail-id-123456-page-12-q%FF/key-%E7%A9%BA%20%FF%2F%E6%A0%BC.html', $link); $url->baseUrl = 'http://127.0.0.1'; $link = $url->generate('goods', 'detail', $params); $this->assertEquals('http://127.0.0.1/goods-detail-id-123456-page-12-q%FF/key-%E7%A9%BA%20%FF%2F%E6%A0%BC.html', $link); }
public function testMostUsedWay() { // 角色 可以是多个 $roles = array('Administrators', 'Users'); $roles = array_merge(array("*"), $roles); // 访问控制列表 deny优先 $acl['allow']['*'][] = 'Index/Index'; $acl['deny']['*'][] = ''; $acl['allow']['Administrators'][] = 'admin/*'; $acl['allow']['Administrators'][] = 'User/*'; $acl['allow']['Users'][] = 'User/View'; $acl['allow']['Users'][] = 'User/Signin'; $acl['allow']['Users'][] = 'User/DoSignin'; $acl['deny']['Users'][] = 'User/AddUser'; $configHandle = new LtConfig(); $configHandle->addConfig(array('rbac.acl' => $acl)); $rbac = new LtRbac(); $rbac->configHandle = $configHandle; $rbac->init(); $this->assertTrue($rbac->checkAcl($roles, 'admin/test')); $this->assertFalse($rbac->checkAcl($roles, 'User/AddUser')); }
* 加载MVC类文件 加载的类很多,且需要注意先后顺序,推荐使用LtAutoloader自动加载 */ $lotusHome = substr(__FILE__, 0, strpos(__FILE__, "example")) . '/'; include $lotusHome . "runtime/Config.php"; include $lotusHome . "runtime/Store.php"; include $lotusHome . "runtime/StoreMemory.php"; include $lotusHome . "runtime/ObjectUtil/ObjectUtil.php"; include $lotusHome . "runtime/MVC/Dispatcher.php"; include $lotusHome . "runtime/MVC/Action.php"; include $lotusHome . "runtime/MVC/Component.php"; include $lotusHome . "runtime/MVC/Context.php"; include $lotusHome . "runtime/MVC/View.php"; include $lotusHome . "runtime/Validator/Validator.php"; include $lotusHome . "runtime/Validator/ValidatorDtd.php"; // 配置文件 $config['validator.error_messages'] = array('ban' => '%s contain banned words', 'mask' => '%s does not match the given format', 'max_length' => '%s is longer than %s', 'min_length' => '%s is shorter than %s', 'max_value' => '%s is bigger than %s', 'min_value' => '%s is smaller than %s', 'max_selected' => '%s is too much', 'min_selected' => '%s is too few', 'required' => '%s is empty', 'equal_to' => '%s is not equal to %s'); $configHandle = new LtConfig(); $configHandle->addConfig($config); /** * 加载Action类文件 */ $appDir = "./simplest_app/"; include $appDir . "action/User-Signin.Action.php"; /** * 实例化 */ $dispatcher = LtObjectUtil::singleton('LtDispatcher'); $dispatcher->configHandle = $configHandle; $dispatcher->viewDir = "./simplest_app/view/"; $dispatcher->dispatchAction("User", "Signin");