From c751cc7fdc49a4d11f6755cefac91740bc39608f Mon Sep 17 00:00:00 2001 From: Rancoud Date: Fri, 15 Dec 2023 19:59:39 +0100 Subject: [PATCH] feat: add message on contact page when mail not configured (#17) --- app/controllers/www/ContactController.php | 14 +++++ tests/tests-invalid-mail-contact-to.env | 67 +++++++++++++++++++++++ tests/www/Contact/ContactTest.php | 19 +++++++ 3 files changed, 100 insertions(+) create mode 100644 tests/tests-invalid-mail-contact-to.env diff --git a/app/controllers/www/ContactController.php b/app/controllers/www/ContactController.php index cbbd998..10f2e4f 100644 --- a/app/controllers/www/ContactController.php +++ b/app/controllers/www/ContactController.php @@ -59,6 +59,20 @@ protected function setTemplateProperties(array $data = []): void */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { + $to = (string) Application::getConfig()->get('MAIL_CONTACT_TO'); + $posArobase = \mb_strpos($to, '@'); + if (!$posArobase || ($posArobase < 1 || $posArobase === \mb_strlen($to) - 1)) { + $this->setTemplateProperties(); + + $this->data += [$this->inputs['CSRF'] => Session::get('csrf')]; + + $formContact = new FormHelper(); + $formContact->setErrorMessage('Error, could not use this form, "MAIL_CONTACT_TO" env variable is invalid.'); + $this->data += ['form-contact' => $formContact]; + + return $this->sendPage(); + } + if ($this->hasSentForm($request, 'POST', $this->inputs, 'error-form-contact')) { $cleanedParams = $this->treatFormContact($request); $this->doProcessContact($request, $cleanedParams); diff --git a/tests/tests-invalid-mail-contact-to.env b/tests/tests-invalid-mail-contact-to.env new file mode 100644 index 0000000..28adbc0 --- /dev/null +++ b/tests/tests-invalid-mail-contact-to.env @@ -0,0 +1,67 @@ +# setup timezone, by default use timezone from php.ini (valid timezones are checked with DateTimeZone::listIdentifiers()) +TIMEZONE="UTC" + +# specific files to load for setting router, if null it will load all files in folder given in Application constructor +ROUTES=null + +# for enabling DEBUG_* parameters +DEBUG=true + +# enable error_reporting: show php errors +DEBUG_PHP=true + +# keep PSR-7 request object +DEBUG_REQUEST=false + +# keep PSR-7 response object +DEBUG_RESPONSE=false + +# return saved queries +DEBUG_DATABASE=false + +# return all values in Session object +DEBUG_SESSION=false + +# return memory usage/limit/percentage +DEBUG_MEMORY=false + +# return elapsed time of each Application->run() +DEBUG_RUN_ELAPSED_TIMES=false + +# return all files included +DEBUG_INCLUDED_FILES=false + +# database +DATABASE_DRIVER=mysql +DATABASE_HOST=127.0.0.1 +DATABASE_USER=blueprintue-self-hosted-edition +DATABASE_PASSWORD=blueprintue-self-hosted-edition +DATABASE_NAME=blueprintue-self-hosted-edition +DATABASE_PERSISTENT_CONNECTION=true + +# session +SESSION_DRIVER=database +SESSION_ENCRYPT_KEY="1+XaQLUMfd/l6g1pdc/t+K1UiWJSuUY7k8GEjr+MLagmlgilqG8lcHrHc59ad9fo" +SESSION_REMEMBER_NAME="remember_token" +SESSION_REMEMBER_LIFETIME=2592000 +SESSION_REMEMBER_PATH="/" +SESSION_REMEMBER_HTTPS=false +SESSION_SAMESITE=Strict + +# host +HOST=blueprintue.test +HTTPS=true + +# site +SITE_NAME=this_site_name +SITE_BASE_TITLE="This is a base title" +SITE_DESCRIPTION="This is a description" + +# anonymous +ANONYMOUS_ID=2 + +# mail +MAIL_FROM_ADDRESS=no-reply@blueprintue.test +MAIL_FROM_NAME=blueprintUE_from_name +MAIL_CONTACT_TO= +MAIL_HEADER_LOGO_PATH="full-logo.png" diff --git a/tests/www/Contact/ContactTest.php b/tests/www/Contact/ContactTest.php index 1c1e899..ad0edea 100644 --- a/tests/www/Contact/ContactTest.php +++ b/tests/www/Contact/ContactTest.php @@ -47,6 +47,25 @@ public function testContactGET(): void $this->doTestNavBarHasNoLinkActive($response); } + /** + * @throws ApplicationException + * @throws EnvironmentException + * @throws RouterException + */ + public function testContactGETInvalidConfigurationEmail(): void + { + $response = $this->getResponseFromApplication('GET', '/contact/', [], [], [], [], [], [], [], 'tests-invalid-mail-contact-to.env'); + $this->doTestHasResponseWithStatusCode($response, 200); + $this->doTestHtmlHead($response, [ + 'title' => 'Contact us | This is a base title', + 'description' => 'Contact us' + ]); + $this->doTestHtmlBody($response, '

Contact

'); + $this->doTestHtmlBody($response, ''); + $this->doTestNavBarIsComplete($response); + $this->doTestNavBarHasNoLinkActive($response); + } + public function dataCases(): array { return [