partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

commit f57173906e005bdd4936a374acfedcf65ff328fe
parent ca5650d97f85468edb39b87dd694371c98ed0678
Author: Felicitus <felicitus@felicitus.org>
Date:   Sat, 12 May 2012 09:46:01 +0200

Added unit tests for the User entity

Diffstat:
Msrc/backend/de/RaumZeitLabor/PartKeepr/User/User.php | 47++++++++++++++++++++++++++++++++++++++++++++++-
Msrc/backend/de/RaumZeitLabor/PartKeepr/UserPreference/UserPreference.php | 19++++++++++++++++++-
Msrc/backend/de/RaumZeitLabor/PartKeepr/UserPreference/UserPreferenceService.php | 36+++++++++++++++++++++++-------------
Asrc/backend/de/RaumZeitLabor/PartKeepr/Util/Exceptions/EntityNotPersistantException.php | 14++++++++++++++
Mtests/User/UserTest.php | 128++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Mtests/UserPreference/UserPreferenceTest.php | 30++++++++++++++++++++++++++++++
Mtests/bootstrap.php | 10+++++-----
7 files changed, 263 insertions(+), 21 deletions(-)

diff --git a/src/backend/de/RaumZeitLabor/PartKeepr/User/User.php b/src/backend/de/RaumZeitLabor/PartKeepr/User/User.php @@ -1,6 +1,8 @@ <?php namespace de\RaumZeitLabor\PartKeepr\User; +use de\RaumZeitLabor\PartKeepr\UserPreference\UserPreference; + use de\RaumZeitLabor\PartKeepr\Util\Deserializable, de\RaumZeitLabor\PartKeepr\Util\Serializable, de\RaumZeitLabor\PartKeepr\Util\BaseEntity, @@ -17,7 +19,6 @@ class User extends BaseEntity implements Serializable, Deserializable { /** @Column(type="boolean") */ private $admin; - /** * Creates a new user object. * @@ -35,6 +36,7 @@ class User extends BaseEntity implements Serializable, Deserializable { $this->setAdmin(false); } + /** * Sets the username. * @@ -181,6 +183,49 @@ class User extends BaseEntity implements Serializable, Deserializable { } } } + + /** + * Sets a user preference + * + * @param string $preferenceKey The preference key + * @param string $preferenceValue The preference value + */ + public function setPreference ($preferenceKey, $preferenceValue) { + return UserPreference::setPreference($this, $preferenceKey, $preferenceValue); + } + + /** + * Returns a given preference object + * + * @param string $preferenceKey The preference key + * @return UserPreference The user preference object + * @thorws UserPreferenceNotFoundException If the preference key was not found + */ + public function getPreference ($preferenceKey) { + return UserPreference::getPreference($this, $preferenceKey); + } + + /** + * Returns a given preference value + * + * @param string $preferenceKey The preference key + * @return UserPreference The user preference object + * @thorws UserPreferenceNotFoundException If the preference key was not found + */ + public function getPreferenceValue ($preferenceKey) { + return UserPreference::getPreferenceValue($this, $preferenceKey); + } + + /** + * Deletes the given preference + * + * @param string $preferenceKey The preference key + * @return UserPreference The user preference object + * @thorws UserPreferenceNotFoundException If the preference key was not found + */ + public function deletePreference ($preferenceKey) { + return UserPreference::deletePreference($this, $preferenceKey); + } /** * Loads a user by name. diff --git a/src/backend/de/RaumZeitLabor/PartKeepr/UserPreference/UserPreference.php b/src/backend/de/RaumZeitLabor/PartKeepr/UserPreference/UserPreference.php @@ -7,6 +7,7 @@ use de\RaumZeitLabor\PartKeepr\Util\Serializable, de\RaumZeitLabor\PartKeepr\Util\Configuration, de\RaumZeitLabor\PartKeepr\Util\BaseEntity, de\RaumZeitLabor\PartKeepr\UserPreference\Exceptions\UserPreferenceNotFoundException, + de\RaumZeitLabor\PartKeepr\Util\Exceptions\EntityNotPersistantException, Doctrine\ORM\NoResultException; /** @@ -110,8 +111,13 @@ class UserPreference implements Serializable { * @param User $user The user to set the preference for * @param string $key The key to set * @param string $value The value to set + * @throws EntityNotPersistantException Thrown if the entity is not persistant */ public static function setPreference (User $user, $key, $value) { + if (!PartKeepr::getEM()->contains($user)) { + throw new EntityNotPersistantException(); + } + $dql = "SELECT up FROM de\RaumZeitLabor\PartKeepr\UserPreference\UserPreference up WHERE up.user = :user AND "; $dql .= "up.preferenceKey = :key"; @@ -142,7 +148,8 @@ class UserPreference implements Serializable { * @param User $user The user to retrieve the preference for * @param string $key The preference key to retrieve * @return string The preference string - * @throws UserPreferenceNotFoundException Thrown if the preference key was not found + * @throws UserPreferenceNotFoundException Thrown if the preference key was not found + * @throws EntityNotPersistantException Thrown if the entity is not persistant */ public static function getPreferenceValue (User $user, $key) { $userPreference = self::getPreference($user, $key); @@ -157,8 +164,13 @@ class UserPreference implements Serializable { * @param string $key The preference key to retrieve * @return UserPreference The preference object * @throws UserPreferenceNotFoundException Thrown if the preference key was not found + * @throws EntityNotPersistantException Thrown if the entity is not persistant */ public static function getPreference (User $user, $key) { + if (!PartKeepr::getEM()->contains($user)) { + throw new EntityNotPersistantException(); + } + $dql = "SELECT up FROM de\RaumZeitLabor\PartKeepr\UserPreference\UserPreference up WHERE up.user = :user AND "; $dql .= "up.preferenceKey = :key"; @@ -179,8 +191,13 @@ class UserPreference implements Serializable { * * @param User $user The user to delete the preference for * @param string $key The key to delete + * @throws EntityNotPersistantException Thrown if the entity is not persistant */ public static function deletePreference (User $user, $key) { + if (!PartKeepr::getEM()->contains($user)) { + throw new EntityNotPersistantException(); + } + $dql = "DELETE FROM de\RaumZeitLabor\PartKeepr\UserPreference\UserPreference up WHERE up.user = :user AND "; $dql .= "up.preferenceKey = :key"; diff --git a/src/backend/de/RaumZeitLabor/PartKeepr/UserPreference/UserPreferenceService.php b/src/backend/de/RaumZeitLabor/PartKeepr/UserPreference/UserPreferenceService.php @@ -18,32 +18,25 @@ use de\RaumZeitLabor\PartKeepr\User\User, */ class UserPreferenceService extends Service implements RestfulService { /** - * Returns the preferences for the current user. + * Returns the preferences for the current user, or a user specified by user_id (admin only). * * (non-PHPdoc) * @see de\RaumZeitLabor\PartKeepr\Service.RestfulService::get() */ public function get () { - $aPreferences = array(); - - /* Extract all preferences */ - $dql = "SELECT up FROM de\RaumZeitLabor\PartKeepr\UserPreference\UserPreference up WHERE up.user = :user"; - - $query = PartKeepr::getEM()->createQuery($dql); + $user = null; if ($this->hasParameter("user_id") && SessionManager::getCurrentSession()->getUser()->isAdmin()) { if ($this->getParameter("user_id") != 0) { - $query->setParameter("user", User::loadById($this->getParameter("user_id"))); - } else { - $query->setParameter("user", 0); + $user = User::loadById($this->getParameter("user_id")); } - } else { - $query->setParameter("user", SessionManager::getCurrentSession()->getUser()); + $user = SessionManager::getCurrentSession()->getUser(); } + $aPreferences = array(); - foreach ($query->getResult() as $result) { + foreach (self::getPreferencesForUser($user) as $result) { $aPreferences[] = $result->serialize(); } @@ -51,6 +44,23 @@ class UserPreferenceService extends Service implements RestfulService { } /** + * Returns the user preferences for a specific user + * @param User $user The user to retrieve the user preferences for + * @return array An array of UserPreference objects + */ + public static function getPreferencesForUser (User $user) { + $aPreferences = array(); + + /* Extract all preferences */ + $dql = "SELECT up FROM de\RaumZeitLabor\PartKeepr\UserPreference\UserPreference up WHERE up.user = :user"; + + $query = PartKeepr::getEM()->createQuery($dql); + $query->setParameter("user", $user); + + return $query->getResult(); + } + + /** * Creates or updates a value for a specific key. * * (non-PHPdoc) diff --git a/src/backend/de/RaumZeitLabor/PartKeepr/Util/Exceptions/EntityNotPersistantException.php b/src/backend/de/RaumZeitLabor/PartKeepr/Util/Exceptions/EntityNotPersistantException.php @@ -0,0 +1,14 @@ +<?php +namespace de\RaumZeitLabor\PartKeepr\Util\Exceptions; + +use de\RaumZeitLabor\PartKeepr\Util\SerializableException, + de\RaumZeitLabor\PartKeepr\PartKeepr; + +/** + * Thrown when an entity via loadById() was not found. + */ +class EntityNotPersistantException extends SerializableException { + public function __construct () { + parent::__construct("The entity is not persistant."); + } +} diff --git a/tests/User/UserTest.php b/tests/User/UserTest.php @@ -1,12 +1,21 @@ <?php namespace de\RaumZeitLabor\PartKeepr\Tests\User; -use de\RaumZeitLabor\PartKeepr\User\User; +use de\RaumZeitLabor\PartKeepr\User\UserManager, + de\RaumZeitLabor\PartKeepr\PartKeepr, + de\RaumZeitLabor\PartKeepr\User\User; class UserTest extends \PHPUnit_Framework_TestCase { protected $backupGlobals = false; /** + * Clear out any non-flushed changes from the EntityManager after the test is complete + */ + public static function tearDownAfterClass () { + PartKeepr::getEM()->clear(); + } + + /** * Tests if the admin user can be loaded by it's name */ public function testloadByName () { @@ -20,4 +29,121 @@ class UserTest extends \PHPUnit_Framework_TestCase { public function testloadByNameException () { $user = User::loadByName("testloadByNameException"); } + + /** + * Tests creating new users. + */ + public function testCreateUser () { + $username = "test1"; + $password = "123"; + + $user = new User($username, $password); + + // Make sure that the new user isn't an admin. + $this->assertEquals(false, $user->isAdmin()); + + $this->assertEquals($username, $user->getUsername()); + $this->assertEquals(true, $user->comparePassword($password)); + } + + /** + * Tests the password hashing + */ + public function testPasswordHashing () { + $username = "test2"; + $password = "123"; + $hashedPassword = md5($password); + + $user = new User($username, $password); + + $this->assertEquals($hashedPassword, $user->getHashedPassword()); + + // Makes sure that the compareHashedPassword method works + $this->assertEquals(true, $user->compareHashedPassword($hashedPassword)); + + // This must fail, because we aren't comparing a hashed password + $this->assertEquals(false, $user->compareHashedPassword($password)); + } + + /** + * Tests the serialize method + */ + public function testSerialize () { + $username = "test3"; + $password = "123"; + + $user = new User($username, $password); + + $expectedArray = array ("id" => null, "username" => $username); + + $this->assertEquals($expectedArray, $user->serialize()); + } + + /** + * Tests the deserialize method + */ + public function testDeserialize () { + $username = "test4"; + $password = "123"; + $hashedPassword = md5($password); + + $input = array("username" => $username, "password" => $password); + + $user = new User(); + $user->deserialize($input); + + $this->assertEquals($username, $user->getUsername()); + $this->assertEquals($hashedPassword, $user->getHashedPassword()); + } + + /** + * Makes sure that an exception is thrown when attempting to set a preference if the user is not persistant yet. + * + * @expectedException de\RaumZeitLabor\PartKeepr\Util\Exceptions\EntityNotPersistantException + */ + public function testSetNonPersistantUserPreference () { + $user = new User(); + $user->setPreference("test", "foo"); + } + + /** + * Makes sure that an exception is thrown when attempting to get a preference if the user is not persistant yet. + * + * @expectedException de\RaumZeitLabor\PartKeepr\Util\Exceptions\EntityNotPersistantException + */ + public function testGetNonPersistantUserPreference () { + $user = new User(); + $user->getPreference("test"); + } + + /** + * Makes sure that an exception is thrown when attempting to delete a preference if the user is not persistant yet. + * + * @expectedException de\RaumZeitLabor\PartKeepr\Util\Exceptions\EntityNotPersistantException + */ + public function testDeleteNonPersistantUserPreference () { + $user = new User(); + $user->deletePreference("test"); + } + + /** + * Test the helper methods for setting / getting / deleting user preferences + */ + public function testUserPreference () { + $username = "test5"; + $user = new User($username, "123"); + PartKeepr::getEM()->persist($user); + PartKeepr::getEM()->flush(); + + $user->setPreference("test", "foo"); + + $this->assertEquals("foo", $user->getPreferenceValue("test")); + $this->assertEquals("foo", $user->getPreference("test")->getValue()); + + $user->deletePreference("test"); + + echo $user->getId(); + + UserManager::getInstance()->deleteUser($user->getId()); + } } \ No newline at end of file diff --git a/tests/UserPreference/UserPreferenceTest.php b/tests/UserPreference/UserPreferenceTest.php @@ -142,4 +142,34 @@ class UserPreferenceTest extends \PHPUnit_Framework_TestCase { $this->assertEquals($expectedArray, $userPreference->serialize()); } + + /** + * Makes sure that an exception is thrown when attempting to set a preference if the user is not persistant yet. + * + * @expectedException de\RaumZeitLabor\PartKeepr\Util\Exceptions\EntityNotPersistantException + */ + public function testSetNonPersistantUserPreference () { + $user = new User(); + UserPreference::setPreference($user, "test", "foo"); + } + + /** + * Makes sure that an exception is thrown when attempting to get a preference if the user is not persistant yet. + * + * @expectedException de\RaumZeitLabor\PartKeepr\Util\Exceptions\EntityNotPersistantException + */ + public function testGetNonPersistantUserPreference () { + $user = new User(); + UserPreference::getPreference($user, "test"); + } + + /** + * Makes sure that an exception is thrown when attempting to delete a preference if the user is not persistant yet. + * + * @expectedException de\RaumZeitLabor\PartKeepr\Util\Exceptions\EntityNotPersistantException + */ + public function testDeleteNonPersistantUserPreference () { + $user = new User(); + UserPreference::deletePreference($user, "test"); + } } \ No newline at end of file diff --git a/tests/bootstrap.php b/tests/bootstrap.php @@ -36,12 +36,12 @@ function initializeEnvironment () { PartKeepr::getEM()->persist($user); /* Create a blank regular user */ - $user = new User(); - $user->setUsername("regular"); - $user->setPassword("regular"); - $user->setAdmin(false); + $user2 = new User(); + $user2->setUsername("regular"); + $user2->setPassword("regular"); + $user2->setAdmin(false); - PartKeepr::getEM()->persist($user); + PartKeepr::getEM()->persist($user2); PartKeepr::getEM()->flush(); }