<?php
error_reporting
(E_ALL | E_STRICT);
$path
= dirname(
__FILE__
).
'/../../src/'
;
set_include_path(get_include_path().PATH_SEPARATOR.
$path
);
require_once
'PHPUnit/Framework/TestCase.php'
;
require_once
'PHPUnit/Framework/IncompleteTestError.php'
;
require_once
'Pluf.php'
;
class
PlufUserTest
extends
PHPUnit_Framework_TestCase {
protected
function
setUp()
{
Pluf::start(dirname(
__FILE__
).
'/../conf/pluf.config.php'
);
$db
= Pluf::db();
$schema
= Pluf::factory(
'Pluf_DB_Schema'
,
$db
);
$models
=
array
(
'Pluf_Group'
,
'Pluf_User'
,
'Pluf_Permission'
,
'Pluf_Message'
,
'Pluf_RowPermission'
);
foreach
(
$models
as
$model
) {
$schema
->model = Pluf::factory(
$model
);
$schema
->dropTables();
if
(true !== (
$res
=
$schema
->createTables())) {
throw
new
Exception(
$res
);
}
}
$perms
=
array
();
for
(
$i
=1;
$i
<=10;
$i
++) {
$perm
=
new
Pluf_Permission();
$perm
->application =
'DummyModel'
;
$perm
->code_name =
'code-'
.
$i
;
$perm
->name =
'code-'
.
$i
;
$perm
->description =
'code-'
.
$i
;
$perm
->create();
$perms
[] =
clone
(
$perm
);
}
$groups
=
array
();
for
(
$i
=1;
$i
<=10;
$i
++) {
$group
=
new
Pluf_Group();
$group
->name =
'Group '
.
$i
;
$group
->description =
'Group '
.
$i
;
$group
->create();
$groups
[] =
clone
(
$group
);
}
$groups
[0]->setAssoc(
$perms
[0]);
$groups
[0]->setAssoc(
$perms
[1]);
$groups
[0]->setAssoc(
$perms
[2]);
$groups
[0]->setAssoc(
$perms
[3]);
$groups
[1]->setAssoc(
$perms
[0]);
$groups
[0]->setAssoc(
$perms
[4]);
$groups
[0]->setAssoc(
$perms
[5]);
$user
=
new
Pluf_User();
$user
->login =
'test'
;
$user
->first_name =
'test'
;
$user
->last_name =
'test'
;
$user
->email =
'toto@example.com'
;
$user
->setPassword(
'test'
);
$user
->active = true;
if
(true !==
$user
->create()) {
throw
new
Exception();
}
$user
->setAssoc(
$groups
[0]);
$user
->setAssoc(
$groups
[1]);
$user
->setAssoc(
$perms
[7]);
$user
->setAssoc(
$perms
[8]);
}
protected
function
tearDown()
{
$db
= Pluf::db();
$schema
= Pluf::factory(
'Pluf_DB_Schema'
,
$db
);
$models
=
array
(
'Pluf_Group'
,
'Pluf_User'
,
'Pluf_Permission'
,
'Pluf_RowPermission'
,
'Pluf_Message'
);
foreach
(
$models
as
$model
) {
$schema
->model = Pluf::factory(
$model
);
$schema
->dropTables();
}
}
public
function
testGetMessages()
{
$user
=
new
Pluf_User(1);
$mess
=
$user
->get_pluf_message_list();
$this
->assertEquals(0,
$mess
->
count
());
}
public
function
testUniqueLogin()
{
$user
=
new
Pluf_User();
$user
->login =
'test'
;
$user
->first_name =
'test'
;
$user
->last_name =
'test'
;
$user
->email =
'toto@example.com'
;
$user
->setPassword(
'test'
);
$user
->active = true;
try
{
$user
->create();
}
catch
(Exception
$e
) {
return
;
}
$this
->fail();
}
public
function
testValidationUnique()
{
$this
->markTestSkipped(
'Need to rewrite the form handling first.'
);
$user
=
new
Pluf_User();
$user
->login =
'test'
;
$user
->first_name =
'test'
;
$user
->last_name =
'test'
;
$user
->email =
'toto@example.com'
;
$user
->setPassword(
'test'
);
$user
->active = true;
$form
=
new
Pluf_Form_Create();
$form
->defineFromModel(
$user
);
$new_data
=
$user
->getData();
$errors
=
$form
->getValidationErrors(
$new_data
);
$this
->assertEquals(1,
count
(
$errors
));
}
public
function
testGetPermissions()
{
$user
=
new
Pluf_User(1);
$a
=
$user
->getAllPermissions();
$this
->assertEquals(8,
count
(
$a
));
}
public
function
testHasPermission()
{
$user
=
new
Pluf_User(1);
$this
->assertEquals(true,
$user
->hasPerm(
'DummyModel.code-5'
));
$this
->assertEquals(false,
$user
->hasPerm(
'DummyModel.code-7'
));
$user
->administrator = true;
$this
->assertEquals(true,
$user
->hasPerm(
'DummyModel.code-7'
));
$user
->active = false;
$this
->assertEquals(false,
$user
->hasPerm(
'DummyModel.code-5'
));
}
public
function
testHasAppPermissions()
{
$user
=
new
Pluf_User(1);
$this
->assertEquals(true,
$user
->hasAppPerms(
'DummyModel'
));
$this
->assertEquals(false,
$user
->hasPerm(
'DummyModel2'
));
$user
->administrator = true;
$this
->assertEquals(true,
$user
->hasPerm(
'DummyModel2'
));
}
public
function
testRowPermission()
{
$user
=
new
Pluf_User(1);
$group
=
new
Pluf_Group();
$group
->name =
'testRowPermission'
;
$group
->description =
'testRowPermission'
;
$group
->create();
for
(
$i
=1;
$i
<=5;
$i
++) {
$mess
=
new
Pluf_Message();
$mess
->user =
$user
;
$mess
->message =
'Dummy object to test against: '
.
$i
;
$mess
->create();
}
$perm
=
new
Pluf_Permission();
$perm
->application =
'Pluf_RowPermission'
;
$perm
->code_name =
'test1'
;
$perm
->name =
'test1'
;
$perm
->description =
'test1'
;
$perm
->create();
$mess
=
new
Pluf_Message(1);
Pluf_RowPermission::add(
$group
,
$mess
,
$perm
);
$this
->assertEquals(false,
$user
->hasPerm(
'Pluf_RowPermission.test1'
,
$mess
));
$user
->setAssoc(
$group
);
$user
->getAllPermissions(true);
$this
->assertEquals(true,
$user
->hasPerm(
'Pluf_RowPermission.test1'
,
$mess
));
$user
->delAssoc(
$group
);
$user
->getAllPermissions(true);
$this
->assertEquals(false,
$user
->hasPerm(
'Pluf_RowPermission.test1'
,
$mess
));
$user
->setAssoc(
$group
);
$user
->getAllPermissions(true);
$this
->assertEquals(true,
$user
->hasPerm(
'Pluf_RowPermission.test1'
,
$mess
));
Pluf_RowPermission::remove(
$group
,
$mess
,
$perm
);
$user
->getAllPermissions(true);
$this
->assertEquals(false,
$user
->hasPerm(
'Pluf_RowPermission.test1'
,
$mess
));
Pluf_RowPermission::add(
$user
,
$mess
,
$perm
);
$user
->getAllPermissions(true);
$this
->assertEquals(true,
$user
->hasPerm(
'Pluf_RowPermission.test1'
,
$mess
));
Pluf_RowPermission::remove(
$user
,
$mess
,
$perm
);
$user
->getAllPermissions(true);
$this
->assertEquals(false,
$user
->hasPerm(
'Pluf_RowPermission.test1'
,
$mess
));
Pluf_RowPermission::add(
$user
,
$mess
,
'Pluf_RowPermission.test1'
);
$user
->getAllPermissions(true);
$this
->assertEquals(true,
$user
->hasPerm(
'Pluf_RowPermission.test1'
,
$mess
));
Pluf_RowPermission::remove(
$user
,
$mess
,
'Pluf_RowPermission.test1'
);
$user
->getAllPermissions(true);
$this
->assertEquals(false,
$user
->hasPerm(
'Pluf_RowPermission.test1'
,
$mess
));
}
}
?>