Реализована форма обратной связи

This commit is contained in:
ErickSkrauch 2016-06-15 23:54:13 +03:00
parent 22296f246a
commit 912606e27f
7 changed files with 213 additions and 0 deletions

View 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
View 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) ?>

View 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;
}
}

View File

@ -1,4 +1,5 @@
<?php
return [
'fromEmail' => 'account@ely.by',
'supportEmail' => 'support@ely.by',
];

View File

@ -1,4 +1,5 @@
<?php
return [
'fromEmail' => 'account@ely.by',
'supportEmail' => 'support@ely.by',
];

View File

@ -1,4 +1,5 @@
<?php
return [
'fromEmail' => 'account@ely.by',
'supportEmail' => 'support@ely.by',
];

View 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;
}
}