]> source.dussan.org Git - nextcloud-server.git/commitdiff
add unit test for mail detection
authorArthur Schiwon <blizzz@owncloud.com>
Wed, 25 Jun 2014 16:01:04 +0000 (18:01 +0200)
committerArthur Schiwon <blizzz@owncloud.com>
Fri, 29 Aug 2014 09:14:35 +0000 (11:14 +0200)
apps/user_ldap/lib/wizard.php
apps/user_ldap/tests/wizard.php
lib/private/log/owncloud.php

index 25dc4598ffb8c76255df436a142828804b7d5878..36b855e83f6f09c28fcc4e1be45af7b5c5d9158f 100644 (file)
@@ -155,7 +155,7 @@ class Wizard extends LDAPUtility {
         * detects the most often used email attribute for users applying to the
         * user list filter. If a setting is already present that returns at least
         * one hit, the detection will be canceled.
-        * @return bool
+        * @return bool|string
         */
        public function detectEmailAttribute() {
                if(!$this->checkRequirements(array('ldapHost',
@@ -189,6 +189,7 @@ class Wizard extends LDAPUtility {
                        $this->result->addChange('ldap_email_attr', $winner);
                }
 
+               return $winner;
        }
 
        /**
index fcf4d71087f5aaa292377013dbf5da291fe81107..d6de83bbdd6771112c611eb34c664d4ed53194b5 100644 (file)
@@ -63,7 +63,7 @@ class Test_Wizard extends \PHPUnit_Framework_TestCase {
                $access = $this->getMock('\OCA\user_ldap\lib\Access',
                        $accMethods, array($connector, $lw, $um));
 
-               return array(new Wizard($conf, $lw, $access), $conf, $lw);
+               return array(new Wizard($conf, $lw, $access), $conf, $lw, $access);
        }
 
        private function prepareLdapWrapperForConnections(&$ldap) {
@@ -218,6 +218,142 @@ class Test_Wizard extends \PHPUnit_Framework_TestCase {
                unset($uidnumber);
        }
 
+       public function testDetectEmailAttributeAlreadySet() {
+               list($wizard, $configuration, $ldap, $access)
+                       = $this->getWizardAndMocks();
+
+               $configuration->expects($this->any())
+                       ->method('__get')
+                       ->will($this->returnCallback(function ($name) {
+                               if($name === 'ldapEmailAttribute') {
+                                       return 'myEmailAttribute';
+                               } else {
+                                       //for requirement checks
+                                       return 'let me pass';
+                               }
+                       }));
+
+               $access->expects($this->once())
+                       ->method('countUsers')
+                       ->will($this->returnValue(42));
+
+               $wizard->detectEmailAttribute();
+       }
+
+       public function testDetectEmailAttributeOverrideSet() {
+               list($wizard, $configuration, $ldap, $access)
+                       = $this->getWizardAndMocks();
+
+               $configuration->expects($this->any())
+                       ->method('__get')
+                       ->will($this->returnCallback(function ($name) {
+                               if($name === 'ldapEmailAttribute') {
+                                       return 'myEmailAttribute';
+                               } else {
+                                       //for requirement checks
+                                       return 'let me pass';
+                               }
+                       }));
+
+               $access->expects($this->exactly(3))
+                       ->method('combineFilterWithAnd')
+                       ->will($this->returnCallback(function ($filterParts) {
+                               return str_replace('=*', '', array_pop($filterParts));
+                       }));
+
+               $access->expects($this->exactly(3))
+                       ->method('countUsers')
+                       ->will($this->returnCallback(function ($filter) {
+                               if($filter === 'myEmailAttribute') {
+                                       return 0;
+                               } else if($filter === 'mail') {
+                                       return 3;
+                               } else if($filter === 'mailPrimaryAddress') {
+                                       return 17;
+                               }
+                               var_dump($filter);
+                       }));
+
+               $resultAttribute = $wizard->detectEmailAttribute();
+               $this->assertSame('mailPrimaryAddress', $resultAttribute);
+       }
+
+       public function testDetectEmailAttributeFind() {
+               list($wizard, $configuration, $ldap, $access)
+                       = $this->getWizardAndMocks();
+
+               $configuration->expects($this->any())
+                       ->method('__get')
+                       ->will($this->returnCallback(function ($name) {
+                               if($name === 'ldapEmailAttribute') {
+                                       return '';
+                               } else {
+                                       //for requirement checks
+                                       return 'let me pass';
+                               }
+                       }));
+
+               $access->expects($this->exactly(2))
+                       ->method('combineFilterWithAnd')
+                       ->will($this->returnCallback(function ($filterParts) {
+                               return str_replace('=*', '', array_pop($filterParts));
+                       }));
+
+               $access->expects($this->exactly(2))
+                       ->method('countUsers')
+                       ->will($this->returnCallback(function ($filter) {
+                               if($filter === 'myEmailAttribute') {
+                                       return 0;
+                               } else if($filter === 'mail') {
+                                       return 3;
+                               } else if($filter === 'mailPrimaryAddress') {
+                                       return 17;
+                               }
+                               var_dump($filter);
+                       }));
+
+               $resultAttribute = $wizard->detectEmailAttribute();
+               $this->assertSame('mailPrimaryAddress', $resultAttribute);
+       }
+
+       public function testDetectEmailAttributeFindNothing() {
+               list($wizard, $configuration, $ldap, $access)
+                       = $this->getWizardAndMocks();
+
+               $configuration->expects($this->any())
+                       ->method('__get')
+                       ->will($this->returnCallback(function ($name) {
+                               if($name === 'ldapEmailAttribute') {
+                                       return 'myEmailAttribute';
+                               } else {
+                                       //for requirement checks
+                                       return 'let me pass';
+                               }
+                       }));
+
+               $access->expects($this->exactly(3))
+                       ->method('combineFilterWithAnd')
+                       ->will($this->returnCallback(function ($filterParts) {
+                               return str_replace('=*', '', array_pop($filterParts));
+                       }));
+
+               $access->expects($this->exactly(3))
+                       ->method('countUsers')
+                       ->will($this->returnCallback(function ($filter) {
+                               if($filter === 'myEmailAttribute') {
+                                       return 0;
+                               } else if($filter === 'mail') {
+                                       return 0;
+                               } else if($filter === 'mailPrimaryAddress') {
+                                       return 0;
+                               }
+                               var_dump($filter);
+                       }));
+
+               $resultAttribute = $wizard->detectEmailAttribute();
+               $this->assertSame('', $resultAttribute);
+       }
+
        public function testCumulativeSearchOnAttributeSkipReadDN() {
                // tests that there is no infinite loop, when skipping already processed
                // DNs (they can be returned multiple times for multiple filters )
index 08d0b7d5f93daee020061e7dc704b4fcd6a56c2c..9fcfccd48c23f6f834e74ae0509262f6b60873f4 100644 (file)
@@ -85,7 +85,7 @@ class OC_Log_Owncloud {
                        }
                        $entry = json_encode($entry);
                        $handle = @fopen(self::$logFile, 'a');
-                       @chmod(self::$logFile, 0640);
+//                     @chmod(self::$logFile, 0640);
                        if ($handle) {
                                fwrite($handle, $entry."\n");
                                fclose($handle);