Skip to content

Commit 924e508

Browse files
committed
Fix: test
1 parent be0527d commit 924e508

3 files changed

Lines changed: 115 additions & 2 deletions

File tree

src/Controller/BaseController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use PhpList\RestApiClient\Endpoint\AuthClient;
88
use PhpList\RestApiClient\Entity\Administrator;
9-
use PhpList\RestApiClient\Exception\AuthenticationException;
9+
use PhpList\RestApiClient\Exception\ApiException;
1010
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1111

1212
class BaseController extends AbstractController
@@ -20,7 +20,7 @@ protected function getAdmin(): ?Administrator
2020
{
2121
try {
2222
$admin = $this->authClient->getSessionUser();
23-
} catch (AuthenticationException $e) {
23+
} catch (ApiException) {
2424
$admin = null;
2525
}
2626

tests/Integration/Controller/PublicSubscribeControllerPantherTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function testAnonymousUserCanAccessPublicSubscribeAssetRoutes(string $pat
2727
'browser' => static::CHROME,
2828
'connection_timeout_in_ms' => 10000,
2929
]);
30+
$client->getCookieJar()->clear();
3031
$client->request('GET', $path);
3132

3233
$currentPath = (string) parse_url($client->getCurrentURL(), PHP_URL_PATH);
@@ -56,6 +57,7 @@ public function testAnonymousUserCanAccessPublicSubscribeAndUnsubscribePages(str
5657
'browser' => static::CHROME,
5758
'connection_timeout_in_ms' => 10000,
5859
]);
60+
$client->getCookieJar()->clear();
5961
$client->request('GET', $path);
6062

6163
$currentPath = (string) parse_url($client->getCurrentURL(), PHP_URL_PATH);
@@ -70,6 +72,7 @@ public function testSubscribePageDisplaysEmailFieldAndSubmitButton(): void
7072
'browser' => static::CHROME,
7173
'connection_timeout_in_ms' => 10000,
7274
]);
75+
$client->getCookieJar()->clear();
7376
$client->request('GET', '/index.php/subscribe/1');
7477

7578
$client->takeScreenshot('var/screenshots/public-subscribe-0.png');
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\WebFrontend\Tests\Integration\Controller;
6+
7+
use PhpList\Core\Domain\Configuration\Model\ConfigOption;
8+
use PhpList\Core\Domain\Configuration\Service\Provider\ConfigProvider;
9+
use PhpList\RestApiClient\Endpoint\AuthClient;
10+
use PhpList\RestApiClient\Endpoint\SubscribePagesClient;
11+
use PhpList\RestApiClient\Entity\SubscribePagePublic;
12+
use PhpList\RestApiClient\Exception\ApiException;
13+
use PhpList\WebFrontend\Controller\PublicSubscribeController;
14+
use PhpList\WebFrontend\Service\LanguageService;
15+
use PhpList\WebFrontend\Service\PublicSubscribeFormBuilder;
16+
use PhpList\WebFrontend\Service\PublicSubscribeFormValidator;
17+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
18+
use Symfony\Component\HttpFoundation\Request;
19+
20+
class PublicSubscribeControllerTest extends KernelTestCase
21+
{
22+
public function testSubscribePageRendersForAnonymousUserWhenAdminLookupApiFails(): void
23+
{
24+
self::bootKernel();
25+
26+
$subscribePagesClient = $this->createMock(SubscribePagesClient::class);
27+
$subscribePagesClient->expects(self::once())
28+
->method('getPublicSubscribePage')
29+
->with(1)
30+
->willReturn($this->createPublicSubscribePage());
31+
32+
$authClient = $this->createMock(AuthClient::class);
33+
$authClient->expects(self::once())
34+
->method('getSessionUser')
35+
->willThrowException(new ApiException('API error occurred', 500));
36+
37+
$config = $this->createMock(ConfigProvider::class);
38+
$config->expects(self::once())
39+
->method('getValue')
40+
->with(ConfigOption::PoweredByImage)
41+
->willReturn('');
42+
43+
$container = static::getContainer();
44+
$formBuilder = $this->createMock(PublicSubscribeFormBuilder::class);
45+
$formBuilder->expects(self::once())
46+
->method('normalizeHtmlChoice')
47+
->with('checkforhtml')
48+
->willReturn('checkforhtml');
49+
$formBuilder->expects(self::once())
50+
->method('buildAttributeConfig')
51+
->willReturn([]);
52+
$formBuilder->expects(self::once())
53+
->method('buildInitialFormData')
54+
->willReturn([
55+
'email' => '',
56+
'email_confirm' => '',
57+
'make_confirmed' => '0',
58+
'htmlemail' => true,
59+
'selected_lists' => [1],
60+
'attributes' => [],
61+
]);
62+
63+
$controller = new PublicSubscribeController(
64+
$subscribePagesClient,
65+
$authClient,
66+
new LanguageService(),
67+
$formBuilder,
68+
$this->createMock(PublicSubscribeFormValidator::class),
69+
$config,
70+
false
71+
);
72+
$controller->setContainer($container);
73+
74+
$response = $controller->subscribe(Request::create('/subscribe/1'), 1);
75+
$content = (string) $response->getContent();
76+
77+
self::assertSame(200, $response->getStatusCode());
78+
self::assertStringContainsString('form method="post"', $content);
79+
self::assertStringContainsString('name="email"', $content);
80+
self::assertStringContainsString('type="email"', $content);
81+
self::assertStringContainsString('type="submit"', $content);
82+
self::assertStringNotContainsString('adminmessage', $content);
83+
}
84+
85+
private function createPublicSubscribePage(): SubscribePagePublic
86+
{
87+
return new SubscribePagePublic([
88+
'id' => 1,
89+
'title' => 'Subscribe to our newsletter',
90+
'data' => [
91+
'attributes' => [],
92+
'button' => 'Subscribe',
93+
'emaildoubleentry' => 'no',
94+
'footer' => '',
95+
'header' => '',
96+
'htmlchoice' => 'checkforhtml',
97+
'intro' => '<p>Subscribe to our newsletter</p>',
98+
'lists' => [
99+
[
100+
'id' => 1,
101+
'name' => 'News',
102+
'description' => 'Main list',
103+
'list_position' => 0,
104+
],
105+
],
106+
'preselectlist' => 1,
107+
],
108+
]);
109+
}
110+
}

0 commit comments

Comments
 (0)