|
| 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