Skip to content
Snippets Groups Projects
Commit 5dab97dd authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Autojoin DAO changes and tests

parent 9f6a4bd6
Branches
No related tags found
No related merge requests found
...@@ -119,6 +119,12 @@ Create the logs directory and assign ownership to the Apache user (usually www-d ...@@ -119,6 +119,12 @@ Create the logs directory and assign ownership to the Apache user (usually www-d
mkdir logs mkdir logs
sudo chown www-data logs sudo chown www-data logs
### Docker
Database image:
docker build -f docker/db-Dockerfile --tag rap-ia2/database .
### Run Unit Tests and build code coverage report ### Run Unit Tests and build code coverage report
(XDebug or another code coverage driver needs to be installed; e.g. `sudo apt install php-xdebug`) (XDebug or another code coverage driver needs to be installed; e.g. `sudo apt install php-xdebug`)
......
...@@ -251,17 +251,30 @@ class MySQLUserDAO extends BaseMySQLDAO implements UserDAO { ...@@ -251,17 +251,30 @@ class MySQLUserDAO extends BaseMySQLDAO implements UserDAO {
try { try {
$dbh->beginTransaction(); $dbh->beginTransaction();
// Moving identities from user2 to user1 // Deleting keep_separated pairs
$stmt1 = $dbh->prepare("UPDATE `identity` SET `user_id` = :id1 WHERE `user_id` = :id2"); $stmt1 = $dbh->prepare("DELETE FROM keep_separated WHERE "
. "(user_id1 = :id1 AND user_id2 = :id2) OR"
. "(user_id1 = :id2 AND user_id2 = :id1)");
$stmt1->bindParam(':id1', $userId1); $stmt1->bindParam(':id1', $userId1);
$stmt1->bindParam(':id2', $userId2); $stmt1->bindParam(':id2', $userId2);
$stmt1->execute(); $stmt1->execute();
// Deleting user2 // Deleting keep_separated records of user that is going to be deleted
$stmt2 = $dbh->prepare("DELETE FROM `user` WHERE `id` = :id2"); $stmt2 = $dbh->prepare("DELETE FROM keep_separated WHERE user_id1 = :id2 OR user_id2 = :id2");
$stmt2->bindParam(':id2', $userId2); $stmt2->bindParam(':id2', $userId2);
$stmt2->execute(); $stmt2->execute();
// Moving identities from user2 to user1
$stmt3 = $dbh->prepare("UPDATE `identity` SET `user_id` = :id1 WHERE `user_id` = :id2");
$stmt3->bindParam(':id1', $userId1);
$stmt3->bindParam(':id2', $userId2);
$stmt3->execute();
// Deleting user2
$stmt4 = $dbh->prepare("DELETE FROM `user` WHERE `id` = :id2");
$stmt4->bindParam(':id2', $userId2);
$stmt4->execute();
$dbh->commit(); $dbh->commit();
} catch (Exception $ex) { } catch (Exception $ex) {
$dbh->rollBack(); $dbh->rollBack();
......
FROM mariadb:10.5
ENV MYSQL_ALLOW_EMPTY_PASSWORD yes
ENV MYSQL_DATABASE rap
COPY sql/setup-database.sql /docker-entrypoint-initdb.d/01-setup-database.sql
COPY sql/delete-user-procedure.sql /docker-entrypoint-initdb.d/02-delete-user-procedure.sql
<?php
abstract class BaseDAOTest extends \PHPUnit\Framework\TestCase {
protected function getFakeLocator(): \RAP\Locator {
$config = (object) [
"databaseConfig" => (object) [
"dbtype" => 'MySQL',
"hostname" => "127.0.0.1",
"port" => 3307,
"username" => "root",
"password" => "",
"dbname" => "rap"
]
];
$locatorStub = $this->createMock(\RAP\Locator::class);
$locatorStub->config = $config;
return $locatorStub;
}
}
<?php
class MySQLUserDAOTest extends BaseDAOTest {
public function setUp(): void {
$this->dao = new \RAP\MySQLUserDAO($this->getFakeLocator());
}
public function testJoinUsersAfterKeepSeparated() {
$user1 = $this->createUser(\RAP\Identity::EDU_GAIN, 'name.surname@inaf.it', '001');
$user2 = $this->createUser(\RAP\Identity::GOOGLE, 'name.surname@inaf.it', '002');
$user3 = $this->createUser(\RAP\Identity::LINKEDIN, 'test@inaf.it', '003');
$joinable1 = $this->dao->findJoinableUsersByUserId($user1->id);
$this->assertEquals(1, count($joinable1));
$this->assertEquals($user2->id, $joinable1[0]);
$joinable2 = $this->dao->findJoinableUsersByUserId($user2->id);
$this->assertEquals(1, count($joinable2));
$this->assertEquals($user1->id, $joinable2[0]);
// Add records to keep_separated table to test the two DELETE statements before the join
$this->dao->insertRejectedJoin($user1->id, $user2->id);
$this->dao->insertRejectedJoin($user2->id, $user3->id);
$joinable = $this->dao->findJoinableUsersByUserId($user1->id);
$this->assertEquals(0, count($joinable));
$this->dao->joinUsers($user1->id, $user2->id);
$joinedUser = $this->dao->findUserById($user1->id);
$this->assertEquals(2, count($joinedUser->identities));
$this->assertNull($this->dao->findUserById($user2->id));
}
private function createUser(string $identityType, string $email, string $typedId) {
$user = new \RAP\User();
$user->id = $this->dao->createUser();
$identity = new \RAP\Identity($identityType);
$identity->email = $email;
$identity->typedId = $typedId;
$this->dao->insertIdentity($identity, $user->id);
$savedUser = $this->dao->findUserById($user->id);
$this->assertEquals($user->id, $savedUser->id);
return $savedUser;
}
}
...@@ -23,7 +23,7 @@ include 'include/header.php'; ...@@ -23,7 +23,7 @@ include 'include/header.php';
<div class="row"> <div class="row">
<div class="col-xs-12 col-md-6"> <div class="col-xs-12 col-md-6">
<h4>User id: <?php echo $user->id; ?></h4> <h4><?php echo $user->id === null ? '&nbsp;' : ('User id: ' . $user->id); ?></h4>
<div class="panel"> <div class="panel">
<div class="panel-body"> <div class="panel-body">
<?php <?php
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment