Skip to content

Commit

Permalink
Add comment from field.
Browse files Browse the repository at this point in the history
Add web test.
Refactoring.
  • Loading branch information
rmrevin committed Oct 5, 2015
1 parent d772f10 commit f9fe88d
Show file tree
Hide file tree
Showing 27 changed files with 538 additions and 60 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
2015-10-05 - 1.4.0
------------------
* Improve dependency injection.
* Add comment from field.
* Add web test.
* Refactoring.

2015-06-27 - 1.3.1
------------------
* Add param `theme` to `CommentFormWidget`
Expand Down
31 changes: 19 additions & 12 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use rmrevin\yii\module\Comments\forms\CommentCreateForm;
use rmrevin\yii\module\Comments\models\Comment;
use rmrevin\yii\module\Comments\models\queries\CommentQuery;

use yii\helpers\ArrayHelper;

/**
Expand All @@ -20,7 +19,8 @@
class Module extends \yii\base\Module
{

const NAME = 'comments';
/** @var string module name */
public static $moduleName = 'comments';

/** @var string|null */
public $userIdentityClass = null;
Expand All @@ -30,21 +30,23 @@ class Module extends \yii\base\Module

/**
* Array that will store the models used in the package
* e.g. : ['Comment' => 'frontend/models/comments/CommentModel'
* e.g. :
* [
* 'Comment' => 'frontend/models/comments/CommentModel'
* ]
*
* The classes defined here will be merged with getDefaultModels()
* having he manually defined by the user preference.
*
* @var array
*/
public $modelMap = [];

public function init()
{
parent::init();

if ($this->userIdentityClass === null)
{
if ($this->userIdentityClass === null) {
$this->userIdentityClass = \Yii::$app->getUser()->identityClass;
}

Expand All @@ -53,11 +55,19 @@ public function init()
$this->defineModelClasses();
}

/**
* @return static
*/
public static function instance()
{
return \Yii::$app->getModule(static::$moduleName);
}

/**
* Merges the default and user defined model classes
* Also let's the developer to set new ones with the
* parameter being those the ones with most preference.
*
*
* @param array $modelClasses
*/
public function defineModelClasses($modelClasses = [])
Expand Down Expand Up @@ -96,18 +106,15 @@ public function model($name, $config = [])
{
$modelData = $this->modelMap[ucfirst($name)];

if (!empty($config))
{
if (is_string($modelData))
{
if (!empty($config)) {
if (is_string($modelData)) {
$modelData = ['class' => $modelData];
}

$modelData = ArrayHelper::merge(
$modelData,
$config
);

}

return $modelData;
Expand Down
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,11 @@
"psr-4": {
"rmrevin\\yii\\module\\Comments\\": ""
}
},
"extra": {
"asset-installer-paths": {
"npm-asset-library": "vendor/npm",
"bower-asset-library": "vendor/bower"
}
}
}
20 changes: 15 additions & 5 deletions forms/CommentCreateForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class CommentCreateForm extends \yii\base\Model

public $id;
public $entity;
public $from;
public $text;

/** @var Comments\models\Comment */
Expand All @@ -30,7 +31,14 @@ public function init()
if (false === $this->Comment->isNewRecord) {
$this->id = $Comment->id;
$this->entity = $Comment->entity;
$this->from = $Comment->from;
$this->text = $Comment->text;
} elseif (!\Yii::$app->getUser()->getIsGuest()) {
$User = \Yii::$app->getUser()->getIdentity();

$this->from = $User instanceof Comments\interfaces\CommentatorInterface
? $User->getCommentatorName()
: null;
}
}

Expand All @@ -39,11 +47,11 @@ public function init()
*/
public function rules()
{
$CommentModelClassName = \Yii::$app->getModule(Comments\Module::NAME)->model('comment');
$CommentModelClassName = Comments\Module::instance()->model('comment');

return [
[['entity', 'text'], 'required'],
[['entity', 'text'], 'string'],
[['entity', 'from', 'text'], 'string'],
[['id'], 'integer'],
[['id'], 'exist', 'targetClass' => $CommentModelClassName, 'targetAttribute' => 'id'],
];
Expand All @@ -56,6 +64,7 @@ public function attributeLabels()
{
return [
'entity' => \Yii::t('app', 'Entity'),
'from' => \Yii::t('app', 'Your name'),
'text' => \Yii::t('app', 'Text'),
];
}
Expand All @@ -68,13 +77,13 @@ public function save()
{
$Comment = $this->Comment;

$CommentModelClassName = \Yii::$app->getModule(Comments\Module::NAME)->model('comment');
$CommentModelClassName = Comments\Module::instance()->model('comment');

if (empty($this->id)) {
$Comment = \Yii::createObject($CommentModelClassName);
$Comment = \Yii::createObject($CommentModelClassName);
} elseif ($this->id > 0 && $Comment->id !== $this->id) {
/** @var Comments\models\Comment $CommentModel */
$CommentModel = \Yii::createObject($CommentModelClassName);
$CommentModel = \Yii::createObject($CommentModelClassName);
$Comment = $CommentModel::find()
->byId($this->id)
->one();
Expand All @@ -85,6 +94,7 @@ public function save()
}

$Comment->entity = $this->entity;
$Comment->from = $this->from;
$Comment->text = $this->text;

$result = $Comment->save();
Expand Down
18 changes: 18 additions & 0 deletions migrations/m151005_165040_comment_from.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use yii\db\Migration;
use yii\db\Schema;

class m151005_165040_comment_from extends Migration
{

public function up()
{
$this->addColumn('{{%comment}}', 'from', Schema::TYPE_STRING . ' AFTER [[entity]]');
}

public function down()
{
$this->dropColumn('{{%comment}}', 'from');
}
}
39 changes: 16 additions & 23 deletions models/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*
* @property integer $id
* @property string $entity
* @property string $from
* @property string $text
* @property integer $deleted
* @property integer $created_by
Expand Down Expand Up @@ -49,7 +50,7 @@ public function rules()
{
return [
[['text'], 'required'],
[['text'], 'string'],
[['from', 'text'], 'string'],
[['created_by', 'updated_by', 'created_at', 'updated_at'], 'integer'],
[['deleted'], 'boolean'],
[['deleted'], 'default', 'value' => self::NOT_DELETED],
Expand All @@ -64,6 +65,7 @@ public function attributeLabels()
return [
'id' => \Yii::t('app', 'ID'),
'entity' => \Yii::t('app', 'Entity'),
'from' => \Yii::t('app', 'Comment author'),
'text' => \Yii::t('app', 'Text'),
'created_by' => \Yii::t('app', 'Created by'),
'updated_by' => \Yii::t('app', 'Updated by'),
Expand Down Expand Up @@ -93,10 +95,7 @@ public function isDeleted()
*/
public static function canCreate()
{
/** @var Comments\Module $Module */
$Module = \Yii::$app->getModule(Comments\Module::NAME);

return $Module->useRbac === true
return Comments\Module::instance()->useRbac === true
? \Yii::$app->getUser()->can(Comments\Permission::CREATE)
: true;
}
Expand All @@ -106,56 +105,50 @@ public static function canCreate()
*/
public function canUpdate()
{
/** @var Comments\Module $Module */
$Module = \Yii::$app->getModule(Comments\Module::NAME);
$User = \Yii::$app->getUser();

return $Module->useRbac === true
return Comments\Module::instance()->useRbac === true
? \Yii::$app->getUser()->can(Comments\Permission::UPDATE) || \Yii::$app->getUser()->can(Comments\Permission::UPDATE_OWN, ['Comment' => $this])
: $this->created_by === \Yii::$app->get('user')->id;
: $User->isGuest ? false : $this->created_by === $User->id;
}

/**
* @return bool
*/
public function canDelete()
{
/** @var Comments\Module $Module */
$Module = \Yii::$app->getModule(Comments\Module::NAME);
$User = \Yii::$app->getUser();

return $Module->useRbac === true
return Comments\Module::instance()->useRbac === true
? \Yii::$app->getUser()->can(Comments\Permission::DELETE) || \Yii::$app->getUser()->can(Comments\Permission::DELETE_OWN, ['Comment' => $this])
: $this->created_by === \Yii::$app->get('user')->id;
: $User->isGuest ? false : $this->created_by === $User->id;
}

/**
* @return queries\CommentQuery
*/
public function getAuthor()
{
/** @var Comments\Module $Module */
$Module = \Yii::$app->getModule(Comments\Module::NAME);

return $this->hasOne($Module->userIdentityClass, ['id' => 'created_by']);
return $this->hasOne(Comments\Module::instance()->userIdentityClass, ['id' => 'created_by']);
}

/**
* @return queries\CommentQuery
*/
public function getLastUpdateAuthor()
{
/** @var Comments\Module $Module */
$Module = \Yii::$app->getModule(Comments\Module::NAME);

return $this->hasOne($Module->userIdentityClass, ['id' => 'updated_by']);
return $this->hasOne(Comments\Module::instance()->userIdentityClass, ['id' => 'updated_by']);
}

/**
* @return queries\CommentQuery
*/
public static function find()
{
$CommentQueryModelClassName = \Yii::$app->getModule(Comments\Module::NAME)->model('commentQuery');
return \Yii::createObject($CommentQueryModelClassName, [get_called_class()]);
return \Yii::createObject(
Comments\Module::instance()->model('commentQuery'),
[get_called_class()]
);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions models/queries/CommentQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ public function byEntity($entity)
*/
public function withoutDeleted()
{
$CommentModelClassName = \Yii::$app->getModule(Comments\Module::NAME)->model('comment');
/** @var Comments\models\Comment $CommentModel */
$CommentModel = \Yii::createObject($CommentModelClassName);
$CommentModel = \Yii::createObject(Comments\Module::instance()->model('comment'));

$this->andWhere(['deleted' => $CommentModel::NOT_DELETED]);

Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
convertWarningsToExceptions="true"
stopOnFailure="false">
<filter>
<whitelist>
<directory suffix=".php">.</directory>
</whitelist>
<blacklist>
<directory suffix=".php">./tests</directory>
<directory suffix=".php">./vendor</directory>
Expand Down
18 changes: 18 additions & 0 deletions tests/web/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Web test for current extension
==============================

Run
---

Execute migrations
```
./yii migrate/up --migrationPath=../../migrations
./yii migrate/up
```

Run local web server
```
php -S 127.0.0.1:8899
```

Open in browser http://127.0.0.1:8899
29 changes: 29 additions & 0 deletions tests/web/assets/AppAssetBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* AppAssetBundle.php
* @author Revin Roman
* @link https://rmrevin.com
*/

namespace rmrevin\yii\module\Comments\tests\web\assets;

/**
* Class AppAssetBundle
* @package rmrevin\yii\module\Comments\tests\web\assets
*/
class AppAssetBundle extends \yii\web\AssetBundle
{

public $css = [
'https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css',
'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css',
];

public $js = [
'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js',
];

public $depends = [
'yii\web\JqueryAsset',
];
}
12 changes: 12 additions & 0 deletions tests/web/config/console.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

return [
'id' => 'basic-console',
'basePath' => dirname(__DIR__),
'bootstrap' => [],
'controllerNamespace' => 'app\commands',
'components' => [
'db' => require(__DIR__ . '/db.php'),
],
'params' => [],
];
Loading

0 comments on commit f9fe88d

Please sign in to comment.