Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: コメントのみ承認ありにしてコメントを追加しても即時公開になってしまうバグ修正 #124

Merged
merged 1 commit into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Model/BbsArticle.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class BbsArticle extends BbsesAppModel {
'Likes.Like',
'NetCommons.OriginalKey',
'Workflow.WorkflowComment',
'Workflow.Workflow',
'Bbses.BbsesWorkflow',
'Mails.MailQueue' => array(
'embedTags' => array(
'X-SUBJECT' => 'BbsArticle.title',
Expand Down
62 changes: 62 additions & 0 deletions Model/Behavior/BbsesWorkflowBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Workflow Behavior
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/

App::uses('WorkflowBehavior', 'Workflow.Model/Behavior');

/**
* Workflow Behavior
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package Bbses\Workflow\Model\Befavior
*/
class BbsesWorkflowBehavior extends WorkflowBehavior {

/**
* beforeValidate is called before a model is validated, you can use this callback to
* add behavior validation rules into a models validate array. Returning false
* will allow you to make the validation fail.
*
* @param Model $model Model using this behavior
* @param array $options Options passed from Model::save().
* @return mixed False or null will abort the operation. Any other result will continue.
* @see Model::save()
*/
public function beforeValidate(Model $model, $options = array()) {
if (empty($model->data['BbsArticleTree']['root_id'])) {
return parent::beforeValidate($model, $options);
}

if (Current::permission('content_comment_publishable')) {
$statuses = WorkflowBehavior::$statuses;
} else {
$statuses = WorkflowBehavior::$statusesForEditor;
}

$model->validate = ValidateMerge::merge($model->validate, array(
'status' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => __d('net_commons', 'Invalid request.'),
'allowEmpty' => false,
'required' => true,
'on' => 'create', // Limit validation to 'create' or 'update' operations
),
'inList' => array(
'rule' => array('inList', $statuses),
'message' => __d('net_commons', 'Invalid request.'),
)
),
));

return true;
}

}
10 changes: 5 additions & 5 deletions Test/Case/Controller/BbsArticlesController/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,11 @@ public function dataProviderViewByEditable() {
*/
public function testViewError($urlOptions, $assert, $exception = null, $return = 'view') {
//Exception
ClassRegistry::removeObject('WorkflowBehavior');
$workflowBehaviorMock = $this->getMock('WorkflowBehavior', ['canReadWorkflowContent']);
ClassRegistry::addObject('WorkflowBehavior', $workflowBehaviorMock);
$this->BbsArticle->Behaviors->unload('Workflow');
$this->BbsArticle->Behaviors->load('Workflow', $this->BbsArticle->actsAs['Workflow.Workflow']);
ClassRegistry::removeObject('BbsesWorkflowBehavior');
$workflowBehaviorMock = $this->getMock('BbsesWorkflowBehavior', ['canReadWorkflowContent']);
ClassRegistry::addObject('BbsesWorkflowBehavior', $workflowBehaviorMock);
$this->BbsArticle->Behaviors->unload('BbsesWorkflow');
$this->BbsArticle->Behaviors->load('BbsesWorkflow', $this->BbsArticle->actsAs['Bbses.BbsesWorkflow']);

$workflowBehaviorMock
->expects($this->once())
Expand Down
38 changes: 38 additions & 0 deletions Test/Case/Model/BbsArticle/SaveBbsArticleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,51 @@ public function setUp() {
Current::write('Language.id', '2');

parent::setUp();
Current::writePermission('2', 'content_comment_publishable', true);

$model = $this->_modelName;
$this->$model->Behaviors->unload('Like');
$this->$model->Behaviors->unload('Topics');
$this->$model->BbsArticleTree = ClassRegistry::init('Bbses.BbsArticleTree');
$this->$model->BbsArticleTree->Behaviors->unload('Like');
}

/**
* Test to call BbsesWorkflowBehavior::beforeSave
*
* BbsesWorkflowBehaviorをモックに置き換えて登録処理を呼び出します。<br>
* BbsesWorkflowBehavior::beforeSaveが1回呼び出されることをテストします。<br>
* ##### 参考URL
* http://stackoverflow.com/questions/19833495/how-to-mock-a-cakephp-behavior-for-unit-testing]
*
* @param array $data 登録データ
* @dataProvider dataProviderSave
* @return void
* @throws CakeException Workflow.Workflowがロードされていないとエラー
*/
public function testCallWorkflowBehavior($data) {
$model = $this->_modelName;
$method = $this->_methodName;

if (! $this->$model->Behaviors->loaded('Bbses.BbsesWorkflow')) {
$error = '"Workflow.Workflow" not loaded in ' . $this->$model->alias . '.';
throw new CakeException($error);
}

ClassRegistry::removeObject('BbsesWorkflowBehavior');
$workflowBehaviorMock = $this->getMock('BbsesWorkflowBehavior', ['beforeSave']);
ClassRegistry::addObject('BbsesWorkflowBehavior', $workflowBehaviorMock);
$this->$model->Behaviors->unload('BbsesWorkflow');
$this->$model->Behaviors->load('BbsesWorkflow', $this->$model->actsAs['Bbses.BbsesWorkflow']);

$workflowBehaviorMock
->expects($this->once())
->method('beforeSave')
->will($this->returnValue(true));

$this->$model->$method($data);
}

/**
* SaveのDataProvider
*
Expand Down
22 changes: 12 additions & 10 deletions View/Elements/BbsArticles/edit_form.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,19 @@
</div>

<?php
if ($this->params['action'] === 'edit' && $this->data['BbsArticleTree']['root_id']) {
echo $this->BbsesForm->replyEditButtons('BbsArticle.status');
} else {
if ($this->params['action'] === 'edit' && $this->data['BbsArticleTree']['root_id'] ||
$this->params['action'] === 'reply') {
if ($this->params['action'] === 'reply') {
$key = isset($currentBbsArticle['BbsArticle']['key'])
? $currentBbsArticle['BbsArticle']['key']
: null;
$cancelUrl = NetCommonsUrl::blockUrl(
array('action' => 'view', 'key' => $key)
);
} elseif ($this->params['action'] === 'edit') {
$key = $currentBbsArticle['BbsArticle']['key'] ?? null;
} else {
$key = $this->request->data['BbsArticle']['key'] ?? null;
}
$cancelUrl = NetCommonsUrl::blockUrl(
array('action' => 'view', 'key' => $key)
);
echo $this->BbsesForm->replyButtons('BbsArticle.status', $cancelUrl);
} else {
if ($this->params['action'] === 'edit') {
$key = isset($this->request->data['BbsArticle']['key'])
? $this->request->data['BbsArticle']['key']
: null;
Expand Down
12 changes: 3 additions & 9 deletions View/Helper/BbsesFormHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ class BbsesFormHelper extends AppHelper {
* 返信フォームのボタン表示
*
* @param string $statusFieldName ステータスのフィールド名("Modelname.fieldname")
* @param string $cancelUrl キャンセルボタン
* @return string ボタンHTML
*/
public function replyEditButtons($statusFieldName) {
public function replyButtons($statusFieldName, $cancelUrl) {
$output = '';
$output .= '<div class="panel-footer text-center">';

Expand All @@ -55,13 +56,6 @@ public function replyEditButtons($statusFieldName) {
//変更前のstatusを保持する
$output .= $this->NetCommonsForm->hidden('status_', array('value' => $status));

$key = null;
if (isset($this->_View->request->data['BbsArticle']['key'])) {
$key = $this->_View->request->data['BbsArticle']['key'];
}
$cancelUrl = NetCommonsUrl::blockUrl(
array('action' => 'view', 'key' => $key)
);
$cancelOptions = array(
'ng-class' => '{disabled: sending}',
'ng-click' => 'sending=true',
Expand All @@ -74,7 +68,7 @@ public function replyEditButtons($statusFieldName) {
'ng-class' => '{disabled: sending}'
);

if (Current::permission('content_publishable')) {
if (Current::permission('content_comment_publishable')) {
$saveOptions = array(
'label' => __d('net_commons', 'OK'),
'class' => 'btn btn-primary' . $this->Button->getButtonSize() . ' btn-workflow',
Expand Down