-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clean up and clarify the OgAccess unit tests #230
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dc33a3a
Clean up and document the OgAccess unit tests.
pfrenssen 2a9bde1
Merge remote-tracking branch 'origin/8.x-1.x' into cleanup-og-access-…
pfrenssen 18c49b2
Merge remote-tracking branch 'origin/8.x-1.x' into cleanup-og-access-…
pfrenssen 77c3c94
Merge remote-tracking branch 'origin/8.x-1.x' into cleanup-og-access-…
pfrenssen 7ac7f60
Rename method, this actually returns test permissions, not operations.
pfrenssen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,5 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains \Drupal\Tests\og\Unit\OgAccessTestBase. | ||
*/ | ||
|
||
namespace Drupal\Tests\og\Unit; | ||
|
||
use Drupal\Core\Cache\Context\CacheContextsManager; | ||
|
@@ -22,29 +17,50 @@ | |
use Drupal\user\EntityOwnerInterface; | ||
use Prophecy\Argument; | ||
|
||
/** | ||
* Base class for tests of the OgAccess class. | ||
*/ | ||
class OgAccessTestBase extends UnitTestCase { | ||
|
||
/** | ||
* The mocked config handler. | ||
* | ||
* @var \Drupal\Core\Config\Config|\Prophecy\Prophecy\ObjectProphecy | ||
*/ | ||
protected $config; | ||
|
||
/** | ||
* @var \Drupal\user\UserInterface | ||
* A mocked test user. | ||
* | ||
* @var \Drupal\user\UserInterface|\Prophecy\Prophecy\ObjectProphecy | ||
*/ | ||
protected $user; | ||
|
||
/** | ||
* The entity type ID of the test group. | ||
* | ||
* @var string | ||
*/ | ||
protected $entityTypeId; | ||
|
||
/** | ||
* The bundle ID of the test group. | ||
* | ||
* @var string | ||
*/ | ||
protected $bundle; | ||
|
||
/** | ||
* The mocked test group. | ||
* | ||
* @var \Drupal\Core\Entity\EntityInterface|\Prophecy\Prophecy\ObjectProphecy | ||
*/ | ||
protected $group; | ||
|
||
/** | ||
* @var \Drupal\og\GroupManager | ||
* The mocked group manager. | ||
* | ||
* @var \Drupal\og\GroupManager|\Prophecy\Prophecy\ObjectProphecy | ||
*/ | ||
protected $groupManager; | ||
|
||
|
@@ -87,6 +103,7 @@ public function setUp() { | |
$this->config->getCacheTags()->willReturn([]); | ||
$this->config->getCacheMaxAge()->willReturn(0); | ||
|
||
// Create a mocked test user. | ||
$this->user = $this->prophesize(AccountInterface::class); | ||
$this->user->isAuthenticated()->willReturn(TRUE); | ||
$this->user->id()->willReturn(2); | ||
|
@@ -106,6 +123,7 @@ public function setUp() { | |
|
||
$entity_id = 20; | ||
|
||
// Mock all dependencies for the system under test. | ||
$account_proxy = $this->prophesize(AccountProxyInterface::class); | ||
$module_handler = $this->prophesize(ModuleHandlerInterface::class); | ||
|
||
|
@@ -148,7 +166,9 @@ public function setUp() { | |
|
||
$reflection_property->setValue($values); | ||
|
||
// Set the allowed permissions cache. | ||
// Set the allowed permissions cache. This simulates that the access results | ||
// have been retrieved from the database in an earlier pass. This saves us | ||
// from having to mock all the database interaction. | ||
$r = new \ReflectionClass('Drupal\og\OgAccess'); | ||
$reflection_property = $r->getProperty('permissionsCache'); | ||
$reflection_property->setAccessible(TRUE); | ||
|
@@ -162,19 +182,26 @@ public function setUp() { | |
} | ||
|
||
/** | ||
* Returns a mocked test group. | ||
* | ||
* @param bool $is_owner | ||
* Whether or not this test group should be owned by the test user which is | ||
* used in the test. | ||
* | ||
* @return \Drupal\Core\Entity\EntityInterface | ||
* @return \Drupal\Core\Entity\EntityInterface|\Prophecy\Prophecy\ObjectProphecy | ||
* The test group. | ||
*/ | ||
protected function groupEntity($is_owner = FALSE) { | ||
$group_entity = $this->prophesize(EntityInterface::class); | ||
if ($is_owner) { | ||
$group_entity->willImplement(EntityOwnerInterface::class); | ||
// Our test user is hardcoded to have UID 2. | ||
$group_entity->getOwnerId()->willReturn(2); | ||
} | ||
$group_entity->getEntityTypeId()->willReturn($this->entityTypeId); | ||
$group_entity->bundle()->willReturn($this->bundle); | ||
$group_entity->id()->willReturn($this->randomMachineName()); | ||
|
||
return $this->addCache($group_entity); | ||
} | ||
|
||
|
@@ -188,6 +215,15 @@ protected function addCache($prophecy) { | |
return $prophecy; | ||
} | ||
|
||
/** | ||
* Provides operations to use in access tests. | ||
* | ||
* @return array | ||
* An array of test operations. | ||
* | ||
* @todo These are not really 'operations' but rather 'permissions', rename | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
* this? | ||
*/ | ||
public function operationProvider() { | ||
return [ | ||
// In the unit tests we don't really care about the permission name - it | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is puzzling to me. It seems to be the intention to hand off the 'view' permission to core, but this is counterintuitive. What would be the use case of not granting 'view' access to a group admin, while all the other permissions and operations are granted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll have to go in and check - I don't really remember what's going on here ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is related to the incorrect crud operation To permission name bug/ feature we have.
The gist of things is - OG's hook_entity_access() cares only about non-view operations. If it's a
view
we are neutral about it. The reason is that for example node access should be determined by the node grants, and not on the fly.So this
should probably change to something like
It can probably be a follow up, as it's out of the scope of this PR. So maybe open an issue instead of the
@todo
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry I mean
$permission_name = OgPermissionHandler::getPermissionFromEntityOperation($entity, 'view');
should happen inside\Drupal\og\OgAccess::userAccessEntity
, so the test can remain as is.