Skip to content

Commit

Permalink
Added more unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
ptorn committed Oct 5, 2017
1 parent 7bc27b9 commit 03e22e5
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/User/UserActiveRecordModel.php
Expand Up @@ -68,7 +68,7 @@ public function updateUser(User $user)
/**
* Delete user.
*
* @param integer $id, integer for userid.
* @param integer $id integer for userid.
*
* @return void
*/
Expand Down
8 changes: 7 additions & 1 deletion src/User/UserController.php
Expand Up @@ -108,6 +108,7 @@ public function getPostUpdateUser($id)

if (!$loggedInUser) {
$this->response->redirect("login");
return false;
}

if ($loggedInUser->id != $id) {
Expand Down Expand Up @@ -141,8 +142,13 @@ public function getPostDeleteUser($id)
{
$loggedInUser = $this->userService->getCurrentLoggedInUser();

if (!$loggedInUser && !$loggedInUser->administrator) {
if (!$loggedInUser) {
$this->response->redirect("login");
return false;
}
if (!$loggedInUser->administrator) {
$this->response->redirect("login");
return false;
}

$title = "Radera en användare";
Expand Down
4 changes: 0 additions & 4 deletions src/User/UserService.php
Expand Up @@ -135,10 +135,6 @@ public function login($username, $password)
throw new Exception("Empty password field.");
}

if (empty($user)) {
throw new Exception("Error, not valid credentials.");
}

if ($user->id === null) {
throw new Exception("Error, not valid credentials.");
}
Expand Down
61 changes: 50 additions & 11 deletions test/src/User/UserControllerTest.php
Expand Up @@ -15,25 +15,40 @@ class UserControllerTest extends TestCase
protected static $userController;


/**
* Setup before testing class.
*/
public static function setUpBeforeClass()
// /**
// * Setup before testing class.
// */
// public static function setUpBeforeClass()
// {
// self::$di = new \Anax\DI\DIFactoryConfig("testDi.php");
// self::$userController = new UserController();
// self::$userController->setDi(self::$di);
// self::$userController->init();
// self::$session = self::$di->get("session");
// $user = new User();
// $user->id = 2;
// $user->username = "admin";
// $user->administrator = true;
// $user->enabled = true;
//
// self::$session->set("user", $user);
// }

public function setUp()
{
self::$di = new \Anax\DI\DIFactoryConfig("testDi.php");
self::$userController = new UserController();
self::$userController->setDi(self::$di);
self::$userController->init();
self::$session = self::$di->get("session");
$user = new User();
$user->username = "admin";
$user->administrator = true;
$user->enabled = true;

self::$session->set("user", $user);
}

$admin = new User();
$admin->administrator = true;
$admin->enabled = true;
$admin->deleted = null;

self::$session->set("user", $admin);
}

/**
* Initiate the controller test.
Expand Down Expand Up @@ -81,13 +96,37 @@ public function testGetPostCreateUser()
public function testGetPostUpdateUser()
{
self::$userController->getPostUpdateUser(1);

self::$session->delete("user");
self::$userController->getPostUpdateUser(1);

$user = new User();
$user->id = 2;
$user->username = "admin";
$user->administrator = false;
$user->enabled = true;

self::$session->set("user", $user);
self::$userController->getPostUpdateUser(1);
}



public function testGetPostDeleteUser()
{
self::$userController->getPostDeleteUser(1);

self::$session->delete("user");
self::$userController->getPostDeleteUser(2);

$user = new User();
$user->id = 2;
$user->username = "admin";
$user->administrator = false;
$user->enabled = true;

self::$session->set("user", $user);
self::$userController->getPostDeleteUser(2);
}


Expand Down

0 comments on commit 03e22e5

Please sign in to comment.