From d0a43407a4e1e1704305296a6f0f3498d79d5db1 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Tue, 9 Aug 2016 17:32:35 +0200 Subject: [PATCH] POC: shaarli controllers --- application/PageBuilder.php | 2 + controllers/Controller.php | 141 ++++++++++++++++++++++++++++ controllers/LoginController.php | 37 ++++++++ index.php | 9 ++ tests/views/ControllerTest.php | 91 ++++++++++++++++++ tests/views/LoginControllerTest.php | 70 ++++++++++++++ 6 files changed, 350 insertions(+) create mode 100644 controllers/Controller.php create mode 100644 controllers/LoginController.php create mode 100644 tests/views/ControllerTest.php create mode 100644 tests/views/LoginControllerTest.php diff --git a/application/PageBuilder.php b/application/PageBuilder.php index 42932f326..c424dacd0 100644 --- a/application/PageBuilder.php +++ b/application/PageBuilder.php @@ -1,5 +1,7 @@ tpl = $tpl; + $this->conf = $conf; + $this->pluginManager = $pluginManager; + $this->linkDB = $linkDB; + $this->server = $server; + $this->get = $get; + $this->post = $post; + } + + /** + * Handle headers redirection. + * + * @return boolean true if a redirection is necessary, false otherwise. + */ + public abstract function redirect(); + + /** + * Render the view through the template engine. + */ + public abstract function render(); + + /** + * @param PageBuilder $tpl + */ + public function setTpl($tpl) + { + $this->tpl = $tpl; + } + + /** + * @param ConfigManager $conf + */ + public function setConf($conf) + { + $this->conf = $conf; + } + + /** + * @param PluginManager $pluginManager + */ + public function setPluginManager($pluginManager) + { + $this->pluginManager = $pluginManager; + } + + /** + * @param LinkDB $linkDB + */ + public function setLinkDB($linkDB) + { + $this->linkDB = $linkDB; + } + + /** + * @param array $server + */ + public function setServer($server) + { + $this->server = $server; + } + + /** + * @param array $get + */ + public function setGet($get) + { + $this->get = $get; + } + + /** + * @param array $post + */ + public function setPost($post) + { + $this->post = $post; + } +} \ No newline at end of file diff --git a/controllers/LoginController.php b/controllers/LoginController.php new file mode 100644 index 000000000..66bfdf63f --- /dev/null +++ b/controllers/LoginController.php @@ -0,0 +1,37 @@ +conf->get('security.open_shaarli')) { + header('Location: ?'); + return true; + } + return false; + } + + /** + * {@inheritdoc} + */ + public function render() + { + if (isset($this->get['username'])) { + $this->tpl->assign('username', escape($this->get['username'])); + } + $referer = isset($this->server['HTTP_REFERER']) ? escape($this->server['HTTP_REFERER']) : ''; + $this->tpl->assign('returnurl', $referer); + $this->tpl->renderPage('loginform'); + } +} \ No newline at end of file diff --git a/index.php b/index.php index 9ae798bab..293e7d07d 100644 --- a/index.php +++ b/index.php @@ -780,6 +780,15 @@ function renderPage($conf, $pluginManager) $PAGE->assign('plugins_' . $name, $plugin_data); } + $viewClass = ucfirst($targetPage) . 'Controller'; + /** @var Controller $view */ + $view = new $viewClass($PAGE, $conf, $pluginManager, $LINKSDB, $_SERVER, $_GET, $_POST); + if ($view->redirect()) { + exit; + } + $view->render(); + exit; + // -------- Display login form. if ($targetPage == Router::$PAGE_LOGIN) { diff --git a/tests/views/ControllerTest.php b/tests/views/ControllerTest.php new file mode 100644 index 000000000..d63eac7ff --- /dev/null +++ b/tests/views/ControllerTest.php @@ -0,0 +1,91 @@ +conf = new ConfigManager('tests/utils/config/configJson'); + $this->pageBuilder = new PageBuilder($this->conf); + $this->pluginManager = new PluginManager($this->conf); + + $refDB = new ReferenceLinkDB(); + $refDB->write(self::$testDatastore); + + $this->linkDB = new LinkDB(self::$testDatastore, true, false); + } + + /** + * Called after every test. + * Delete the test datastore. + */ + public function tearDown() + { + if (file_exists(self::$testDatastore)) { + unlink(self::$testDatastore); + } + } + + /** + * Test if a header has been set. + * + * @param string $expected Expected header regex, without delimiters. + * @param array $headers Headers set. + * + * @return bool true if the header has been set, false otherwise. + */ + public function isHeaderSetRegex($expected, $headers) + { + foreach ($headers as $header) { + if (preg_match('/'. $expected .'/', $header)) { + return true; + } + } + return false; + } +} \ No newline at end of file diff --git a/tests/views/LoginControllerTest.php b/tests/views/LoginControllerTest.php new file mode 100644 index 000000000..0e43ed25d --- /dev/null +++ b/tests/views/LoginControllerTest.php @@ -0,0 +1,70 @@ +controller = new LoginController( + $this->pageBuilder, + $this->conf, + $this->pluginManager, + $this->linkDB, + array(), + array(), + array() + ); + } + + /** + * Test login redirection: should only redirect if open shaarli is enabled. + * + * @runInSeparateProcess + */ + public function testLoginRedirect() { + $this->assertFalse($this->controller->redirect()); + $headers = xdebug_get_headers(); + $this->assertFalse($this->isHeaderSetRegex('Location:.*', $headers)); + + $this->conf->set('security.open_shaarli', true); + $this->assertTrue($this->controller->redirect()); + $headers = xdebug_get_headers(); + $this->assertTrue($this->isHeaderSetRegex('Location: \?', $headers)); + } + + /** + * Test login rendering without any data set. + */ + public function testLoginRenderWithoutLogin() { + $this->markTestIncomplete('This test is not doable now due to ban function relying on $_SERVER.'); + $this->expectOutputRegex('expectOutputRegex('expectOutputRegex('controller->render(); + } + + /** + * Test login rendering with a username set in $_GET. + */ + public function testLoginRenderWithLogin() { + $this->markTestIncomplete('This test is not doable now due to ban function relying on $_SERVER.'); + $username = 'John Doe'; + $this->controller->setGet(array('username' => $username)); + $this->expectOutputRegex('controller->render(); + } + + /** + * Test login rendering with a username set in $_GET. + */ + public function testLoginRenderBanned() { + $this->markTestIncomplete('This test is not doable now due to ban function relying on $_SERVER.'); + } +} \ No newline at end of file