| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Tests\Unit\Permissions; |
| 22 |
|
| 23 |
use InvalidArgumentException; |
| 24 |
use MediaWiki\Page\PageIdentity; |
| 25 |
use MediaWiki\Page\PageIdentityValue; |
| 26 |
use MediaWiki\Permissions\PermissionStatus; |
| 27 |
use MediaWiki\Permissions\SimpleAuthority; |
| 28 |
use MediaWiki\User\UserIdentityValue; |
| 29 |
use MediaWikiUnitTestCase; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class SimpleAuthorityTest extends MediaWikiUnitTestCase { |
| 35 |
|
| 36 |
public function testGetAuthor() { |
| 37 |
$actor = new UserIdentityValue( 12, 'Test' ); |
| 38 |
$authority = new SimpleAuthority( $actor, [] ); |
| 39 |
|
| 40 |
$this->assertSame( $actor, $authority->getUser() ); |
| 41 |
} |
| 42 |
|
| 43 |
public function testPermissions() { |
| 44 |
$actor = new UserIdentityValue( 12, 'Test' ); |
| 45 |
$authority = new SimpleAuthority( $actor, [ 'foo', 'bar' ] ); |
| 46 |
|
| 47 |
$this->assertTrue( $authority->isAllowed( 'foo' ) ); |
| 48 |
$this->assertTrue( $authority->isAllowed( 'bar' ) ); |
| 49 |
$this->assertFalse( $authority->isAllowed( 'quux' ) ); |
| 50 |
|
| 51 |
$this->assertTrue( $authority->isAllowedAll( 'foo', 'bar' ) ); |
| 52 |
$this->assertTrue( $authority->isAllowedAny( 'bar', 'quux' ) ); |
| 53 |
|
| 54 |
$this->assertFalse( $authority->isAllowedAll( 'foo', 'quux' ) ); |
| 55 |
$this->assertFalse( $authority->isAllowedAny( 'xyzzy', 'quux' ) ); |
| 56 |
} |
| 57 |
|
| 58 |
public function testProbablyCan() { |
| 59 |
$target = new PageIdentityValue( 321, NS_MAIN, __METHOD__, PageIdentity::LOCAL ); |
| 60 |
$actor = new UserIdentityValue( 12, 'Test' ); |
| 61 |
$authority = new SimpleAuthority( $actor, [ 'foo', 'bar' ] ); |
| 62 |
|
| 63 |
$this->assertTrue( $authority->probablyCan( 'foo', $target ) ); |
| 64 |
$this->assertTrue( $authority->probablyCan( 'bar', $target ) ); |
| 65 |
$this->assertFalse( $authority->probablyCan( 'quux', $target ) ); |
| 66 |
|
| 67 |
$status = new PermissionStatus(); |
| 68 |
$authority->probablyCan( 'foo', $target, $status ); |
| 69 |
$this->assertStatusOK( $status ); |
| 70 |
|
| 71 |
$authority->probablyCan( 'quux', $target, $status ); |
| 72 |
$this->assertStatusNotOK( $status ); |
| 73 |
} |
| 74 |
|
| 75 |
public function testDefinitelyCan() { |
| 76 |
$target = new PageIdentityValue( 321, NS_MAIN, __METHOD__, PageIdentity::LOCAL ); |
| 77 |
$actor = new UserIdentityValue( 12, 'Test' ); |
| 78 |
$authority = new SimpleAuthority( $actor, [ 'foo', 'bar' ] ); |
| 79 |
|
| 80 |
$this->assertTrue( $authority->definitelyCan( 'foo', $target ) ); |
| 81 |
$this->assertTrue( $authority->definitelyCan( 'bar', $target ) ); |
| 82 |
$this->assertFalse( $authority->definitelyCan( 'quux', $target ) ); |
| 83 |
|
| 84 |
$status = new PermissionStatus(); |
| 85 |
$authority->definitelyCan( 'foo', $target, $status ); |
| 86 |
$this->assertStatusOK( $status ); |
| 87 |
|
| 88 |
$authority->definitelyCan( 'quux', $target, $status ); |
| 89 |
$this->assertStatusNotOK( $status ); |
| 90 |
} |
| 91 |
|
| 92 |
public function testAuthorize() { |
| 93 |
$actor = new UserIdentityValue( 12, 'Test' ); |
| 94 |
$authority = new SimpleAuthority( $actor, [ 'foo', 'bar' ] ); |
| 95 |
|
| 96 |
$this->assertTrue( $authority->authorizeAction( 'foo' ) ); |
| 97 |
$this->assertTrue( $authority->authorizeAction( 'bar' ) ); |
| 98 |
$this->assertFalse( $authority->authorizeAction( 'quux' ) ); |
| 99 |
|
| 100 |
$status = new PermissionStatus(); |
| 101 |
$authority->authorizeAction( 'foo', $status ); |
| 102 |
$this->assertStatusOK( $status ); |
| 103 |
|
| 104 |
$authority->authorizeAction( 'quux', $status ); |
| 105 |
$this->assertStatusNotOK( $status ); |
| 106 |
} |
| 107 |
|
| 108 |
public function testAuthorizeRead() { |
| 109 |
$target = new PageIdentityValue( 321, NS_MAIN, __METHOD__, PageIdentity::LOCAL ); |
| 110 |
$actor = new UserIdentityValue( 12, 'Test' ); |
| 111 |
$authority = new SimpleAuthority( $actor, [ 'foo', 'bar' ] ); |
| 112 |
|
| 113 |
$this->assertTrue( $authority->authorizeRead( 'foo', $target ) ); |
| 114 |
$this->assertTrue( $authority->authorizeRead( 'bar', $target ) ); |
| 115 |
$this->assertFalse( $authority->authorizeRead( 'quux', $target ) ); |
| 116 |
|
| 117 |
$status = new PermissionStatus(); |
| 118 |
$authority->authorizeRead( 'foo', $target, $status ); |
| 119 |
$this->assertStatusOK( $status ); |
| 120 |
|
| 121 |
$authority->authorizeRead( 'quux', $target, $status ); |
| 122 |
$this->assertStatusNotOK( $status ); |
| 123 |
} |
| 124 |
|
| 125 |
public function testAuthorizeWrite() { |
| 126 |
$target = new PageIdentityValue( 321, NS_MAIN, __METHOD__, PageIdentity::LOCAL ); |
| 127 |
$actor = new UserIdentityValue( 12, 'Test' ); |
| 128 |
$authority = new SimpleAuthority( $actor, [ 'foo', 'bar' ] ); |
| 129 |
|
| 130 |
$this->assertTrue( $authority->authorizeWrite( 'foo', $target ) ); |
| 131 |
$this->assertTrue( $authority->authorizeWrite( 'bar', $target ) ); |
| 132 |
$this->assertFalse( $authority->authorizeWrite( 'quux', $target ) ); |
| 133 |
|
| 134 |
$status = new PermissionStatus(); |
| 135 |
$authority->authorizeWrite( 'foo', $target, $status ); |
| 136 |
$this->assertStatusOK( $status ); |
| 137 |
|
| 138 |
$authority->authorizeWrite( 'quux', $target, $status ); |
| 139 |
$this->assertStatusNotOK( $status ); |
| 140 |
} |
| 141 |
|
| 142 |
public function testIsAllowedAnyThrowsOnEmptySet() { |
| 143 |
$actor = new UserIdentityValue( 12, 'Test' ); |
| 144 |
$authority = new SimpleAuthority( $actor, [ 'foo', 'bar' ] ); |
| 145 |
|
| 146 |
$this->expectException( InvalidArgumentException::class ); |
| 147 |
$authority->isAllowedAny(); |
| 148 |
} |
| 149 |
|
| 150 |
public function testIsAllowedAllThrowsOnEmptySet() { |
| 151 |
$actor = new UserIdentityValue( 12, 'Test' ); |
| 152 |
$authority = new SimpleAuthority( $actor, [ 'foo', 'bar' ] ); |
| 153 |
|
| 154 |
$this->expectException( InvalidArgumentException::class ); |
| 155 |
$authority->isAllowedAll(); |
| 156 |
} |
| 157 |
|
| 158 |
public function testGetBlock() { |
| 159 |
$actor = new UserIdentityValue( 12, 'Test' ); |
| 160 |
$authority = new SimpleAuthority( $actor, [] ); |
| 161 |
|
| 162 |
$this->assertNull( $authority->getBlock() ); |
| 163 |
} |
| 164 |
|
| 165 |
} |