Skip to content

Commit

Permalink
Add user role unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
kayue committed Jun 28, 2013
1 parent a13285f commit 8a5710b
Showing 1 changed file with 45 additions and 18 deletions.
63 changes: 45 additions & 18 deletions Tests/Model/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Kayue\WordpressBundle\Tests\Model;

use Kayue\WordpressBundle\Model\User;
use Kayue\WordpressBundle\Entity\UserMeta;
use Kayue\WordpressBundle\Entity\User;

class UserTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -24,35 +25,61 @@ public function testEmail()
$this->assertEquals('[email protected]', $user->getEmail());
}

public function testTrueHasRole()
public function testAddRole()
{
$this->markTestIncomplete();
/** @var $user User */
$user = new User();
$meta = new UserMeta();
$meta->setKey('wp_capabilities');
$meta->setValue(array('X' => true));
$user->addMeta($meta);

$user = $this->getUser();
$this->assertContains('ROLE_WP_X', $user->getRoles());

return $user;
}

$defaultrole = User::ROLE_DEFAULT;
$newrole = 'ROLE_X';
/**
* @depends testAddRole
*/
public function testAddAnotherRole(User $user)
{
// get existing capabilities
/** @var $capabilities UserMeta */
$capabilities = $user->getMetas()->filter(function(UserMeta $meta) {
return $meta->getKey() === 'wp_capabilities';
})->first();

$this->assertTrue($user->hasRole($defaultrole));
$capabilities->setValue(array_merge($capabilities->getValue(), array('Y' => true)));

$user->addRole($defaultrole);
$this->assertTrue($user->hasRole($defaultrole));
$this->assertContains('ROLE_WP_X', $user->getRoles());
$this->assertContains('ROLE_WP_Y', $user->getRoles());

$user->addRole($newrole);
$this->assertTrue($user->hasRole($newrole));
return $user;
}

public function testFalseHasRole()
/**
* @depends testAddAnotherRole
*/
public function testRemoveRole(User $user)
{
$this->markTestIncomplete();
// get existing capabilities
/** @var $capabilities UserMeta */
$capabilities = $user->getMetas()->filter(function(UserMeta $meta) {
return $meta->getKey() === 'wp_capabilities';
})->first();

$user = $this->getUser();
$newrole = 'ROLE_X';
$value = $capabilities->getValue();
$capabilities->setValue($value['X']);

$this->assertFalse($user->hasRole($newrole));
$this->assertContainsOnly('ROLE_WP_X', $user->getRoles());
}

public function testNoRole()
{
$user = new User();

$user->addRole($newrole);
$this->assertTrue($user->hasRole($newrole));
$this->assertEmpty($user->getRoles());
}

/**
Expand Down

0 comments on commit 8a5710b

Please sign in to comment.