class SubscriptionControllerTest in Organic groups 8
Tests the subscription controller.
@group og @coversDefaultClass \Drupal\og\Controller\SubscriptionController
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\og\Unit\SubscriptionControllerTest
Expanded class hierarchy of SubscriptionControllerTest
File
- tests/
src/ Unit/ SubscriptionControllerTest.php, line 28
Namespace
Drupal\Tests\og\UnitView source
class SubscriptionControllerTest extends UnitTestCase {
/**
* The entity for builder object.
*
* @var \Drupal\Core\Entity\EntityFormBuilderInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $entityFormBuilder;
/**
* The group entity.
*
* @var \Drupal\Core\Entity\ContentEntityInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $group;
/**
* The membership manager service.
*
* @var \Drupal\og\MembershipManagerInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $membershipManager;
/**
* OG access service.
*
* @var \Drupal\og\OgAccessInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $ogAccess;
/**
* The mocked messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $messenger;
/**
* The OG membership entity.
*
* @var \Drupal\og\OgMembershipInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $ogMembership;
/**
* The URL object.
*
* @var \Drupal\Core\Url|\Prophecy\Prophecy\ObjectProphecy
*/
protected $url;
/**
* The user entity.
*
* @var \Drupal\user\UserInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $user;
/**
* A user ID to use in the test.
*
* @var int
*/
protected $userId;
/**
* The mocked entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $entityTypeManager;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
$this->entityFormBuilder = $this
->prophesize(EntityFormBuilderInterface::class);
$this->group = $this
->prophesize(ContentEntityInterface::class);
$this->membershipManager = $this
->prophesize(MembershipManagerInterface::class);
$this->ogAccess = $this
->prophesize(OgAccessInterface::class);
$this->messenger = $this
->prophesize(MessengerInterface::class);
$this->ogMembership = $this
->prophesize(OgMembershipInterface::class);
$this->url = $this
->prophesize(Url::class);
$this->user = $this
->prophesize(AccountInterface::class);
$this->entityTypeManager = $this
->prophesize(EntityTypeManagerInterface::class);
$this->userId = rand(20, 50);
$this->user
->id()
->willReturn($this->userId);
// Set the container for the string translation service.
$container = new ContainerBuilder();
$container
->set('current_user', $this->user
->reveal());
$container
->set('entity.form_builder', $this->entityFormBuilder
->reveal());
$container
->set('og.membership_manager', $this->membershipManager
->reveal());
$container
->set('string_translation', $this
->getStringTranslationStub());
$container
->set('entity_type.manager', $this->entityTypeManager
->reveal());
\Drupal::setContainer($container);
}
/**
* Tests non-member trying to unsubscribe from group.
*
* @covers ::unsubscribe
*/
public function testNotMember() {
$states = [
OgMembershipInterface::STATE_ACTIVE,
OgMembershipInterface::STATE_PENDING,
OgMembershipInterface::STATE_BLOCKED,
];
$this->membershipManager
->getMembership($this->group
->reveal(), $this->userId, $states)
->willReturn(NULL);
$this
->expectException(AccessDeniedHttpException::class);
$this
->unsubscribe();
}
/**
* Tests blocked member trying to unsubscribe from group.
*
* @covers ::unsubscribe
*/
public function testBlockedMember() {
$states = [
OgMembershipInterface::STATE_ACTIVE,
OgMembershipInterface::STATE_PENDING,
OgMembershipInterface::STATE_BLOCKED,
];
$this->membershipManager
->getMembership($this->group
->reveal(), $this->userId, $states)
->willReturn($this->ogMembership
->reveal());
$this->ogMembership
->getState()
->willReturn(OgMembershipInterface::STATE_BLOCKED);
$this
->expectException(AccessDeniedHttpException::class);
$this
->unsubscribe();
}
/**
* Tests active and pending members trying to unsubscribe from group.
*
* @covers ::unsubscribe
* @dataProvider memberProvider
*/
public function testMember($state) {
$states = [
OgMembershipInterface::STATE_ACTIVE,
OgMembershipInterface::STATE_PENDING,
OgMembershipInterface::STATE_BLOCKED,
];
$this->membershipManager
->getMembership($this->group
->reveal(), $this->userId, $states)
->willReturn($this->ogMembership
->reveal());
$this->ogMembership
->getState()
->willReturn($state);
$this->entityFormBuilder
->getForm($this->ogMembership
->reveal(), 'unsubscribe')
->shouldBeCalled();
$this
->unsubscribe();
}
/**
* Provides test data to test members unsubscribe.
*
* @return array
* Array with the membership state.
*/
public function memberProvider() {
return [
[
OgMembershipInterface::STATE_ACTIVE,
],
[
OgMembershipInterface::STATE_PENDING,
],
];
}
/**
* Tests group manager trying to unsubscribe from group.
*
* @covers ::unsubscribe
* @dataProvider memberProvider
*/
public function testGroupManager($state) {
$states = [
OgMembershipInterface::STATE_ACTIVE,
OgMembershipInterface::STATE_PENDING,
OgMembershipInterface::STATE_BLOCKED,
];
$this->group
->willImplement(EntityOwnerInterface::class);
$this->membershipManager
->getMembership($this->group
->reveal(), $this->userId, $states)
->willReturn($this->ogMembership
->reveal());
$this->ogMembership
->getState()
->willReturn($state);
$this->group
->getOwnerId()
->willReturn($this->userId);
$this->group
->label()
->shouldBeCalled();
$this->group
->toUrl()
->willReturn($this->url
->reveal());
$this->url
->setAbsolute()
->willReturn($this->url
->reveal());
$this->url
->toString()
->willReturn($this
->randomMachineName());
$this->entityFormBuilder
->getForm($this->ogMembership
->reveal(), 'unsubscribe')
->shouldNotBeCalled();
$this
->unsubscribe();
}
/**
* Invoke the unsubscribe method.
*/
protected function unsubscribe() {
$controller = new SubscriptionController($this->ogAccess
->reveal(), $this->messenger
->reveal(), $this->entityTypeManager
->reveal());
$controller
->unsubscribe($this->group
->reveal());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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. | |
SubscriptionControllerTest:: |
protected | property | The entity for builder object. | |
SubscriptionControllerTest:: |
protected | property | The mocked entity type manager. | |
SubscriptionControllerTest:: |
protected | property | The group entity. | |
SubscriptionControllerTest:: |
protected | property | The membership manager service. | |
SubscriptionControllerTest:: |
protected | property | The mocked messenger service. | |
SubscriptionControllerTest:: |
protected | property | OG access service. | |
SubscriptionControllerTest:: |
protected | property | The OG membership entity. | |
SubscriptionControllerTest:: |
protected | property | The URL object. | |
SubscriptionControllerTest:: |
protected | property | The user entity. | |
SubscriptionControllerTest:: |
protected | property | A user ID to use in the test. | |
SubscriptionControllerTest:: |
public | function | Provides test data to test members unsubscribe. | |
SubscriptionControllerTest:: |
protected | function |
Overrides UnitTestCase:: |
|
SubscriptionControllerTest:: |
public | function | Tests blocked member trying to unsubscribe from group. | |
SubscriptionControllerTest:: |
public | function | Tests group manager trying to unsubscribe from group. | |
SubscriptionControllerTest:: |
public | function | Tests active and pending members trying to unsubscribe from group. | |
SubscriptionControllerTest:: |
public | function | Tests non-member trying to unsubscribe from group. | |
SubscriptionControllerTest:: |
protected | function | Invoke the unsubscribe method. | |
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. |