summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog1
-rw-r--r--app/Model/Base.php20
-rw-r--r--app/Model/User.php8
-rw-r--r--app/Template/user/create_remote.php6
-rw-r--r--tests/units/Model/UserTest.php162
5 files changed, 181 insertions, 16 deletions
diff --git a/ChangeLog b/ChangeLog
index d331045e..e0cbd5ed 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -31,6 +31,7 @@ Bug fixes:
* Fix Markdown preview links focus
* Avoid dropdown menu to be truncated inside a column with scrolling
* Deleting subtask doesn't update task time tracking
+* Fix Mysql error about gitlab_id when creating remote user
Version 1.0.18
--------------
diff --git a/app/Model/Base.php b/app/Model/Base.php
index 973462cc..e700f326 100644
--- a/app/Model/Base.php
+++ b/app/Model/Base.php
@@ -62,14 +62,30 @@ abstract class Base extends \Core\Base
public function removeFields(array &$values, array $keys)
{
foreach ($keys as $key) {
- if (isset($values[$key])) {
+ if (array_key_exists($key, $values)) {
+ unset($values[$key]);
+ }
+ }
+ }
+
+ /**
+ * Remove keys from an array if empty
+ *
+ * @access public
+ * @param array $values Input array
+ * @param string[] $keys List of keys to remove
+ */
+ public function removeEmptyFields(array &$values, array $keys)
+ {
+ foreach ($keys as $key) {
+ if (array_key_exists($key, $values) && empty($values[$key])) {
unset($values[$key]);
}
}
}
/**
- * Force some fields to be at 0 if empty
+ * Force fields to be at 0 if empty
*
* @access public
* @param array $values Input array
diff --git a/app/Model/User.php b/app/Model/User.php
index 8a7eff4a..5792a549 100644
--- a/app/Model/User.php
+++ b/app/Model/User.php
@@ -91,7 +91,7 @@ class User extends Base
->table(User::TABLE)
->eq('id', $user_id)
->eq('is_admin', 1)
- ->count() === 1;
+ ->exists();
}
/**
@@ -251,7 +251,7 @@ class User extends Base
$result = array();
foreach ($users as $user) {
- $result[$user['id']] = $user['name'] ?: $user['username'];
+ $result[$user['id']] = $this->getFullname($user);
}
asort($result);
@@ -278,7 +278,9 @@ class User extends Base
}
$this->removeFields($values, array('confirmation', 'current_password'));
- $this->resetFields($values, array('is_admin', 'is_ldap_user', 'is_project_admin'));
+ $this->resetFields($values, array('is_admin', 'is_ldap_user', 'is_project_admin', 'disable_login_form'));
+ $this->removeEmptyFields($values, array('gitlab_id'));
+ $this->convertIntegerFields($values, array('gitlab_id'));
}
/**
diff --git a/app/Template/user/create_remote.php b/app/Template/user/create_remote.php
index 1d04bc87..559d3d72 100644
--- a/app/Template/user/create_remote.php
+++ b/app/Template/user/create_remote.php
@@ -21,13 +21,13 @@
<?= $this->form->email('email', $values, $errors) ?><br/>
<?= $this->form->label(t('Google Id'), 'google_id') ?>
- <?= $this->form->password('google_id', $values, $errors) ?><br/>
+ <?= $this->form->text('google_id', $values, $errors) ?><br/>
<?= $this->form->label(t('Github Id'), 'github_id') ?>
- <?= $this->form->password('github_id', $values, $errors) ?><br/>
+ <?= $this->form->text('github_id', $values, $errors) ?><br/>
<?= $this->form->label(t('Gitlab Id'), 'gitlab_id') ?>
- <?= $this->form->password('gitlab_id', $values, $errors) ?><br/>
+ <?= $this->form->text('gitlab_id', $values, $errors) ?><br/>
</div>
<div class="form-column">
diff --git a/tests/units/Model/UserTest.php b/tests/units/Model/UserTest.php
index 5b7edfb9..ac16a300 100644
--- a/tests/units/Model/UserTest.php
+++ b/tests/units/Model/UserTest.php
@@ -76,6 +76,109 @@ class UserTest extends Base
$this->assertEmpty($u->getByGoogleId(''));
}
+ public function testGetByToken()
+ {
+ $u = new User($this->container);
+ $this->assertNotFalse($u->create(array('username' => 'user1', 'token' => 'random')));
+ $this->assertNotFalse($u->create(array('username' => 'user2', 'token' => '')));
+
+ $this->assertNotEmpty($u->getByToken('random'));
+ $this->assertEmpty($u->getByToken(''));
+ }
+
+ public function testGetByUsername()
+ {
+ $u = new User($this->container);
+ $this->assertNotFalse($u->create(array('username' => 'user1')));
+
+ $this->assertNotEmpty($u->getByUsername('user1'));
+ $this->assertEmpty($u->getByUsername('user2'));
+ $this->assertEmpty($u->getByUsername(''));
+ }
+
+ public function testExists()
+ {
+ $u = new User($this->container);
+ $this->assertNotFalse($u->create(array('username' => 'user1')));
+
+ $this->assertTrue($u->exists(1));
+ $this->assertTrue($u->exists(2));
+ $this->assertFalse($u->exists(3));
+ }
+
+ public function testCount()
+ {
+ $u = new User($this->container);
+ $this->assertNotFalse($u->create(array('username' => 'user1')));
+ $this->assertEquals(2, $u->count());
+ }
+
+ public function testGetAll()
+ {
+ $u = new User($this->container);
+ $this->assertEquals(2, $u->create(array('username' => 'you')));
+ $this->assertEquals(3, $u->create(array('username' => 'me', 'name' => 'Me')));
+
+ $users = $u->getAll();
+ $this->assertCount(3, $users);
+ $this->assertEquals('admin', $users[0]['username']);
+ $this->assertEquals('me', $users[1]['username']);
+ $this->assertEquals('you', $users[2]['username']);
+ }
+
+ public function testGetList()
+ {
+ $u = new User($this->container);
+ $this->assertEquals(2, $u->create(array('username' => 'you')));
+ $this->assertEquals(3, $u->create(array('username' => 'me', 'name' => 'Me too')));
+
+ $users = $u->getList();
+
+ $expected = array(
+ 1 => 'admin',
+ 3 => 'Me too',
+ 2 => 'you',
+ );
+
+ $this->assertEquals($expected, $users);
+
+ $users = $u->getList(true);
+
+ $expected = array(
+ User::EVERYBODY_ID => 'Everybody',
+ 1 => 'admin',
+ 3 => 'Me too',
+ 2 => 'you',
+ );
+
+ $this->assertEquals($expected, $users);
+ }
+
+ public function testGetFullname()
+ {
+ $u = new User($this->container);
+ $this->assertEquals(2, $u->create(array('username' => 'user1')));
+ $this->assertEquals(3, $u->create(array('username' => 'user2', 'name' => 'User #2')));
+
+ $user1 = $u->getById(2);
+ $user2 = $u->getById(3);
+
+ $this->assertNotEmpty($user1);
+ $this->assertNotEmpty($user2);
+
+ $this->assertEquals('user1', $u->getFullname($user1));
+ $this->assertEquals('User #2', $u->getFullname($user2));
+ }
+
+ public function testIsAdmin()
+ {
+ $u = new User($this->container);
+ $this->assertEquals(2, $u->create(array('username' => 'user1')));
+
+ $this->assertTrue($u->isAdmin(1));
+ $this->assertFalse($u->isAdmin(2));
+ }
+
public function testPassword()
{
$password = 'test123';
@@ -134,15 +237,46 @@ class UserTest extends Base
$u->prepare($input);
$this->assertEquals(array('id' => 2, 'name' => 'me'), $input);
+
+ $input = array(
+ 'gitlab_id' => '1234',
+ );
+
+ $u->prepare($input);
+ $this->assertEquals(array('gitlab_id' => 1234), $input);
+
+ $input = array(
+ 'gitlab_id' => '',
+ );
+
+ $u->prepare($input);
+ $this->assertEquals(array(), $input);
+
+ $input = array(
+ 'gitlab_id' => 'something',
+ );
+
+ $u->prepare($input);
+ $this->assertEquals(array('gitlab_id' => 0), $input);
+
+ $input = array(
+ 'username' => 'something',
+ 'password' => ''
+ );
+
+ $u->prepare($input);
+ $this->assertEquals(array('username' => 'something'), $input);
}
public function testCreate()
{
$u = new User($this->container);
- $this->assertNotFalse($u->create(array('username' => 'toto', 'password' => '123456', 'name' => 'Toto')));
- $this->assertNotFalse($u->create(array('username' => 'titi', 'is_ldap_user' => 1)));
- $this->assertNotFalse($u->create(array('username' => 'papa', 'is_project_admin' => 1)));
- $this->assertFalse($u->create(array('username' => 'toto')));
+ $this->assertEquals(2, $u->create(array('username' => 'user #1', 'password' => '123456', 'name' => 'User')));
+ $this->assertEquals(3, $u->create(array('username' => 'user #2', 'is_ldap_user' => 1)));
+ $this->assertEquals(4, $u->create(array('username' => 'user #3', 'is_project_admin' => 1)));
+ $this->assertEquals(5, $u->create(array('username' => 'user #4', 'gitlab_id' => '')));
+ $this->assertEquals(6, $u->create(array('username' => 'user #5', 'gitlab_id' => '1234')));
+ $this->assertFalse($u->create(array('username' => 'user #1')));
$user = $u->getById(1);
$this->assertNotFalse($user);
@@ -155,15 +289,15 @@ class UserTest extends Base
$user = $u->getById(2);
$this->assertNotFalse($user);
$this->assertTrue(is_array($user));
- $this->assertEquals('toto', $user['username']);
- $this->assertEquals('Toto', $user['name']);
+ $this->assertEquals('user #1', $user['username']);
+ $this->assertEquals('User', $user['name']);
$this->assertEquals(0, $user['is_admin']);
$this->assertEquals(0, $user['is_ldap_user']);
$user = $u->getById(3);
$this->assertNotFalse($user);
$this->assertTrue(is_array($user));
- $this->assertEquals('titi', $user['username']);
+ $this->assertEquals('user #2', $user['username']);
$this->assertEquals('', $user['name']);
$this->assertEquals(0, $user['is_admin']);
$this->assertEquals(1, $user['is_ldap_user']);
@@ -171,9 +305,21 @@ class UserTest extends Base
$user = $u->getById(4);
$this->assertNotFalse($user);
$this->assertTrue(is_array($user));
- $this->assertEquals('papa', $user['username']);
+ $this->assertEquals('user #3', $user['username']);
$this->assertEquals(0, $user['is_admin']);
$this->assertEquals(1, $user['is_project_admin']);
+
+ $user = $u->getById(5);
+ $this->assertNotFalse($user);
+ $this->assertTrue(is_array($user));
+ $this->assertEquals('user #4', $user['username']);
+ $this->assertEquals('', $user['gitlab_id']);
+
+ $user = $u->getById(6);
+ $this->assertNotFalse($user);
+ $this->assertTrue(is_array($user));
+ $this->assertEquals('user #5', $user['username']);
+ $this->assertEquals('1234', $user['gitlab_id']);
}
public function testUpdate()