Skip to content

Commit

Permalink
#73: add register / login / logout functional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
solverat committed Jan 3, 2019
1 parent 3065e6e commit eebd793
Show file tree
Hide file tree
Showing 15 changed files with 437 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function __construct(
UrlGeneratorInterface $router,
SessionInterface $session,
TokenGenerator $tokenGenerator,
$postEventType
string $postEventType
) {
$this->userManager = $userManager;
$this->mailer = $pimcoreMailer;
Expand Down
19 changes: 10 additions & 9 deletions src/MembersBundle/EventListener/UserChangeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace MembersBundle\EventListener;

use MembersBundle\Adapter\User\UserInterface;
use MembersBundle\Configuration\Configuration;
use MembersBundle\Mailer\Mailer;
use MembersBundle\Manager\UserManagerInterface;
use Pimcore\Event\DataObjectEvents;
Expand All @@ -23,22 +22,25 @@ class UserChangeListener implements EventSubscriberInterface
protected $mailer;

/**
* @var Configuration
* @var string
*/
protected $configuration;
protected $postEventType;

/**
* UserChangeListener constructor.
*
* @param UserManagerInterface $userManager
* @param Mailer $pimcoreMailer
* @param Configuration $configuration
* @param string $postEventType
*/
public function __construct(UserManagerInterface $userManager, Mailer $pimcoreMailer, Configuration $configuration)
{
public function __construct(
UserManagerInterface $userManager,
Mailer $pimcoreMailer,
string $postEventType
) {
$this->userManager = $userManager;
$this->mailer = $pimcoreMailer;
$this->configuration = $configuration;
$this->postEventType = $postEventType;
}

/**
Expand All @@ -58,8 +60,7 @@ public function handleObjectUpdate(DataObjectEvent $e)
{
$user = $e->getObject();

if (!$user instanceof UserInterface
|| $this->configuration->getConfig('post_register_type') !== 'confirm_by_admin') {
if (!$user instanceof UserInterface || $this->postEventType !== 'confirm_by_admin') {
return;
}

Expand Down
1 change: 0 additions & 1 deletion src/MembersBundle/Manager/UserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ private function setupNewUser(UserInterface $user)
}

$objects = $listing->getObjects();

if (count($objects) > 0) {
$userGroups[] = $objects[0];
}
Expand Down
2 changes: 2 additions & 0 deletions src/MembersBundle/Resources/config/services/event.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ services:

# event: listen to changes on user updates. send mails if available.
MembersBundle\EventListener\UserChangeListener:
arguments:
$postEventType: '%members.registration.event.type%'
tags:
- { name: kernel.event_subscriber }

Expand Down
159 changes: 159 additions & 0 deletions tests/_support/Helper/MembersFrontend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php

namespace DachcomBundle\Test\Helper;

use Codeception\Module;
use MembersBundle\Adapter\User\UserInterface;
use MembersBundle\Configuration\Configuration;
use MembersBundle\Manager\UserManager;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\MembersUser;
use Pimcore\Model\Document\Email;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class MembersFrontend extends Module
{
/**
* @var PimcoreBackend
*/
protected $pimcoreBackend;

/**
* @return array|mixed
*/
public function _depends()
{
return ['DachcomBundle\Test\Helper\PimcoreBackend' => 'MembersFrontend needs the PimcoreBackend module to work.'];
}

/**
* @param PimcoreBackend $connection
*/
public function _inject(PimcoreBackend $connection)
{
$this->pimcoreBackend = $connection;
}

public function haveARegisteredFrontEndUser($published = false)
{
$userManager = $this->getContainer()->get(UserManager::class);
$configuration = $this->getContainer()->get(Configuration::class);

$membersStoreObject = DataObject::getByPath($configuration->getConfig('storage_path'));

$userObject = $userManager->createUser();
$userObject->setParent($membersStoreObject);
$userObject->setEmail('[email protected]');
$userObject->setUserName('chuck');
$userObject->setPlainPassword('test');
$userObject->setPublished(false);

$user = $userManager->updateUser($userObject);

if ($published === true) {
$user->setConfirmationToken(null);
$user->setPublished(true);
$userManager->updateUser($user);
}

return $user;
}

public function haveALoggedInFrontEndUser()
{
$tokenStorage = $this->getContainer()->get('security.token_storage');

$this->assertNotNull($tokenStorage->getToken());
$this->assertInstanceOf(UserInterface::class, $tokenStorage->getToken()->getUser());
}

public function haveANotLoggedInFrontEndUser()
{
$tokenStorage = $this->getContainer()->get('security.token_storage');

$this->assertNotNull($tokenStorage->getToken());
$this->assertSame('anon.', $tokenStorage->getToken()->getUser());
}

/**
* Actor Function to get confirmation link from email
*
* @param Email $email
*
* @return string|null
*/
public function haveConfirmationLinkInEmail(Email $email)
{
$foundEmails = $this->pimcoreBackend->getEmailsFromDocumentIds([$email->getId()]);
$serializer = $this->pimcoreBackend->getSerializer();

$propertyKey = 'confirmationUrl';
$link = null;
foreach ($foundEmails as $email) {
$params = $serializer->decode($email->getParams(), 'json', ['json_decode_associative' => true]);
$key = array_search($propertyKey, array_column($params, 'key'));
if ($key === false) {
$this->fail(sprintf('Failed asserting that mail params array has the key "%s".', $propertyKey));
} else {
$data = $params[$key];
$link = $data['data']['value'];
}
break;
}

$this->assertNotEmpty($link);

return $link;

}

public function seeAUserWithValidToken()
{
$user = $this->grabOneUserAfterRegistration();
$this->assertNotEmpty($user->getConfirmationToken());
}

public function seeAUserWithInvalidatedToken()
{
$user = $this->grabOneUserAfterRegistration();
$this->assertNull($user->getConfirmationToken());
}

public function seeAPublishedUserAfterRegistration()
{
$user = $this->grabOneUserAfterRegistration();
$this->assertTrue($user->getPublished());
}

public function seeAUnpublishedUserAfterRegistration()
{
$user = $this->grabOneUserAfterRegistration();
$this->assertFalse($user->getPublished());
}

/**
* @return UserInterface
* @throws \Exception
*/
public function grabOneUserAfterRegistration()
{
$list = MembersUser::getList(['unpublished' => true]);
$users = $list->load();

$this->assertCount(1, $users);
$this->assertInstanceOf(UserInterface::class, $users[0]);

return $users[0];

}

/**
* @return Container
* @throws \Codeception\Exception\ModuleException
*/
protected function getContainer()
{
return $this->getModule('\\' . PimcoreCore::class)->getContainer();
}
}
7 changes: 3 additions & 4 deletions tests/_support/Helper/PimcoreBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function _after(TestInterface $test)
TestHelper::cleanUp();
FileGeneratorHelper::cleanUp();
MembersHelper::cleanUp();
MembersHelper::reCreateMembersFolder();
MembersHelper::reCreateMembersStructure();

parent::_after($test);
}
Expand Down Expand Up @@ -299,7 +299,7 @@ public function seeKeyInFrontendTranslations(string $key)
*
* @return Log[]
*/
protected function getEmailsFromDocumentIds(array $documentIds)
public function getEmailsFromDocumentIds(array $documentIds)
{
$emailLogs = new Log\Listing();
$emailLogs->addConditionParam(sprintf('documentId IN (%s)', implode(',', $documentIds)));
Expand Down Expand Up @@ -469,7 +469,6 @@ protected function createMembersArea()
sprintf('%s:1.hideWhenLoggedIn', MembersHelper::AREA_TEST_NAMESPACE) => $hideWhenLoggedIn,
sprintf('%s:1.showSnippedWhenLoggedIn', MembersHelper::AREA_TEST_NAMESPACE) => $showSnippedWhenLoggedIn,
];

}

/**
Expand All @@ -484,7 +483,7 @@ protected function getContainer()
/**
* @return Serializer
*/
protected function getSerializer()
public function getSerializer()
{
$serializer = null;

Expand Down
2 changes: 1 addition & 1 deletion tests/_support/Test/DachcomBundleTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protected function _after()
{
TestHelper::cleanUp();
MembersHelper::cleanUp();
MembersHelper::reCreateMembersFolder();
MembersHelper::reCreateMembersStructure();
FileGeneratorHelper::cleanUp();

parent::_after();
Expand Down
18 changes: 4 additions & 14 deletions tests/_support/Util/MembersHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace DachcomBundle\Test\Util;

use Pimcore\Model\DataObject;
use MembersBundle\Tool\Install;

class MembersHelper
{
Expand Down Expand Up @@ -44,19 +44,9 @@ public static function generateEditableConfiguration(string $name, string $type,
return $data;
}

public static function reCreateMembersFolder()
public static function reCreateMembersStructure()
{
//re-create members data folder.
try {
$folder = new DataObject\Folder();
$folder->setParentId(1);
$folder->setKey('members');
$folder->setLocked(true);
$folder->save();
} catch (\Exception $e) {
\Codeception\Util\Debug::debug(
sprintf('[MEMBERS ERROR] error while re-creating members object folder. message was: ' . $e->getMessage())
);
}
$installer = \Pimcore::getContainer()->get(Install::class);
$installer->initializeFreshSetup();
}
}
4 changes: 3 additions & 1 deletion tests/bundle_tests/functional.suite.dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ modules:
- \DachcomBundle\Test\Helper\PimcoreAdminJson:
depends: \DachcomBundle\Test\Helper\PimcoreCore
- \DachcomBundle\Test\Helper\PimcoreBackend
- \DachcomBundle\Test\Helper\PimcoreUser
- \DachcomBundle\Test\Helper\PimcoreUser
- \DachcomBundle\Test\Helper\MembersFrontend:
depends: \DachcomBundle\Test\Helper\PimcoreBackend
Loading

0 comments on commit eebd793

Please sign in to comment.