UserRightsTest.php 1.6 KB
Newer Older
Evan Prodromou's avatar
Evan Prodromou committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php

if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
    print "This script must be run from the command line\n";
    exit();
}

define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
define('STATUSNET', true);

require_once INSTALLDIR . '/lib/common.php';

class UserRightsTest extends PHPUnit_Framework_TestCase
{
    protected $user = null;

    function setUp()
    {
Brion Vibber's avatar
Brion Vibber committed
19 20 21 22 23 24 25
        $user = User::staticGet('nickname', 'userrightstestuser');
        if ($user) {
            // Leftover from a broken test run?
            $profile = $user->getProfile();
            $user->delete();
            $profile->delete();
        }
Evan Prodromou's avatar
Evan Prodromou committed
26
        $this->user = User::register(array('nickname' => 'userrightstestuser'));
Brion Vibber's avatar
Brion Vibber committed
27 28 29
        if (!$this->user) {
            throw new Exception("Couldn't register userrightstestuser");
        }
Evan Prodromou's avatar
Evan Prodromou committed
30 31 32 33
    }

    function tearDown()
    {
Brion Vibber's avatar
Brion Vibber committed
34 35 36 37 38
        if ($this->user) {
            $profile = $this->user->getProfile();
            $this->user->delete();
            $profile->delete();
        }
Evan Prodromou's avatar
Evan Prodromou committed
39 40 41 42 43 44 45 46 47
    }

    function testInvalidRole()
    {
        $this->assertFalse($this->user->hasRole('invalidrole'));
    }

    function standardRoles()
    {
Brion Vibber's avatar
Brion Vibber committed
48 49
        return array(array('admin'),
                     array('moderator'));
Evan Prodromou's avatar
Evan Prodromou committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    }

    /**
     * @dataProvider standardRoles
     *
     */

    function testUngrantedRole($role)
    {
        $this->assertFalse($this->user->hasRole($role));
    }

    /**
     * @dataProvider standardRoles
     *
     */

    function testGrantedRole($role)
    {
        $this->user->grantRole($role);
Brion Vibber's avatar
Brion Vibber committed
70
        $this->assertTrue($this->user->hasRole($role));
Evan Prodromou's avatar
Evan Prodromou committed
71 72
    }
}