diff --git a/src/Ltb/AttributeValue.php b/src/Ltb/AttributeValue.php index c7f55f7..2b76e8a 100644 --- a/src/Ltb/AttributeValue.php +++ b/src/Ltb/AttributeValue.php @@ -58,11 +58,10 @@ public static function ldap_get_first_available_value($ldap, $entry, $attributes * Get from ldap entry first value corresponding to $mail_attributes (globally configured) * @param $ldap php_ldap connection object * @param $entry ldap entry to parse + * @param $mail_attributes array containing mail attributes * @return mail to use for notification or empty string if not found */ - public static function ldap_get_mail_for_notification($ldap, $entry) { - # mail_attibutes are set globally in configuration - global $mail_attributes; + public static function ldap_get_mail_for_notification($ldap, $entry, $mail_attributes) { $mailValue = \Ltb\AttributeValue::ldap_get_first_available_value($ldap, $entry, $mail_attributes); $mail=""; if ( $mailValue ) { diff --git a/src/Ltb/Ldap.php b/src/Ltb/Ldap.php index 74a8265..ed320e9 100644 --- a/src/Ltb/Ldap.php +++ b/src/Ltb/Ldap.php @@ -1,29 +1,58 @@ ldap_url = $ldap_url; + $this->ldap_starttls = $ldap_starttls; + $this->ldap_binddn = $ldap_binddn; + $this->ldap_bindpw = $ldap_bindpw; + $this->ldap_network_timeout = $ldap_network_timeout; + $this->ldap_user_base = $ldap_user_base; + $this->ldap_size_limit = $ldap_size_limit; + $this->ldap_krb5ccname = $ldap_krb5ccname; + + } - static function connect($ldap_url, $ldap_starttls, $ldap_binddn, $ldap_bindpw, $ldap_network_timeout, $ldap_krb5ccname) { + function connect() { # Connect to LDAP - $ldap = \Ltb\PhpLDAP::ldap_connect($ldap_url); + $ldap = \Ltb\PhpLDAP::ldap_connect($this->ldap_url); \Ltb\PhpLDAP::ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3); \Ltb\PhpLDAP::ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0); - if ( isset($ldap_network_timeout) ) { - \Ltb\PhpLDAP::ldap_set_option($ldap, LDAP_OPT_NETWORK_TIMEOUT, $ldap_network_timeout); + if ( isset($this->ldap_network_timeout) ) { + \Ltb\PhpLDAP::ldap_set_option($ldap, LDAP_OPT_NETWORK_TIMEOUT, $this->ldap_network_timeout); } - if ( $ldap_starttls && !\Ltb\PhpLDAP::ldap_start_tls($ldap) ) { + if ( $this->ldap_starttls && !\Ltb\PhpLDAP::ldap_start_tls($ldap) ) { error_log("LDAP - Unable to use StartTLS"); return array(false, "ldaperror"); } # Bind - if ( isset($ldap_binddn) && isset($ldap_bindpw) ) { - $bind = \Ltb\PhpLDAP::ldap_bind($ldap, $ldap_binddn, $ldap_bindpw); - } elseif ( isset($ldap_krb5ccname) ) { + if ( isset($this->ldap_binddn) && isset($this->ldap_bindpw) ) { + $bind = \Ltb\PhpLDAP::ldap_bind($ldap, $this->ldap_binddn, $this->ldap_bindpw); + } elseif ( isset($this->ldap_krb5ccname) ) { putenv("KRB5CCNAME=".$ldap_krb5ccname); $bind = \Ltb\PhpLDAP::ldap_sasl_bind($ldap, NULL, NULL, 'GSSAPI') or error_log('LDAP - GSSAPI Bind failed'); } else { @@ -43,7 +72,65 @@ static function connect($ldap_url, $ldap_starttls, $ldap_binddn, $ldap_bindpw, $ return array($ldap, false); } - static function get_list($ldap, $ldap_base, $ldap_filter, $key, $value) { + function search($ldap_filter,$attributes, $attributes_map, $search_result_title, $search_result_sortby, $search_result_items) + { + + $result = ""; + $nb_entries = 0; + $entries = array(); + $size_limit_reached = false; + + # Connect to LDAP + $ldap_connection = $this->connect(); + + $ldap = $ldap_connection[0]; + $result = $ldap_connection[1]; + + if ($ldap) { + + foreach( $search_result_items as $item ) { + $attributes[] = $attributes_map[$item]['attribute']; + } + $attributes[] = $attributes_map[$search_result_title]['attribute']; + $attributes[] = $attributes_map[$search_result_sortby]['attribute']; + + # Search for users + $search = \Ltb\PhpLDAP::ldap_search($ldap, $this->ldap_user_base, $ldap_filter, $attributes, 0, $this->ldap_size_limit); + + $errno = \Ltb\PhpLDAP::ldap_errno($ldap); + + if ( $errno == 4) { + $size_limit_reached = true; + } + if ( $errno != 0 and $errno !=4 ) { + $result = "ldaperror"; + error_log("LDAP - Search error $errno (".\Ltb\PhpLDAP::ldap_error($ldap).")"); + } else { + + # Get search results + $nb_entries = \Ltb\PhpLDAP::ldap_count_entries($ldap, $search); + + if ($nb_entries === 0) { + $result = "noentriesfound"; + } else { + $entries = \Ltb\PhpLDAP::ldap_get_entries($ldap, $search); + + # Sort entries + if (isset($search_result_sortby)) { + $sortby = $attributes_map[$search_result_sortby]['attribute']; + $this->ldapSort($entries, $sortby); + } + + unset($entries["count"]); + } + } + } + + return [$ldap,$result,$nb_entries,$entries,$size_limit_reached]; + + } + + function get_list($ldap, $ldap_base, $ldap_filter, $key, $value) { $return = array(); @@ -70,7 +157,7 @@ static function get_list($ldap, $ldap_base, $ldap_filter, $key, $value) { } # if key is not found in attributes, order of entries is preserved - static function ldapSort(array &$entries, $key) + function ldapSort(array &$entries, $key) { # 'count' is an additionnal attribute of ldap entries that will be preserved # remove it since lost by usort ( changed to integer index ) @@ -96,11 +183,9 @@ static function ldapSort(array &$entries, $key) } - # not yet fully tested, please use ldapSort directly - # # ldap_search + ldap_sort combined done at server side if possible # if not supported fallback on client sorting. - static function sorted_search($ldap, $ldap_base, $ldap_filter, $attributes, $sortby, $ldap_size_limit) { + function sorted_search($ldap, $ldap_base, $ldap_filter, $attributes, $sortby, $ldap_size_limit) { if (isset($sortby) and $sortby) { @@ -127,7 +212,7 @@ static function sorted_search($ldap, $ldap_base, $ldap_filter, $attributes, $sor if ( $errno === 0 ) { $entries=\Ltb\PhpLDAP::ldap_get_entries($ldap, $ldap_result); - Ldap::ldapSort($entries,$sortby); + $this->ldapSort($entries,$sortby); } else { var_dump($errno); @@ -144,7 +229,7 @@ static function sorted_search($ldap, $ldap_base, $ldap_filter, $attributes, $sor * @param string $pwdattribute the Attribute that contains the password * @return array|false the values of the password, as returned by ldap_get_values */ - static function get_password_values($ldap, $dn, $pwdattribute): array|false { + function get_password_values($ldap, $dn, $pwdattribute): array|false { $search_userpassword = \Ltb\PhpLDAP::ldap_read($ldap, $dn, "(objectClass=*)", array($pwdattribute)); if ($search_userpassword) { return \Ltb\PhpLDAP::ldap_get_values($ldap, \Ltb\PhpLDAP::ldap_first_entry($ldap, $search_userpassword), $pwdattribute); @@ -160,7 +245,7 @@ static function get_password_values($ldap, $dn, $pwdattribute): array|false { * @param string $password the new password * @return array [$error_code, $error_msg] */ - static function change_ad_password_as_user($ldap, $dn, $oldpassword, $password): array { + function change_ad_password_as_user($ldap, $dn, $oldpassword, $password): array { # The AD password change procedure is modifying the attribute unicodePwd by # first deleting unicodePwd with the old password and them adding it with the # the new password @@ -185,7 +270,7 @@ static function change_ad_password_as_user($ldap, $dn, $oldpassword, $password): return array($error_code, $error_msg); } - static protected function get_ppolicy_error_code($ctrls) { + protected function get_ppolicy_error_code($ctrls) { if (isset($ctrls[LDAP_CONTROL_PASSWORDPOLICYRESPONSE])) { $value = $ctrls[LDAP_CONTROL_PASSWORDPOLICYRESPONSE]['value']; if (isset($value['error'])) { @@ -207,7 +292,7 @@ static protected function get_ppolicy_error_code($ctrls) { * @param bool $use_ppolicy_control * @return array 0: error_code, 1: error_msg, 2: ppolicy_error_code */ - static function change_password_with_exop($ldap, $dn, $oldpassword, $password, $use_ppolicy_control): array { + function change_password_with_exop($ldap, $dn, $oldpassword, $password, $use_ppolicy_control): array { $ppolicy_error_code = false; $exop_passwd = FALSE; if ( $use_ppolicy_control ) { @@ -233,7 +318,7 @@ static function change_password_with_exop($ldap, $dn, $oldpassword, $password, $ * @param array $userdata the array, containing the modifications * @return array 0: error_code, 1: error_msg, 2: ppolicy_error_code */ - static function modify_attributes_using_ppolicy($ldap, $dn, $userdata): array { + function modify_attributes_using_ppolicy($ldap, $dn, $userdata): array { $error_code = ""; $error_msg = ""; $matcheddn = null; @@ -254,7 +339,7 @@ static function modify_attributes_using_ppolicy($ldap, $dn, $userdata): array { * @param array $userdata the array, containing the new (hashed) password * @return array 0: error_code, 1: error_msg */ - static function modify_attributes($ldap, $dn, $userdata): array { + function modify_attributes($ldap, $dn, $userdata): array { \Ltb\PhpLDAP::ldap_mod_replace($ldap, $dn, $userdata); $error_code = \Ltb\PhpLDAP::ldap_errno($ldap); $error_msg = \Ltb\PhpLDAP::ldap_error($ldap); diff --git a/src/Ltb/LtbUtil.php b/src/Ltb/LtbUtil.php deleted file mode 100644 index c15daa0..0000000 --- a/src/Ltb/LtbUtil.php +++ /dev/null @@ -1,71 +0,0 @@ -Priority = $mail_priority; - $mailer->CharSet = $mail_charset; - $mailer->ContentType = $mail_contenttype; - $mailer->WordWrap = $mail_wordwrap; - $mailer->Sendmail = $mail_sendmailpath; - $mailer->Mailer = $mail_protocol; - $mailer->SMTPDebug = $mail_smtp_debug; - $mailer->Debugoutput = $mail_debug_format; - $mailer->Host = $mail_smtp_host; - $mailer->Port = $mail_smtp_port; - $mailer->SMTPSecure = $mail_smtp_secure; - $mailer->SMTPAutoTLS = $mail_smtp_autotls; - $mailer->SMTPAuth = $mail_smtp_auth; - $mailer->Username = $mail_smtp_user; - $mailer->Password = $mail_smtp_pass; - $mailer->SMTPKeepAlive = $mail_smtp_keepalive; - $mailer->SMTPOptions = $mail_smtp_options; - $mailer->Timeout = $mail_smtp_timeout; - - return $mailer; + parent::__construct(); + + $this->Priority = $mail_priority; + $this->CharSet = $mail_charset; + $this->ContentType = $mail_contenttype; + $this->WordWrap = $mail_wordwrap; + $this->Sendmail = $mail_sendmailpath; + $this->Mailer = $mail_protocol; + $this->SMTPDebug = $mail_smtp_debug; + $this->Debugoutput = $mail_debug_format; + $this->Host = $mail_smtp_host; + $this->Port = $mail_smtp_port; + $this->SMTPSecure = $mail_smtp_secure; + $this->SMTPAutoTLS = $mail_smtp_autotls; + $this->SMTPAuth = $mail_smtp_auth; + $this->Username = $mail_smtp_user; + $this->Password = $mail_smtp_pass; + $this->SMTPKeepAlive = $mail_smtp_keepalive; + $this->SMTPOptions = $mail_smtp_options; + $this->Timeout = $mail_smtp_timeout; + } - /* @function boolean send_mail_gloabl(PHPMailer $mailer, string $mail, string $mail_from, string $subject, string $body, array $data) + /* @function boolean send_mail(string $mail, string $mail_from, string $subject, string $body, array $data) * Send a mail, replace strings in body - * - * use global PHPMailer $mailer, create one from mail_XXX configurations if needed. - # - * @param mail Destination - * @param mail_from Sender - * @param subject Subject - * @param body Body - * @param data Data for string replacement - * @return result - */ - static function send_mail_global($mail, $mail_from, $mail_from_name, $subject, $body, $data) { - global $mailer; - if ( ! isset($mailer) ) - { - \Ltb\Mail::init_mailer(); - } - return \Ltb\Mail::send_mail($mailer, $mail, $mail_from, $mail_from_name, $subject, $body, $data); - } - - /* @function boolean send_mail(PHPMailer $mailer, string $mail, string $mail_from, string $subject, string $body, array $data) - * Send a mail, replace strings in body - * @param mailer PHPMailer object * @param mail Destination or array of destinations. * @param mail_from Sender * @param subject Subject @@ -79,15 +59,10 @@ static function send_mail_global($mail, $mail_from, $mail_from_name, $subject, $ * @param data Data for string replacement * @return result */ - static function send_mail($mailer, $mail, $mail_from, $mail_from_name, $subject, $body, $data) { + public function send_mail($mail, $mail_from, $mail_from_name, $subject, $body, $data) { $result = false; - if (!is_a($mailer, 'PHPMailer\PHPMailer\PHPMailer')) { - error_log("send_mail: PHPMailer object required!"); - return $result; - } - if (!$mail) { error_log("send_mail: no mail given, exiting..."); return $result; @@ -103,25 +78,25 @@ static function send_mail($mailer, $mail, $mail_from, $mail_from_name, $subject, } # if not done addAddress and addReplyTo are cumulated at each call - $mailer->clearAddresses(); - $mailer->setFrom($mail_from, $mail_from_name); - $mailer->addReplyTo($mail_from, $mail_from_name); + $this->clearAddresses(); + $this->setFrom($mail_from, $mail_from_name); + $this->addReplyTo($mail_from, $mail_from_name); # support list of mails if ( is_array($mail) ) { foreach( $mail as $mailstr ) { - $mailer->addAddress($mailstr); + $this->addAddress($mailstr); } } else { - $mailer->addAddress($mail); + $this->addAddress($mail); } - $mailer->Subject = $subject; - $mailer->Body = $body; + $this->Subject = $subject; + $this->Body = $body; - $result = $mailer->send(); + $result = $this->send(); if (!$result) { - error_log("send_mail: ".$mailer->ErrorInfo); + error_log("send_mail: ".$this->ErrorInfo); } return $result; diff --git a/tests/Ltb/AttributeValueTest.php b/tests/Ltb/AttributeValueTest.php index c2b0209..4ce1a85 100644 --- a/tests/Ltb/AttributeValueTest.php +++ b/tests/Ltb/AttributeValueTest.php @@ -46,7 +46,7 @@ public function test_ldap_get_mail_for_notification(): void { // global variable for ldap_get_mail_for_notification function - $GLOBALS['mail_attributes'] = array("mail"); + $mail_attributes = array("mail"); $phpLDAPMock = Mockery::mock('overload:Ltb\PhpLDAP'); $phpLDAPMock->shouldreceive([ @@ -59,7 +59,7 @@ public function test_ldap_get_mail_for_notification(): void ]); # Test ldap_get_mail_for_notification - $mail = Ltb\AttributeValue::ldap_get_mail_for_notification(null, null); + $mail = Ltb\AttributeValue::ldap_get_mail_for_notification(null, null, $mail_attributes); $this->assertEquals('test1@domain.com', $mail, "not getting test1@domain.com as mail for notification"); } @@ -67,7 +67,7 @@ public function test_ldap_get_proxy_for_notification(): void { // global variable for ldap_get_mail_for_notification function - $GLOBALS['mail_attributes'] = array("proxyAddresses"); + $mail_attributes = array("proxyAddresses"); $phpLDAPMock = Mockery::mock('overload:Ltb\PhpLDAP'); $phpLDAPMock->shouldreceive([ @@ -80,7 +80,7 @@ public function test_ldap_get_proxy_for_notification(): void ]); # Test ldap_get_mail_for_notification - $mail = Ltb\AttributeValue::ldap_get_mail_for_notification(null, null); + $mail = Ltb\AttributeValue::ldap_get_mail_for_notification(null, null, $mail_attributes); $this->assertEquals('test1@domain.com', $mail, "not getting test1@domain.com as proxyAddress for notification"); } diff --git a/tests/Ltb/LdapTest.php b/tests/Ltb/LdapTest.php index 045a5e2..6a4e652 100644 --- a/tests/Ltb/LdapTest.php +++ b/tests/Ltb/LdapTest.php @@ -2,12 +2,41 @@ require __DIR__ . '/../../vendor/autoload.php'; -// global variable for ldap_get_mail_for_notification function -$GLOBALS['mail_attributes'] = array("mail"); - final class LdapTest extends \Mockery\Adapter\Phpunit\MockeryTestCase { + // connection variables + public $ldap_url = "ldap://test.my-domain.com"; + public $ldap_starttls = false; + public $ldap_binddn = "cn=test,dc=my-domain,dc=com"; + public $ldap_bindpw = "secret"; + public $ldap_network_timeout = 10; + public $ldap_user_base = "ou=people,dc=my-domain,dc=com"; + public $ldap_size_limit = 1000; + public $ldap_krb5ccname = null; + + public function test_construct(): void + { + $ldapInstance = new \Ltb\Ldap( + $this->ldap_url, + $this->ldap_starttls, + $this->ldap_binddn, + $this->ldap_bindpw, + $this->ldap_network_timeout, + $this->ldap_user_base, + $this->ldap_size_limit, + $this->ldap_krb5ccname + ); + $this->assertEquals($this->ldap_url, $ldapInstance->ldap_url, "Error while initializing ldap_url"); + $this->assertEquals($this->ldap_starttls, $ldapInstance->ldap_starttls, "Error while initializing ldap_starttls"); + $this->assertEquals($this->ldap_binddn, $ldapInstance->ldap_binddn, "Error while initializing ldap_binddn"); + $this->assertEquals($this->ldap_bindpw, $ldapInstance->ldap_bindpw, "Error while initializing ldap_bindpw"); + $this->assertEquals($this->ldap_network_timeout, $ldapInstance->ldap_network_timeout, "Error while initializing ldap_network_timeout"); + $this->assertEquals($this->ldap_user_base, $ldapInstance->ldap_user_base, "Error while initializing ldap_user_base"); + $this->assertEquals($this->ldap_size_limit, $ldapInstance->ldap_size_limit, "Error while initializing ldap_size_limit"); + $this->assertEquals($this->ldap_krb5ccname, $ldapInstance->ldap_krb5ccname, "Error while initializing ldap_krb5ccname"); + } + public function test_connect(): void { @@ -24,12 +53,164 @@ public function test_connect(): void ->with("ldap_connection", "cn=test,dc=my-domain,dc=com","secret") ->andReturn(true); - list($ldap, $msg) = Ltb\Ldap::connect("ldap://test.my-domain.com", false, "cn=test,dc=my-domain,dc=com", "secret", 10, null); + $ldapInstance = new \Ltb\Ldap( + $this->ldap_url, + $this->ldap_starttls, + $this->ldap_binddn, + $this->ldap_bindpw, + $this->ldap_network_timeout, + $this->ldap_user_base, + $this->ldap_size_limit, + $this->ldap_krb5ccname + ); + list($ldap, $msg) = $ldapInstance->connect(); $this->assertNotFalse($ldap, "Error while connecting to LDAP server"); $this->assertFalse($msg, "Error message returned while connecting to LDAP server"); } + + + public function test_search(): void + { + + $ldap_filter = "(objectClass=inetOrgPerson)"; + $attributes = array("cn", "sn"); + $attributes_map = array( + 'authtimestamp' => array( 'attribute' => 'authtimestamp', 'faclass' => 'lock', 'type' => 'date' ), + 'businesscategory' => array( 'attribute' => 'businesscategory', 'faclass' => 'briefcase', 'type' => 'text' ), + 'carlicense' => array( 'attribute' => 'carlicense', 'faclass' => 'car', 'type' => 'text' ), + 'created' => array( 'attribute' => 'createtimestamp', 'faclass' => 'clock-o', 'type' => 'date' ), + 'description' => array( 'attribute' => 'description', 'faclass' => 'info-circle', 'type' => 'text' ), + 'displayname' => array( 'attribute' => 'displayname', 'faclass' => 'user-circle', 'type' => 'text' ), + 'employeenumber' => array( 'attribute' => 'employeenumber', 'faclass' => 'hashtag', 'type' => 'text' ), + 'employeetype' => array( 'attribute' => 'employeetype', 'faclass' => 'id-badge', 'type' => 'text' ), + 'fax' => array( 'attribute' => 'facsimiletelephonenumber', 'faclass' => 'fax', 'type' => 'tel' ), + 'firstname' => array( 'attribute' => 'givenname', 'faclass' => 'user-o', 'type' => 'text' ), + 'fullname' => array( 'attribute' => 'cn', 'faclass' => 'user-circle', 'type' => 'text' ), + 'identifier' => array( 'attribute' => 'uid', 'faclass' => 'user-o', 'type' => 'text' ), + 'l' => array( 'attribute' => 'l', 'faclass' => 'globe', 'type' => 'text' ), + 'lastname' => array( 'attribute' => 'sn', 'faclass' => 'user-o', 'type' => 'text' ), + 'mail' => array( 'attribute' => 'mail', 'faclass' => 'envelope-o', 'type' => 'mailto' ), + 'mailquota' => array( 'attribute' => 'gosamailquota', 'faclass' => 'pie-chart', 'type' => 'bytes' ), + 'manager' => array( 'attribute' => 'manager', 'faclass' => 'user-circle-o', 'type' => 'dn_link' ), + 'mobile' => array( 'attribute' => 'mobile', 'faclass' => 'mobile', 'type' => 'tel' ), + 'modified' => array( 'attribute' => 'modifytimestamp', 'faclass' => 'clock-o', 'type' => 'date' ), + 'organization' => array( 'attribute' => 'o', 'faclass' => 'building', 'type' => 'text' ), + 'organizationalunit' => array( 'attribute' => 'ou', 'faclass' => 'building-o', 'type' => 'text' ), + 'pager' => array( 'attribute' => 'pager', 'faclass' => 'mobile', 'type' => 'tel' ), + 'phone' => array( 'attribute' => 'telephonenumber', 'faclass' => 'phone', 'type' => 'tel' ), + 'postaladdress' => array( 'attribute' => 'postaladdress', 'faclass' => 'map-marker', 'type' => 'address' ), + 'postalcode' => array( 'attribute' => 'postalcode', 'faclass' => 'globe', 'type' => 'text' ), + 'pwdaccountlockedtime' => array( 'attribute' => 'pwdaccountlockedtime', 'faclass' => 'lock', 'type' => 'date' ), + 'pwdchangedtime' => array( 'attribute' => 'pwdchangedtime', 'faclass' => 'lock', 'type' => 'date' ), + 'pwdfailuretime' => array( 'attribute' => 'pwdfailuretime', 'faclass' => 'lock', 'type' => 'date' ), + 'pwdlastsuccess' => array( 'attribute' => 'pwdlastsuccess', 'faclass' => 'lock', 'type' => 'date' ), + 'pwdreset' => array( 'attribute' => 'pwdreset', 'faclass' => 'lock', 'type' => 'boolean' ), + 'secretary' => array( 'attribute' => 'secretary', 'faclass' => 'user-circle-o', 'type' => 'dn_link' ), + 'state' => array( 'attribute' => 'st', 'faclass' => 'globe', 'type' => 'text' ), + 'street' => array( 'attribute' => 'street', 'faclass' => 'map-marker', 'type' => 'text' ), + 'title' => array( 'attribute' => 'title', 'faclass' => 'certificate', 'type' => 'text' ), + ); + $search_result_title = "fullname"; + $search_result_sortby = "lastname"; + $search_result_items = array('identifier', 'mail', 'mobile'); + + + $entries = [ + 'count' => 2, + 0 => [ + 'count' => 2, + 0 => 'cn', + 1 => 'sn', + 'cn' => [ + 'count' => 1, + 0 => 'testcn1' + ], + 'sn' => [ + 'count' => 1, + 0 => 'zzzzzz' + ] + ], + 1 => [ + 'count' => 2, + 0 => 'cn', + 1 => 'sn', + 'cn' => [ + 'count' => 1, + 0 => 'testcn2' + ], + 'sn' => [ + 'count' => 1, + 0 => 'aaaaaa' + ] + ] + ]; + + $phpLDAPMock = Mockery::mock('overload:\Ltb\PhpLDAP'); + + $phpLDAPMock->shouldreceive('ldap_connect') + ->with($this->ldap_url) + ->andReturn("ldap_connection"); + + $phpLDAPMock->shouldreceive('ldap_set_option') + ->andReturn(null); + + $phpLDAPMock->shouldreceive('ldap_bind') + ->with("ldap_connection", $this->ldap_binddn, $this->ldap_bindpw) + ->andReturn(true); + + $phpLDAPMock->shouldreceive('ldap_search') + ->with("ldap_connection", + $this->ldap_user_base, + "(objectClass=inetOrgPerson)", + [0 => 'cn', 1 => 'sn', 2 => 'uid', 3 => 'mail', 4 => 'mobile', 5 => 'cn', 6 => 'sn'], + 0, + $this->ldap_size_limit + ) + ->andReturn("ldap_search_result"); + + $phpLDAPMock->shouldreceive('ldap_errno') + ->with("ldap_connection") + ->andReturn(0); + + $phpLDAPMock->shouldreceive('ldap_count_entries') + ->with("ldap_connection", "ldap_search_result") + ->andReturn(2); + + $phpLDAPMock->shouldreceive('ldap_get_entries') + ->with("ldap_connection","ldap_search_result") + ->andReturn($entries); + + $ldapInstance = new \Ltb\Ldap( + $this->ldap_url, + $this->ldap_starttls, + $this->ldap_binddn, + $this->ldap_bindpw, + $this->ldap_network_timeout, + $this->ldap_user_base, + $this->ldap_size_limit, + $this->ldap_krb5ccname + ); + list($ldap, $msg) = $ldapInstance->connect(); + + list($ldap,$result,$nb_entries,$res_entries,$size_limit_reached) = + $ldapInstance->search( $ldap_filter, + $attributes, + $attributes_map, + $search_result_title, + $search_result_sortby, + $search_result_items + ); + + $this->assertEquals("ldap_connection", $ldap, "Error while getting ldap_connection in search function"); + $this->assertFalse($result, "Error message returned while connecting to LDAP server in search function"); + $this->assertEquals(2, $nb_entries, "Wrong number of entries returned by search function"); + $this->assertEquals("testcn2", $res_entries[0]["cn"][0], "Wrong cn received in first entry. Entries may have not been sorted?"); + $this->assertEquals("testcn1", $res_entries[1]["cn"][0], "Wrong cn received in second entry. Entries may have not been sorted?"); + $this->assertFalse($size_limit_reached, "Unexpected size limit reached in search function"); + } + public function test_get_list(): void { @@ -75,8 +256,9 @@ public function test_get_list(): void ] ]); + $ldapInstance = new \Ltb\Ldap( null, null, null, null, null, null, null, null ); // return hashmap: [ cn_value => sn_value ] - $result = Ltb\Ldap::get_list("ldap_connection", "ou=people,dc=my-domain,dc=com", "(uid=test)", "cn","sn"); + $result = $ldapInstance->get_list("ldap_connection", "ou=people,dc=my-domain,dc=com", "(uid=test)", "cn","sn"); $this->assertEquals('testcn1', array_keys($result)[0], "not getting testcn1 as key in get_list function"); $this->assertEquals('testsn1', $result["testcn1"], "not getting testsn1 as value in get_list function"); @@ -119,7 +301,8 @@ public function test_ldapSort(): void ] ]; - $return = Ltb\Ldap::ldapSort($entries, "sn"); + $ldapInstance = new \Ltb\Ldap( null, null, null, null, null, null, null, null ); + $return = $ldapInstance->ldapSort($entries, "sn"); $this->assertTrue($return, "Weird value returned by ldapSort function"); $this->assertEquals('testcn2', $entries[0]['cn'][0], "testcn2 has not been ordered correctly in entries array"); @@ -208,7 +391,8 @@ public function test_sorted_search_with_sort_control(): void ->with("ldap_connection") ->andReturn(0); - list($ldap_result,$errno,$entries) = Ltb\Ldap::sorted_search("ldap_connection", + $ldapInstance = new \Ltb\Ldap( null, null, null, null, null, null, null, null ); + list($ldap_result,$errno,$entries) = $ldapInstance->sorted_search("ldap_connection", "ou=people,dc=my-domain,dc=com", "(objectClass=InetOrgPerson)", ["cn", "sn"], @@ -302,13 +486,14 @@ public function test_sorted_search_without_sort_control(): void ->with("ldap_connection") ->andReturn(0); - list($ldap_result,$errno,$entries) = Ltb\Ldap::sorted_search("ldap_connection", - "ou=people,dc=my-domain,dc=com", - "(objectClass=InetOrgPerson)", - ["cn", "sn"], - "sn", - 1000 - ); + $ldapInstance = new \Ltb\Ldap( null, null, null, null, null, null, null, null ); + list($ldap_result,$errno,$entries) = $ldapInstance->sorted_search("ldap_connection", + "ou=people,dc=my-domain,dc=com", + "(objectClass=InetOrgPerson)", + ["cn", "sn"], + "sn", + 1000 + ); $this->assertEquals("ldap_search_result", $ldap_result, "error while getting ldap_search sorted result"); $this->assertEquals(0, $errno, "error code invalid while getting ldap_search sorted result"); @@ -342,11 +527,12 @@ public function test_get_password_value(): void ->with($ldap_connection, "result_entry", $pwdattribute) ->andReturn($expectedValues); - $values = Ltb\Ldap::get_password_values( - $ldap_connection, - $dn, - $pwdattribute - ); + $ldapInstance = new \Ltb\Ldap( null, null, null, null, null, null, null, null ); + $values = $ldapInstance->get_password_values( + $ldap_connection, + $dn, + $pwdattribute + ); $this->assertEquals(1, $values['count'], "error while getting cardinal of password values in get_password_value"); $this->assertEquals('secret', $values[0], "wrong password value in get_password_value"); @@ -366,11 +552,12 @@ public function test_get_password_value_with_dummy_pwdattribute(): void ->with($ldap_connection, $dn, '(objectClass=*)', [ $pwdattribute ]) ->andReturn(false); - $values = Ltb\Ldap::get_password_values( - $ldap_connection, - $dn, - $pwdattribute - ); + $ldapInstance = new \Ltb\Ldap( null, null, null, null, null, null, null, null ); + $values = $ldapInstance->get_password_values( + $ldap_connection, + $dn, + $pwdattribute + ); $this->assertFalse($values, 'Weird returned value in get_password_value while sending dummy $pwdattribute'); } @@ -423,13 +610,14 @@ public function test_change_ad_password_as_user(): void ->andReturn($hased_old_password); + $ldapInstance = new \Ltb\Ldap( null, null, null, null, null, null, null, null ); list($error_code, $error_msg) = - Ltb\Ldap::change_ad_password_as_user( - $ldap_connection, - $dn, - $old_password, - $new_password - ); + $ldapInstance->change_ad_password_as_user( + $ldap_connection, + $dn, + $old_password, + $new_password + ); $this->assertEquals(0, $error_code, 'Weird error code returned in change_ad_password_as_user'); $this->assertEquals("ok", $error_msg, 'Weird msg returned in change_ad_password_as_user'); @@ -439,7 +627,7 @@ public function test_change_ad_password_as_user(): void public function test_get_ppolicy_error_code(): void { - // method get_ppolicy_error_code cannot be tested as it is protected (and Ldap class is final) + // method get_ppolicy_error_code cannot be tested as it is protected $this->assertTrue(method_exists("\Ltb\Ldap",'get_ppolicy_error_code'), 'No method get_ppolicy_error_code in class'); @@ -469,14 +657,15 @@ public function test_change_password_with_exop_noppolicy(): void ->with($ldap_connection) ->andReturn("ok"); + $ldapInstance = new \Ltb\Ldap( null, null, null, null, null, null, null, null ); list($error_code, $error_msg, $ppolicy_error_code) = - Ltb\Ldap::change_password_with_exop( - $ldap_connection, - $dn, - $old_password, - $new_password, - $ppolicy - ); + $ldapInstance->change_password_with_exop( + $ldap_connection, + $dn, + $old_password, + $new_password, + $ppolicy + ); $this->assertEquals(0, $error_code, 'Weird error code returned in change_password_with_exop'); $this->assertEquals("ok", $error_msg, 'Weird msg returned in change_password_with_exop'); @@ -508,14 +697,15 @@ public function test_change_password_with_exop_ppolicy(): void ->with($ldap_connection) ->andReturn("ok"); + $ldapInstance = new \Ltb\Ldap( null, null, null, null, null, null, null, null ); list($error_code, $error_msg, $ppolicy_error_code) = - Ltb\Ldap::change_password_with_exop( - $ldap_connection, - $dn, - $old_password, - $new_password, - $ppolicy - ); + $ldapInstance->change_password_with_exop( + $ldap_connection, + $dn, + $old_password, + $new_password, + $ppolicy + ); $this->assertEquals(0, $error_code, 'Weird error code returned in change_password_with_exop with policy'); $this->assertEquals("ok", $error_msg, 'Weird msg returned in change_password_with_exop with policy'); @@ -547,14 +737,15 @@ public function test_change_password_with_exop_ppolicy_fail(): void ->with($ldap_connection) ->andReturn("Invalid credentials"); + $ldapInstance = new \Ltb\Ldap( null, null, null, null, null, null, null, null ); list($error_code, $error_msg, $ppolicy_error_code) = - Ltb\Ldap::change_password_with_exop( - $ldap_connection, - $dn, - $old_password, - $new_password, - $ppolicy - ); + $ldapInstance->change_password_with_exop( + $ldap_connection, + $dn, + $old_password, + $new_password, + $ppolicy + ); $this->assertEquals(49, $error_code, 'Weird error code returned in failing change_password_with_exop with policy'); $this->assertEquals("Invalid credentials", $error_msg, 'Weird msg returned in failing change_password_with_exop with policy'); @@ -586,8 +777,9 @@ public function test_modify_attributes_using_ppolicy(): void ->andReturn($res); + $ldapInstance = new \Ltb\Ldap( null, null, null, null, null, null, null, null ); list($error_code, $error_msg, $ppolicy_error_code) = - Ltb\Ldap::modify_attributes_using_ppolicy( + $ldapInstance->modify_attributes_using_ppolicy( $ldap_connection, $dn, $userdata @@ -625,12 +817,13 @@ public function test_modify_attributes(): void ->with($ldap_connection) ->andReturn("ok"); + $ldapInstance = new \Ltb\Ldap( null, null, null, null, null, null, null, null ); list($error_code, $error_msg) = - Ltb\Ldap::modify_attributes( - $ldap_connection, - $dn, - $userdata - ); + $ldapInstance->modify_attributes( + $ldap_connection, + $dn, + $userdata + ); $this->assertEquals(0, $error_code, 'Weird error code returned in modify_attributes'); $this->assertEquals("ok", $error_msg, 'Weird msg returned in modify_attributes'); diff --git a/tests/Ltb/LtbUtilTest.php b/tests/Ltb/LtbUtilTest.php deleted file mode 100644 index 43e948b..0000000 --- a/tests/Ltb/LtbUtilTest.php +++ /dev/null @@ -1,138 +0,0 @@ - array( 'attribute' => 'authtimestamp', 'faclass' => 'lock', 'type' => 'date' ), - 'businesscategory' => array( 'attribute' => 'businesscategory', 'faclass' => 'briefcase', 'type' => 'text' ), - 'carlicense' => array( 'attribute' => 'carlicense', 'faclass' => 'car', 'type' => 'text' ), - 'created' => array( 'attribute' => 'createtimestamp', 'faclass' => 'clock-o', 'type' => 'date' ), - 'description' => array( 'attribute' => 'description', 'faclass' => 'info-circle', 'type' => 'text' ), - 'displayname' => array( 'attribute' => 'displayname', 'faclass' => 'user-circle', 'type' => 'text' ), - 'employeenumber' => array( 'attribute' => 'employeenumber', 'faclass' => 'hashtag', 'type' => 'text' ), - 'employeetype' => array( 'attribute' => 'employeetype', 'faclass' => 'id-badge', 'type' => 'text' ), - 'fax' => array( 'attribute' => 'facsimiletelephonenumber', 'faclass' => 'fax', 'type' => 'tel' ), - 'firstname' => array( 'attribute' => 'givenname', 'faclass' => 'user-o', 'type' => 'text' ), - 'fullname' => array( 'attribute' => 'cn', 'faclass' => 'user-circle', 'type' => 'text' ), - 'identifier' => array( 'attribute' => 'uid', 'faclass' => 'user-o', 'type' => 'text' ), - 'l' => array( 'attribute' => 'l', 'faclass' => 'globe', 'type' => 'text' ), - 'lastname' => array( 'attribute' => 'sn', 'faclass' => 'user-o', 'type' => 'text' ), - 'mail' => array( 'attribute' => 'mail', 'faclass' => 'envelope-o', 'type' => 'mailto' ), - 'mailquota' => array( 'attribute' => 'gosamailquota', 'faclass' => 'pie-chart', 'type' => 'bytes' ), - 'manager' => array( 'attribute' => 'manager', 'faclass' => 'user-circle-o', 'type' => 'dn_link' ), - 'mobile' => array( 'attribute' => 'mobile', 'faclass' => 'mobile', 'type' => 'tel' ), - 'modified' => array( 'attribute' => 'modifytimestamp', 'faclass' => 'clock-o', 'type' => 'date' ), - 'organization' => array( 'attribute' => 'o', 'faclass' => 'building', 'type' => 'text' ), - 'organizationalunit' => array( 'attribute' => 'ou', 'faclass' => 'building-o', 'type' => 'text' ), - 'pager' => array( 'attribute' => 'pager', 'faclass' => 'mobile', 'type' => 'tel' ), - 'phone' => array( 'attribute' => 'telephonenumber', 'faclass' => 'phone', 'type' => 'tel' ), - 'postaladdress' => array( 'attribute' => 'postaladdress', 'faclass' => 'map-marker', 'type' => 'address' ), - 'postalcode' => array( 'attribute' => 'postalcode', 'faclass' => 'globe', 'type' => 'text' ), - 'pwdaccountlockedtime' => array( 'attribute' => 'pwdaccountlockedtime', 'faclass' => 'lock', 'type' => 'date' ), - 'pwdchangedtime' => array( 'attribute' => 'pwdchangedtime', 'faclass' => 'lock', 'type' => 'date' ), - 'pwdfailuretime' => array( 'attribute' => 'pwdfailuretime', 'faclass' => 'lock', 'type' => 'date' ), - 'pwdlastsuccess' => array( 'attribute' => 'pwdlastsuccess', 'faclass' => 'lock', 'type' => 'date' ), - 'pwdreset' => array( 'attribute' => 'pwdreset', 'faclass' => 'lock', 'type' => 'boolean' ), - 'secretary' => array( 'attribute' => 'secretary', 'faclass' => 'user-circle-o', 'type' => 'dn_link' ), - 'state' => array( 'attribute' => 'st', 'faclass' => 'globe', 'type' => 'text' ), - 'street' => array( 'attribute' => 'street', 'faclass' => 'map-marker', 'type' => 'text' ), - 'title' => array( 'attribute' => 'title', 'faclass' => 'certificate', 'type' => 'text' ), -); -$GLOBALS['search_result_title'] = "fullname"; -$GLOBALS['search_result_sortby'] = "lastname"; -$GLOBALS['search_result_items'] = array('identifier', 'mail', 'mobile'); - -final class LtbUtilTest extends \Mockery\Adapter\Phpunit\MockeryTestCase -{ - - public function test_search(): void - { - - $entries = [ - 'count' => 2, - 0 => [ - 'count' => 2, - 0 => 'cn', - 1 => 'sn', - 'cn' => [ - 'count' => 1, - 0 => 'testcn1' - ], - 'sn' => [ - 'count' => 1, - 0 => 'zzzzzz' - ] - ], - 1 => [ - 'count' => 2, - 0 => 'cn', - 1 => 'sn', - 'cn' => [ - 'count' => 1, - 0 => 'testcn2' - ], - 'sn' => [ - 'count' => 1, - 0 => 'aaaaaa' - ] - ] - ]; - - $phpLDAPMock = Mockery::mock('overload:\Ltb\PhpLDAP'); - - $phpLDAPMock->shouldreceive('ldap_connect') - ->with($GLOBALS['ldap_url']) - ->andReturn("ldap_connection"); - - $phpLDAPMock->shouldreceive('ldap_set_option') - ->andReturn(null); - - $phpLDAPMock->shouldreceive('ldap_bind') - ->with("ldap_connection", $GLOBALS['ldap_binddn'], $GLOBALS['ldap_bindpw']) - ->andReturn(true); - - $phpLDAPMock->shouldreceive('ldap_search') - ->with("ldap_connection", - $GLOBALS['ldap_user_base'], - "(objectClass=inetOrgPerson)", - [0 => 'cn', 1 => 'sn', 2 => 'uid', 3 => 'mail', 4 => 'mobile', 5 => 'cn', 6 => 'sn'], - 0, - $GLOBALS['ldap_size_limit'] - ) - ->andReturn("ldap_search_result"); - - $phpLDAPMock->shouldreceive('ldap_errno') - ->with("ldap_connection") - ->andReturn(0); - - $phpLDAPMock->shouldreceive('ldap_count_entries') - ->with("ldap_connection", "ldap_search_result") - ->andReturn(2); - - $phpLDAPMock->shouldreceive('ldap_get_entries') - ->with("ldap_connection","ldap_search_result") - ->andReturn($entries); - - list($ldap,$result,$nb_entries,$res_entries,$size_limit_reached) = - Ltb\LtbUtil::search( "(objectClass=inetOrgPerson)", - array("cn", "sn") - ); - - $this->assertEquals("ldap_connection", $ldap, "Error while getting ldap_connection in search function"); - $this->assertFalse($result, "Error message returned while connecting to LDAP server in search function"); - $this->assertEquals(2, $nb_entries, "Wrong number of entries returned by search function"); - $this->assertEquals("testcn2", $res_entries[0]["cn"][0], "Wrong cn received in first entry. Entries may have not been sorted?"); - $this->assertEquals("testcn1", $res_entries[1]["cn"][0], "Wrong cn received in second entry. Entries may have not been sorted?"); - $this->assertFalse($size_limit_reached, "Unexpected size limit reached in search function"); - } - -} diff --git a/tests/Ltb/MailTest.php b/tests/Ltb/MailTest.php index ab8aece..c727230 100644 --- a/tests/Ltb/MailTest.php +++ b/tests/Ltb/MailTest.php @@ -2,53 +2,71 @@ require __DIR__ . '/../../vendor/autoload.php'; -$GLOBALS['mail_priority'] = 3; // Options: null (default), 1 = High, 3 = Normal, 5 = low. When null, the header is not set at all. -$GLOBALS['mail_charset'] = 'utf-8'; -$GLOBALS['mail_contenttype'] = 'text/plain'; -$GLOBALS['mail_wordwrap'] = 0; -$GLOBALS['mail_sendmailpath'] = '/usr/sbin/sendmail'; -$GLOBALS['mail_protocol'] = 'smtp'; -$GLOBALS['mail_smtp_debug'] = 0; -$GLOBALS['mail_debug_format'] = 'error_log'; -$GLOBALS['mail_smtp_host'] = '127.0.0.1'; -$GLOBALS['mail_smtp_port'] = '25'; -$GLOBALS['mail_smtp_secure'] = false; -$GLOBALS['mail_smtp_autotls'] = false; -$GLOBALS['mail_smtp_auth'] = false; -$GLOBALS['mail_smtp_user'] = ''; -$GLOBALS['mail_smtp_pass'] = ''; -$GLOBALS['mail_smtp_keepalive'] = false; -$GLOBALS['mail_smtp_options'] = array(); -$GLOBALS['mail_smtp_timeout'] = 30; - final class MailTest extends \Mockery\Adapter\Phpunit\MockeryTestCase { - public function test_init_mailer(): void + public function test_constructor(): void { - $mailer = Ltb\Mail::init_mailer(); - - $this->assertEquals($GLOBALS['mail_priority'], $mailer->Priority, "Error while setting mail_priority"); - $this->assertEquals($GLOBALS['mail_charset'], $mailer->CharSet, "Error while setting mail_charset"); - $this->assertEquals($GLOBALS['mail_contenttype'], $mailer->ContentType, "Error while setting mail_contenttype"); - $this->assertEquals($GLOBALS['mail_wordwrap'], $mailer->WordWrap, "Error while setting mail_wordwrap"); - $this->assertEquals($GLOBALS['mail_sendmailpath'], $mailer->Sendmail, "Error while setting mail_sendmailpath"); - $this->assertEquals($GLOBALS['mail_protocol'], $mailer->Mailer, "Error while setting mail_protocol"); - $this->assertEquals($GLOBALS['mail_smtp_debug'], $mailer->SMTPDebug, "Error while setting mail_smtp_debug"); - $this->assertEquals($GLOBALS['mail_debug_format'], $mailer->Debugoutput, "Error while setting mail_debug_format"); - $this->assertEquals($GLOBALS['mail_smtp_host'], $mailer->Host, "Error while setting mail_smtp_host"); - $this->assertEquals($GLOBALS['mail_smtp_port'], $mailer->Port, "Error while setting mail_smtp_port"); - $this->assertEquals($GLOBALS['mail_smtp_secure'], $mailer->SMTPSecure, "Error while setting mail_smtp_secure"); - $this->assertEquals($GLOBALS['mail_smtp_autotls'], $mailer->SMTPAutoTLS, "Error while setting mail_smtp_autotls"); - $this->assertEquals($GLOBALS['mail_smtp_auth'], $mailer->SMTPAuth, "Error while setting mail_smtp_auth"); - $this->assertEquals($GLOBALS['mail_smtp_user'], $mailer->Username, "Error while setting mail_smtp_user"); - $this->assertEquals($GLOBALS['mail_smtp_pass'], $mailer->Password, "Error while setting mail_smtp_pass"); - $this->assertEquals($GLOBALS['mail_smtp_keepalive'], $mailer->SMTPKeepAlive, "Error while setting mail_smtp_keepalive"); - $this->assertEquals($GLOBALS['mail_smtp_options'], $mailer->SMTPOptions, "Error while setting mail_smtp_options"); - $this->assertEquals($GLOBALS['mail_smtp_timeout'], $mailer->Timeout, "Error while setting mail_smtp_timeout"); + $mail_priority = 3; // Options: null (default), 1 = High, 3 = Normal, 5 = low. When null, the header is not set at all. + $mail_charset = 'utf-8'; + $mail_contenttype = 'text/plain'; + $mail_wordwrap = 0; + $mail_sendmailpath = '/usr/sbin/sendmail'; + $mail_protocol = 'smtp'; + $mail_smtp_debug = 0; + $mail_debug_format = 'error_log'; + $mail_smtp_host = '127.0.0.1'; + $mail_smtp_port = '25'; + $mail_smtp_secure = false; + $mail_smtp_autotls = false; + $mail_smtp_auth = false; + $mail_smtp_user = ''; + $mail_smtp_pass = ''; + $mail_smtp_keepalive = false; + $mail_smtp_options = array(); + $mail_smtp_timeout = 30; + + $mailer = new \Ltb\Mail( $mail_priority, + $mail_charset, + $mail_contenttype, + $mail_wordwrap, + $mail_sendmailpath, + $mail_protocol, + $mail_smtp_debug, + $mail_debug_format, + $mail_smtp_host, + $mail_smtp_port, + $mail_smtp_secure, + $mail_smtp_autotls, + $mail_smtp_auth, + $mail_smtp_user, + $mail_smtp_pass, + $mail_smtp_keepalive, + $mail_smtp_options, + $mail_smtp_timeout + ); + + $this->assertEquals($mail_priority, $mailer->Priority, "Error while setting mail_priority"); + $this->assertEquals($mail_charset, $mailer->CharSet, "Error while setting mail_charset"); + $this->assertEquals($mail_contenttype, $mailer->ContentType, "Error while setting mail_contenttype"); + $this->assertEquals($mail_wordwrap, $mailer->WordWrap, "Error while setting mail_wordwrap"); + $this->assertEquals($mail_sendmailpath, $mailer->Sendmail, "Error while setting mail_sendmailpath"); + $this->assertEquals($mail_protocol, $mailer->Mailer, "Error while setting mail_protocol"); + $this->assertEquals($mail_smtp_debug, $mailer->SMTPDebug, "Error while setting mail_smtp_debug"); + $this->assertEquals($mail_debug_format, $mailer->Debugoutput, "Error while setting mail_debug_format"); + $this->assertEquals($mail_smtp_host, $mailer->Host, "Error while setting mail_smtp_host"); + $this->assertEquals($mail_smtp_port, $mailer->Port, "Error while setting mail_smtp_port"); + $this->assertEquals($mail_smtp_secure, $mailer->SMTPSecure, "Error while setting mail_smtp_secure"); + $this->assertEquals($mail_smtp_autotls, $mailer->SMTPAutoTLS, "Error while setting mail_smtp_autotls"); + $this->assertEquals($mail_smtp_auth, $mailer->SMTPAuth, "Error while setting mail_smtp_auth"); + $this->assertEquals($mail_smtp_user, $mailer->Username, "Error while setting mail_smtp_user"); + $this->assertEquals($mail_smtp_pass, $mailer->Password, "Error while setting mail_smtp_pass"); + $this->assertEquals($mail_smtp_keepalive, $mailer->SMTPKeepAlive, "Error while setting mail_smtp_keepalive"); + $this->assertEquals($mail_smtp_options, $mailer->SMTPOptions, "Error while setting mail_smtp_options"); + $this->assertEquals($mail_smtp_timeout, $mailer->Timeout, "Error while setting mail_smtp_timeout"); } @@ -68,7 +86,7 @@ public function test_send_mail(): void $subject = 'Mail test to {login}'; $body = 'Hello {login}, this is a mail test from {mail_from}. Your new password is {password}'; - $mailerMock = Mockery::mock('PHPMailer\PHPMailer\PHPMailer'); + $mailerMock = Mockery::mock('\Ltb\Mail')->makePartial(); $mailerMock->shouldreceive('clearAddresses') ->andReturn(true); @@ -88,19 +106,22 @@ public function test_send_mail(): void $mailerMock->shouldreceive('send') ->andReturn(true); - $result = Ltb\Mail::send_mail($mailerMock, $mail, $mail_from, $mail_from_name, $subject, $body, $data); + $mailerMock->__construct(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null); + + $result = $mailerMock->send_mail($mail, $mail_from, $mail_from_name, $subject, $body, $data); $this->assertEquals('Mail test to ltbtest', $mailerMock->Subject, "Error while processing subject"); $this->assertEquals('Hello ltbtest, this is a mail test from ltbadminsender@example.com. Your new password is secret', $mailerMock->Body, "Error while processing body"); + $this->assertNotFalse($result, "Error in send() result"); } - public function test_send_mail_global(): void + public function test_send_mail_not_initialized(): void { $mail_from = "{mail_from}"; $mail_from_name = "ltb admin sender"; $mail_signature = ""; - $mail = ['{mail_to}','ltbadmin@domain.com']; + $mail = null; $data = [ 'mail_from' => 'ltbadminsender@example.com', "login" => 'ltbtest', @@ -110,30 +131,10 @@ public function test_send_mail_global(): void $subject = 'Mail test to {login}'; $body = 'Hello {login}, this is a mail test from {mail_from}. Your new password is {password}'; - $GLOBALS['mailer'] = Mockery::mock('PHPMailer\PHPMailer\PHPMailer'); + $mailer = new \Ltb\Mail(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null); - $GLOBALS['mailer']->shouldreceive('clearAddresses') - ->andReturn(true); + $result = $mailer->send_mail($mail, $mail_from, $mail_from_name, $subject, $body, $data); - $GLOBALS['mailer']->shouldreceive('setFrom') - ->with('ltbadminsender@example.com', 'ltb admin sender') - ->andReturn(true); - - $GLOBALS['mailer']->shouldreceive('addReplyTo') - ->with('ltbadminsender@example.com', 'ltb admin sender') - ->andReturn(true); - - $GLOBALS['mailer']->shouldreceive('addAddress') - ->with(Mockery::anyOf('ltbtest@domain.com', 'ltbadmin@domain.com')) - ->andReturn(true); - - $GLOBALS['mailer']->shouldreceive('send') - ->andReturn(true); - - $result = Ltb\Mail::send_mail_global($mail, $mail_from, $mail_from_name, $subject, $body, $data); - - $this->assertEquals('Mail test to ltbtest', $GLOBALS['mailer']->Subject, "Error while processing subject"); - $this->assertEquals('Hello ltbtest, this is a mail test from ltbadminsender@example.com. Your new password is secret', $GLOBALS['mailer']->Body, "Error while processing body"); + $this->assertFalse($result, "Unexpected 'not false' send() result"); } - }