From 710f2c7bb046b43ec9878ae795a181101f6d7515 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 5 Sep 2015 23:30:56 -0400 Subject: Improve unit tests --- app/Auth/Ldap.php | 45 +++++++++++++++++++++++++-------------------- app/Controller/Board.php | 2 +- app/Controller/Doc.php | 2 +- 3 files changed, 27 insertions(+), 22 deletions(-) (limited to 'app') diff --git a/app/Auth/Ldap.php b/app/Auth/Ldap.php index c1459b4e..e46d9b81 100644 --- a/app/Auth/Ldap.php +++ b/app/Auth/Ldap.php @@ -98,7 +98,7 @@ class Ldap extends Base { $ldap = $this->connect(); - if (is_resource($ldap) && $this->bind($ldap, $username, $password)) { + if ($ldap !== false && $this->bind($ldap, $username, $password)) { return $this->search($ldap, $username, $password); } @@ -108,13 +108,14 @@ class Ldap extends Base /** * LDAP connection * - * @access private - * @return resource $ldap LDAP connection + * @access public + * @return resource|boolean */ - private function connect() + public function connect() { if (! function_exists('ldap_connect')) { - die('The PHP LDAP extension is required'); + $this->logger->error('The PHP LDAP extension is required'); + return false; } // Skip SSL certificate verification @@ -124,8 +125,9 @@ class Ldap extends Base $ldap = ldap_connect(LDAP_SERVER, LDAP_PORT); - if (! is_resource($ldap)) { - die('Unable to connect to the LDAP server: "'.LDAP_SERVER.'"'); + if ($ldap === false) { + $this->logger->error('Unable to connect to the LDAP server: "'.LDAP_SERVER.'"'); + return false; } ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3); @@ -134,7 +136,8 @@ class Ldap extends Base ldap_set_option($ldap, LDAP_OPT_TIMELIMIT, 1); if (LDAP_START_TLS && ! @ldap_start_tls($ldap)) { - die('Unable to use ldap_start_tls()'); + $this->logger->error('Unable to use ldap_start_tls()'); + return false; } return $ldap; @@ -143,21 +146,24 @@ class Ldap extends Base /** * LDAP bind * - * @access private - * @param resource $ldap LDAP connection - * @param string $username Username - * @param string $password Password + * @access public + * @param resource $ldap + * @param string $username + * @param string $password + * @param string $ldap_type + * @param string $ldap_username + * @param string $ldap_password * @return boolean */ - private function bind($ldap, $username, $password) + public function bind($ldap, $username, $password, $ldap_type = LDAP_BIND_TYPE, $ldap_username = LDAP_USERNAME, $ldap_password = LDAP_PASSWORD) { - if (LDAP_BIND_TYPE === 'user') { - $ldap_username = sprintf(LDAP_USERNAME, $username); + if ($ldap_type === 'user') { + $ldap_username = sprintf($ldap_username, $username); $ldap_password = $password; } - else if (LDAP_BIND_TYPE === 'proxy') { - $ldap_username = LDAP_USERNAME; - $ldap_password = LDAP_PASSWORD; + else if ($ldap_type === 'proxy') { + $ldap_username = $ldap_username; + $ldap_password = $ldap_password; } else { $ldap_username = null; @@ -191,13 +197,12 @@ class Ldap extends Base $info = ldap_get_entries($ldap, $sr); // User not found - if (count($info) == 0 || $info['count'] == 0) { + if (count($info) === 0 || $info['count'] == 0) { return false; } // We got our user if (@ldap_bind($ldap, $info[0]['dn'], $password)) { - return array( 'username' => $username, 'name' => $this->getFromInfo($info, LDAP_ACCOUNT_FULLNAME), diff --git a/app/Controller/Board.php b/app/Controller/Board.php index 360a705f..a552b9cf 100644 --- a/app/Controller/Board.php +++ b/app/Controller/Board.php @@ -327,7 +327,7 @@ class Board extends Base */ public function swimlane() { - $project = $this->getProject(); + $this->getProject(); $swimlane = $this->swimlane->getById($this->request->getIntegerParam('swimlane_id')); $this->response->html($this->template->render('board/tooltip_description', array('task' => $swimlane))); } diff --git a/app/Controller/Doc.php b/app/Controller/Doc.php index 19644b84..d9f7b5e7 100644 --- a/app/Controller/Doc.php +++ b/app/Controller/Doc.php @@ -16,7 +16,7 @@ class Doc extends Base { $url = $this->helper->url; $data = file_get_contents($filename); - list($title,, $content) = explode("\n", $data, 3); + list($title,) = explode("\n", $data, 2); $replaceUrl = function (array $matches) use ($url) { return '('.$url->to('doc', 'show', array('file' => str_replace('.markdown', '', $matches[1]))).')'; -- cgit v1.2.3