✅💡 Add Unit Tests for AccessList class.
Also some Doxygen docu for this class.
This commit is contained in:
parent
3645eb18f8
commit
8f0a0add1e
@ -2,20 +2,54 @@
|
||||
|
||||
namespace Zotlabs\Access;
|
||||
|
||||
|
||||
/**
|
||||
* @brief AccessList class.
|
||||
*
|
||||
* A class to hold an AccessList object with allowed and denied contacts and
|
||||
* groups.
|
||||
*/
|
||||
class AccessList {
|
||||
|
||||
/**
|
||||
* @brief Allow contacts
|
||||
* @var string
|
||||
*/
|
||||
private $allow_cid;
|
||||
/**
|
||||
* @brief Allow groups
|
||||
* @var string
|
||||
*/
|
||||
private $allow_gid;
|
||||
/**
|
||||
* @brief Deny contacts
|
||||
* @var string
|
||||
*/
|
||||
private $deny_cid;
|
||||
/**
|
||||
* @brief Deny groups
|
||||
* @var string
|
||||
*/
|
||||
private $deny_gid;
|
||||
|
||||
/* indicates if we are using the default constructor values or values that have been set explicitly. */
|
||||
|
||||
/**
|
||||
* @brief Indicates if we are using the default constructor values or
|
||||
* values that have been set explicitly.
|
||||
* @var boolean
|
||||
*/
|
||||
private $explicit;
|
||||
|
||||
function __construct($channel) {
|
||||
|
||||
/**
|
||||
* @brief Constructor for AccessList class.
|
||||
*
|
||||
* @note The array to pass to the constructor is different from the array
|
||||
* that you provide to the set() or set_from_array() functions.
|
||||
*
|
||||
* @param array $channel A channel array, where these entries are evaluated:
|
||||
* * \e string \b channel_allow_cid => string of allowed cids
|
||||
* * \e string \b channel_allow_gid => string of allowed gids
|
||||
* * \e string \b channel_deny_cid => string of denied cids
|
||||
* * \e string \b channel_deny_gid => string of denied gids
|
||||
*/
|
||||
function __construct($channel) {
|
||||
if($channel) {
|
||||
$this->allow_cid = $channel['channel_allow_cid'];
|
||||
$this->allow_gid = $channel['channel_allow_gid'];
|
||||
@ -32,15 +66,30 @@ class AccessList {
|
||||
$this->explicit = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get if we are using the default constructor values
|
||||
* or values that have been set explicitly.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
function get_explicit() {
|
||||
return $this->explicit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set AccessList from strings such as those in already
|
||||
* existing stored data items
|
||||
* @brief Set access list from strings such as those in already
|
||||
* existing stored data items.
|
||||
*
|
||||
* @note The array to pass to this set function is different from the array
|
||||
* that you provide to the constructor or set_from_array().
|
||||
*
|
||||
* @param array $arr
|
||||
* * \e string \b allow_cid => string of allowed cids
|
||||
* * \e string \b allow_gid => string of allowed gids
|
||||
* * \e string \b deny_cid => string of denied cids
|
||||
* * \e string \b deny_gid => string of denied gids
|
||||
* @param boolean $explicit (optional) default true
|
||||
*/
|
||||
|
||||
function set($arr, $explicit = true) {
|
||||
$this->allow_cid = $arr['allow_cid'];
|
||||
$this->allow_gid = $arr['allow_gid'];
|
||||
@ -51,26 +100,41 @@ class AccessList {
|
||||
}
|
||||
|
||||
/**
|
||||
* return an array consisting of the current
|
||||
* access list components where the elements
|
||||
* are directly storable.
|
||||
* @brief Return an array consisting of the current access list components
|
||||
* where the elements are directly storable.
|
||||
*
|
||||
* @return Associative array with:
|
||||
* * \e string \b allow_cid => string of allowed cids
|
||||
* * \e string \b allow_gid => string of allowed gids
|
||||
* * \e string \b deny_cid => string of denied cids
|
||||
* * \e string \b deny_gid => string of denied gids
|
||||
*/
|
||||
|
||||
function get() {
|
||||
return array(
|
||||
return [
|
||||
'allow_cid' => $this->allow_cid,
|
||||
'allow_gid' => $this->allow_gid,
|
||||
'deny_cid' => $this->deny_cid,
|
||||
'deny_gid' => $this->deny_gid,
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Set AccessList from arrays, such as those provided by
|
||||
* acl_selector(). For convenience, a string (or non-array) input is
|
||||
* assumed to be a comma-separated list and auto-converted into an array.
|
||||
* @brief Set access list components from arrays, such as those provided by
|
||||
* acl_selector().
|
||||
*
|
||||
* For convenience, a string (or non-array) input is assumed to be a
|
||||
* comma-separated list and auto-converted into an array.
|
||||
*
|
||||
* @note The array to pass to this set function is different from the array
|
||||
* that you provide to the constructor or set().
|
||||
*
|
||||
* @param array $arr An associative array with:
|
||||
* * \e array|string \b contact_allow => array with cids or comma-seperated string
|
||||
* * \e array|string \b group_allow => array with gids or comma-seperated string
|
||||
* * \e array|string \b contact_deny => array with cids or comma-seperated string
|
||||
* * \e array|string \b group_deny => array with gids or comma-seperated string
|
||||
* @param boolean $explicit (optional) default true
|
||||
*/
|
||||
|
||||
function set_from_array($arr, $explicit = true) {
|
||||
$this->allow_cid = perms2str((is_array($arr['contact_allow']))
|
||||
? $arr['contact_allow'] : explode(',', $arr['contact_allow']));
|
||||
@ -84,9 +148,13 @@ class AccessList {
|
||||
$this->explicit = $explicit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns true if any access lists component is set.
|
||||
*
|
||||
* @return boolean Return true if any of allow_* deny_* values is set.
|
||||
*/
|
||||
function is_private() {
|
||||
return (($this->allow_cid || $this->allow_gid || $this->deny_cid || $this->deny_gid) ? true : false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
189
tests/unit/Access/AccessListTest.php
Normal file
189
tests/unit/Access/AccessListTest.php
Normal file
@ -0,0 +1,189 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (c) 2017 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.
|
||||
*/
|
||||
|
||||
namespace Zotlabs\Tests\Unit\Access;
|
||||
|
||||
use Zotlabs\Tests\Unit\UnitTestCase;
|
||||
use Zotlabs\Access\AccessList;
|
||||
|
||||
/**
|
||||
* @brief Unit Test case for AccessList class.
|
||||
*
|
||||
* @covers Zotlabs\Access\AccessList
|
||||
*/
|
||||
class AccessListTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* @brief Expected result for most tests.
|
||||
* @var array
|
||||
*/
|
||||
protected $expectedResult = [
|
||||
'allow_cid' => '<acid><acid2>',
|
||||
'allow_gid' => '<agid>',
|
||||
'deny_cid' => '',
|
||||
'deny_gid' => '<dgid><dgid2>'
|
||||
];
|
||||
|
||||
|
||||
|
||||
public function testConstructor() {
|
||||
$channel = [
|
||||
'channel_allow_cid' => '<acid><acid2>',
|
||||
'channel_allow_gid' => '<agid>',
|
||||
'channel_deny_cid' => '',
|
||||
'channel_deny_gid' => '<dgid><dgid2>'
|
||||
];
|
||||
|
||||
$accessList = new AccessList($channel);
|
||||
|
||||
$this->assertEquals($this->expectedResult, $accessList->get());
|
||||
$this->assertFalse($accessList->get_explicit());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException PHPUnit\Framework\Error\Error
|
||||
*/
|
||||
public function testPHPErrorOnInvalidConstructor() {
|
||||
$accessList = new AccessList('invalid');
|
||||
// Causes: "Illegal string offset 'channel_allow_cid'"
|
||||
}
|
||||
|
||||
public function testDefaultGetExplicit() {
|
||||
$accessList = new AccessList([]);
|
||||
|
||||
$this->assertFalse($accessList->get_explicit());
|
||||
}
|
||||
|
||||
public function testDefaultGet() {
|
||||
$arr = [
|
||||
'allow_cid' => '',
|
||||
'allow_gid' => '',
|
||||
'deny_cid' => '',
|
||||
'deny_gid' => ''
|
||||
];
|
||||
|
||||
$accessList = new AccessList([]);
|
||||
|
||||
$this->assertEquals($arr, $accessList->get());
|
||||
}
|
||||
|
||||
public function testSet() {
|
||||
$arr = [
|
||||
'allow_cid' => '<acid><acid2>',
|
||||
'allow_gid' => '<agid>',
|
||||
'deny_cid' => '',
|
||||
'deny_gid' => '<dgid><dgid2>'
|
||||
];
|
||||
$accessList = new AccessList([]);
|
||||
|
||||
// default explicit true
|
||||
$accessList->set($arr);
|
||||
|
||||
$this->assertEquals($this->expectedResult, $accessList->get());
|
||||
$this->assertTrue($accessList->get_explicit());
|
||||
|
||||
// set explicit false
|
||||
$accessList->set($arr, false);
|
||||
|
||||
$this->assertEquals($this->expectedResult, $accessList->get());
|
||||
$this->assertFalse($accessList->get_explicit());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException PHPUnit\Framework\Error\Error
|
||||
*/
|
||||
public function testPHPErrorOnInvalidSet() {
|
||||
$accessList = new AccessList([]);
|
||||
|
||||
$accessList->set('invalid');
|
||||
// Causes: "Illegal string offset 'allow_cid'"
|
||||
}
|
||||
|
||||
/**
|
||||
* set_from_array() calls some other functions, too which are not yet unit tested.
|
||||
* @uses ::perms2str()
|
||||
*/
|
||||
public function testSetFromArray() {
|
||||
// array
|
||||
$arraySetFromArray = [
|
||||
'contact_allow' => ['acid', 'acid2'],
|
||||
'group_allow' => ['agid'],
|
||||
'contact_deny' => [],
|
||||
'group_deny' => ['dgid', 'dgid2']
|
||||
];
|
||||
$accessList = new AccessList([]);
|
||||
$accessList->set_from_array($arraySetFromArray);
|
||||
|
||||
$this->assertEquals($this->expectedResult, $accessList->get());
|
||||
$this->assertTrue($accessList->get_explicit());
|
||||
|
||||
|
||||
// string
|
||||
$stringSetFromArray = [
|
||||
'contact_allow' => 'acid,acid2',
|
||||
'group_allow' => 'agid',
|
||||
'contact_deny' => '',
|
||||
'group_deny' => 'dgid, dgid2'
|
||||
];
|
||||
$accessList2 = new AccessList([]);
|
||||
$accessList2->set_from_array($stringSetFromArray, false);
|
||||
|
||||
$this->assertEquals($this->expectedResult, $accessList2->get());
|
||||
$this->assertFalse($accessList2->get_explicit());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider isprivateProvider
|
||||
*/
|
||||
public function testIsPrivate($channel) {
|
||||
$accessListPublic = new AccessList([]);
|
||||
$this->assertFalse($accessListPublic->is_private());
|
||||
|
||||
$accessListPrivate = new AccessList($channel);
|
||||
$this->assertTrue($accessListPrivate->is_private());
|
||||
}
|
||||
|
||||
public function isprivateProvider() {
|
||||
return [
|
||||
'all set' => [[
|
||||
'channel_allow_cid' => '<acid>',
|
||||
'channel_allow_gid' => '<agid>',
|
||||
'channel_deny_cid' => '<dcid>',
|
||||
'channel_deny_gid' => '<dgid>'
|
||||
]],
|
||||
'only one set' => [[
|
||||
'channel_allow_cid' => '<acid>',
|
||||
'channel_allow_gid' => '',
|
||||
'channel_deny_cid' => '',
|
||||
'channel_deny_gid' => ''
|
||||
]],
|
||||
'acid+null' => [[
|
||||
'channel_allow_cid' => '<acid>',
|
||||
'channel_allow_gid' => null,
|
||||
'channel_deny_cid' => '',
|
||||
'channel_deny_gid' => ''
|
||||
]]
|
||||
];
|
||||
}
|
||||
|
||||
}
|
@ -29,6 +29,8 @@ use Zotlabs\Lib\PermissionDescription;
|
||||
|
||||
/**
|
||||
* @brief Unit Test case for PermissionDescription class.
|
||||
*
|
||||
* @covers Zotlabs\Lib\PermissionDescription
|
||||
*/
|
||||
class PermissionDescriptionTest extends UnitTestCase {
|
||||
|
||||
|
Reference in New Issue
Block a user