class NodeOptionPremiumHelperTest in Node Option Premium 8
@coversDefaultClass \Drupal\nopremium\NodeOptionPremiumHelper
@group nopremium
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\nopremium\Unit\NodeOptionPremiumHelperTest
Expanded class hierarchy of NodeOptionPremiumHelperTest
File
- tests/
src/ Unit/ NodeOptionPremiumHelperTest.php, line 17
Namespace
Drupal\Tests\nopremium\UnitView source
class NodeOptionPremiumHelperTest extends UnitTestCase {
/**
* The service under test.
*
* @var \Drupal\nopremium\NodeOptionPremiumHelper
*/
protected $helper;
/**
* The entity to test with.
*
* @var \Drupal\Core\Entity\EntityInterface
*/
protected $entity;
/**
* The account to test with.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $account;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Create the service to test.
$this->helper = new NodeOptionPremiumHelper();
// Create the entity to test with.
$this->entity = $this
->prophesize(ContentEntityInterface::class);
// The entity's bundle is 'foo'.
$this->entity
->bundle()
->willReturn('foo');
// Create the account to test with.
$this->account = $this
->prophesize(AccountInterface::class);
}
/**
* Creates a prophesized node.
*
* @return \Prophecy\Prophecy\ProphecyInterface|\Drupal\node\NodeInterface
* The mocked node.
*/
protected function createPremiumNode() {
// Pick an entity type that implements both ContentEntityInterface and
// EntityOwnerInterface.
$node = $this
->prophesize(NodeInterface::class);
// Configure that the entity does have a premium field.
$node
->hasField('premium')
->willReturn(TRUE);
// The entity's bundle is 'foo'.
$node
->bundle()
->willReturn('foo');
return $node;
}
/**
* Tests full access on an entity without a premium field.
*
* @covers ::hasFullAccess
*/
public function testHasFullAccessOnEntityWithNoPremiumField() {
// Configure that the entity has *no* premium field.
$this->entity
->hasField('premium')
->willReturn(FALSE);
$this
->assertTrue($this->helper
->hasFullAccess($this->entity
->reveal(), $this->account
->reveal()));
}
/**
* Tests full access on an entity that is not premium.
*
* @covers ::hasFullAccess
*/
public function testHasFullAccessOnNonPremiumEntity() {
// Configure that the entity does have a premium field.
$this->entity
->hasField('premium')
->willReturn(TRUE);
// Configure the entity to be non-premium.
$entity = $this->entity
->reveal();
$entity->premium = new \stdClass();
$entity->premium->value = FALSE;
$this
->assertTrue($this->helper
->hasFullAccess($entity, $this->account
->reveal()));
}
/**
* Tests full access on a premium entity with certain permissions.
*
* @param bool $expected
* Whether or not access is expected.
* @param string $permission
* The permission to check.
*
* @covers ::hasFullAccess
* @dataProvider accessPermissionsProvider
*/
public function testHasFullAccessWithPermissions($expected, $permission) {
// Configure that the entity does have a premium field.
$this->entity
->hasField('premium')
->willReturn(TRUE);
// The account has no update access for this entity.
$this->entity
->access('update', $this->account
->reveal())
->willReturn(FALSE);
// Configure the entity to be premium.
$entity = $this->entity
->reveal();
$entity->premium = new \stdClass();
$entity->premium->value = TRUE;
$this->account
->hasPermission(Argument::type('string'))
->will(function ($args) use ($permission) {
return $args[0] === $permission;
});
$this
->assertSame($expected, $this->helper
->hasFullAccess($entity, $this->account
->reveal()));
}
/**
* Data provider for ::testHasFullAccessWithPermissions().
*
* @return array
* A list of cases.
*/
public function accessPermissionsProvider() {
return [
[
FALSE,
'access content',
],
[
TRUE,
'administer nodes',
],
[
TRUE,
'view full premium content of any type',
],
[
TRUE,
'view full foo premium content',
],
[
FALSE,
'view full bar premium content',
],
];
}
/**
* Tests that an user who may edit the entity, has access.
*
* @covers ::hasFullAccess
*/
public function testHasFullAccessAsEditor() {
// Configure that the entity does have a premium field.
$this->entity
->hasField('premium')
->willReturn(TRUE);
// The account has none of the permissions.
$this->account
->hasPermission(Argument::type('string'))
->wilLReturn(FALSE);
// The account does have update access for this entity.
$this->entity
->access('update', $this->account
->reveal())
->willReturn(TRUE);
// Configure the entity to be premium.
$entity = $this->entity
->reveal();
$entity->premium = new \stdClass();
$entity->premium->value = TRUE;
$this
->assertTrue($this->helper
->hasFullAccess($entity, $this->account
->reveal()));
}
/**
* Tests that the owner of an entity always has access.
*
* @covers ::hasFullAccess
*/
public function testHasFullAccessAsOwner() {
$node = $this
->createPremiumNode();
// The account has none of the permissions.
$this->account
->hasPermission(Argument::type('string'))
->wilLReturn(FALSE);
// The account is authenticated.
$this->account
->isAuthenticated()
->willReturn(TRUE);
// The entity's owner ID and the account ID are the same.
$node
->getOwnerId()
->willReturn(4);
$this->account
->id()
->willReturn(4);
// The account has no update access for this entity.
$node
->access('update', $this->account
->reveal())
->willReturn(FALSE);
// Configure the entity to be premium.
$node = $node
->reveal();
$node->premium = new \stdClass();
$node->premium->value = TRUE;
$this
->assertTrue($this->helper
->hasFullAccess($node, $this->account
->reveal()));
}
/**
* Tests that non-owners have no access.
*
* @covers ::hasFullAccess
*/
public function testNoAccessAsNonOwner() {
$node = $this
->createPremiumNode();
// The account has none of the permissions.
$this->account
->hasPermission(Argument::type('string'))
->wilLReturn(FALSE);
// The account is authenticated.
$this->account
->isAuthenticated()
->willReturn(TRUE);
// The entity's owner ID and the account ID aren't the same.
$node
->getOwnerId()
->willReturn(3);
$this->account
->id()
->willReturn(4);
// The account has no update access for this entity.
$node
->access('update', $this->account
->reveal())
->willReturn(FALSE);
// Configure the entity to be premium.
$node = $node
->reveal();
$node->premium = new \stdClass();
$node->premium->value = TRUE;
$this
->assertFalse($this->helper
->hasFullAccess($node, $this->account
->reveal()));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
NodeOptionPremiumHelperTest:: |
protected | property | The account to test with. | |
NodeOptionPremiumHelperTest:: |
protected | property | The entity to test with. | |
NodeOptionPremiumHelperTest:: |
protected | property | The service under test. | |
NodeOptionPremiumHelperTest:: |
public | function | Data provider for ::testHasFullAccessWithPermissions(). | |
NodeOptionPremiumHelperTest:: |
protected | function | Creates a prophesized node. | |
NodeOptionPremiumHelperTest:: |
protected | function |
Overrides UnitTestCase:: |
|
NodeOptionPremiumHelperTest:: |
public | function | Tests that an user who may edit the entity, has access. | |
NodeOptionPremiumHelperTest:: |
public | function | Tests that the owner of an entity always has access. | |
NodeOptionPremiumHelperTest:: |
public | function | Tests full access on an entity without a premium field. | |
NodeOptionPremiumHelperTest:: |
public | function | Tests full access on an entity that is not premium. | |
NodeOptionPremiumHelperTest:: |
public | function | Tests full access on a premium entity with certain permissions. | |
NodeOptionPremiumHelperTest:: |
public | function | Tests that non-owners have no access. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |