[BUGFIX] for fromStandalonePermission().
Not sure if this method is used anywhere, but it should not have worked correctly. This should correct it. Add a unit test for this class.
This commit is contained in:
parent
0d939b5ab0
commit
b71e148cea
@ -20,14 +20,17 @@ class PermissionDescription {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor is private.
|
* Constructor is private.
|
||||||
* Use static methods fromGlobalPermission(), fromStandalonePermission(), or fromDescription()
|
* Use static methods fromGlobalPermission(), fromStandalonePermission(),
|
||||||
* to create instances.
|
* or fromDescription() to create instances.
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
* @param int $global_perm
|
||||||
|
* @param int $channel_perm
|
||||||
|
* @param string $description (optional) default empty
|
||||||
*/
|
*/
|
||||||
private function __construct($global_perm, $channel_perm, $description = '') {
|
private function __construct($global_perm, $channel_perm, $description = '') {
|
||||||
|
|
||||||
$this->global_perm = $global_perm;
|
$this->global_perm = $global_perm;
|
||||||
$this->channel_perm = $channel_perm;
|
$this->channel_perm = $channel_perm;
|
||||||
|
|
||||||
$this->fallback_description = ($description == '') ? t('Visible to your default audience') : $description;
|
$this->fallback_description = ($description == '') ? t('Visible to your default audience') : $description;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,7 +46,6 @@ class PermissionDescription {
|
|||||||
return new PermissionDescription('', 0x80000, $description);
|
return new PermissionDescription('', 0x80000, $description);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Use this method only if the interpretation of an empty ACL doesn't fall back to a global
|
* Use this method only if the interpretation of an empty ACL doesn't fall back to a global
|
||||||
* default permission. You should pass one of the constants from boot.php - PERMS_PUBLIC,
|
* default permission. You should pass one of the constants from boot.php - PERMS_PUBLIC,
|
||||||
@ -56,10 +58,10 @@ class PermissionDescription {
|
|||||||
|
|
||||||
$result = new PermissionDescription('', $perm);
|
$result = new PermissionDescription('', $perm);
|
||||||
|
|
||||||
$checkPerm = $this->get_permission_description();
|
$checkPerm = $result->get_permission_description();
|
||||||
if ($checkPerm == $this->fallback_description) {
|
if($checkPerm == $result->fallback_description) {
|
||||||
$result = null;
|
$result = null;
|
||||||
logger('null PermissionDescription from unknown standalone permission: ' . $perm ,LOGGER_DEBUG, LOG_ERROR);
|
logger('null PermissionDescription from unknown standalone permission: ' . $perm, LOGGER_DEBUG, LOG_ERR);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
@ -87,11 +89,11 @@ class PermissionDescription {
|
|||||||
$result = new PermissionDescription('', $channelPerm);
|
$result = new PermissionDescription('', $channelPerm);
|
||||||
} else {
|
} else {
|
||||||
// The acl dialog can handle null arguments, but it shouldn't happen
|
// The acl dialog can handle null arguments, but it shouldn't happen
|
||||||
logger('null PermissionDescription from unknown global permission: ' . $permname ,LOGGER_DEBUG, LOG_ERROR);
|
logger('null PermissionDescription from unknown global permission: ' . $permname, LOGGER_DEBUG, LOG_ERR);
|
||||||
}
|
|
||||||
return $result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a localized description of the permission, or a generic message if the permission
|
* Gets a localized description of the permission, or a generic message if the permission
|
||||||
@ -102,7 +104,6 @@ class PermissionDescription {
|
|||||||
public function get_permission_description() {
|
public function get_permission_description() {
|
||||||
|
|
||||||
switch($this->channel_perm) {
|
switch($this->channel_perm) {
|
||||||
|
|
||||||
case 0: return t('Only me');
|
case 0: return t('Only me');
|
||||||
case PERMS_PUBLIC: return t('Public');
|
case PERMS_PUBLIC: return t('Public');
|
||||||
case PERMS_NETWORK: return t('Anybody in the $Projectname network');
|
case PERMS_NETWORK: return t('Anybody in the $Projectname network');
|
||||||
@ -124,7 +125,6 @@ class PermissionDescription {
|
|||||||
public function get_permission_icon() {
|
public function get_permission_icon() {
|
||||||
|
|
||||||
switch($this->channel_perm) {
|
switch($this->channel_perm) {
|
||||||
|
|
||||||
case 0:/* only me */ return 'fa-eye-slash';
|
case 0:/* only me */ return 'fa-eye-slash';
|
||||||
case PERMS_PUBLIC: return 'fa-globe';
|
case PERMS_PUBLIC: return 'fa-globe';
|
||||||
case PERMS_NETWORK: return 'fa-share-alt-square'; // fa-share-alt-square is very similiar to the hubzilla logo, but we should create our own logo class to use
|
case PERMS_NETWORK: return 'fa-share-alt-square'; // fa-share-alt-square is very similiar to the hubzilla logo, but we should create our own logo class to use
|
||||||
@ -137,7 +137,6 @@ class PermissionDescription {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a localized description of where the permission came from, if this is known.
|
* Returns a localized description of where the permission came from, if this is known.
|
||||||
* If it's not know, or if the permission is standalone and didn't come from a default
|
* If it's not know, or if the permission is standalone and didn't come from a default
|
||||||
@ -148,7 +147,6 @@ class PermissionDescription {
|
|||||||
public function get_permission_origin_description() {
|
public function get_permission_origin_description() {
|
||||||
|
|
||||||
switch($this->global_perm) {
|
switch($this->global_perm) {
|
||||||
|
|
||||||
case PERMS_R_STREAM: return t('This is your default setting for the audience of your normal stream, and posts.');
|
case PERMS_R_STREAM: return t('This is your default setting for the audience of your normal stream, and posts.');
|
||||||
case PERMS_R_PROFILE: return t('This is your default setting for who can view your default channel profile');
|
case PERMS_R_PROFILE: return t('This is your default setting for who can view your default channel profile');
|
||||||
case PERMS_R_ABOOK: return t('This is your default setting for who can view your connections');
|
case PERMS_R_ABOOK: return t('This is your default setting for who can view your connections');
|
||||||
|
110
tests/unit/Lib/PermissionDescriptionTest.php
Normal file
110
tests/unit/Lib/PermissionDescriptionTest.php
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2016 Hubzilla
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Global namespace for fully qualified \App class.
|
||||||
|
namespace {
|
||||||
|
// General channel permissions in boot.php
|
||||||
|
// 0 = Only you
|
||||||
|
define ( 'PERMS_PUBLIC' , 0x0001 ); // anybody
|
||||||
|
define ( 'PERMS_NETWORK' , 0x0002 ); // anybody in this network
|
||||||
|
define ( 'PERMS_SITE' , 0x0004 ); // anybody on this site
|
||||||
|
define ( 'PERMS_CONTACTS' , 0x0008 ); // any of my connections
|
||||||
|
define ( 'PERMS_SPECIFIC' , 0x0080 ); // only specific connections
|
||||||
|
define ( 'PERMS_AUTHED' , 0x0100 ); // anybody authenticated (could include visitors from other networks)
|
||||||
|
define ( 'PERMS_PENDING' , 0x0200 ); // any connections including those who haven't yet been approved
|
||||||
|
// log levels in boot.php
|
||||||
|
define ( 'LOGGER_DEBUG', 2 );
|
||||||
|
|
||||||
|
// Stub global fully qualified \App class for static function calls
|
||||||
|
class App {
|
||||||
|
// Stub get_hostname()
|
||||||
|
public static function get_hostname() {
|
||||||
|
return 'phpunit';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stub global functions used in PermissionDescription with the help of
|
||||||
|
// PHP's namespace resolution rules.
|
||||||
|
namespace Zotlabs\Lib {
|
||||||
|
// Stub global translate function t()
|
||||||
|
function t($s) {
|
||||||
|
return $s;
|
||||||
|
}
|
||||||
|
// Stub global log function logger()
|
||||||
|
function logger($msg, $level = LOGGER_NORMAL, $priority = LOG_INFO) {
|
||||||
|
// doesn't matter
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// regular namespace for this unit test
|
||||||
|
namespace Zotlabs\Tests\Unit\Lib {
|
||||||
|
|
||||||
|
use Zotlabs\Tests\Unit\UnitTestCase;
|
||||||
|
use Zotlabs\Lib\PermissionDescription;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Unit Test case for ConnectionPool class.
|
||||||
|
*/
|
||||||
|
class PermissionDescriptionTest extends UnitTestCase {
|
||||||
|
|
||||||
|
public function testFromDescription() {
|
||||||
|
$permDesc = PermissionDescription::fromDescription('test');
|
||||||
|
$permDesc2 = PermissionDescription::fromDescription('test');
|
||||||
|
$permDesc3 = PermissionDescription::fromDescription('test2');
|
||||||
|
|
||||||
|
$this->assertEquals($permDesc, $permDesc2);
|
||||||
|
$this->assertNotEquals($permDesc, $permDesc3);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFromStandalonePermission() {
|
||||||
|
$permDescUnknown = PermissionDescription::fromStandalonePermission(-1);
|
||||||
|
$permDescSelf = PermissionDescription::fromStandalonePermission(0);
|
||||||
|
|
||||||
|
$this->assertNull($permDescUnknown);
|
||||||
|
$this->assertNotNull($permDescSelf);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFromGlobalPermission() {
|
||||||
|
//$permDesc = PermissionDescription::fromGlobalPermission('view_profile');
|
||||||
|
|
||||||
|
$this->markTestIncomplete(
|
||||||
|
'For this test we need more stubs...'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetPermissionDescription() {
|
||||||
|
|
||||||
|
// fromStandalonePermission uses get_permission_description(), so that will not help
|
||||||
|
//$permDescSelf = PermissionDescription::fromStandalonePermission(0);
|
||||||
|
//$permDescPublic = PermissionDescription::fromStandalonePermission(PERMS_PUBLIC);
|
||||||
|
|
||||||
|
$this->markTestIncomplete(
|
||||||
|
'For this test we need a mock of PermissionDescription...'
|
||||||
|
);
|
||||||
|
//$permDescSelf =
|
||||||
|
//$this->assertEquals($permDescSelf->, 'Only me');
|
||||||
|
//$this->assertEquals($permDescPublic, 'Public');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user