84.锁定话题(一)

未匹配的标注

本节说明

  • 对应视频教程第 84 小节:An Administrator May Lock Any Thread

本节内容

本节我们开始锁定话题功能的开发。如果管理员锁定了某一话题,那么该话题将不能进行回复。首先新建测试:
forum\tests\Unit\ThreadTest.php

    .
    .
    /** @test */
    public function a_thread_can_be_locked()
    {
        $this->assertFalse($this->thread->locked);

        $this->thread->lock();

        $this->assertTrue($this->thread->locked);
    }
}

接着我们修改迁移文件,添加locked字段:
forum\database\migrations{timestamp}_create_threads_table.php

    .
    .
    public function up()
    {
        Schema::create('threads', function (Blueprint $table) {
            $table->increments('id');
            $table->string('slug')->unique()->nullable();
            $table->unsignedInteger('user_id');
            $table->unsignedInteger('channel_id');
            $table->unsignedInteger('replies_count')->default(0);
            $table->unsignedInteger('visits')->default(0);
            $table->string('title');
            $table->text('body');
            $table->unsignedInteger('best_reply_id')->nullable();
            $table->boolean('locked')->default(false);
            $table->timestamps();
        });
    }
    .
    .

为了方便测试,我们修改下模型工厂文件:
forum\database\factories\ModelFactory.php

.
.
$factory->define(App\Thread::class,function ($faker){
   $title = $faker->sentence;

    return [
       'user_id' => function () {
            return factory('App\User')->create()->id;
       },
       'channel_id' => function () {
            return factory('App\Channel')->create()->id;
       },
       'title' => $title,
       'body' => $faker->paragraph,
       'visits' => 0,
       'slug' => str_slug($title),
       'locked' => false
    ];
});
.
.

然后我们添加lock()方法:
forum\app\Thread.php

    .
    .
    public function addReply($reply)
    {
        $reply = $this->replies()->create($reply);

        event(new ThreadReceivedNewReply($reply));

        return $reply;
    }

    public function lock()
    {
        $this->update(['locked' => true]);
    }
    .
    .

运行测试:
file
接下来我们为锁定话题功能新建一个测试文件,并添加第一个功能测试:
forum\tests\Feature\LockThreadsTest.php

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;

class LockThreadsTest extends TestCase
{
    use DatabaseMigrations;

    /** @test */
    public function once_locked_thread_may_not_receive_new_replies()
    {
        $this->signIn();

        $thread = create('App\Thread');

        $thread->lock();

        $this->post($thread->path() . '/replies',[
            'body' => 'Foobar',
            'user_id' => auth()->id()
        ])->assertStatus(422);
    }
}

运行测试:
file
因为我们没有对锁定话题发布评论的动作做限制,我们来加上限制:
forum\app\Http\Controllers\RepliesController.php

    .
    .
    public function store($channelId, Thread $thread, CreatePostRequest $form)
    {
        if($thread->locked) {
            return response('Thread is locked.',422);
        }

        return $thread->addReply([
                'body' => request('body'),
                'user_id' => auth()->id(),
            ])->load('owner');
    }
    .
    .

再次运行测试:
file

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 0
发起讨论 只看当前版本


暂无话题~