diff options
Diffstat (limited to 'demos/time-tracker/tests/unit')
6 files changed, 323 insertions, 218 deletions
diff --git a/demos/time-tracker/tests/unit/AddUserToProjectTestCase.php b/demos/time-tracker/tests/unit/AddUserToProjectTestCase.php deleted file mode 100644 index 36defc57..00000000 --- a/demos/time-tracker/tests/unit/AddUserToProjectTestCase.php +++ /dev/null @@ -1,35 +0,0 @@ -<?php
-
-require_once(dirname(__FILE__).'/ProjectDaoTestCase.php');
-
-class AddUserToProjectTestCase extends ProjectDaoTestCase
-{
- function testCanAddNewUserToProject()
- {
- $project = $this->createNewTestProject();
-
- $user = new TimeTrackerUser();
- $user->ID = 3;
- $user->Name = "test user 1";
-
- if(($conn = $this->connection) instanceof MockTSqlMapper)
- {
- $this->setupMockConnectionFor($project);
- $conn->setReturnReference('queryForObject', $user, array('GetUserByName', $user->Name));
- $conn->setReturnValue('queryForList', array(), array('GetProjectMembers', $project));
-
- $param['project'] = $project;
- $param['user'] = $user;
-
- $conn->setReturnValue('insert', true, array('AddNewUserToProject', $param));
-
- $conn->expectAtLeastOnce('insert');
- $conn->expectAtLeastOnce('queryForList');
- }
-
- $this->assertTrue($this->dao->createNewProject($project));
- $this->assertTrue($this->dao->addUserToProject($project, $user));
- }
-}
-
-?>
\ No newline at end of file diff --git a/demos/time-tracker/tests/unit/BaseTestCase.php b/demos/time-tracker/tests/unit/BaseTestCase.php new file mode 100644 index 00000000..8ce3cca8 --- /dev/null +++ b/demos/time-tracker/tests/unit/BaseTestCase.php @@ -0,0 +1,35 @@ +<?php
+
+class BaseTestCase extends UnitTestCase
+{
+ protected $sqlmap;
+
+ function setup()
+ {
+ $app = Prado::getApplication();
+ $this->sqlmap = $app->getModule('daos')->getConnection();
+ }
+
+ function flushDatabase()
+ {
+ $conn = $this->sqlmap->openConnection();
+ $file = Prado::getPathOfNamespace('Application.App_Data.mysql-reset','.sql');
+ if(is_file($file))
+ $this->runScript($conn, $file);
+ else
+ throw new Exception('unable to find script file '.$file);
+ }
+
+ protected function runScript($connection, $script)
+ {
+ $sql = file_get_contents($script);
+ $lines = explode(';', $sql);
+ foreach($lines as $line)
+ {
+ $line = trim($line);
+ if(strlen($line) > 0)
+ $connection->execute($line);
+ }
+ }
+}
+?>
\ No newline at end of file diff --git a/demos/time-tracker/tests/unit/CreateNewProjectTestCase.php b/demos/time-tracker/tests/unit/CreateNewProjectTestCase.php deleted file mode 100644 index 0aa67405..00000000 --- a/demos/time-tracker/tests/unit/CreateNewProjectTestCase.php +++ /dev/null @@ -1,90 +0,0 @@ -<?php
-
-require_once(dirname(__FILE__).'/ProjectDaoTestCase.php');
-
-class CreateNewProjectTestCase extends ProjectDaoTestCase
-{
- function testProjectDaoCanCreateNewProject()
- {
- $project = $this->createNewTestProject();
-
- if(($conn = $this->connection) instanceof MockTSqlMapper)
- {
- $this->setupMockConnectionFor($project);
- $conn->expectMinimumCallCount('queryForObject', 3);
- $conn->expectAtLeastOnce('insert');
- }
-
- $this->assertProjectCreated($project);
- }
-
- function testProjectExistsException()
- {
- $project = $this->createNewTestProject();
-
- if(($conn = $this->connection) instanceof MockTSqlMapper)
- {
- //make the project exist
- $conn->setReturnValue('queryForObject',
- $project, array('GetProjectByName', 'Project 1'));
- $this->setupMockConnectionFor($project);
- }
-
- try
- {
- $this->assertProjectCreated($project);
- $this->fail();
- }
- catch(TimeTrackerException $e)
- {
- $this->pass();
- }
- }
- function testProjectCustomerNotExistsException()
- {
- $project = $this->createNewTestProject();
-
- if(($conn = $this->connection) instanceof MockTSqlMapper)
- {
- //customer does not exist
- $conn->setReturnValue('queryForObject',
- null, array('GetUserByName', 'Customer A'));
- $this->setupMockConnectionFor($project);
- }
-
- try
- {
- $this->assertProjectCreated($project);
- $this->fail();
- }
- catch(TimeTrackerException $e)
- {
- $this->pass();
- }
- }
-
- function testProjectManagerNotExistsException()
- {
- $project = $this->createNewTestProject();
-
- if(($conn = $this->connection) instanceof MockTSqlMapper)
- {
- //manager does not exist
- $conn->setReturnValue('queryForObject',
- null, array('GetUserByName', 'Manager A'));
- $this->setupMockConnectionFor($project);
- }
-
- try
- {
- $this->assertProjectCreated($project);
- $this->fail();
- }
- catch(TimeTrackerException $e)
- {
- $this->pass();
- }
- }
-}
-
-?>
\ No newline at end of file diff --git a/demos/time-tracker/tests/unit/ProjectDaoTestCase.php b/demos/time-tracker/tests/unit/ProjectDaoTestCase.php deleted file mode 100644 index bc576630..00000000 --- a/demos/time-tracker/tests/unit/ProjectDaoTestCase.php +++ /dev/null @@ -1,92 +0,0 @@ -<?php
-
-//formerly PradoDaoTestCase.php
-
-Prado::using('Application.APP_CODE.*');
-Prado::using('System.DataAccess.SQLMap.TSqlMapper');
-
-Mock::generate('TSqlMapper');
-
-class ProjectDaoTestCase extends UnitTestCase
-{
- protected $dao;
- protected $connection;
-
- function setup()
- {
- $this->dao= new ProjectDao();
- $this->connection = new MockTSqlMapper($this);
- $this->dao->setConnection($this->connection);
- }
-
-/*
- //Simple test case, will not detect project existanc
- //This case will clash with the more complete test case below.
- function testProjectDaoCanCreateNewProject()
- {
- $project = new Project();
- $project->Name = "Project 1";
-
- if(($conn = $this->connection) instanceof MockTSqlMapper)
- {
- $conn->expectOnce('insert', array('CreateNewProject', $project));
- $conn->setReturnValue('insert', true);
-
- $conn->expectOnce('queryForObject', array('GetProjectByID', 1));
- $conn->setReturnReference('queryForObject', $project);
- }
-
- $this->assertTrue($this->dao->createNewProject($project));
- $this->assertEqual($this->dao->getProjectByID(1), $project);
- }
-*/
- function setupMockConnectionFor($project)
- {
- $customer = new TimeTrackerUser();
- $customer->ID = 1;
- $customer->Name = "Customer A";
-
- $manager = new TimeTrackerUser();
- $manager->ID = 2;
- $manager->Name = "Manager A";
-
- $conn = $this->connection;
-
- //return the customer and manager
- $conn->setReturnValue('queryForObject',
- $customer, array('GetUserByName', 'Customer A'));
- $conn->setReturnValue('queryForObject',
- $manager, array('GetUserByName', 'Manager A'));
-
- //project does not exist
- $conn->setReturnValue('queryForObject',
- null, array('GetProjectByName', 'Project 1'));
-
- $param['project'] = $project;
- $param['creator'] = $customer->ID;
- $param['manager'] = $manager->ID;
-
- $conn->setReturnValue('insert',
- true, array('CreateNewProject', $param));
- $conn->setReturnReference('queryForObject',
- $project, array('GetProjectByID', 1));
- }
-
- function createNewTestProject()
- {
- $project = new Project();
- $project->Name = "Project 1";
- $project->CreatorUserName = "Customer A";
- $project->ManagerUserName = "Manager A";
-
- return $project;
- }
-
- function assertProjectCreated($project)
- {
- $this->assertTrue($this->dao->createNewProject($project));
- $this->assertEqual($this->dao->getProjectByID(1), $project);
- }
-
-}
-?>
\ No newline at end of file diff --git a/demos/time-tracker/tests/unit/ProjectTestCase.php b/demos/time-tracker/tests/unit/ProjectTestCase.php index a61c4f77..59f612bd 100644 --- a/demos/time-tracker/tests/unit/ProjectTestCase.php +++ b/demos/time-tracker/tests/unit/ProjectTestCase.php @@ -3,13 +3,17 @@ //import Project class.
Prado::using('Application.APP_CODE.Project');
-class ProjectTestCase extends UnitTestCase
+require_once(dirname(__FILE__).'/BaseTestCase.php');
+
+class ProjectTestCase extends BaseTestCase
{
function testProjectClassExists()
{
$project = new Project();
$this->pass();
}
+
+
}
?>
\ No newline at end of file diff --git a/demos/time-tracker/tests/unit/UserDaoTestCase.php b/demos/time-tracker/tests/unit/UserDaoTestCase.php new file mode 100644 index 00000000..d1f3c728 --- /dev/null +++ b/demos/time-tracker/tests/unit/UserDaoTestCase.php @@ -0,0 +1,283 @@ +<?php
+
+require_once(dirname(__FILE__).'/BaseTestCase.php');
+
+class UserDaoTestCase extends BaseTestCase
+{
+ protected $userDao;
+
+ function setup()
+ {
+ parent::setup();
+ $app = Prado::getApplication();
+ $this->userDao = $app->getModule('daos')->getDao('UserDao');
+ }
+
+ function assertIsAdmin($user)
+ {
+ if(!$user)
+ return $this->fail();
+ $this->assertEqual($user->getName(), 'admin');
+ $this->assertEqual($user->getEmailAddress(), 'admin@pradosoft.com');
+ }
+
+ function assertSameUser($user1, $user2)
+ {
+ if(is_null($user1) || is_null($user2))
+ return $this->fail();
+
+ $this->assertEqual($user1->getName(), $user2->getName());
+ $this->assertEqual($user1->getEmailAddress(), $user2->getEmailAddress());
+ }
+
+ function assertIsAdminRole($user)
+ {
+ if(is_null($user))
+ return $this->fail();
+
+ $this->assertTrue($user->isInRole('admin'));
+ }
+
+ function assertIsManagerRole($user)
+ {
+ if(is_null($user))
+ return $this->fail();
+
+ $this->assertTrue($user->isInRole('manager'));
+ }
+
+ function assertIsConsultantRole($user)
+ {
+ if(is_null($user))
+ return $this->fail();
+
+ $this->assertTrue($user->isInRole('consultant'));
+ }
+
+ function assertNotConsultantRole($user)
+ {
+ if(is_null($user))
+ return $this->fail();
+
+ $this->assertFalse($user->isInRole('consultant'));
+ }
+
+ function testGetUserByName()
+ {
+ $user = $this->userDao->getUserByName('admin');
+ $this->assertNotNull($user);
+ $this->assertIsAdmin($user);
+ }
+
+ function testGetNonExistentUser()
+ {
+ $user = $this->userDao->getUserByName('none');
+ $this->assertNull($user);
+ }
+
+ function testGetUsers()
+ {
+ $users = $this->userDao->getAllUsers();
+ $this->assertEqual(count($users), 3);
+ }
+
+ function testUserLogon()
+ {
+ $success = $this->userDao->validateUser('admin', 'admin');
+ $this->assertTrue($success);
+ }
+
+ function testBadLogin()
+ {
+ $success = $this->userDao->validateUser('admin', 'hahah');
+ $this->assertFalse($success);
+ }
+
+
+ function testAddNewUser()
+ {
+ $user = new TimeTrackerUser(new UserManager());
+ $user->Name = "user1";
+ $user->EmailAddress = 'user1@pradosoft.com';
+
+ $this->userDao->addNewUser($user, 'password');
+
+ $check = $this->userDao->getUserByName($user->Name);
+
+ $this->assertSameUser($check, $user);
+ $this->flushDatabase();
+ }
+
+ function testDeleteUserByName()
+ {
+ $this->userDao->deleteUserByName('admin');
+
+ $admin = $this->userDao->getUserByName('admin');
+ $this->assertNull($admin);
+
+ $users = $this->userDao->getAllUsers();
+ $this->assertEqual(count($users), 2);
+
+ $this->flushDatabase();
+ }
+
+ function testAutoSignon()
+ {
+ $user = new TimeTrackerUser(new UserManager());
+ $user->Name = "admin";
+
+ $token = $this->userDao->createSignonToken($user);
+
+ $check = $this->userDao->validateSignon($token);
+
+ $this->assertIsAdmin($check);
+
+ $this->flushDatabase();
+ }
+
+
+ function testBadAutoSignon()
+ {
+ $user = new TimeTrackerUser(new UserManager());
+ $user->Name = "admin";
+
+ $token = $this->userDao->createSignonToken($user);
+
+ $check = $this->userDao->validateSignon('adasd');
+ $this->assertNull($check);
+
+ $this->flushDatabase();
+ }
+
+ function testAdminRoles()
+ {
+ $user = $this->userDao->getUserByName('admin');
+ $this->assertIsAdminRole($user);
+ $this->assertIsManagerRole($user);
+ $this->assertIsConsultantRole($user);
+ }
+
+ function testSetUserRoles()
+ {
+ $user = new TimeTrackerUser(new UserManager());
+ $user->Name = "user1";
+ $user->EmailAddress = 'user1@pradosoft.com';
+ $user->Roles = array("manager", "consultant");
+
+ $this->userDao->addNewUser($user, 'password');
+ $check = $this->userDao->getUserByName('user1');
+
+ $this->assertIsManagerRole($check);
+ $this->assertIsConsultantRole($check);
+
+ $this->flushDatabase();
+ }
+
+ function testSetUserRoleNoNullUser()
+ {
+ $user = new TimeTrackerUser(new UserManager());
+ $user->Name = "user1";
+ $user->EmailAddress = 'user1@pradosoft.com';
+ $user->Roles = array("manager", "consultant");
+
+ try
+ {
+ $this->userDao->updateUserRoles($user);
+ $this->fail();
+ }
+ catch(TDataMapperException $e)
+ {
+ $this->pass();
+ }
+
+ $check = $this->sqlmap->queryForObject('GetUserByName', 'user1');
+ $this->assertNull($check);
+ }
+
+ function testUpdateUser()
+ {
+ $user = $this->userDao->getUserByName('admin');
+ $user->EmailAddress = 'something@pradosoft.com';
+ $user->Roles = array('manager', 'admin');
+
+ $this->userDao->updateUser($user);
+
+ $check = $this->userDao->getUserByName('admin');
+ $this->assertIsAdminRole($check);
+ $this->assertIsManagerRole($check);
+ $this->assertNotConsultantRole($check);
+
+ $this->flushDatabase();
+ }
+
+ function testUpdateUserPassword()
+ {
+ $user = $this->userDao->getUserByName('admin');
+ $user->EmailAddress = 'something@pradosoft.com';
+ $user->Roles = array('manager', 'admin');
+
+ $pass = 'newpasword';
+
+ $this->userDao->updateUser($user, $pass);
+
+ $success = $this->userDao->validateUser('admin', $pass);
+
+ $this->assertTrue($success);
+
+ $this->flushDatabase();
+ }
+
+ function testClearSignonTokens()
+ {
+ $user = new TimeTrackerUser(new UserManager());
+ $user->Name = "admin";
+
+ $token1 = $this->userDao->createSignonToken($user);
+ sleep(1);
+ $token2 = $this->userDao->createSignonToken($user);
+ $this->assertNotEqual($token1, $token2);
+
+ $check1 = $this->userDao->validateSignon($token1);
+ $check2 = $this->userDao->validateSignon($token2);
+
+ $this->assertIsAdmin($check1);
+ $this->assertIsAdmin($check2);
+
+ $this->userDao->clearSignonTokens($user);
+
+ $check3 = $this->userDao->validateSignon($token1);
+ $check4 = $this->userDao->validateSignon($token2);
+
+ $this->assertNull($check3);
+ $this->assertNull($check4);
+ }
+
+ function testClearAllSigonTokens()
+ {
+ $user1 = new TimeTrackerUser(new UserManager());
+ $user1->Name = "admin";
+
+ $user2 = new TimeTrackerUser(new UserManager());
+ $user2->Name = "manager";
+
+ $token1 = $this->userDao->createSignonToken($user1);
+ $token2 = $this->userDao->createSignonToken($user2);
+
+ $check1 = $this->userDao->validateSignon($token1);
+ $check2 = $this->userDao->validateSignon($token2);
+
+ $this->assertIsAdmin($check1);
+ $this->assertNotNull($check2);
+ $this->assertEqual($check2->Name, $user2->Name);
+
+ $this->userDao->clearSignonTokens();
+
+ $check3 = $this->userDao->validateSignon($token1);
+ $check4 = $this->userDao->validateSignon($token2);
+
+ $this->assertNull($check3);
+ $this->assertNull($check4);
+ }
+}
+
+?>
\ No newline at end of file |