gnu-social/tests/Core/UserRightsTest.php

98 lines
2.5 KiB
PHP
Raw Normal View History

2009-09-15 20:28:11 +01:00
<?php
2019-07-12 16:31:14 +01:00
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
2009-09-15 20:28:11 +01:00
2019-07-12 16:31:14 +01:00
namespace Tests\Unit;
if (!defined('INSTALLDIR')) {
define('INSTALLDIR', dirname(dirname(__DIR__)));
}
2019-07-25 01:29:20 +01:00
if (!defined('PUBLICDIR')) {
define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
}
2019-07-12 16:31:14 +01:00
if (!defined('GNUSOCIAL')) {
define('GNUSOCIAL', true);
}
if (!defined('STATUSNET')) { // Compatibility
define('STATUSNET', true);
2009-09-15 20:28:11 +01:00
}
2019-07-12 16:31:14 +01:00
use Exception;
use PHPUnit\Framework\TestCase;
use User;
2009-09-15 20:28:11 +01:00
require_once INSTALLDIR . '/lib/common.php';
2019-07-12 16:31:14 +01:00
final class UserRightsTest extends TestCase
2009-09-15 20:28:11 +01:00
{
protected $user = null;
2019-07-25 01:29:20 +01:00
function setUp(): void
2009-09-15 20:28:11 +01:00
{
$user = User::getKV('nickname', 'userrightstestuser');
2009-12-15 22:49:53 +00:00
if ($user) {
// Leftover from a broken test run?
$profile = $user->getProfile();
$user->delete();
$profile->delete();
}
2009-09-15 20:28:11 +01:00
$this->user = User::register(array('nickname' => 'userrightstestuser'));
2009-12-15 22:49:53 +00:00
if (!$this->user) {
throw new Exception("Couldn't register userrightstestuser");
}
2009-09-15 20:28:11 +01:00
}
2019-07-25 01:29:20 +01:00
function tearDown(): void
2009-09-15 20:28:11 +01:00
{
2009-12-15 22:49:53 +00:00
if ($this->user) {
$profile = $this->user->getProfile();
$this->user->delete();
$profile->delete();
}
2009-09-15 20:28:11 +01:00
}
function testInvalidRole()
{
$this->assertFalse($this->user->hasRole('invalidrole'));
}
function standardRoles()
{
2009-12-15 22:49:53 +00:00
return array(array('admin'),
2019-07-12 16:31:14 +01:00
array('moderator'));
2009-09-15 20:28:11 +01:00
}
/**
* @dataProvider standardRoles
2019-07-12 16:31:14 +01:00
* @param $role
2009-09-15 20:28:11 +01:00
*/
function testUngrantedRole($role)
{
$this->assertFalse($this->user->hasRole($role));
}
/**
* @dataProvider standardRoles
2019-07-12 16:31:14 +01:00
* @param $role
2009-09-15 20:28:11 +01:00
*/
function testGrantedRole($role)
{
$this->user->grantRole($role);
2009-12-15 22:49:53 +00:00
$this->assertTrue($this->user->hasRole($role));
2009-09-15 20:28:11 +01:00
}
}