Skip to content

Commit

Permalink
increase code coverage for Users
Browse files Browse the repository at this point in the history
and remove duplicate code
  • Loading branch information
NicolasCARPi committed Aug 19, 2021
1 parent 23d6381 commit d6fe90d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 32 deletions.
44 changes: 12 additions & 32 deletions src/models/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ public function readFromQuery(string $query, bool $teamFilter = false): array
$sql = "SELECT DISTINCT users.userid,
users.firstname, users.lastname, users.email, users.mfa_secret,
users.validated, users.usergroup, users.archived, users.last_login,
CONCAT(users.firstname, ' ', users.lastname) AS fullname
CONCAT(users.firstname, ' ', users.lastname) AS fullname,
users.cellphone, users.phone, users.website, users.skype
FROM users
CROSS JOIN users2teams ON (users2teams.users_id = users.userid " . $teamFilterSql . ')
WHERE (users.email LIKE :query OR users.firstname LIKE :query OR users.lastname LIKE :query)
Expand All @@ -213,27 +214,7 @@ public function readFromQuery(string $query, bool $teamFilter = false): array
*/
public function readAllFromTeam(): array
{
$sql = "SELECT DISTINCT users.userid, CONCAT (users.firstname, ' ', users.lastname) AS fullname,
users.email,
users.phone,
users.cellphone,
users.website,
users.skype,
users.validated,
users.usergroup
FROM users
CROSS JOIN users2teams ON (users2teams.users_id = users.userid AND users2teams.teams_id = :team)
LEFT JOIN teams ON (teams.id = :team)
WHERE teams.id = :team ORDER BY fullname";
$req = $this->Db->prepare($sql);
$req->bindValue(':team', $this->team);
$this->Db->execute($req);

$res = $req->fetchAll();
if ($res === false) {
return array();
}
return $res;
return $this->readFromQuery('', true);
}

public function getLockedUsersCount(): int
Expand All @@ -249,23 +230,22 @@ public function getLockedUsersCount(): int
*
* @param array<string, mixed> $params POST
*/
public function update(array $params): void
public function update(array $params): bool
{
$this->checkEmail($params['email']);

$firstname = Filter::sanitize($params['firstname']);
$lastname = Filter::sanitize($params['lastname']);

// (Sys)admins can only disable 2FA
// input is disabled if there is no mfa active so no need for an else case
$mfaSql = '';
if (!isset($params['use_mfa']) || $params['use_mfa'] === 'off') {
if ($params['use_mfa'] === 'off') {
$mfaSql = ', mfa_secret = null';
} elseif ($params['use_mfa'] === 'on' && !$this->userData['mfa_secret']) {
throw new ImproperActionException('Only users themselves can activate two factor authentication!');
}

$validated = 0;
if ($params['validated'] == 1) {
if ($params['validated'] === '1') {
$validated = 1;
}

Expand All @@ -286,19 +266,19 @@ public function update(array $params): void
$req = $this->Db->prepare($sql);
$req->bindParam(':firstname', $firstname);
$req->bindParam(':lastname', $lastname);
$req->bindParam(':email', $email);
$req->bindParam(':validated', $validated);
$req->bindParam(':email', $params['email']);
$req->bindParam(':usergroup', $usergroup);
$req->bindParam(':validated', $validated);
$req->bindParam(':userid', $this->userData['userid'], PDO::PARAM_INT);
$this->Db->execute($req);
return $this->Db->execute($req);
}

/**
* Update things from UCP
*
* @param array<string, mixed> $params
*/
public function updateAccount(array $params): void
public function updateAccount(array $params): bool
{
$this->checkEmail($params['email']);

Expand Down Expand Up @@ -334,7 +314,7 @@ public function updateAccount(array $params): void
$req->bindParam(':skype', $params['skype']);
$req->bindParam(':website', $params['website']);
$req->bindParam(':userid', $this->userData['userid'], PDO::PARAM_INT);
$this->Db->execute($req);
return $this->Db->execute($req);
}

/**
Expand Down
54 changes: 54 additions & 0 deletions tests/unit/models/UsersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Elabftw\Models;

use Elabftw\Elabftw\ContentParams;
use Elabftw\Exceptions\ImproperActionException;
use Elabftw\Exceptions\ResourceNotFoundException;
use Elabftw\Maps\UserPreferences;
Expand All @@ -35,6 +36,59 @@ public function testAllowUntrustedLogin(): void
$this->assertTrue((new Users(2, 1))->allowUntrustedLogin());
}

public function testRead(): void
{
$res = $this->Users->read(new ContentParams('php'));
$this->assertEquals('1 - Phpunit TestUser', $res[0]);
}

public function testReadAllFromTeam(): void
{
$this->assertIsArray($this->Users->readAllFromTeam());
}

public function testUpdate(): void
{
$post = array(
'email' => 'tata@yopmail.com',
'firstname' => 'Tata',
'lastname' => 'Yep',
'password' => '',
'usergroup' => '2',
'validated' => '1',
'use_mfa' => 'off',
);
$this->assertTrue((new Users(4))->update($post));
}

public function testUpdateWithEmailAndPasswordChange(): void
{
$post = array(
'email' => 'tata2@yopmail.com',
'firstname' => 'Tata',
'lastname' => 'Yep',
'password' => 'new super password',
'usergroup' => '2',
'validated' => '1',
'use_mfa' => 'off',
);
$this->assertTrue((new Users(4))->update($post));
}

public function testUpdateAccount(): void
{
$post = array(
'email' => 'tata@yopmail.com',
'firstname' => 'Tata',
'lastname' => 'Yep',
'phone' => '+336123456',
'cellphone' => 'Nope',
'skype' => 'suxx',
'website' => 'https://www.elabftw.net',
);
$this->assertTrue((new Users(4))->updateAccount($post));
}

public function testUpdatePreferences(): void
{
$prefsArr = array(
Expand Down

0 comments on commit d6fe90d

Please sign in to comment.