Commit 5dab97dd authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Autojoin DAO changes and tests

parent 9f6a4bd6
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -119,6 +119,12 @@ Create the logs directory and assign ownership to the Apache user (usually www-d
    mkdir 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

(XDebug or another code coverage driver needs to be installed; e.g. `sudo apt install php-xdebug`)
+18 −5
Original line number Diff line number Diff line
@@ -251,17 +251,30 @@ class MySQLUserDAO extends BaseMySQLDAO implements UserDAO {
        try {
            $dbh->beginTransaction();

            // Moving identities from user2 to user1
            $stmt1 = $dbh->prepare("UPDATE `identity` SET `user_id` = :id1 WHERE `user_id` = :id2");
            // Deleting keep_separated pairs
            $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(':id2', $userId2);
            $stmt1->execute();
            
            // Deleting user2
            $stmt2 = $dbh->prepare("DELETE FROM `user` WHERE `id` = :id2");
            // Deleting keep_separated records of user that is going to be deleted
            $stmt2 = $dbh->prepare("DELETE FROM keep_separated WHERE user_id1 = :id2 OR user_id2 = :id2");
            $stmt2->bindParam(':id2', $userId2);
            $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();
        } catch (Exception $ex) {
            $dbh->rollBack();

docker/db-Dockerfile

0 → 100644
+7 −0
Original line number Diff line number Diff line
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

tests/BaseDAOTest.php

0 → 100644
+24 −0
Original line number Diff line number Diff line
<?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;
    }

}
+55 −0
Original line number Diff line number Diff line
<?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;
    }

}
Loading