9824: Change students loginname pattern
authorsstamer <s.stamer@metaways.de>
Thu, 10 Apr 2014 14:37:57 +0000 (16:37 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Fri, 11 Apr 2014 11:31:29 +0000 (13:31 +0200)
https://forge.tine20.org/mantisbt/view.php?id=9824
- add pattern 3: 1-x chars from firstname . lastname

Change-Id: I460498b2f62b42be722bd01a1616c3e5fc616327
Reviewed-on: http://gerrit.tine20.com/customers/510
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tests/tine20/Courses/JsonTest.php
tine20/Courses/Config.php
tine20/Tinebase/User/Abstract.php

index 6be571b..894410e 100644 (file)
@@ -41,7 +41,17 @@ class Courses_JsonTest extends TestCase
      * @var Tinebase_Model_Department
      */
     protected $_department = NULL;
-
+    
+    /**
+     * Student name pattern config
+     */
+    protected $_schemaConfig;
+    
+    /**
+     * @var Boolean
+     */
+    protected $_schemaConfigChanged = FALSE;
+    
     /**
      * Sets up the fixture.
      * This method is called before a test is executed.
@@ -74,6 +84,8 @@ class Courses_JsonTest extends TestCase
             'logonscript_postfix_member' => '.cmd',
             'baseprofilepath' => '\\\\jo\\profiles\\',
         )));
+        
+        $this->_schemaConfig = Courses_Config::getInstance()->get(Courses_Config::STUDENTS_USERNAME_SCHEMA);
     }
 
     /**
@@ -85,6 +97,9 @@ class Courses_JsonTest extends TestCase
     protected function tearDown()
     {
         $this->_groupIdsToDelete = $this->_groupsToDelete->getArrayOfIds();
+        if ($this->_schemaConfigChanged) {
+            Courses_Config::getInstance()->set(Courses_Config::STUDENTS_USERNAME_SCHEMA, $this->_schemaConfig);
+        }
         
         parent::tearDown();
     }
@@ -417,6 +432,37 @@ class Courses_JsonTest extends TestCase
         $this->assertEquals(array(array('field' => 'name', 'operator' => 'equals', 'value' => $course['name'])), $filter->toArray());
     }
     
+    /**
+     * Test students loginname with schema 3
+     */
+    public function testStudentNameSchema3()
+    {
+        $this->_schemaConfigChanged = true;
+        Courses_Config::getInstance()->set(Courses_Config::STUDENTS_USERNAME_SCHEMA, 3);
+        
+        $course = $this->_getCourseData();
+        $courseData = $this->_json->saveCourse($course);
+        $this->_groupsToDelete->addRecord(Tinebase_Group::getInstance()->getGroupById($courseData['group_id']));
+        
+        $result = $this->_json->addNewMember(array(
+            'accountFirstName' => 'jams',
+            'accountLastName'  => 'hot',
+        ), $courseData);
+        
+        $this->assertEquals(2, count($result['results']));
+        
+        $id = NULL;
+        foreach ($result['results'] as $result) {
+            if ($result['name'] === 'hot, jams') {
+                $id = $result['id'];
+            }
+        }
+        $this->assertTrue($id !== NULL);
+        
+        $newUser = Tinebase_User::getInstance()->getFullUserById($id);
+        $this->assertEquals('j.hot', $newUser->accountLoginName);
+    }
+    
     /************ protected helper funcs *************/
     
     /**
index 3d324f6..d5d9a2f 100644 (file)
@@ -130,7 +130,7 @@ class Courses_Config extends Tinebase_Config_Abstract
         //_('Student username schema')
             'label'                 => 'Student username schema',
         //_('Student username schema (0 = only lastname (10 chars), 1 = lastname + 2 chars of firstname')
-            'description'           => 'Student username schema (0 = only lastname (10 chars), 1 = lastname + 2 chars of firstname',
+            'description'           => 'Student username schema (0 = only lastname (10 chars), 1 = lastname + 2 chars of firstname, 3 = 1-x chars of firstname . lastname',
             'type'                  => 'int',
             'default'               => 1,
             'clientRegistryInclude' => FALSE,
index 23a0bf8..50fcd2f 100644 (file)
@@ -344,11 +344,12 @@ abstract class Tinebase_User_Abstract implements Tinebase_User_Interface
     public function generateUserName($_account, $_schema = 1)
     {
         if (! empty($_account->accountFirstName) && $_schema > 0) {
-            $userName = ($_schema === 1) ? $this->_generateUserWithSchema1($_account) : $this->_generateUserWithSchema2($_account);
-        } else {
-            $userName = strtolower(replaceSpecialChars(substr($_account->accountLastName, 0, 10)));
+            if (method_exists($this, '_generateUserWithSchema' . $_schema)) {
+                $userName = call_user_func_array(array($this, '_generateUserWithSchema' . $_schema), array($_account));
+            } else {
+                $userName = strtolower(replaceSpecialChars(substr($_account->accountLastName, 0, 10)));
+            }
         }
-        
         $userName = $this->_addSuffixToUsernameIfExists($userName);
         
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . '  generated username: ' . $userName);
@@ -390,6 +391,27 @@ abstract class Tinebase_User_Abstract implements Tinebase_User_Interface
     }
     
     /**
+     * schema 3 = 1-x chars of firstname . lastname
+     * 
+     * @param Tinebase_Model_FullUser $_account
+     * @return string
+     */
+    protected function _generateUserWithSchema3($_account)
+    {
+        $result = $_account->accountLastName;
+        for ($i=0; $i < strlen($_account->accountFirstName); $i++) {
+        
+            $userName = strtolower(replaceSpecialChars(substr($_account->accountFirstName, 0, $i+1) . '.' . $_account->accountLastName));
+            if (! $this->userNameExists($userName)) {
+                $result = $userName;
+                break;
+            }
+        }
+        
+        return $result;
+    }
+    
+    /**
      * add a suffix to username if it already exists
      * 
      * @param string $_userName