Skip to content

Commit 96c0327

Browse files
committed
completed user test class
1 parent de0de1a commit 96c0327

File tree

1 file changed

+70
-4
lines changed

1 file changed

+70
-4
lines changed

tests/phpunit/UserTest.php

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,77 @@ public function testUserExistsByEmail() {
278278
}
279279

280280
public function testUserDoesNotExistsByEmail() {
281-
$userExists = User::userIdExists("foo@example.com");
281+
$userExists = User::userEmailExists("foo@example.com");
282282
$this->assertFalse($userExists);
283283
}
284+
285+
public function testAllowClientForUser() {
286+
$newUser = [
287+
"password" => "hello123!@#ABC",
288+
"email" => "user11@example.com",
289+
"hello" => "world"
290+
];
291+
$createdUser = User::createUser($newUser);
292+
293+
$clientId = "12345";
294+
$result = User::allowClientForUser($clientId, $createdUser['userId']);
295+
$this->assertTrue($result);
296+
297+
$allowedClients = User::getAllowedClients($createdUser['userId']);
298+
$this->assertTrue(in_array($clientId, $allowedClients));
299+
300+
$user = User::getUser($newUser['email']);
301+
$this->assertTrue(in_array($clientId, $user['allowedClients']));
302+
}
303+
304+
public function testDeleteAccount() {
305+
$newUser = [
306+
"password" => "hello123!@#ABC",
307+
"email" => "user11@example.com",
308+
"hello" => "world"
309+
];
310+
$createdUser = User::createUser($newUser);
311+
$clientId = "12345";
312+
$result = User::allowClientForUser($clientId, $createdUser['userId']);
313+
314+
$this->assertTrue(User::userIdExists($createdUser['userId']));
315+
$this->assertTrue(User::userEmailExists($newUser['email']));
316+
$allowedClients = User::getAllowedClients($createdUser['userId']);
317+
$this->assertTrue(in_array($clientId, $allowedClients));
318+
319+
User::deleteAccount($newUser['email']);
320+
321+
$this->assertFalse(User::userIdExists($createdUser['userId']));
322+
$this->assertFalse(User::userEmailExists($newUser['email']));
323+
$allowedClients = User::getAllowedClients($createdUser['userId']);
324+
$this->assertEmpty($allowedClients);
325+
}
326+
327+
public function testCleanup() {
328+
// empty the verify table first so we have dependable numbers
329+
$query = Db::$pdo->prepare('DELETE FROM verify WHERE NOT code=""');
330+
$query->execute();
331+
332+
$token1 = User::saveVerifyToken("verify", [
333+
"hello" => "world",
334+
"expires" => time() - 10
335+
]);
336+
$token2 = User::saveVerifyToken("verify", [
337+
"hello" => "world",
338+
"expires" => time() - 10
339+
]);
340+
$query = Db::$pdo->prepare('SELECT count(*) AS count FROM verify');
341+
$query->execute();
342+
$result = $query->fetchAll();
343+
$beforeCleanup = $result[0]['count'];
344+
$this->assertEquals(2, $beforeCleanup);
284345

285-
// @TODO Write tests for these functions:
286-
// deleteAccount
287-
// cleanupTokens
346+
User::cleanupTokens();
347+
$query = Db::$pdo->prepare('SELECT count(*) AS count FROM verify');
348+
$query->execute();
349+
$result = $query->fetchAll();
350+
$afterCleanup = $result[0]['count'];
351+
352+
$this->assertEquals(0, $afterCleanup);
353+
}
288354
}

0 commit comments

Comments
 (0)