mirror of
https://github.com/elyby/accounts.git
synced 2024-11-30 10:42:16 +05:30
Реализована форма обратной связи
This commit is contained in:
parent
22296f246a
commit
912606e27f
39
api/controllers/FeedbackController.php
Normal file
39
api/controllers/FeedbackController.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
namespace api\controllers;
|
||||
|
||||
use api\models\FeedbackForm;
|
||||
use Yii;
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
class FeedbackController extends Controller {
|
||||
|
||||
public function behaviors() {
|
||||
return ArrayHelper::merge(parent::behaviors(), [
|
||||
'authenticator' => [
|
||||
'except' => ['index'],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function verbs() {
|
||||
return [
|
||||
'index' => ['POST'],
|
||||
];
|
||||
}
|
||||
|
||||
public function actionIndex() {
|
||||
$model = new FeedbackForm();
|
||||
$model->load(Yii::$app->request->post());
|
||||
if (!$model->sendMessage()) {
|
||||
return [
|
||||
'success' => false,
|
||||
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'success' => true,
|
||||
];
|
||||
}
|
||||
|
||||
}
|
29
api/mails/feedback.php
Normal file
29
api/mails/feedback.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
/**
|
||||
* @var \yii\base\View $this
|
||||
* @var \api\models\FeedbackForm $model
|
||||
* @var \common\models\Account|null $account
|
||||
*/
|
||||
|
||||
use yii\helpers\Html;
|
||||
|
||||
?>
|
||||
|
||||
<h2>Форма обратной связи Account Ely.by</h2>
|
||||
<hr />
|
||||
<br />
|
||||
<br />
|
||||
Письмо отправлено: <?= date('d.m.Y, в H:i') ?><br />
|
||||
E-mail отправителя: <?= $model->email ?><br />
|
||||
<?php if ($account) { ?>
|
||||
Ник отправителя: <?= $account->username ?><br />
|
||||
<?php if ($account->email !== $model->email) { ?>
|
||||
Регистрационный E-mail: <?= $account->email ?><br />
|
||||
<?php } ?>
|
||||
<?php $link = 'http://ely.by/u' . $account->id; ?>
|
||||
Ссылка на профиль: <?= Html::a($link, $link) ?><br />
|
||||
Дата регистрации: <?= date('d.m.Y, в H:i', $account->created_at) ?><br />
|
||||
<?php } ?>
|
||||
Тема: <?= Yii::$app->formatter->asText($model->subject) ?><br />
|
||||
Текст:<br />
|
||||
<?= Yii::$app->formatter->asNtext($model->message) ?>
|
65
api/models/FeedbackForm.php
Normal file
65
api/models/FeedbackForm.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
namespace api\models;
|
||||
|
||||
use api\models\base\ApiForm;
|
||||
use Yii;
|
||||
use yii\base\ErrorException;
|
||||
use yii\base\InvalidConfigException;
|
||||
|
||||
class FeedbackForm extends ApiForm {
|
||||
|
||||
public $subject;
|
||||
|
||||
public $email;
|
||||
|
||||
public $type;
|
||||
|
||||
public $message;
|
||||
|
||||
public function rules() {
|
||||
return [
|
||||
[['subject', 'email', 'message'], 'required', 'message' => 'error.{attribute}_required'],
|
||||
[['subject'], 'string', 'max' => 255],
|
||||
[['email'], 'email'],
|
||||
[['message'], 'string', 'max' => 65535],
|
||||
];
|
||||
}
|
||||
|
||||
public function sendMessage() : bool {
|
||||
if (!$this->validate()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @var \yii\swiftmailer\Mailer $mailer */
|
||||
$mailer = Yii::$app->mailer;
|
||||
$fromEmail = Yii::$app->params['supportEmail'];
|
||||
if (!$fromEmail) {
|
||||
throw new InvalidConfigException('Please specify supportEmail value in app params');
|
||||
}
|
||||
|
||||
$account = $this->getAccount();
|
||||
/** @var \yii\swiftmailer\Message $message */
|
||||
$message = $mailer->compose('@app/mails/feedback', [
|
||||
'model' => $this,
|
||||
'account' => $account,
|
||||
]);
|
||||
$message
|
||||
->setTo($this->email)
|
||||
->setFrom([$this->email => $account ? $account->username : $this->email])
|
||||
->setSubject($this->subject);
|
||||
|
||||
if (!$message->send()) {
|
||||
throw new ErrorException('Unable send feedback email.');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \common\models\Account|null $account
|
||||
*/
|
||||
protected function getAccount() {
|
||||
return Yii::$app->user->identity;
|
||||
}
|
||||
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
<?php
|
||||
return [
|
||||
'fromEmail' => 'account@ely.by',
|
||||
'supportEmail' => 'support@ely.by',
|
||||
];
|
||||
|
@ -1,4 +1,5 @@
|
||||
<?php
|
||||
return [
|
||||
'fromEmail' => 'account@ely.by',
|
||||
'supportEmail' => 'support@ely.by',
|
||||
];
|
||||
|
@ -1,4 +1,5 @@
|
||||
<?php
|
||||
return [
|
||||
'fromEmail' => 'account@ely.by',
|
||||
'supportEmail' => 'support@ely.by',
|
||||
];
|
||||
|
77
tests/codeception/api/unit/models/FeedbackFormTest.php
Normal file
77
tests/codeception/api/unit/models/FeedbackFormTest.php
Normal file
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
namespace codeception\api\unit\models;
|
||||
|
||||
use api\models\FeedbackForm;
|
||||
use Codeception\Specify;
|
||||
use common\models\Account;
|
||||
use tests\codeception\api\unit\TestCase;
|
||||
use Yii;
|
||||
|
||||
class FeedbackFormTest extends TestCase {
|
||||
use Specify;
|
||||
|
||||
const FILE_NAME = 'testing_message.eml';
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
/** @var \yii\swiftmailer\Mailer $mailer */
|
||||
$mailer = Yii::$app->mailer;
|
||||
$mailer->fileTransportCallback = function() {
|
||||
return self::FILE_NAME;
|
||||
};
|
||||
}
|
||||
|
||||
protected function tearDown() {
|
||||
if (file_exists($this->getMessageFile())) {
|
||||
unlink($this->getMessageFile());
|
||||
}
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function testSendMessage() {
|
||||
$this->specify('send email', function() {
|
||||
$model = new FeedbackForm([
|
||||
'subject' => 'Тема обращения',
|
||||
'email' => 'erickskrauch@ely.by',
|
||||
'message' => 'Привет мир!',
|
||||
]);
|
||||
expect($model->sendMessage())->true();
|
||||
expect_file('message file exists', $this->getMessageFile())->exists();
|
||||
});
|
||||
|
||||
$this->specify('send email with user info', function() {
|
||||
/** @var FeedbackForm|\PHPUnit_Framework_MockObject_MockObject $model */
|
||||
$model = $this->getMockBuilder(FeedbackForm::class)
|
||||
->setMethods(['getAccount'])
|
||||
->setConstructorArgs([[
|
||||
'subject' => 'Тема обращения',
|
||||
'email' => 'erickskrauch@ely.by',
|
||||
'message' => 'Привет мир!',
|
||||
]])
|
||||
->getMock();
|
||||
|
||||
$model
|
||||
->expects($this->any())
|
||||
->method('getAccount')
|
||||
->will($this->returnValue(new Account([
|
||||
'id' => '123',
|
||||
'username' => 'Erick',
|
||||
'email' => 'find-this@email.net',
|
||||
'created_at' => time() - 86400,
|
||||
])));
|
||||
expect($model->sendMessage())->true();
|
||||
expect_file('message file exists', $this->getMessageFile())->exists();
|
||||
$data = file_get_contents($this->getMessageFile());
|
||||
expect(strpos($data, 'find-this@email.net'))->notEquals(false);
|
||||
});
|
||||
}
|
||||
|
||||
private function getMessageFile() {
|
||||
/** @var \yii\swiftmailer\Mailer $mailer */
|
||||
$mailer = Yii::$app->mailer;
|
||||
|
||||
return Yii::getAlias($mailer->fileTransportPath) . '/' . self::FILE_NAME;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user