Skip to content

Commit

Permalink
Add swiftmailer bc-layer and update pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
martinlagler committed Apr 10, 2024
1 parent d538845 commit f7f7396
Show file tree
Hide file tree
Showing 16 changed files with 193 additions and 90 deletions.
20 changes: 19 additions & 1 deletion .github/workflows/test-application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,25 @@ jobs:
SYMFONY_DEPRECATIONS_HELPER: weak

- php-version: '8.1'
lint: true
lint: false
dependency-versions: 'highest'
tools: 'composer:v2'
env:
SYMFONY_DEPRECATIONS_HELPER: weak

- php-version: '8.2'
lint: false
dependency-versions: 'highest'
tools: 'composer:v2'
env:
SYMFONY_DEPRECATIONS_HELPER: weak

- php-version: '8.3'
lint: true
dependency-versions: 'highest'
tools: 'composer:v2'
env:
SYMFONY_DEPRECATIONS_HELPER: weak
services:
mysql:
image: mysql:5.7
Expand All @@ -71,6 +84,11 @@ jobs:
if: ${{ matrix.php-version == '7.2' }}
run: composer remove php-cs-fixer/shim --dev --no-interaction

- name: Install additional lowest dependencies
if: ${{ matrix.dependency-versions == 'lowest' }}
run: |
composer require symfony/swiftmailer-bundle --no-interaction --no-update
- name: Install composer dependencies
uses: ramsey/composer-install@v1
with:
Expand Down
41 changes: 18 additions & 23 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,33 @@
$config->setRiskyAllowed(true)
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'ordered_imports' => true,
'concat_space' => ['spacing' => 'one'],
'array_syntax' => ['syntax' => 'short'],
'phpdoc_align' => ['align' => 'left'],
'class_definition' => [
'multi_line_extends_each_single_line' => true,
] ,
'linebreak_after_opening_tag' => true,
// 'declare_strict_types' => true,
'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'],
'class_definition' => false,
'concat_space' => ['spacing' => 'one'],
'function_declaration' => ['closure_function_spacing' => 'none'],
'header_comment' => ['header' => $header],
'native_constant_invocation' => true,
'native_function_casing' => true,
'native_function_invocation' => ['include' => ['@internal']],
'no_php4_constructor' => true,
'global_namespace_import' => ['import_classes' => false, 'import_constants' => false, 'import_functions' => false],
'no_superfluous_phpdoc_tags' => ['allow_mixed' => true, 'remove_inheritdoc' => true],
'no_unreachable_default_argument_value' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'php_unit_strict' => true,
'phpdoc_order' => true,
'semicolon_after_instruction' => true,
'strict_comparison' => true,
'strict_param' => true,
'array_indentation' => true,
'multiline_whitespace_before_semicolons' => true,
'ordered_imports' => true,
'phpdoc_align' => ['align' => 'left'],
'phpdoc_types_order' => false,
'single_line_throw' => false,
'visibility_required' => ['elements' => ['property', 'method', 'const']],
'single_line_comment_spacing' => false,
'phpdoc_to_comment' => [
'ignored_tags' => ['todo', 'var'],
],
'trailing_comma_in_multiline' => ['elements' => ['arrays', /*'arguments', 'parameters' */]],
'phpdoc_separation' => [
'groups' => [
['Serializer\\*', 'VirtualProperty', 'Accessor', 'Type', 'Groups', 'Expose', 'Exclude', 'SerializedName', 'Inline', 'ExclusionPolicy'],
],
],
'get_class_to_class_keyword' => false, // should be enabled as soon as support for php < 8 is dropped
'nullable_type_declaration_for_default_null_value' => true,
'no_null_property_initialization' => false,
'fully_qualified_strict_types' => false,
])
->setFinder($finder);

Expand Down
14 changes: 5 additions & 9 deletions Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,11 @@ protected function encodePassword(User $user, string $plainPassword): string
$hasher = $this->container->get('?security.password_hasher');

return $hasher->hashPassword($user, $plainPassword);
} else {
/** @var \Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface $encoder */
$encoder = $this->container->get('?security.password_encoder');

return $encoder->encodePassword($user, $plainPassword);
}
/** @var \Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface $encoder */
$encoder = $this->container->get('?security.password_encoder');

return $encoder->encodePassword($user, $plainPassword);
}

/**
Expand Down Expand Up @@ -147,9 +146,6 @@ private function getTemplateAttributes(array $custom = []): array
return $this->getTemplateAttributeResolver()->resolve($custom);
}

/**
* @return User
*/
public function getUser(): ?User
{
$user = parent::getUser();
Expand Down Expand Up @@ -190,7 +186,7 @@ private function addAddress(User $user): void
/**
* @param mixed[] $parameters
*/
public function render(string $view, array $parameters = [], Response $response = null): Response
public function render(string $view, array $parameters = [], ?Response $response = null): Response
{
return parent::render(
$view,
Expand Down
1 change: 1 addition & 0 deletions Controller/BlacklistItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* Provides admin-api for blacklist-items.
*
* @NamePrefix("sulu_community.")
*
* @RouteResource("blacklist-item")
*/
class BlacklistItemController extends AbstractRestController implements ClassResourceInterface
Expand Down
2 changes: 1 addition & 1 deletion Controller/ConfirmationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function indexAction(Request $request, string $token): Response
$redirectTo = $communityManager->getConfigTypeProperty(self::TYPE, Configuration::REDIRECT_TO);

if ($redirectTo) {
if (0 === \strpos($redirectTo, '/')) {
if (\str_starts_with($redirectTo, '/')) {
$url = \str_replace('{localization}', $request->getLocale(), $redirectTo);
} else {
$url = $this->getRouter()->generate($redirectTo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
* },
* delete_user: bool,
* }
*
* @phpstan-type Config array{
* from: string|string[],
* to: string|string[],
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/SuluCommunityExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function prepend(ContainerBuilder $container): void
'orm' => [
'dql' => [
'string_functions' => [
'regexp' => RegExp::class,
'regexp' => Regexp::class,
],
],
],
Expand Down
4 changes: 0 additions & 4 deletions Entity/BlacklistItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ class BlacklistItem
*/
private $type;

/**
* @param string $pattern
* @param string $type
*/
public function __construct(?string $pattern = null, ?string $type = null)
{
$this->type = $type;
Expand Down
2 changes: 1 addition & 1 deletion EventListener/LastLoginListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static function getSubscribedEvents()
*/
public function onRequest(RequestEvent $event): void
{
if (!$event->isMasterRequest()) {
if (!$event->isMainRequest()) {
return;
}

Expand Down
2 changes: 0 additions & 2 deletions Mail/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ class Mail
* user_template: string|null,
* admin_template: string|null,
* } $config
*
* @return Mail
*/
public static function create($from, $to, array $config): self
{
Expand Down
41 changes: 26 additions & 15 deletions Mail/MailFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Sulu\Bundle\CommunityBundle\Mail;

use Sulu\Bundle\SecurityBundle\Entity\User;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
Expand All @@ -25,7 +26,7 @@
class MailFactory implements MailFactoryInterface
{
/**
* @var MailerInterface
* @var MailerInterface|\Swift_Mailer
*/
protected $mailer;

Expand All @@ -39,7 +40,7 @@ class MailFactory implements MailFactoryInterface
*/
protected $translator;

public function __construct(MailerInterface $mailer, Environment $twig, TranslatorInterface $translator)
public function __construct($mailer, Environment $twig, TranslatorInterface $translator)
{
$this->mailer = $mailer;
$this->twig = $twig;
Expand Down Expand Up @@ -72,7 +73,6 @@ public function sendEmails(Mail $mail, User $user, array $parameters = []): void
}
}


/**
* Create and send email.
*
Expand All @@ -84,30 +84,41 @@ protected function sendEmail($from, $to, string $subject, string $template, arra
{
$body = $this->twig->render($template, $data);

$email = (new Email())
->subject($this->translator->trans($subject))
->from($this->getAddress($from))
->to($this->getAddress($to))
->html($body);
if ($this->mailer instanceof \Swift_Mailer) {
$email = $this->mailer->createMessage()
->setSubject($this->translator->trans($subject))
->setFrom($from)
->setTo($to)
->setBody($body, 'text/html');
} else {
if (!$this->getAddress($from) || !$this->getAddress($to)) {
return;
}

$email = (new Email())
->subject($this->translator->trans($subject))
->from($this->getAddress($from))
->to($this->getAddress($to))
->html($body);
}

$this->mailer->send($email);
}

/**
* Convert string/array email address to an Address object
* Convert string/array email address to an Address object.
*
* @param $address
* @return Address
* @param mixed $address
*/
protected function getAddress($address): ?Address
{
$name = '';

if (is_array($address)) {
if(empty($address)) {
if (\is_array($address)) {
if (empty($address)) {
return null;
} else if (!isset($address['email'])) {
$email = $address[array_keys($address)[0]];
} elseif (!isset($address['email'])) {
$email = $address[\array_keys($address)[0]];
} else {
$email = $address['email'];
$name = $address['name'] ?? '';
Expand Down
3 changes: 0 additions & 3 deletions Manager/CommunityManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
* },
* delete_user: bool,
* }
*
* @phpstan-type Config array{
* from: string|string[],
* to: string|string[],
Expand Down Expand Up @@ -137,8 +136,6 @@ public function sendEmails(string $type, User $user): void;

/**
* Save profile for given user.
*
* @return User
*/
public function saveProfile(User $user): ?User;
}
2 changes: 1 addition & 1 deletion Tests/Application/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\HttpKernel\Kernel;

return static function(PhpFileLoader $loader, ContainerBuilder $container) {
return static function (PhpFileLoader $loader, ContainerBuilder $container) {
$context = $container->getParameter('sulu.context');

$loader->import('context_' . $context . '.yml');
Expand Down
34 changes: 31 additions & 3 deletions Tests/Functional/Controller/RegistrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ public function testRegistrationBlacklistedBlocked(): void
]
);
$this->client->submit($form);
$this->setUserSalt();
$this->assertHttpStatusCode(200, $this->client->getResponse());
$content = $this->client->getResponse()->getContent();

Expand All @@ -181,8 +182,12 @@ public function testRegistrationBlacklistedBlocked(): void
$this->assertNull($this->findUser());
}

public function testRegistrationBlacklistedRequested(): RawMessage
public function testRegistrationBlacklistedRequested(): ?RawMessage
{
if (\class_exists(\Swift_Mailer::class)) {
$this->markTestSkipped('Skip test for swift mailer.');
}

$this->createBlacklistItem($this->getEntityManager(), '*@sulu.io', BlacklistItem::TYPE_REQUEST);

$crawler = $this->client->request('GET', '/registration');
Expand Down Expand Up @@ -274,6 +279,10 @@ public function testBlacklistBlocked(): void

public function testPasswordForget(): void
{
if (\class_exists(\Swift_Mailer::class)) {
$this->markTestSkipped('Skip test for swift mailer.');
}

$user = $this->testConfirmation();

$crawler = $this->client->request('GET', '/password-forget');
Expand Down Expand Up @@ -320,10 +329,10 @@ public function testPasswordForget(): void
);
$this->client->submit($form);

$this->getEntityManager()->clear();
//$this->getEntityManager()->clear();

/** @var User $user */
$user = $this->findUser();
$user = $this->findUser('sulu');
$password = $user->getPassword();
$this->assertNotNull($password);
$this->assertStringStartsWith('my-new-password', $password);
Expand All @@ -349,6 +358,25 @@ private function findUser(string $username = 'sulu'): ?User
}
}

/**
* Find user by username.
*/
private function setUserSalt(string $username = 'sulu'): void
{
$repository = $this->getContainer()->get('sulu.repository.user');

try {
/** @var User $user */
$user = $repository->findUserByUsername($username);
$user->setSalt('');
$user->setPassword('my-sulu');
$this->getEntityManager()->flush();

} catch (NoResultException $exception) {
// @ignoreException
}
}

/**
* @return array{
* 'sulu.context': SuluKernel::CONTEXT_WEBSITE,
Expand Down
Loading

0 comments on commit f7f7396

Please sign in to comment.