function __construct() { parent::Controller(); $this->load->library('unit_test'); $this->load->helper('unit_test'); $this->load->library('type/Type'); create_context(TRUE); }
/** * 預先載入相關的library * * 如果還有用到其他的library,請在此載入 */ function __construct() { parent::Controller(); $this->load->helper('url'); $this->load->helper('web_apps'); $this->load->config('kals'); create_context(); }
function Ut_webpage() { parent::Controller(); $this->load->library('unit_test'); $this->load->helper('unit_test'); $this->load->library('kals_resource/Webpage'); create_context(TRUE); }
function ut_scope_anchor_text() { parent::Controller(); $this->load->library('unit_test'); $this->load->helper('unit_test'); $this->load->library('scope/Scope_anchor_text'); create_context(TRUE); }
function Ut_user() { parent::Controller(); $this->load->library('unit_test'); $this->load->helper('unit_test'); $this->load->library('kals_actor/User'); $this->load->database(); create_context(TRUE); }
function __construct() { parent::Controller(); $this->load->library('unit_test'); $this->load->helper('unit_test'); $this->load->library('type/Annotation_type_factory'); $this->factory = $this->annotation_type_factory; create_context(TRUE); }
function __construct() { parent::Controller(); $this->load->library('unit_test'); $this->load->helper('unit_test'); $this->load->library('kals_actor/User'); create_context(TRUE); $this->unit->set_benchmark('Construc Complete'); }
function __construct() { parent::Controller(); $this->load->library('unit_test'); $this->load->helper('unit_test'); $this->load->library('fuzzy/Clustering'); create_context(TRUE); $this->unit->set_benchmark('Construc Complete'); }
function __construct() { parent::Controller(); $this->load->library('unit_test'); $this->load->helper('unit_test'); $this->load->library('kals_resource/Annotation'); $this->load->library('recommend/Annotation_recommend'); create_context(TRUE); $this->unit->set_benchmark('Construc Complete'); }
function __construct() { parent::Controller(); $this->load->library('unit_test'); $this->load->helper('unit_test'); $this->load->library('fuzzy/Output_language_variable_collection'); $this->coll = $this->output_language_variable_collection; create_context(TRUE); $this->unit->set_benchmark('Construc Complete'); }
function __construct() { parent::Controller(); $this->load->library('unit_test'); $this->load->helper('unit_test'); $this->load->library('scope/Annotation_scope'); $this->load->library('scope/Annotation_scope_collection'); $this->scope = $this->annotation_scope; $this->coll = $this->annotation_scope_collection; create_context(TRUE); $this->unit->set_benchmark('First Unit Test'); }
function __construct() { parent::Controller(); $this->load->library('unit_test'); $this->load->helper('unit_test'); $this->load->library('scope/Annotation_scope_collection'); $this->load->library('scope/Annotation_scope'); $this->load->library('kals_resource/Annotation'); $this->load->library('kals_actor/User'); create_context(TRUE); //set_ignore_authorize(TRUE); $this->unit->set_benchmark('Construc Complete', 47); }
function __construct() { parent::Controller(); $this->load->helper('url'); $this->load->helper('web_apps'); $this->load->config('kals'); if ($this->controller_enable_cache) { $this->_enable_cache(); } create_context(); if ($this->login_require === TRUE) { login_require(true); } }
function index() { //log區 $array_data = array('ip' => get_client_ip(), 'browser' => $_SERVER['HTTP_USER_AGENT'], 'request_uri' => $_SERVER["REQUEST_URI"]); create_context(); $user = get_context_user(); $user_id = NULL; if (isset($user)) { $user_id = $user->get_id(); } $action = 28; kals_log($this->db, $action, array('memo' => $array_data, 'user_id' => $user_id)); $this->load->view('help/index'); }
function index() { // $url = 'http://www.lib.nccu.edu.tw/?m=1109&sn=54&id=92&mm=1101'; // $host = 'http://www.lib.nccu.edu.tw/'; // $title = '政治大學圖書館網站'; $url = 'http://pulipuli.blogspot.com/2010/07/2010711.html'; $host = 'http://pulipuli.blogspot.com/'; $title = '布丁布丁吃?'; //以下才是正是的測試開始 create_context(TRUE); $this->load->library('kals_resource/Domain'); $domain2 = new Domain(); $domain2->set_field('url', $url); $domain2->update(); $this->unit->run($domain2->get_field("url"), $host, '以new建立domain2,測試get_field("url")'); $domain = $this->domain->create($url); $domain_id = $domain->get_id(); $this->unit->run($domain_id, 'is_int', '用ctreate()來建立Domain,看看是否有id'); $test_domain = $this->domain->create($url); $test_domain_id = $test_domain->get_id(); //echo $test_domain_id; $this->unit->run($test_domain->get_field("url"), $host, '測試get_host'); $this->unit->run($domain_id != NULL, TRUE, '測試test_domain是不是真的find到domain'); $this->unit->run($domain_id, $test_domain_id, '測試test_domain是不是真的find到domain'); $this->unit->run($test_domain->get_id(), $domain_id, '用ctreate()以同樣的url來建立Domain,看看是否有id'); $this->unit->run($test_domain->get_field('title'), $title, '再建立時,是否就能找到title?'); $id = $test_domain->get_id(); $test_domain = new Domain($id); $this->unit->run($test_domain->get_id() != NULL, TRUE, '測試用id來find Domain'); $this->unit->run($test_domain->get_field('url'), $host, '測試用id來find Domain 測試get_host'); $this->unit->run($test_domain->get_field('title'), $title, '測試title'); $domains = $this->domain->find_all(); $this->unit->run(count($domains) > 0, TRUE, '測試find_all是否有找到東西'); $domains = $this->domain->find_all(array('domain_id' => $id)); $this->unit->run(count($domains), 1, '測試find_all加上條件,是否能找到測試時建立的domain'); $this->unit->run($domains[0]->get_id(), $id, '測試find_all中能不能get_id()'); $this->unit->run($domains[0]->get_host(), $host, '測試find_all中能不能get_host()'); $test_domain->delete(); $this->unit->run($test_domain->get_id(), NULL, '測試刪除之後,Domain ID還在不在這件事情'); $domain_deleted = $this->domain->find($url); $this->unit->run($domain_deleted, NULL, '測試刪除之後,Domain還能不能找到這件事情'); //context_complete(); //context_abort(); unit_test_report($this); //destory_context(); }
function __construct() { parent::Controller(); $this->load->library('unit_test'); $this->load->helper('unit_test'); $this->load->library('recommend/Tip'); $this->load->library('recommend/Tip_speech'); $this->load->library('recommend/Tip_location'); $this->load->library('recommend/Tip_length'); $this->load->library('recommend/Tip_collection'); $this->load->library('kals_resource/Annotation'); $this->load->library('kals_resource/Webpage'); $this->load->library('kals_actor/User'); $this->load->library('scope/Annotation_scope_collection'); $this->load->library('scope/Annotation_scope'); create_context(TRUE); $this->unit->set_benchmark('Construc Complete'); }
function __construct() { parent::Controller(); $this->load->library('kals_actor/User'); $this->load->helper('url'); $this->load->helper('web_apps'); $this->load->config('kals'); if ($this->controller_enable_cache) { $this->_enable_cache(); } create_context(); if ($this->login_require === TRUE) { login_require(true); } $this->lang->load('kals_mobile_apps'); $this->lang->load('kals_web_apps'); $this->client_ip = array('ip' => get_client_ip(), 'browser' => $_SERVER['HTTP_USER_AGENT']); }
function __construct() { parent::Controller(); $this->load->library('unit_test'); $this->load->helper('unit_test'); $this->load->library('kals_actor/Notification'); $this->load->library('kals_actor/Notification_responded'); $this->responded = $this->notification_responded; $this->load->library('kals_actor/Notification_liked'); $this->liked = $this->notification_liked; $this->load->library('kals_actor/Notification_recommended'); $this->recommended = $this->notification_recommended; $this->load->library('scope/Annotation_scope_collection'); $this->load->library('scope/Annotation_scope'); $this->load->library('kals_resource/Annotation'); $this->load->library('kals_actor/User'); create_context(TRUE); $this->unit->set_benchmark('Construc Complete'); }
function __construct() { parent::Controller(); $this->load->library('unit_test'); $this->load->helper('unit_test'); $this->load->library('fuzzy/Language_variable'); $this->load->library('fuzzy/Language_variable_consensus'); $this->load->library('fuzzy/Language_variable_length'); $this->load->library('fuzzy/Language_variable_like'); $this->load->library('fuzzy/Language_variable_location'); $this->load->library('fuzzy/Language_variable_speech'); $this->load->library('fuzzy/Language_variable_type'); $this->load->library('fuzzy/Annotation_score_calculator'); $this->load->library('kals_resource/Annotation'); $this->load->library('kals_resource/Webpage'); $this->load->library('kals_actor/User'); $this->load->library('scope/Annotation_scope_collection'); $this->load->library('scope/Annotation_scope'); create_context(TRUE); $this->unit->set_benchmark('Construc Complete'); }
/** * Get the context instance as an object. This function will create the * context instance if it does not exist yet. * @param integer $level The context level, for example CONTEXT_COURSE, or CONTEXT_MODULE. * @param integer $instance The instance id. For $level = CONTEXT_COURSE, this would be $course->id, * for $level = CONTEXT_MODULE, this would be $cm->id. And so on. * @return object The context object. */ function get_context_instance($contextlevel = NULL, $instance = 0) { global $context_cache, $context_cache_id, $CONTEXT; static $allowed_contexts = array(CONTEXT_SYSTEM, CONTEXT_PERSONAL, CONTEXT_USER, CONTEXT_COURSECAT, CONTEXT_COURSE, CONTEXT_GROUP, CONTEXT_MODULE, CONTEXT_BLOCK); // Yu: Separating site and site course context - removed CONTEXT_COURSE override when SITEID // fix for MDL-9016 if ($contextlevel == 'clearcache') { // Clear ALL cache $context_cache = array(); $context_cache_id = array(); $CONTEXT = ''; return false; } /// If no level is supplied then return the current global context if there is one if (empty($contextlevel)) { if (empty($CONTEXT)) { //fatal error, code must be fixed error("Error: get_context_instance() called without a context"); } else { return $CONTEXT; } } /// Backwards compatibility with obsoleted (CONTEXT_SYSTEM, SITEID) if ($contextlevel == CONTEXT_SYSTEM) { $instance = 0; } /// check allowed context levels if (!in_array($contextlevel, $allowed_contexts)) { // fatal error, code must be fixed - probably typo or switched parameters error('Error: get_context_instance() called with incorrect context level "' . s($contextlevel) . '"'); } /// Check the cache if (isset($context_cache[$contextlevel][$instance])) { // Already cached return $context_cache[$contextlevel][$instance]; } /// Get it from the database, or create it if (!($context = get_record('context', 'contextlevel', $contextlevel, 'instanceid', $instance))) { create_context($contextlevel, $instance); $context = get_record('context', 'contextlevel', $contextlevel, 'instanceid', $instance); } /// Only add to cache if context isn't empty. if (!empty($context)) { $context_cache[$contextlevel][$instance] = $context; // Cache it for later $context_cache_id[$context->id] = $context; // Cache it for later } return $context; }
/** * Get the context instance as an object. This function will create the * context instance if it does not exist yet. * @param integer $level The context level, for example CONTEXT_COURSE, or CONTEXT_MODULE. * @param integer $instance The instance id. For $level = CONTEXT_COURSE, this would be $course->id, * for $level = CONTEXT_MODULE, this would be $cm->id. And so on. * @return object The context object. */ function get_context_instance($contextlevel, $instance = 0) { global $DB, $ACCESSLIB_PRIVATE; static $allowed_contexts = array(CONTEXT_SYSTEM, CONTEXT_USER, CONTEXT_COURSECAT, CONTEXT_COURSE, CONTEXT_MODULE, CONTEXT_BLOCK); if ($contextlevel === 'clearcache') { // TODO: Remove for v2.0 // No longer needed, but we'll catch it to avoid erroring out on custom code. // This used to be a fix for MDL-9016 // "Restoring into existing course, deleting first // deletes context and doesn't recreate it" return false; } /// System context has special cache if ($contextlevel == CONTEXT_SYSTEM) { return get_system_context(); } /// check allowed context levels if (!in_array($contextlevel, $allowed_contexts)) { // fatal error, code must be fixed - probably typo or switched parameters print_error('invalidcourselevel'); } if (!is_array($instance)) { /// Check the cache if (isset($ACCESSLIB_PRIVATE->contexts[$contextlevel][$instance])) { // Already cached return $ACCESSLIB_PRIVATE->contexts[$contextlevel][$instance]; } /// Get it from the database, or create it if (!($context = $DB->get_record('context', array('contextlevel' => $contextlevel, 'instanceid' => $instance)))) { $context = create_context($contextlevel, $instance); } /// Only add to cache if context isn't empty. if (!empty($context)) { cache_context($context); } return $context; } /// ok, somebody wants to load several contexts to save some db queries ;-) $instances = $instance; $result = array(); foreach ($instances as $key => $instance) { /// Check the cache first if (isset($ACCESSLIB_PRIVATE->contexts[$contextlevel][$instance])) { // Already cached $result[$instance] = $ACCESSLIB_PRIVATE->contexts[$contextlevel][$instance]; unset($instances[$key]); continue; } } if ($instances) { list($instanceids, $params) = $DB->get_in_or_equal($instances, SQL_PARAMS_QM); array_unshift($params, $contextlevel); $sql = "SELECT instanceid, id, contextlevel, path, depth\n FROM {context}\n WHERE contextlevel=? AND instanceid {$instanceids}"; if (!($contexts = $DB->get_records_sql($sql, $params))) { $contexts = array(); } foreach ($instances as $instance) { if (isset($contexts[$instance])) { $context = $contexts[$instance]; } else { $context = create_context($contextlevel, $instance); } if (!empty($context)) { cache_context($context); } $result[$instance] = $context; } } return $result; }
/** * Get the context instance as an object. This function will create the * context instance if it does not exist yet. * @param integer $level The context level, for example CONTEXT_COURSE, or CONTEXT_MODULE. * @param integer $instance The instance id. For $level = CONTEXT_COURSE, this would be $course->id, * for $level = CONTEXT_MODULE, this would be $cm->id. And so on. * @return object The context object. */ function get_context_instance($contextlevel, $instance = 0) { global $context_cache, $context_cache_id, $CFG; static $allowed_contexts = array(CONTEXT_SYSTEM, CONTEXT_USER, CONTEXT_COURSECAT, CONTEXT_COURSE, CONTEXT_MODULE, CONTEXT_BLOCK); if ($contextlevel === 'clearcache') { // TODO: Remove for v2.0 // No longer needed, but we'll catch it to avoid erroring out on custom code. // This used to be a fix for MDL-9016 // "Restoring into existing course, deleting first // deletes context and doesn't recreate it" return false; } /// System context has special cache if ($contextlevel == CONTEXT_SYSTEM) { return get_system_context(); } /// check allowed context levels if (!in_array($contextlevel, $allowed_contexts)) { // fatal error, code must be fixed - probably typo or switched parameters error('Error: get_context_instance() called with incorrect context level "' . s($contextlevel) . '"'); } if (!is_array($instance)) { /// Check the cache if (isset($context_cache[$contextlevel][$instance])) { // Already cached return $context_cache[$contextlevel][$instance]; } /// Get it from the database, or create it if (!($context = get_record('context', 'contextlevel', $contextlevel, 'instanceid', $instance))) { $context = create_context($contextlevel, $instance); } /// Only add to cache if context isn't empty. if (!empty($context)) { cache_context($context); } return $context; } /// ok, somebody wants to load several contexts to save some db queries ;-) $instances = $instance; $result = array(); foreach ($instances as $key => $instance) { /// Check the cache first if (isset($context_cache[$contextlevel][$instance])) { // Already cached $result[$instance] = $context_cache[$contextlevel][$instance]; unset($instances[$key]); continue; } } if ($instances) { if (count($instances) > 1) { $instanceids = implode(',', $instances); $instanceids = "instanceid IN ({$instanceids})"; } else { $instance = reset($instances); $instanceids = "instanceid = {$instance}"; } if (!($contexts = get_records_sql("SELECT instanceid, id, contextlevel, path, depth\n FROM {$CFG->prefix}context\n WHERE contextlevel={$contextlevel} AND {$instanceids}"))) { $contexts = array(); } foreach ($instances as $instance) { if (isset($contexts[$instance])) { $context = $contexts[$instance]; } else { $context = create_context($contextlevel, $instance); } if (!empty($context)) { cache_context($context); } $result[$instance] = $context; } } return $result; }
/** * Get the context instance as an object. This function will create the * context instance if it does not exist yet. * * @todo Remove code branch from previous fix MDL-9016 which is no longer needed * * @param integer $level The context level, for example CONTEXT_COURSE, or CONTEXT_MODULE. * @param integer $instance The instance id. For $level = CONTEXT_COURSE, this would be $course->id, * for $level = CONTEXT_MODULE, this would be $cm->id. And so on. Defaults to 0 * @param int $strictness IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found; * MUST_EXIST means throw exception if no record or multiple records found * @return object The context object. */ function get_context_instance($contextlevel, $instance = 0, $strictness = IGNORE_MISSING) { global $DB, $ACCESSLIB_PRIVATE; static $allowed_contexts = array(CONTEXT_SYSTEM, CONTEXT_USER, CONTEXT_COURSECAT, CONTEXT_COURSE, CONTEXT_MODULE, CONTEXT_BLOCK); /// System context has special cache if ($contextlevel == CONTEXT_SYSTEM) { return get_system_context(); } /// check allowed context levels if (!in_array($contextlevel, $allowed_contexts)) { // fatal error, code must be fixed - probably typo or switched parameters print_error('invalidcourselevel'); } // Various operations rely on context cache $cache = $ACCESSLIB_PRIVATE->contexcache; if (!is_array($instance)) { /// Check the cache $context = $cache->get($contextlevel, $instance); if ($context) { return $context; } /// Get it from the database, or create it if (!($context = $DB->get_record('context', array('contextlevel' => $contextlevel, 'instanceid' => $instance)))) { $context = create_context($contextlevel, $instance, $strictness); } /// Only add to cache if context isn't empty. if (!empty($context)) { $cache->add($context); } return $context; } /// ok, somebody wants to load several contexts to save some db queries ;-) $instances = $instance; $result = array(); foreach ($instances as $key => $instance) { /// Check the cache first if ($context = $cache->get($contextlevel, $instance)) { // Already cached $result[$instance] = $context; unset($instances[$key]); continue; } } if ($instances) { list($instanceids, $params) = $DB->get_in_or_equal($instances, SQL_PARAMS_QM); array_unshift($params, $contextlevel); $sql = "SELECT instanceid, id, contextlevel, path, depth\n FROM {context}\n WHERE contextlevel=? AND instanceid {$instanceids}"; if (!($contexts = $DB->get_records_sql($sql, $params))) { $contexts = array(); } foreach ($instances as $instance) { if (isset($contexts[$instance])) { $context = $contexts[$instance]; } else { $context = create_context($contextlevel, $instance); } if (!empty($context)) { $cache->add($context); } $result[$instance] = $context; } } return $result; }
function index() { create_context(TRUE); $del_user = array(); //建立$group1 $group1 = $this->group->create_group($this->url, $this->group_name1); //[R]$group1->get_name() $this->unit->run($group1->get_name(), $this->group_name1, '建立$group1 [R]$group1->get_name()'); //$group1find = find by name $group1finds = $this->group->find_groups($this->url, $this->group_name1); $group1find = $group1finds[0]; //$group1find->get_domain() $domain1 = $group1find->get_domain(); //[R]取得domain的host $this->unit->run($domain1->get_host(), $this->host, '$group1find = find by name get_domain() [R]取得domain的host'); //建立$user1 //把$user1加入$group1中 $user1 = $this->user->create_user($this->url, $this->user_name1); $group1->add_actor($user1); //$group1->get_users() $users = $group1->get_users(); //[R]測試get_users()數量是否為1 $this->unit->run(count($users), 1, '把$user1加入$group1中 $group1->get_users() [R]測試get_users()數量是否為1'); //記得$group1_id //$group1find = find by id $group1_id = $group1->get_id(); //$group1find = $this->group->find('id', $group1_id); $group1find = new Group($group1_id); //$group1find->get_users() //[R]測試users[0]的名字是否符合 $users = $group1find->get_users(); $this->unit->run($users[0]->get_email(), $this->user_name1, '$group1find = find by id | $group1find->get_users() | [R]測試users[0]的名字是否符合' . $users[0]->get_id()); //從$group1中移除$user1 //[R]$group1看看是否數量為0 $group1->remove_actor($users[0]); $users = $group1->get_users(); $this->unit->run(count($users), 0, '從$group1中移除$user1 | [R]$group1看看是否數量為0'); //$group1find = find by id //[R] 看看get_users數量是否為0 //$group1find = $this->group->find(array('group_id'=> $group1_id)); $group1find = new Group($group1_id); $users = $group1find->get_users(); $this->unit->run(count($users), 0, '$group1find = find by id | [R] 看看get_users數量是否為0'); //建立$group2 //把$group2加入$group1中 $group2 = $this->group->create_group($this->url, $this->group_name2); $group1->add_actor($group2); //從$group1中get_group //[R]檢查groups[0]是否跟$group2->equals() $groups = $group1->get_groups(); $this->unit->run($group2->equals($groups[0]), TRUE, '從$group1中get_group | [R]檢查groups[0]是否跟$group2->equals()'); //建立$user2 //把$user2加入$group2中 //取得$user2->get_parent_groups() //[R] 檢查數量是否有2個 $user2 = $this->user->create_user($this->url, $this->user_name2); $group2->add_actor($user2); $groups = $user2->get_parent_groups(); $this->unit->run(count($groups), 2, '建立$user2 | 把$user2加入$group2中 | 取得$user2->get_parent_groups() | [R] 檢查數量是否有2個'); //[R] 檢查第一個是否equals $group1 $this->unit->run($groups[0]->equals($group2), TRUE, '[R] 檢查第一個是否equals $group2'); //[R] 檢查第二個是否跟$group2的名稱一樣 $this->unit->run($group1->equals($groups[1]), TRUE, '[R] 檢查第二個是否跟$group1的名稱一樣'); //刪除$group1 $group1->delete(); //取得$user2的get_parent_groups //[R] 數量是否為1 $groups = $user2->get_parent_groups(); $this->unit->run(count($groups), 1, '取得$user2的get_parent_groups | [R] 數量是否為1'); //[R] 是否equals $group2 $this->unit->run($groups[0]->equals($group2), TRUE, '[R] 是否equals $group2'); //刪除$group2 $group2->delete(); //[R] 確認$group2的名稱消失 $this->unit->run($group2->get_name(), NULL, '刪除$group2 | [R] 確認$group2的名稱消失'); //用id去找尋$group1 //[R] 確認找不到 //$group1find = $this->group->find('group_id', $group1_id); $group1find = new Group($group1_id); $this->unit->run($group1find->get_name(), NULL, '用id去找尋$group1 | [R] 確認get_name取不到名字'); //刪除$user1 $user2 //用資料庫刪除 // $user1_id = $user1->get_id(); // $user2_id = $user2->get_id(); // $user1->delete(); // $user2->delete(); // $this->db->delete('domain2user', array('user_id'=>$user1_id)); // $this->db->delete('domain2user', array('user_id'=>$user2_id)); // $this->db->delete('user', array('user_id'=>$user1_id)); // $this->db->delete('user', array('user_id'=>$user2_id)); // $this->unit->run($test_result, // $expected_result, // $test_name); unit_test_report($this); }
public function should_resolve_overloaded_attributes() { $c = create_context(array('person' => $p = new Person())); expects($c->resolve(':person.name'))->should_be('The king'); expects($c->resolve(':person.age'))->should_be(19); }
public function check_auth() { create_context(TRUE); $url = 'http://www.plurk.com/p/6dhwp7'; $email = '*****@*****.**'; $group_name = 'check_auth'; $user = $this->user->create_user($url, $email); $group = $this->group->create_group($url, $group_name); $group->add_actor($user); $annotation = new Annotation(784); $this->authorize_manager->set_resource($annotation); $this->authorize_manager->policy_add_actor(5, $group); clear_context_user(); $search = new Search_annotation_collection(); $count = $search->length(); $this->unit->run($count, 'is_int', '先找看看有沒有,應該是有的'); set_context_user($user); $search = new Search_annotation_collection(); $this->unit->run($search->length(), $count + 1, '設定權限之後,應該能找到被設定的那一個項目'); unit_test_report($this); }
function index() { create_context(); //初始設定開始! //先建立$webpage //建立$user1 //建立$group1 $webpage = $this->webpage->create($this->url); $user = $this->user->create_user($this->url, $this->user_email); $group1 = $this->group->create_group($this->url, $this->group1_name); $group2 = $this->group->create_group($this->url, $this->group2_name); $group1->add_actor($user); $auth = $this->authorize_manager; $auth->set_resource($webpage); $auth->set_actor($group1); $auth->set_throw_exception(FALSE); //初始設定結束! //------------------------------------------------------- //Round 1 測試尚未設定Policy之前 // test_msg('Round 1 測試尚未設定Policy之前'); $this->unit->run($auth->has_policy($this->action_webpage_admin), FALSE, '測試尚未設定Policy之前,$webpage是否有設定過$action_webpage_adming的權限'); $this->unit->run($auth->allow($this->action_webpage_admin), FALSE, '測試尚未設定Policy之前,任意人士是否允許管理Webpage'); $this->unit->run($auth->has_policy($this->action_webpage_admin), FALSE, '測試尚未設定Policy之前,$webpage是否有設定過action_webpage_read的權限?'); $this->unit->run($auth->allow($this->action_webpage_read), TRUE, '測試尚未設定Policy之前,任意人士是否允許讀取'); $this->unit->run($auth->has_policy($this->action_webpage_admin), FALSE, '測試尚未設定Policy之前,$webpage是否有設定過action_domain_admin的權限?'); $this->unit->run($auth->allow($this->action_domain_admin), FALSE, '測試尚未設定Policy之前,輸入適用對象錯誤的Action看看'); //-------------------------------------------------------- //Round 2 設定Policy之後 // test_msg('Round 2 設定Policy之後'); $auth->policy_add_actor($this->action_webpage_admin); $this->unit->run($auth->has_policy($this->action_webpage_admin), TRUE, '設定Policy之後,$webpage是否有設定過$action_webpage_adming的權限'); $this->unit->run($auth->allow($this->action_webpage_admin), TRUE, '設定Policy之後,$group1是否允許管理Webpage'); //------------------------------------------------------------ //Round 3 設定不相干人士看看 // test_msg('Round 3 設定不相干人士看看'); $auth->set_actor($group2); $this->unit->run($auth->has_policy($this->action_webpage_admin), TRUE, '設定不相干人士看看,$webpage是否有設定過action_webpage_admin的權限?'); $this->unit->run($auth->allow($this->action_webpage_admin), FALSE, '設定不相干人士看看,$group2是否允許管理Webpage'); //------------------------------------------------------------ //Round 4 設定子成員看看 // test_msg('Round 4 設定子成員看看'); $auth->set_actor($user); $this->unit->run($auth->has_policy($this->action_webpage_admin), TRUE, '設定子成員看看,$webpage是否有設定過action_webpage_admin的權限?'); $this->unit->run($auth->allow($this->action_webpage_admin), TRUE, '設定子成員看看,$user是否允許管理Webpage'); //------------------------------------------------------------ //Round 5 加入$group2看看 // test_msg('Round 5 加入$group2看看'); $auth->policy_add_actor($this->action_webpage_admin, $group2); $this->unit->run($auth->has_policy($this->action_webpage_admin), TRUE, '加入$group2看看,$webpage是否有設定過action_webpage_admin的權限?'); $this->unit->run($auth->allow($this->action_webpage_admin), TRUE, '加入$group2看看,$group2是否允許管理Webpage'); //------------------------------------------------------------ //Round 6 測試policy // test_msg('Round 6 測試policy'); $policy = $auth->get_policy($this->action_webpage_admin); $actors = $policy->get_actors(); $this->unit->run(count($actors), 2, '測試policy的get_actors()數量,應該是有$group1跟$group2'); $passed = FALSE; foreach ($actors as $a) { if ($a->equals($group1)) { $passed = TRUE; break; } } $this->unit->run($passed, TRUE, '測試policy的get_actors(),用foreach $actors[0]->equals($group1)看看'); $passed = FALSE; foreach ($actors as $a) { if ($a->equals($group2)) { $passed = TRUE; break; } } $this->unit->run($passed, TRUE, '測試policy的get_actors(),用$actors[0]->equals($group2)看看'); $passed = FALSE; foreach ($actors as $a) { if ($a->equals($user)) { $passed = TRUE; break; } } $this->unit->run($passed, FALSE, '測試policy的get_actors(),應該是沒有$user'); //---------------------------------------------------------- //Round 7 測試get_polices() // test_msg('Round 7 測試get_polices()'); $auth->policy_add_actor($this->action_webpage_read, $user); $polices = $auth->get_policies(); $this->unit->run(count($polices), 2, '測試get_polices(),應該有兩個policy'); $policy_id = $policy->get_id(); $polices_ids = array(); foreach ($polices as $p) { $polices_ids[] = $p->get_id(); } $this->unit->run(in_array($policy_id, $polices_ids), TRUE, '測試get_polices(),應該包含了policy的ID在裡面'); //---------------------------------------------------------- //Round 8 測試is_admin // test_msg('Round 8 測試is_admin'); $this->unit->run($auth->is_admin($user), TRUE, '測試is_admin,$user可以管理$webpage嗎?'); $this->unit->run($auth->is_admin($group1), TRUE, '測試is_admin,$group1可以管理$webpage嗎?'); $auth->policy_remove_actor($this->action_webpage_admin, $group2); $this->unit->run($auth->is_admin($group2), FALSE, '測試is_admin,$group2可以管理$webpage嗎?'); //---------------------------------------------------------- //Round 9 測試remove_entrie_policy() // test_msg('Round 9 測試remove_entrie_policy()'); $count = count($auth->get_actors($this->action_webpage_read)); $auth->policy_remove_actor($this->action_webpage_read, $group2); $this->unit->run($auth->has_policy($this->action_webpage_read), TRUE, '測試remove_entrie_policy(),移除不相干的人看看會怎樣'); $this->unit->run(count($auth->get_actors($this->action_webpage_read)), $count, '測試remove_entrie_policy(),移除不相干的人之後,count應該還是一樣吧?'); $auth->policy_remove_actor($this->action_webpage_read, $user); $this->unit->run($auth->has_policy($this->action_webpage_read), FALSE, '測試remove_entrie_policy(),移除相干的人看看會怎樣'); $polices = $auth->get_policies(); $this->unit->run(count($polices), 1, '測試remove_entrie_policy(),應該只剩下1個'); $this->unit->run($polices[0]->get_id(), $policy_id, '測試remove_entrie_policy(),應該只剩下action_webpage_admin了'); //---------------------------------------------------------- //Round 10 remove_entrie_policy()不指定actor // test_msg('Round 10 remove_entrie_policy()不指定actor'); $auth->remove_entrie_policy($this->action_webpage_admin); $this->unit->run($auth->has_policy($this->action_webpage_admin), FALSE, 'remove_entrie_policy()不指定actor'); $polices = $auth->get_policies(); $this->unit->run(count($polices), 0, 'remove_entrie_policy()不指定actor,應該只剩下0個'); // //最後要回收不用到的物件 // $webpage->delete(); // $group->delete(); // // $user_id = $user->get_id(); // $user->delete(); // $this->db->delete(array('domain2user', 'user'), array('user_id' => $user_id)); // $this->unit->run($test_result // , $expected_result // , $test_name); unit_test_report($this); }
/** * Get the context instance as an object. This function will create the * context instance if it does not exist yet. * @param integer $level The context level, for example CONTEXT_COURSE, or CONTEXT_MODULE. * @param integer $instance The instance id. For $level = CONTEXT_COURSE, this would be $course->id, * for $level = CONTEXT_MODULE, this would be $cm->id. And so on. * @return object The context object. */ function get_context_instance($contextlevel, $instance = 0) { global $context_cache, $context_cache_id; static $allowed_contexts = array(CONTEXT_SYSTEM, CONTEXT_PERSONAL, CONTEXT_USER, CONTEXT_COURSECAT, CONTEXT_COURSE, CONTEXT_GROUP, CONTEXT_MODULE, CONTEXT_BLOCK); if ($contextlevel === 'clearcache') { // TODO: Remove for v2.0 // No longer needed, but we'll catch it to avoid erroring out on custom code. // This used to be a fix for MDL-9016 // "Restoring into existing course, deleting first // deletes context and doesn't recreate it" return false; } /// System context has special cache if ($contextlevel == CONTEXT_SYSTEM) { return get_system_context(); } /// check allowed context levels if (!in_array($contextlevel, $allowed_contexts)) { // fatal error, code must be fixed - probably typo or switched parameters error('Error: get_context_instance() called with incorrect context level "' . s($contextlevel) . '"'); } /// Check the cache if (isset($context_cache[$contextlevel][$instance])) { // Already cached return $context_cache[$contextlevel][$instance]; } /// Get it from the database, or create it if (!($context = get_record('context', 'contextlevel', $contextlevel, 'instanceid', $instance))) { $context = create_context($contextlevel, $instance); } /// Only add to cache if context isn't empty. if (!empty($context)) { $context_cache[$contextlevel][$instance] = $context; // Cache it for later $context_cache_id[$context->id] = $context; // Cache it for later } return $context; }