gnu-social/tests/Core/UserRightsTest.php

98 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2009-09-16 04:28:11 +09:00
<?php
2019-07-13 00:31:14 +09: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-16 04:28:11 +09:00
2019-07-13 00:31:14 +09:00
namespace Tests\Unit;
if (!defined('INSTALLDIR')) {
define('INSTALLDIR', dirname(dirname(__DIR__)));
}
2019-07-25 09:29:20 +09:00
if (!defined('PUBLICDIR')) {
define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
}
2019-07-13 00:31:14 +09:00
if (!defined('GNUSOCIAL')) {
define('GNUSOCIAL', true);
}
if (!defined('STATUSNET')) { // Compatibility
define('STATUSNET', true);
2009-09-16 04:28:11 +09:00
}
2019-07-13 00:31:14 +09:00
use Exception;
use PHPUnit\Framework\TestCase;
use User;
2009-09-16 04:28:11 +09:00
2019-08-23 21:36:02 +09:00
require_once INSTALLDIR . '/lib/util/common.php';
2009-09-16 04:28:11 +09:00
2019-07-13 00:31:14 +09:00
final class UserRightsTest extends TestCase
2009-09-16 04:28:11 +09:00
{
2020-06-24 21:08:11 +09:00
protected $user;
2009-09-16 04:28:11 +09:00
2020-06-24 21:08:11 +09:00
protected function setUp(): void
2009-09-16 04:28:11 +09:00
{
$user = User::getKV('nickname', 'userrightstestuser');
2009-12-16 07:49:53 +09:00
if ($user) {
// Leftover from a broken test run?
$profile = $user->getProfile();
$user->delete();
$profile->delete();
}
2020-06-24 21:08:11 +09:00
$this->user = User::register(['nickname' => 'userrightstestuser']);
2009-12-16 07:49:53 +09:00
if (!$this->user) {
throw new Exception("Couldn't register userrightstestuser");
}
2009-09-16 04:28:11 +09:00
}
2020-06-24 21:08:11 +09:00
protected function tearDown(): void
2009-09-16 04:28:11 +09:00
{
2009-12-16 07:49:53 +09:00
if ($this->user) {
$profile = $this->user->getProfile();
$this->user->delete();
$profile->delete();
}
2009-09-16 04:28:11 +09:00
}
2020-06-24 21:08:11 +09:00
public function testInvalidRole()
2009-09-16 04:28:11 +09:00
{
2020-06-24 21:08:11 +09:00
static::assertFalse($this->user->hasRole('invalidrole'));
2009-09-16 04:28:11 +09:00
}
2020-06-24 21:08:11 +09:00
public function standardRoles()
2009-09-16 04:28:11 +09:00
{
2020-06-24 21:08:11 +09:00
return [['admin'],
['moderator'],];
2009-09-16 04:28:11 +09:00
}
/**
* @dataProvider standardRoles
2020-06-24 21:08:11 +09:00
*
2019-07-13 00:31:14 +09:00
* @param $role
2009-09-16 04:28:11 +09:00
*/
2020-06-24 21:08:11 +09:00
public function testUngrantedRole($role)
2009-09-16 04:28:11 +09:00
{
2020-06-24 21:08:11 +09:00
static::assertFalse($this->user->hasRole($role));
2009-09-16 04:28:11 +09:00
}
/**
* @dataProvider standardRoles
2020-06-24 21:08:11 +09:00
*
2019-07-13 00:31:14 +09:00
* @param $role
2009-09-16 04:28:11 +09:00
*/
2020-06-24 21:08:11 +09:00
public function testGrantedRole($role)
2009-09-16 04:28:11 +09:00
{
$this->user->grantRole($role);
2020-06-24 21:08:11 +09:00
static::assertTrue($this->user->hasRole($role));
2009-09-16 04:28:11 +09:00
}
}