Skip to content

Commit

Permalink
Added the generateId for userid was not autoinc; Added unit tests and…
Browse files Browse the repository at this point in the history
… fixed some bugs
  • Loading branch information
byjg committed Apr 25, 2016
1 parent 89f9835 commit 20e5633
Show file tree
Hide file tree
Showing 9 changed files with 308 additions and 28 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Expand Up @@ -8,6 +8,5 @@ install:
- composer install

script:
- cd tests
- phpunit -v --bootstrap bootstrap.php src/WebRequestTest.php
- phpunit

28 changes: 28 additions & 0 deletions phpunit.xml.dist
@@ -0,0 +1,28 @@
<?xml version="1.0"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->

<!-- see http://www.phpunit.de/wiki/Documentation -->
<phpunit bootstrap="./vendor/autoload.php"
colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="true">

<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
</filter>

<testsuites>
<testsuite name="Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>

</phpunit>
8 changes: 8 additions & 0 deletions src/Exception/UserExistsException.php
@@ -0,0 +1,8 @@
<?php

namespace ByJG\Authenticate\Exception;

class UserExistsException extends \Exception
{
//put your code here
}
18 changes: 14 additions & 4 deletions src/UsersAnyDataset.php
Expand Up @@ -6,6 +6,7 @@
use ByJG\AnyDataset\Repository\IteratorFilter;
use ByJG\AnyDataset\Repository\IteratorInterface;
use ByJG\AnyDataset\Repository\SingleRow;
use ByJG\Authenticate\Exception\UserExistsException;

class UsersAnyDataset extends UsersBase
{
Expand Down Expand Up @@ -53,24 +54,33 @@ public function save()
* @param string $email
* @param string $password
* @return bool
* @throws UserExistsException
*/
public function addUser($name, $userName, $email, $password)
{
if ($this->getByEmail($email) !== null) {
return false;
throw new UserExistsException('Email already exists');
}
if ($this->getByUsername($userName) !== null) {
return false;
throw new UserExistsException('Username already exists');
}

$userId = $this->generateUserId();
$fixedUsername = preg_replace('/(?:([\w])|([\W]))/', '\1', strtolower($userName));
if (is_null($userId)) {
$userId = $fixedUsername;
}

$this->_anyDataSet->appendRow();

$this->_anyDataSet->addField($this->getUserTable()->id, $userId);
$this->_anyDataSet->addField($this->getUserTable()->username, $fixedUsername);
$this->_anyDataSet->addField($this->getUserTable()->name, $name);
$this->_anyDataSet->addField($this->getUserTable()->username,
preg_replace('/(?:([\w])|([\W]))/', '\1', strtolower($userName)));
$this->_anyDataSet->addField($this->getUserTable()->email, strtolower($email));
$this->_anyDataSet->addField($this->getUserTable()->password, $this->getPasswordHash($password));
$this->_anyDataSet->addField($this->getUserTable()->admin, "");
$this->_anyDataSet->addField($this->getUserTable()->created, date("Y-m-d H:i:s"));

return true;
}

Expand Down
8 changes: 8 additions & 0 deletions src/UsersBase.php
Expand Up @@ -349,4 +349,12 @@ public function isValidToken($username, $token)

return $user;
}

/**
* @inheritdoc
*/
public function generateUserId()
{
return null;
}
}
35 changes: 13 additions & 22 deletions src/UsersDBDataset.php
Expand Up @@ -8,6 +8,7 @@
use ByJG\AnyDataset\Repository\IteratorFilter;
use ByJG\AnyDataset\Repository\IteratorInterface;
use ByJG\AnyDataset\Repository\SingleRow;
use ByJG\Authenticate\Exception\UserExistsException;

class UsersDBDataset extends UsersBase
{
Expand Down Expand Up @@ -152,17 +153,19 @@ public function save()
* @param string $email
* @param string $password
* @return bool
* @throws UserExistsException
*/
public function addUser($name, $userName, $email, $password)
{
if ($this->getByEmail($email) !== null) {
return false;
throw new UserExistsException('Email already exists');
}
if ($this->getByUsername($userName) !== null) {
return false;
throw new UserExistsException('Username already exists');
}
$sql = " INSERT INTO @@Table (@@Name, @@Email, @@Username, @@Password, @@Created ) ";
$sql .=" VALUES ([[name]], [[email]], [[username]], [[password]], [[created]] ) ";

$sql = " INSERT INTO @@Table ( @@UserId, @@Name, @@Email, @@Username, @@Password, @@Created ) ";
$sql .=" VALUES ( [[userid]], [[name]], [[email]], [[username]], [[password]], [[created]] ) ";

$sql = $this->_sqlHelper->createSafeSQL($sql,
array(
Expand All @@ -172,6 +175,7 @@ public function addUser($name, $userName, $email, $password)
'@@Username' => $this->getUserTable()->username,
'@@Password' => $this->getUserTable()->password,
'@@Created' => $this->getUserTable()->created,
'@@UserId' => $this->getUserTable()->id
)
);

Expand All @@ -181,6 +185,7 @@ public function addUser($name, $userName, $email, $password)
$param['username'] = preg_replace('/(?:([\w])|([\W]))/', '\1', strtolower($userName));
$param['password'] = $this->getPasswordHash($password);
$param['created'] = date("Y-m-d H:i:s");
$param['userid'] = $this->generateUserId();

$this->_db->execSQL($sql, $param);

Expand Down Expand Up @@ -244,23 +249,9 @@ public function getUser($filter)
* */
public function removeUserName($login)
{
$baseSql = "DELETE FROM @@Table WHERE @@Username = [[login]] ";
$param = array("login" => $login);
if ($this->getCustomTable()->table != "") {
$sql = $this->_sqlHelper->createSafeSQL($baseSql,
array(
'@@Table' => $this->getCustomTable()->table,
'@@Username' => $this->getUserTable()->username
));
$this->_db->execSQL($sql, $param);
}
$sql = $this->_sqlHelper->createSafeSQL($baseSql,
array(
'@@Table' => $this->getUserTable()->table,
'@@Username' => $this->getUserTable()->username
));
$this->_db->execSQL($sql, $param);
return true;
$user = $this->getByUsername($login);

return $this->removeUserById($user->getField($this->getUserTable()->id));
}

/**
Expand All @@ -271,7 +262,7 @@ public function removeUserName($login)
* */
public function removeUserById($userId)
{
$baseSql = "DELETE FROM @@Table WHERE @@Id = [[login]] ";
$baseSql = "DELETE FROM @@Table WHERE @@Id = [[id]] ";
$param = array("id" => $userId);
if ($this->getCustomTable()->table != "") {
$sql = $this->_sqlHelper->createSafeSQL($baseSql,
Expand Down
6 changes: 6 additions & 0 deletions src/UsersInterface.php
Expand Up @@ -155,4 +155,10 @@ public function getUserTable();
* @return CustomTable Description
*/
public function getCustomTable();

/**
* Return the ID for the user id (if it is not autoincrement)
* @return mixed
*/
public function generateUserId();
}
163 changes: 163 additions & 0 deletions tests/UsersAnyDatasetTest.php
@@ -0,0 +1,163 @@
<?php

namespace ByJG\Authenticate;

use PHPUnit_Framework_TestCase;

/**
* Created by PhpStorm.
* User: jg
* Date: 24/04/16
* Time: 20:21
*/
class UsersAnyDatasetTest extends PHPUnit_Framework_TestCase
{
/**
* @var UsersAnyDataset
*/
protected $object;

protected $prefix = "";

public function setUp()
{
$this->prefix = "user";

$this->object = new UsersAnyDataset('php://memory');
$this->object->addUser('User 1', 'user1', 'user1@gmail.com', 'pwd1');
$this->object->addUser('User 2', 'user2', 'user2@gmail.com', 'pwd2');
$this->object->addUser('User 3', 'user3', 'user3@gmail.com', 'pwd3');
}

public function tearDown()
{
parent::tearDown(); // TODO: Change the autogenerated stub
}

public function testAddUser()
{
$this->object->addUser('John Doe', 'john', 'johndoe@gmail.com', 'mypassword');

$user = $this->object->getByUsername('john');
$this->assertEquals('john', $user->getField($this->object->getUserTable()->id));
$this->assertEquals('John Doe', $user->getField($this->object->getUserTable()->name));
$this->assertEquals('john', $user->getField($this->object->getUserTable()->username));
$this->assertEquals('johndoe@gmail.com', $user->getField($this->object->getUserTable()->email));
$this->assertEquals('91DFD9DDB4198AFFC5C194CD8CE6D338FDE470E2', $user->getField($this->object->getUserTable()->password));
}

/**
* @expectedException \ByJG\Authenticate\Exception\UserExistsException
*/
public function testAddUserError()
{
$this->object->addUser('some user with same username', 'user2', 'bla@gmail.com', 'mypassword');
}

public function testAddUser_generatedId()
{
$mock = $this->getMockBuilder('\ByJG\Authenticate\UsersAnyDataset')
->setMethods( [ 'generateUserId' ] )
->setConstructorArgs( [null] )
->getMock();

$mock->expects($this->once())
->method('generateUserId')
->will($this->returnValue(1234));

$mock->addUser('John Doe', 'john', 'johndoe@gmail.com', 'mypassword');

$user = $mock->getByUsername('john');
$this->assertEquals('1234', $user->getField($this->object->getUserTable()->id));
$this->assertEquals('John Doe', $user->getField($this->object->getUserTable()->name));
$this->assertEquals('john', $user->getField($this->object->getUserTable()->username));
$this->assertEquals('johndoe@gmail.com', $user->getField($this->object->getUserTable()->email));
$this->assertEquals('91DFD9DDB4198AFFC5C194CD8CE6D338FDE470E2', $user->getField($this->object->getUserTable()->password));
}

public function testAddProperty()
{
// Add one property
$this->object->addProperty($this->prefix . '2', 'city', 'Rio de Janeiro');
$user = $this->object->getByUsername('user2');
$this->assertEquals('Rio de Janeiro', $user->getField('city'));

// Add another property (cannot change)
$this->object->addProperty($this->prefix . '2', 'city', 'Belo Horizonte');
$user = $this->object->getByUsername('user2');
$this->assertEquals('Rio de Janeiro', $user->getField('city'));

// Get Property
$this->assertEquals(['Rio de Janeiro', 'Belo Horizonte'], $this->object->getProperty($this->prefix . '2', 'city'));

// Add another property
$this->object->addProperty($this->prefix . '2', 'state', 'RJ');
$user = $this->object->getByUsername('user2');
$this->assertEquals('RJ', $user->getField('state'));

// Remove Property
$this->object->removeProperty($this->prefix . '2', 'state', 'RJ');
$user = $this->object->getByUsername('user2');
$this->assertEmpty($user->getField('state'));

// Remove Property Again
$this->object->removeProperty($this->prefix . '2', 'city', 'Rio de Janeiro');
$this->assertEquals(['Belo Horizonte'], $this->object->getProperty($this->prefix . '2', 'city'));

}

public function testRemoveUserName()
{
$user = $this->object->getByUsername('user1');
$this->assertNotNull($user);

$this->object->removeUserName('user1');
$user = $this->object->getByUsername('user1');
$this->assertNull($user);

}

public function testEditUser()
{
// Getting data
$user = $this->object->getByUsername('user1');
$this->assertEquals('User 1', $user->getField($this->object->getUserTable()->name));

// Change and Persist data
$user->setField($this->object->getUserTable()->name, 'Other name');
$this->object->save();

// Check if data persists
$user = $this->object->getById($this->prefix . '1');
$this->assertEquals('Other name', $user->getField($this->object->getUserTable()->name));
}

public function testIsValidUser()
{
// User Exists!
$user = $this->object->isValidUser('user3', 'pwd3');
$this->assertEquals('User 3', $user->getField($this->object->getUserTable()->name));

// User Does not Exists!
$user = $this->object->isValidUser('user55', 'pwd5');
$this->assertNull($user);
}

public function testIsAdmin()
{
// Set the Admin Flag
$user = $this->object->getByUsername('user3');
$user->setField($this->object->getUserTable()->admin, 'Y');
$this->object->save();

// Check is Admin
$this->assertTrue($this->object->isAdmin($this->prefix . '3'));


}

public function testCreateAuthToken()
{

}
}

0 comments on commit 20e5633

Please sign in to comment.