[Day 19] 针对网页的单元测试(五)

再写登入的验证及功能

今天我们要来做登入的判断跟动作,
我们在HomeController.php引用

use Validator;

然後新增一个方法

function indexProcess(Request $request)
{
    $input = $request->all();

    //验证规则
    $rules = [
        //帐号
        'account' => [
            'required',
        ],
        //密码
        'password' => [
            'required',
        ],
        //勾选
        'terms' => [
            'accepted',
        ],
    ];

    if($request->has(['terms']))
        $input['terms'] = 1;

    //验证资料
    $validator = Validator::make($input, $rules);

    if($validator->fails())
    {
        //资料验证错误
        return redirect('/home/index')
            ->withInput($input)
            ->withErrors($validator);
    }

    if($input['account'] == 'admin' && $input['password'] == 'admin')
    {
        return redirect('/home/main');
    }
    else
    {
        $validator->errors()->add('account', '帐号或密码错误');
        //资料验证错误
        return redirect('/home/index')
            ->withInput($input)
            ->withErrors($validator);
    }
}

登入页改成这样
resources/views/home/index.blade.php

<!DOCTYPE html>

<html> 
<head> 
	<meta charset="utf-8">
	<title>登入</title> 
    <script src="http://cdn.bootcss.com/jquery/1.11.0/jquery.min.js" type="text/javascript"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" href="/css/app.css">
</head>
<body>

<form id="form1" method="post" action="">
<!-- 自动产生 csrf_token 隐藏栏位-->
{!! csrf_field() !!}
<div class="login_form">
    <div class="login_title">登入</div>
    <a href="/home/about" class="login_label">关於我们</a>
    <div class="login_label">帐号</div>
    <div class="login_textbox">
        <input name="account" class="form_textbox" type="text" value="{{ old('account') }}" placeholder="请输入帐号"/>
    </div>
    <div class="login_label">密码</div>
    <div class="login_textbox">
        <input name="password" class="form_textbox" type="password" value="{{ old('password') }}" placeholder="请输入密码"/>
    </div>
    <div class="login_label">
        <input name="terms" class="form-check-input" type="checkbox"
            @if(old('terms') == 1)
                checked
            @endif
        >
        <span>我同意服务条款</span>
    </div>
    <div class="login_error">
        <!-- 错误讯息模板元件 -->
        @include('layout.ValidatorError')
    </div>
    <div class="btn_group">
        <button type="submit" class="btn btn-success btn_login">登入</button>
    </div>
</div>
</form>

</body>
<html>

然後新增一个首页
resources/views/home/main.blade.php

<!DOCTYPE html>

<html> 
<head> 
	<meta charset="utf-8">
	<title>首页</title> 
    <link rel="stylesheet" href="/css/app.css">
</head>
<body>
<h1>首页</h1>
</body>
<html>

我们在原本的测试项目再新增一个方法,
确认密码输入错误的反应
BrowserUnitTest.php

public function test_login_page_login_error()
{
    $this->visit('/home/index')
        ->type('admin', 'account')
        ->type('123456', 'password')
        ->check('terms')
        ->press('登入')
        ->seePageIs('/home/index');
}

首页做完之後我们再来测试一下我们的成果
php artisan test
https://ithelp.ithome.com.tw/upload/images/20210920/20105694W7WQd5vjIH.png

我们终於完成了阶段性任务!


<<:  Day 6 - Using ASHX File for User Authorization Management with ASP.NET Web Forms C# 使用泛型处理常式进行权限分流

>>:  Day 06. 安装 Zabbix Agent 在 Ubuntu

【设计+切版30天实作】|Day10 - 因应Bootstrap的元件去弹性设计Reviews区块

设计大纲 原本想把Reviews做成一行六个、并且可以超出视窗,另外使用者可以用拖曳的方式去移动卡片...

Kotlin Android 第19天,从 0 到 ML - RecyclerView 动态列表

前言: RecyclerView 可以轻松高效地显示大量数据。 RecyclerView回收这些单独...

Day 0x1 - 动机、目标、目录 (消费支付API)

0x1 动机 只是很单纯的有兴趣,练习看API文件, 试着在最短时间规划并建立出一个目标, 就是一个...

Day24:使i用 ForEach、ScrollView 构建 UI

前言 上篇文章讲了加数据进去, 今天来刻 UI。 实作 添加一些 image 进去 asset: 在...

【Day05】数据输入元件 - Input Text / Text Field

元件介绍 Input 是一个输入元件。通常在我们希望用户能够输入一些资讯的时候会需要用到它。由於原生...