diff --git a/.gitignore b/.gitignore index 3ffedb3..5a6b924 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,6 @@ coverage tests/datastore.php tests/dummycache/ phpmd.html + +# Ignore user plugin configuration +plugins/*/config.php \ No newline at end of file diff --git a/application/Config.php b/application/Config.php old mode 100755 new mode 100644 index ec799d7..c71ef68 --- a/application/Config.php +++ b/application/Config.php @@ -1,129 +1,134 @@ - $value) { - $configStr .= '$GLOBALS[\'config\'][\''. $key .'\'] = '.var_export($config['config'][$key], true).';'. PHP_EOL; - } - $configStr .= '?>'; - - if (!file_put_contents($config['config']['CONFIG_FILE'], $configStr) - || strcmp(file_get_contents($config['config']['CONFIG_FILE']), $configStr) != 0 - ) { - throw new Exception( - 'Shaarli could not create the config file. - Please make sure Shaarli has the right to write in the folder is it installed in.' - ); - } -} - -/** - * Milestone 0.9 - shaarli/Shaarli#41: options.php is not supported anymore. - * ==> if user is loggedIn, merge its content with config.php, then delete options.php. - * - * @param array $config contains all configuration fields. - * @param bool $isLoggedIn true if user is logged in. - * - * @return void - */ -function mergeDeprecatedConfig($config, $isLoggedIn) -{ - $config_file = $config['config']['CONFIG_FILE']; - - if (is_file($config['config']['DATADIR'].'/options.php') && $isLoggedIn) { - include $config['config']['DATADIR'].'/options.php'; - - // Load GLOBALS into config - foreach ($GLOBALS as $key => $value) { - $config[$key] = $value; - } - $config['config']['CONFIG_FILE'] = $config_file; - writeConfig($config, $isLoggedIn); - - unlink($config['config']['DATADIR'].'/options.php'); - } -} - -/** - * Exception used if a mandatory field is missing in given configuration. - */ -class MissingFieldConfigException extends Exception -{ - public $field; - - /** - * Construct exception. - * - * @param string $field field name missing. - */ - public function __construct($field) - { - $this->field = $field; - $this->message = 'Configuration value is required for '. $this->field; - } -} - -/** - * Exception used if an unauthorized attempt to edit configuration has been made. - */ -class UnauthorizedConfigException extends Exception -{ - /** - * Construct exception. - */ - public function __construct() - { - $this->message = 'You are not authorized to alter config.'; - } -} + $value) { + $configStr .= '$GLOBALS[\'config\'][\''. $key .'\'] = '.var_export($config['config'][$key], true).';'. PHP_EOL; + } + + if (isset($config['plugins'])) { + foreach ($config['plugins'] as $key => $value) { + $configStr .= '$GLOBALS[\'plugins\'][\''. $key .'\'] = '.var_export($config['plugins'][$key], true).';'. PHP_EOL; + } + } + + if (!file_put_contents($config['config']['CONFIG_FILE'], $configStr) + || strcmp(file_get_contents($config['config']['CONFIG_FILE']), $configStr) != 0 + ) { + throw new Exception( + 'Shaarli could not create the config file. + Please make sure Shaarli has the right to write in the folder is it installed in.' + ); + } +} + +/** + * Milestone 0.9 - shaarli/Shaarli#41: options.php is not supported anymore. + * ==> if user is loggedIn, merge its content with config.php, then delete options.php. + * + * @param array $config contains all configuration fields. + * @param bool $isLoggedIn true if user is logged in. + * + * @return void + */ +function mergeDeprecatedConfig($config, $isLoggedIn) +{ + $config_file = $config['config']['CONFIG_FILE']; + + if (is_file($config['config']['DATADIR'].'/options.php') && $isLoggedIn) { + include $config['config']['DATADIR'].'/options.php'; + + // Load GLOBALS into config + foreach ($GLOBALS as $key => $value) { + $config[$key] = $value; + } + $config['config']['CONFIG_FILE'] = $config_file; + writeConfig($config, $isLoggedIn); + + unlink($config['config']['DATADIR'].'/options.php'); + } +} + +/** + * Exception used if a mandatory field is missing in given configuration. + */ +class MissingFieldConfigException extends Exception +{ + public $field; + + /** + * Construct exception. + * + * @param string $field field name missing. + */ + public function __construct($field) + { + $this->field = $field; + $this->message = 'Configuration value is required for '. $this->field; + } +} + +/** + * Exception used if an unauthorized attempt to edit configuration has been made. + */ +class UnauthorizedConfigException extends Exception +{ + /** + * Construct exception. + */ + public function __construct() + { + $this->message = 'You are not authorized to alter config.'; + } +} diff --git a/application/PluginManager.php b/application/PluginManager.php new file mode 100644 index 0000000..e572ff7 --- /dev/null +++ b/application/PluginManager.php @@ -0,0 +1,184 @@ +authorizedPlugins = $authorizedPlugins; + + $dirs = glob(self::$PLUGINS_PATH . '/*', GLOB_ONLYDIR); + $dirnames = array_map('basename', $dirs); + foreach ($this->authorizedPlugins as $plugin) { + $index = array_search($plugin, $dirnames); + + // plugin authorized, but its folder isn't listed + if ($index === false) { + continue; + } + + try { + $this->loadPlugin($dirs[$index], $plugin); + } + catch (PluginFileNotFoundException $e) { + error_log($e->getMessage()); + } + } + } + + /** + * Execute all plugins registered hook. + * + * @param string $hook name of the hook to trigger. + * @param array $data list of data to manipulate passed by reference. + * @param array $params additional parameters such as page target. + * + * @return void + */ + public function executeHooks($hook, &$data, $params = array()) + { + if (!empty($params['target'])) { + $data['_PAGE_'] = $params['target']; + } + + if (isset($params['loggedin'])) { + $data['_LOGGEDIN_'] = $params['loggedin']; + } + + foreach ($this->loadedPlugins as $plugin) { + $hookFunction = $this->buildHookName($hook, $plugin); + + if (function_exists($hookFunction)) { + $data = call_user_func($hookFunction, $data); + } + } + } + + /** + * Load a single plugin from its files. + * Add them in $loadedPlugins if successful. + * + * @param string $dir plugin's directory. + * @param string $pluginName plugin's name. + * + * @return void + * @throws PluginFileNotFoundException - plugin files not found. + */ + private function loadPlugin($dir, $pluginName) + { + if (!is_dir($dir)) { + throw new PluginFileNotFoundException($pluginName); + } + + $pluginFilePath = $dir . '/' . $pluginName . '.php'; + if (!is_file($pluginFilePath)) { + throw new PluginFileNotFoundException($pluginName); + } + + include_once $pluginFilePath; + + $this->loadedPlugins[] = $pluginName; + } + + /** + * Construct normalize hook name for a specific plugin. + * + * Format: + * hook__ + * + * @param string $hook hook name. + * @param string $pluginName plugin name. + * + * @return string - plugin's hook name. + */ + public function buildHookName($hook, $pluginName) + { + return 'hook_' . $pluginName . '_' . $hook; + } +} + +/** + * Class PluginFileNotFoundException + * + * Raise when plugin files can't be found. + */ +class PluginFileNotFoundException extends Exception +{ + /** + * Construct exception with plugin name. + * Generate message. + * + * @param string $pluginName name of the plugin not found + */ + public function __construct($pluginName) + { + $this->message = 'Plugin "'. $pluginName .'" files not found.'; + } +} \ No newline at end of file diff --git a/application/Router.php b/application/Router.php new file mode 100644 index 0000000..82b2b85 --- /dev/null +++ b/application/Router.php @@ -0,0 +1,105 @@ + /shaarli/ @@ -75,6 +84,8 @@ require_once 'application/TimeZone.php'; require_once 'application/Url.php'; require_once 'application/Utils.php'; require_once 'application/Config.php'; +require_once 'application/PluginManager.php'; +require_once 'application/Router.php'; // Ensure the PHP version is supported try { @@ -119,6 +130,9 @@ include "inc/rain.tpl.class.php"; //include Rain TPL raintpl::$tpl_dir = $GLOBALS['config']['RAINTPL_TPL']; // template directory raintpl::$cache_dir = $GLOBALS['config']['RAINTPL_TMP']; // cache directory +$pluginManager = PluginManager::getInstance(); +$pluginManager->load($GLOBALS['config']['ENABLED_PLUGINS']); + ob_start(); // Output buffering for the page cache. @@ -962,16 +976,31 @@ function showDaily() $fill[$index]+=$length; } $PAGE = new pageBuilder; - $PAGE->assign('linksToDisplay',$linksToDisplay); - $PAGE->assign('linkcount',count($LINKSDB)); - $PAGE->assign('cols', $columns); - $PAGE->assign('day',linkdate2timestamp($day.'_000000')); - $PAGE->assign('previousday',$previousday); - $PAGE->assign('nextday',$nextday); + $data = array( + 'linksToDisplay' => $linksToDisplay, + 'linkcount' => count($LINKSDB), + 'cols' => $columns, + 'day' => linkdate2timestamp($day.'_000000'), + 'previousday' => $previousday, + 'nextday' => $nextday, + ); + $pluginManager = PluginManager::getInstance(); + $pluginManager->executeHooks('render_daily', $data, array('loggedin' => isLoggedIn())); + + foreach ($data as $key => $value) { + $PAGE->assign($key, $value); + } + $PAGE->renderPage('daily'); exit; } +// Renders the linklist +function showLinkList($PAGE, $LINKSDB) { + buildLinkList($PAGE,$LINKSDB); // Compute list of links to display + $PAGE->renderPage('linklist'); +} + // ------------------------------------------------------------------------------------------ // Render HTML page (according to URL parameters and user rights) @@ -983,12 +1012,36 @@ function renderPage() $GLOBALS['config']['HIDE_PUBLIC_LINKS'] ); + $PAGE = new pageBuilder; + + // Determine which page will be rendered. + $query = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : ''; + $targetPage = Router::findPage($query, $_GET, isLoggedIn()); + + // Call plugin hooks for header, footer and includes, specifying which page will be rendered. + // Then assign generated data to RainTPL. + $common_hooks = array( + 'header', + 'footer', + 'includes', + ); + $pluginManager = PluginManager::getInstance(); + foreach($common_hooks as $name) { + $plugin_data = array(); + $pluginManager->executeHooks('render_' . $name, $plugin_data, + array( + 'target' => $targetPage, + 'loggedin' => isLoggedIn() + ) + ); + $PAGE->assign('plugins_' . $name, $plugin_data); + } + // -------- Display login form. - if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=login')) + if ($targetPage == Router::$PAGE_LOGIN) { if ($GLOBALS['config']['OPEN_SHAARLI']) { header('Location: ?'); exit; } // No need to login for open Shaarli $token=''; if (ban_canLogin()) $token=getToken(); // Do not waste token generation if not useful. - $PAGE = new pageBuilder; $PAGE->assign('token',$token); $PAGE->assign('returnurl',(isset($_SERVER['HTTP_REFERER']) ? escape($_SERVER['HTTP_REFERER']):'')); $PAGE->renderPage('loginform'); @@ -1004,7 +1057,7 @@ function renderPage() } // -------- Picture wall - if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=picwall')) + if ($targetPage == Router::$PAGE_PICWALL) { // Optionally filter the results: $links=array(); @@ -1027,15 +1080,22 @@ function renderPage() } } - $PAGE = new pageBuilder; - $PAGE->assign('linkcount',count($LINKSDB)); - $PAGE->assign('linksToDisplay',$linksToDisplay); + $data = array( + 'linkcount' => count($LINKSDB), + 'linksToDisplay' => $linksToDisplay, + ); + $pluginManager->executeHooks('render_picwall', $data, array('loggedin' => isLoggedIn())); + + foreach ($data as $key => $value) { + $PAGE->assign($key, $value); + } + $PAGE->renderPage('picwall'); exit; } // -------- Tag cloud - if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=tagcloud')) + if ($targetPage == Router::$PAGE_TAGCLOUD) { $tags= $LINKSDB->allTags(); @@ -1049,9 +1109,17 @@ function renderPage() { $tagList[$key] = array('count'=>$value,'size'=>log($value, 15) / log($maxcount, 30) * (22-6) + 6); } - $PAGE = new pageBuilder; - $PAGE->assign('linkcount',count($LINKSDB)); - $PAGE->assign('tags',$tagList); + + $data = array( + 'linkcount' => count($LINKSDB), + 'tags' => $tagList, + ); + $pluginManager->executeHooks('render_tagcloud', $data, array('loggedin' => isLoggedIn())); + + foreach ($data as $key => $value) { + $PAGE->assign($key, $value); + } + $PAGE->renderPage('tagcloud'); exit; } @@ -1164,32 +1232,36 @@ function renderPage() header('Location: ?do=login&post='); exit; } - + showLinkList($PAGE, $LINKSDB); if (isset($_GET['edit_link'])) { header('Location: ?do=login&edit_link='. escape($_GET['edit_link'])); exit; } - $PAGE = new pageBuilder; - buildLinkList($PAGE,$LINKSDB); // Compute list of links to display - $PAGE->renderPage('linklist'); exit; // Never remove this one! All operations below are reserved for logged in user. } // -------- All other functions are reserved for the registered user: // -------- Display the Tools menu if requested (import/export/bookmarklet...) - if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=tools')) + if ($targetPage == Router::$PAGE_TOOLS) { - $PAGE = new pageBuilder; - $PAGE->assign('linkcount',count($LINKSDB)); - $PAGE->assign('pageabsaddr',index_url($_SERVER)); + $data = array( + 'linkcount' => count($LINKSDB), + 'pageabsaddr' => index_url($_SERVER), + ); + $pluginManager->executeHooks('render_tools', $data); + + foreach ($data as $key => $value) { + $PAGE->assign($key, $value); + } + $PAGE->renderPage('tools'); exit; } // -------- User wants to change his/her password. - if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=changepasswd')) + if ($targetPage == Router::$PAGE_CHANGEPASSWORD) { if ($GLOBALS['config']['OPEN_SHAARLI']) die('You are not supposed to change a password on an Open Shaarli.'); if (!empty($_POST['setpassword']) && !empty($_POST['oldpassword'])) @@ -1220,7 +1292,6 @@ function renderPage() } else // show the change password form. { - $PAGE = new pageBuilder; $PAGE->assign('linkcount',count($LINKSDB)); $PAGE->assign('token',getToken()); $PAGE->renderPage('changepassword'); @@ -1229,7 +1300,7 @@ function renderPage() } // -------- User wants to change configuration - if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=configure')) + if ($targetPage == Router::$PAGE_CONFIGURE) { if (!empty($_POST['title']) ) { @@ -1265,7 +1336,6 @@ function renderPage() } else // Show the configuration form. { - $PAGE = new pageBuilder; $PAGE->assign('linkcount',count($LINKSDB)); $PAGE->assign('token',getToken()); $PAGE->assign('title', empty($GLOBALS['title']) ? '' : $GLOBALS['title'] ); @@ -1279,11 +1349,10 @@ function renderPage() } // -------- User wants to rename a tag or delete it - if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=changetag')) + if ($targetPage == Router::$PAGE_CHANGETAG) { if (empty($_POST['fromtag'])) { - $PAGE = new pageBuilder; $PAGE->assign('linkcount',count($LINKSDB)); $PAGE->assign('token',getToken()); $PAGE->assign('tags', $LINKSDB->allTags()); @@ -1328,9 +1397,8 @@ function renderPage() } // -------- User wants to add a link without using the bookmarklet: Show form. - if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=addlink')) + if ($targetPage == Router::$PAGE_ADDLINK) { - $PAGE = new pageBuilder; $PAGE->assign('linkcount',count($LINKSDB)); $PAGE->renderPage('addlink'); exit; @@ -1349,6 +1417,9 @@ function renderPage() $link = array('title'=>trim($_POST['lf_title']),'url'=>$url,'description'=>trim($_POST['lf_description']),'private'=>(isset($_POST['lf_private']) ? 1 : 0), 'linkdate'=>$linkdate,'tags'=>str_replace(',',' ',$tags)); if ($link['title']=='') $link['title']=$link['url']; // If title is empty, use the URL as title. + + $pluginManager->executeHooks('save_link', $link); + $LINKSDB[$linkdate] = $link; $LINKSDB->savedb($GLOBALS['config']['PAGECACHE']); // Save to disk. pubsubhub(); @@ -1382,6 +1453,9 @@ function renderPage() // - confirmation is handled by JavaScript // - we are protected from XSRF by the token. $linkdate=$_POST['lf_linkdate']; + + $pluginManager->executeHooks('delete_link', $LINKSDB[$linkdate]); + unset($LINKSDB[$linkdate]); $LINKSDB->savedb($GLOBALS['config']['PAGECACHE']); // save to disk @@ -1423,13 +1497,20 @@ function renderPage() { $link = $LINKSDB[$_GET['edit_link']]; // Read database if (!$link) { header('Location: ?'); exit; } // Link not found in database. - $PAGE = new pageBuilder; - $PAGE->assign('linkcount',count($LINKSDB)); - $PAGE->assign('link',$link); - $PAGE->assign('link_is_new',false); - $PAGE->assign('token',getToken()); // XSRF protection. - $PAGE->assign('http_referer',(isset($_SERVER['HTTP_REFERER']) ? escape($_SERVER['HTTP_REFERER']) : '')); - $PAGE->assign('tags', $LINKSDB->allTags()); + $data = array( + 'linkcount' => count($LINKSDB), + 'link' => $link, + 'link_is_new' => false, + 'token' => getToken(), + 'http_referer' => (isset($_SERVER['HTTP_REFERER']) ? escape($_SERVER['HTTP_REFERER']) : ''), + 'tags' => $LINKSDB->allTags(), + ); + $pluginManager->executeHooks('render_editlink', $data); + + foreach ($data as $key => $value) { + $PAGE->assign($key, $value); + } + $PAGE->renderPage('editlink'); exit; } @@ -1493,24 +1574,30 @@ function renderPage() ); } - $PAGE = new pageBuilder; - $PAGE->assign('linkcount',count($LINKSDB)); - $PAGE->assign('link',$link); - $PAGE->assign('link_is_new',$link_is_new); - $PAGE->assign('token',getToken()); // XSRF protection. - $PAGE->assign('http_referer',(isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '')); - $PAGE->assign('source',(isset($_GET['source']) ? $_GET['source'] : '')); - $PAGE->assign('tags', $LINKSDB->allTags()); + $data = array( + 'linkcount' => count($LINKSDB), + 'link' => $link, + 'link_is_new' => $link_is_new, + 'token' => getToken(), // XSRF protection. + 'http_referer' => (isset($_SERVER['HTTP_REFERER']) ? escape($_SERVER['HTTP_REFERER']) : ''), + 'source' => (isset($_GET['source']) ? $_GET['source'] : ''), + 'tags' => $LINKSDB->allTags(), + ); + $pluginManager->executeHooks('render_editlink', $data); + + foreach ($data as $key => $value) { + $PAGE->assign($key, $value); + } + $PAGE->renderPage('editlink'); exit; } // -------- Export as Netscape Bookmarks HTML file. - if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=export')) + if ($targetPage == Router::$PAGE_EXPORT) { if (empty($_GET['what'])) { - $PAGE = new pageBuilder; $PAGE->assign('linkcount',count($LINKSDB)); $PAGE->renderPage('export'); exit; @@ -1562,9 +1649,8 @@ HTML; } // -------- Show upload/import dialog: - if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=import')) + if ($targetPage == Router::$PAGE_IMPORT) { - $PAGE = new pageBuilder; $PAGE->assign('linkcount',count($LINKSDB)); $PAGE->assign('token',getToken()); $PAGE->assign('maxfilesize',getMaxFileSize()); @@ -1573,9 +1659,7 @@ HTML; } // -------- Otherwise, simply display search form and links: - $PAGE = new pageBuilder; - buildLinkList($PAGE,$LINKSDB); // Compute list of links to display - $PAGE->renderPage('linklist'); + showLinkList($PAGE, $LINKSDB); exit; } @@ -1746,7 +1830,7 @@ function buildLinkList($PAGE,$LINKSDB) $taglist = explode(' ',$link['tags']); uasort($taglist, 'strcasecmp'); $link['taglist']=$taglist; - + $link['shorturl'] = smallHash($link['linkdate']); if ($link["url"][0] === '?' && // Check for both signs of a note: starting with ? and 7 chars long. I doubt that you'll post any links that look like this. strlen($link["url"]) === 7) { $link["url"] = index_url($_SERVER) . $link["url"]; @@ -1766,18 +1850,28 @@ function buildLinkList($PAGE,$LINKSDB) $token = ''; if (isLoggedIn()) $token=getToken(); // Fill all template fields. - $PAGE->assign('linkcount',count($LINKSDB)); - $PAGE->assign('previous_page_url',$previous_page_url); - $PAGE->assign('next_page_url',$next_page_url); - $PAGE->assign('page_current',$page); - $PAGE->assign('page_max',$pagecount); - $PAGE->assign('result_count',count($linksToDisplay)); - $PAGE->assign('search_type',$search_type); - $PAGE->assign('search_crits',$search_crits); - $PAGE->assign('redirector',empty($GLOBALS['redirector']) ? '' : $GLOBALS['redirector']); // Optional redirector URL. - $PAGE->assign('token',$token); - $PAGE->assign('links',$linkDisp); - $PAGE->assign('tags', $LINKSDB->allTags()); + $data = array( + 'linkcount' => count($LINKSDB), + 'previous_page_url' => $previous_page_url, + 'next_page_url' => $next_page_url, + 'page_current' => $page, + 'page_max' => $pagecount, + 'result_count' => count($linksToDisplay), + 'search_type' => $search_type, + 'search_crits' => $search_crits, + 'redirector' => empty($GLOBALS['redirector']) ? '' : $GLOBALS['redirector'], // Optional redirector URL. + 'token' => $token, + 'links' => $linkDisp, + 'tags' => $LINKSDB->allTags(), + ); + + $pluginManager = PluginManager::getInstance(); + $pluginManager->executeHooks('render_linklist', $data, array('loggedin' => isLoggedIn())); + + foreach ($data as $key => $value) { + $PAGE->assign($key, $value); + } + return; } diff --git a/tests/PluginManagerTest.php b/tests/PluginManagerTest.php new file mode 100755 index 0000000..749ce2b --- /dev/null +++ b/tests/PluginManagerTest.php @@ -0,0 +1,66 @@ +load(array(self::$_PLUGIN_NAME)); + + $this->assertTrue(function_exists('hook_test_random')); + + $data = array(0 => 'woot'); + $pluginManager->executeHooks('random', $data); + $this->assertEquals('woot', $data[1]); + + $data = array(0 => 'woot'); + $pluginManager->executeHooks('random', $data, array('target' => 'test')); + $this->assertEquals('page test', $data[1]); + + $data = array(0 => 'woot'); + $pluginManager->executeHooks('random', $data, array('loggedin' => true)); + $this->assertEquals('loggedin', $data[1]); + } + + /** + * Test missing plugin loading. + * + * @return void + */ + public function testPluginNotFound() + { + $pluginManager = PluginManager::getInstance(); + + $pluginManager->load(array()); + + $pluginManager->load(array('nope', 'renope')); + } +} \ No newline at end of file diff --git a/tests/RouterTest.php b/tests/RouterTest.php new file mode 100755 index 0000000..8838bc8 --- /dev/null +++ b/tests/RouterTest.php @@ -0,0 +1,515 @@ +assertEquals( + Router::$PAGE_LOGIN, + Router::findPage('do=login', array(), false) + ); + + $this->assertEquals( + Router::$PAGE_LOGIN, + Router::findPage('do=login', array(), 1) + ); + + $this->assertEquals( + Router::$PAGE_LOGIN, + Router::findPage('do=login&stuff', array(), false) + ); + } + + /** + * Test findPage: login page output. + * Invalid: page shouldn't be return. + * + * @return void + */ + public function testFindPageLoginInvalid() + { + $this->assertNotEquals( + Router::$PAGE_LOGIN, + Router::findPage('do=login', array(), true) + ); + + $this->assertNotEquals( + Router::$PAGE_LOGIN, + Router::findPage('do=other', array(), false) + ); + } + + /** + * Test findPage: picwall page output. + * Valid: page should be return. + * + * @return void + */ + public function testFindPagePicwallValid() + { + $this->assertEquals( + Router::$PAGE_PICWALL, + Router::findPage('do=picwall', array(), false) + ); + + $this->assertEquals( + Router::$PAGE_PICWALL, + Router::findPage('do=picwall', array(), true) + ); + } + + /** + * Test findPage: picwall page output. + * Invalid: page shouldn't be return. + * + * @return void + */ + public function testFindPagePicwallInvalid() + { + $this->assertEquals( + Router::$PAGE_PICWALL, + Router::findPage('do=picwall&stuff', array(), false) + ); + + $this->assertNotEquals( + Router::$PAGE_PICWALL, + Router::findPage('do=other', array(), false) + ); + } + + /** + * Test findPage: tagcloud page output. + * Valid: page should be return. + * + * @return void + */ + public function testFindPageTagcloudValid() + { + $this->assertEquals( + Router::$PAGE_TAGCLOUD, + Router::findPage('do=tagcloud', array(), false) + ); + + $this->assertEquals( + Router::$PAGE_TAGCLOUD, + Router::findPage('do=tagcloud', array(), true) + ); + + $this->assertEquals( + Router::$PAGE_TAGCLOUD, + Router::findPage('do=tagcloud&stuff', array(), false) + ); + } + + /** + * Test findPage: tagcloud page output. + * Invalid: page shouldn't be return. + * + * @return void + */ + public function testFindPageTagcloudInvalid() + { + $this->assertNotEquals( + Router::$PAGE_TAGCLOUD, + Router::findPage('do=other', array(), false) + ); + } + + /** + * Test findPage: linklist page output. + * Valid: page should be return. + * + * @return void + */ + public function testFindPageLinklistValid() + { + $this->assertEquals( + Router::$PAGE_LINKLIST, + Router::findPage('', array(), true) + ); + + $this->assertEquals( + Router::$PAGE_LINKLIST, + Router::findPage('whatever', array(), true) + ); + + $this->assertEquals( + Router::$PAGE_LINKLIST, + Router::findPage('whatever', array(), false) + ); + + $this->assertEquals( + Router::$PAGE_LINKLIST, + Router::findPage('do=tools', array(), false) + ); + } + + /** + * Test findPage: tools page output. + * Valid: page should be return. + * + * @return void + */ + public function testFindPageToolsValid() + { + $this->assertEquals( + Router::$PAGE_TOOLS, + Router::findPage('do=tools', array(), true) + ); + + $this->assertEquals( + Router::$PAGE_TOOLS, + Router::findPage('do=tools&stuff', array(), true) + ); + } + + /** + * Test findPage: tools page output. + * Invalid: page shouldn't be return. + * + * @return void + */ + public function testFindPageToolsInvalid() + { + $this->assertNotEquals( + Router::$PAGE_TOOLS, + Router::findPage('do=tools', array(), 1) + ); + + $this->assertNotEquals( + Router::$PAGE_TOOLS, + Router::findPage('do=tools', array(), false) + ); + + $this->assertNotEquals( + Router::$PAGE_TOOLS, + Router::findPage('do=other', array(), true) + ); + } + + /** + * Test findPage: changepasswd page output. + * Valid: page should be return. + * + * @return void + */ + public function testFindPageChangepasswdValid() + { + $this->assertEquals( + Router::$PAGE_CHANGEPASSWORD, + Router::findPage('do=changepasswd', array(), true) + ); + $this->assertEquals( + Router::$PAGE_CHANGEPASSWORD, + Router::findPage('do=changepasswd&stuff', array(), true) + ); + + } + + /** + * Test findPage: changepasswd page output. + * Invalid: page shouldn't be return. + * + * @return void + */ + public function testFindPageChangepasswdInvalid() + { + $this->assertNotEquals( + Router::$PAGE_CHANGEPASSWORD, + Router::findPage('do=changepasswd', array(), 1) + ); + + $this->assertNotEquals( + Router::$PAGE_CHANGEPASSWORD, + Router::findPage('do=changepasswd', array(), false) + ); + + $this->assertNotEquals( + Router::$PAGE_CHANGEPASSWORD, + Router::findPage('do=other', array(), true) + ); + } + /** + * Test findPage: configure page output. + * Valid: page should be return. + * + * @return void + */ + public function testFindPageConfigureValid() + { + $this->assertEquals( + Router::$PAGE_CONFIGURE, + Router::findPage('do=configure', array(), true) + ); + + $this->assertEquals( + Router::$PAGE_CONFIGURE, + Router::findPage('do=configure&stuff', array(), true) + ); + } + + /** + * Test findPage: configure page output. + * Invalid: page shouldn't be return. + * + * @return void + */ + public function testFindPageConfigureInvalid() + { + $this->assertNotEquals( + Router::$PAGE_CONFIGURE, + Router::findPage('do=configure', array(), 1) + ); + + $this->assertNotEquals( + Router::$PAGE_CONFIGURE, + Router::findPage('do=configure', array(), false) + ); + + $this->assertNotEquals( + Router::$PAGE_CONFIGURE, + Router::findPage('do=other', array(), true) + ); + } + + /** + * Test findPage: changetag page output. + * Valid: page should be return. + * + * @return void + */ + public function testFindPageChangetagValid() + { + $this->assertEquals( + Router::$PAGE_CHANGETAG, + Router::findPage('do=changetag', array(), true) + ); + + $this->assertEquals( + Router::$PAGE_CHANGETAG, + Router::findPage('do=changetag&stuff', array(), true) + ); + } + + /** + * Test findPage: changetag page output. + * Invalid: page shouldn't be return. + * + * @return void + */ + public function testFindPageChangetagInvalid() + { + $this->assertNotEquals( + Router::$PAGE_CHANGETAG, + Router::findPage('do=changetag', array(), 1) + ); + + $this->assertNotEquals( + Router::$PAGE_CHANGETAG, + Router::findPage('do=changetag', array(), false) + ); + + $this->assertNotEquals( + Router::$PAGE_CHANGETAG, + Router::findPage('do=other', array(), true) + ); + } + + /** + * Test findPage: addlink page output. + * Valid: page should be return. + * + * @return void + */ + public function testFindPageAddlinkValid() + { + $this->assertEquals( + Router::$PAGE_ADDLINK, + Router::findPage('do=addlink', array(), true) + ); + + $this->assertEquals( + Router::$PAGE_ADDLINK, + Router::findPage('do=addlink&stuff', array(), true) + ); + } + + /** + * Test findPage: addlink page output. + * Invalid: page shouldn't be return. + * + * @return void + */ + public function testFindPageAddlinkInvalid() + { + $this->assertNotEquals( + Router::$PAGE_ADDLINK, + Router::findPage('do=addlink', array(), 1) + ); + + $this->assertNotEquals( + Router::$PAGE_ADDLINK, + Router::findPage('do=addlink', array(), false) + ); + + $this->assertNotEquals( + Router::$PAGE_ADDLINK, + Router::findPage('do=other', array(), true) + ); + } + + /** + * Test findPage: export page output. + * Valid: page should be return. + * + * @return void + */ + public function testFindPageExportValid() + { + $this->assertEquals( + Router::$PAGE_EXPORT, + Router::findPage('do=export', array(), true) + ); + + $this->assertEquals( + Router::$PAGE_EXPORT, + Router::findPage('do=export&stuff', array(), true) + ); + } + + /** + * Test findPage: export page output. + * Invalid: page shouldn't be return. + * + * @return void + */ + public function testFindPageExportInvalid() + { + $this->assertNotEquals( + Router::$PAGE_EXPORT, + Router::findPage('do=export', array(), 1) + ); + + $this->assertNotEquals( + Router::$PAGE_EXPORT, + Router::findPage('do=export', array(), false) + ); + + $this->assertNotEquals( + Router::$PAGE_EXPORT, + Router::findPage('do=other', array(), true) + ); + } + + /** + * Test findPage: import page output. + * Valid: page should be return. + * + * @return void + */ + public function testFindPageImportValid() + { + $this->assertEquals( + Router::$PAGE_IMPORT, + Router::findPage('do=import', array(), true) + ); + + $this->assertEquals( + Router::$PAGE_IMPORT, + Router::findPage('do=import&stuff', array(), true) + ); + } + + /** + * Test findPage: import page output. + * Invalid: page shouldn't be return. + * + * @return void + */ + public function testFindPageImportInvalid() + { + $this->assertNotEquals( + Router::$PAGE_IMPORT, + Router::findPage('do=import', array(), 1) + ); + + $this->assertNotEquals( + Router::$PAGE_IMPORT, + Router::findPage('do=import', array(), false) + ); + + $this->assertNotEquals( + Router::$PAGE_IMPORT, + Router::findPage('do=other', array(), true) + ); + } + + /** + * Test findPage: editlink page output. + * Valid: page should be return. + * + * @return void + */ + public function testFindPageEditlinkValid() + { + $this->assertEquals( + Router::$PAGE_EDITLINK, + Router::findPage('whatever', array('edit_link' => 1), true) + ); + + $this->assertEquals( + Router::$PAGE_EDITLINK, + Router::findPage('', array('edit_link' => 1), true) + ); + + + $this->assertEquals( + Router::$PAGE_EDITLINK, + Router::findPage('whatever', array('post' => 1), true) + ); + + $this->assertEquals( + Router::$PAGE_EDITLINK, + Router::findPage('whatever', array('post' => 1, 'edit_link' => 1), true) + ); + } + + /** + * Test findPage: editlink page output. + * Invalid: page shouldn't be return. + * + * @return void + */ + public function testFindPageEditlinkInvalid() + { + $this->assertNotEquals( + Router::$PAGE_EDITLINK, + Router::findPage('whatever', array('edit_link' => 1), false) + ); + + $this->assertNotEquals( + Router::$PAGE_EDITLINK, + Router::findPage('whatever', array('edit_link' => 1), 1) + ); + + $this->assertNotEquals( + Router::$PAGE_EDITLINK, + Router::findPage('whatever', array(), true) + ); + } +} \ No newline at end of file diff --git a/tests/plugins/test/test.php b/tests/plugins/test/test.php new file mode 100755 index 0000000..3d750c9 --- /dev/null +++ b/tests/plugins/test/test.php @@ -0,0 +1,21 @@ +