Skip to content
GitLab
Menu
Why GitLab
Pricing
Contact Sales
Explore
Why GitLab
Pricing
Contact Sales
Explore
Sign in
Get free trial
Commits on Source (2)
(fix): assign $entities after checking user entity
· c9f0865f
Marcelo Rivera
authored
Oct 07, 2019
(fix): don't need to merge the $entities array
c9f0865f
(feat): more thorough spec tests
· d1b2d7a4
Marcelo Rivera
authored
Oct 07, 2019
d1b2d7a4
Hide whitespace changes
Inline
Side-by-side
Core/Permissions/Manager.php
View file @
d1b2d7a4
...
...
@@ -50,13 +50,11 @@ class Manager
throw
new
\InvalidArgumentException
(
'User does not exist'
);
}
$entities
=
empty
(
$opts
[
'entities'
])
?
$this
->
entitiesBuilder
->
get
([
'guids'
=>
$opts
[
'guids'
]])
:
$opts
[
'entities'
];
if
(
$user
&&
$user
->
getType
()
!==
'user'
)
{
throw
new
\InvalidArgumentException
(
'Entity is not a user'
);
}
$entities
=
array_merge
(
$entities
,
$opts
[
'entities'
]
)
;
$entities
=
empty
(
$opts
[
'entities'
])
?
$this
->
entitiesBuilder
->
get
([
'guids'
=>
$opts
[
'guids'
]])
:
$opts
[
'entities'
];
$roles
=
new
Roles
();
...
...
Spec/Core/Permissions/ManagerSpec.php
View file @
d1b2d7a4
...
...
@@ -41,16 +41,16 @@ class ManagerSpec extends ObjectBehavior
$this
->
user
->
getType
()
->
willReturn
(
'user'
);
$this
->
user
->
isAdmin
()
->
willReturn
(
false
);
$this
->
user
->
isBanned
()
->
willReturn
(
false
);
$this
->
user
->
getG
uid
()
->
willReturn
(
1
);
$this
->
user
->
getG
UID
()
->
willReturn
(
1
);
$this
->
user
->
getGUID
()
->
willReturn
(
1
);
$this
->
user
->
getMode
()
->
willReturn
(
ChannelMode
::
OPEN
);
$this
->
user
->
isSubscribed
(
1
)
->
willReturn
(
false
);
$this
->
user
->
isSubscribed
(
2
)
->
willReturn
(
true
);
$this
->
user
->
isSubscribed
(
3
)
->
willReturn
(
false
);
$this
->
subscribedChannel
->
getG
uid
()
->
willReturn
(
2
);
$this
->
subscribedChannel
->
getG
UID
()
->
willReturn
(
2
);
$this
->
subscribedChannel
->
getGUID
()
->
willReturn
(
2
);
$this
->
subscribedChannel
->
getMode
()
->
willReturn
(
ChannelMode
::
MODERATED
);
$this
->
unsubscribedChannel
->
getG
uid
()
->
willReturn
(
3
);
$this
->
unsubscribedChannel
->
getG
UID
()
->
willReturn
(
3
);
$this
->
unsubscribedChannel
->
getGUID
()
->
willReturn
(
3
);
$this
->
unsubscribedChannel
->
getMode
()
->
willReturn
(
ChannelMode
::
CLOSED
);
$this
->
group
->
getGuid
()
->
willReturn
(
100
);
...
...
@@ -63,12 +63,6 @@ class ManagerSpec extends ObjectBehavior
$this
->
entitiesBuilder
->
build
(
$this
->
user
)
->
willReturn
(
$this
->
user
);
$this
->
entitiesBuilder
->
build
(
$this
->
subscribedChannel
)
->
willReturn
(
$this
->
subscribedChannel
);
$this
->
entitiesBuilder
->
build
(
$this
->
unsubscribedChannel
)
->
willReturn
(
$this
->
unsubscribedChannel
);
$this
->
entitiesBuilder
->
get
([
"guids"
=>
[
10
,
11
,
12
,
13
]
])
->
willReturn
(
$this
->
mockEntities
());
$this
->
entitiesBuilder
->
get
([
"guids"
=>
[
10
,
11
,
12
,
13
],
])
->
willReturn
(
$this
->
mockEntities
());
$this
->
beConstructedWith
(
$this
->
entitiesBuilder
);
}
...
...
@@ -77,8 +71,19 @@ class ManagerSpec extends ObjectBehavior
$this
->
shouldHaveType
(
Manager
::
class
);
}
public
function
it_should_get_permissions
()
public
function
it_should_get_permissions
_by_guid
()
{
$this
->
entitiesBuilder
->
get
([
"guids"
=>
[
10
,
11
,
12
,
13
],
])
->
shouldBeCalled
()
->
willReturn
(
$this
->
mockEntities
());
$this
->
entitiesBuilder
->
get
([
"guids"
=>
[
10
,
11
,
12
,
13
],
])
->
shouldBeCalled
()
->
willReturn
(
$this
->
mockEntities
());
$permissions
=
$this
->
getList
([
'user_guid'
=>
1
,
'guids'
=>
[
10
,
11
,
12
,
13
],
...
...
@@ -91,6 +96,46 @@ class ManagerSpec extends ObjectBehavior
$entities
->
shouldHaveKey
(
13
);
}
public
function
it_should_get_permissions_by_sending_the_entities
()
{
$this
->
entitiesBuilder
->
get
(
Argument
::
any
())
->
shouldNotBeCalled
();
$permissions
=
$this
->
getList
([
'user_guid'
=>
1
,
'guids'
=>
[],
'entities'
=>
$this
->
mockEntities
(),
]);
$entities
=
$permissions
->
getEntities
();
$entities
->
shouldHaveKey
(
10
);
$entities
->
shouldHaveKey
(
11
);
$entities
->
shouldHaveKey
(
12
);
$entities
->
shouldHaveKey
(
13
);
}
public
function
it_should_throw_an_exception_if_no_user_guid_is_provided
()
{
$this
->
shouldThrow
(
new
\InvalidArgumentException
(
'user_guid is required'
))
->
during
(
'getList'
,
[[]]);
}
public
function
it_should_throw_an_exception_if_the_user_does_not_exist
()
{
$this
->
entitiesBuilder
->
single
(
4
)
->
shouldBeCalled
()
->
willReturn
(
null
);
$this
->
shouldThrow
(
new
\InvalidArgumentException
(
'User does not exist'
))
->
during
(
'getList'
,
[[
'user_guid'
=>
4
]]);
}
public
function
it_should_throw_an_exception_if_the_provided_user_guid_doesnt_correspond_to_a_user_entity
(
Activity
$activity
)
{
$this
->
entitiesBuilder
->
single
(
5
)
->
shouldBeCalled
()
->
willReturn
(
$activity
);
$this
->
shouldThrow
(
new
\InvalidArgumentException
(
'Entity is not a user'
))
->
during
(
'getList'
,
[[
'user_guid'
=>
5
]]);
}
private
function
mockEntities
()
{
$prophet
=
new
Prophet
();
...
...