我在 Laravel 5.7 应用程序中使用配置文件页面(具有“配置文件 View ”页面和“配置文件详细信息”编辑器)进行 http 测试,例如:
$newUser->save();
$testing_user_id = $newUser->id;
$newUserGroup = new UserGroup();
$newUserGroup->group_id = USER_ACCESS_USER;
$newUserGroup->user_id = $newUser->id;
$userGroup= Group::find($newUserGroup->group_id);
$newUserSessionData = [
[
'loggedUserAccessGroups' => ['group_id' => $newUserGroup->group_id, 'group_name' => !empty($userGroup) ? $userGroup->name : ''],
'logged_user_ip' => '0',
]
];
$newUserGroup->save();
// 3. OPEN PROFILE PAGE BLOCK START
$response = $this->actingAs($newUser)
->withSession($newUserSessionData)
->get('/profile/view');
// 3. OPEN PROFILE PAGE BLOCK END
// 4. MAKING CHECKING PROFILE FOR USER CREATED AT 2) BLOCK START
$response->assertStatus(200); // to use HTTP_RESPONSE_OK
$response->assertSee(htmlspecialchars("Profile : " . $newUser->username, ENT_QUOTES));
// 4. MAKING CHECKING PROFILE FOR USER CREATED AT 2) BLOCK END
// 5. OPEN PROFILE DETAILS VIEW PAGE BLOCK START
$response = $this->actingAs($newUser)
->withSession($newUserSessionData)
->get('profile/view');
$response->assertStatus(200);
$response->assertSee(htmlspecialchars("Profile : " . $newUser->username, ENT_QUOTES));
// 5. OPEN PROFILE DETAILS VIEW PAGE BLOCK END
// 6. OPEN PROFILE DETAILS EDITOR PAGE BLOCK START
$response = $this->actingAs($newUser)
->withSession($newUserSessionData)
->get('profile/edit-details'); // http://local-votes.com/profile/edit-details
$response->assertStatus(HTTP_RESPONSE_OK);
$response->assertSee(htmlspecialchars("Profile : Details"));
// 6. OPEN PROFILE DETAILS EDITOR PAGE BLOCK END
// 7. MODIFY PROFILE DETAILS PAGE BLOCK START
$response = $this->actingAs($newUser)
->withSession($newUserSessionData)
->post('profile/edit-details-post', [
'first_name' => 'Modified : ' . $newUser->first_name,
'last_name' => 'Modified : ' . $newUser->last_name,
'phone' => 'Modified : ' . $newUser->phone,
'website' => 'Modified : ' . $newUser->website,
'_token' => $csrf_token
]);
// $response->assertStatus(205); // ???
// 7. MODIFY PROFILE DETAILS PAGE BLOCK END
////////////////////////
// 8. OPEN PROFILE DETAILS VIEW PAGE AFTER MODIFICATIONS BLOCK START
$response = $this->actingAs($newUser)
->withSession($newUserSessionData)
->get('profile/view');
$response->assertStatus(200);
$response->assertSee( htmlspecialchars('Modified : ' . $newUser->last_name) );
// 8. OPEN PROFILE DETAILS VIEW PAGE AFTER MODIFICATIONS BLOCK END
已添加新用户并且可以正常打开“个人资料 View ”页面,但在步骤//7. 修改个人资料详细信息页面 block 开始时出现问题 正如我在 sql 跟踪中看到的那样,新用户已插入但未更新。
在我的控件 app/Http/Controllers/ProfileController.php 中,更新方法是使用验证请求定义的:
public function update_details(ProfileUserDetailsRequest $request)
{
$userProfile = Auth::user();
$requestData = $request->all();
$userProfile->first_name = $requestData['first_name'];
$userProfile->last_name = $requestData['last_name'];
$userProfile->phone = $requestData['phone'];
$userProfile->website = $requestData['website'];
$userProfile->updated_at = now();
$userProfile->save();
$this->setFlashMessage('Profile updated successfully !', 'success', 'Profile');
return Redirect::route('profile-view');
} // public function update_details(ProfileUserDetailsRequest $request)
1) 原因是否在 ProfileUserDetailsRequest 中以及如何处理? 我的个人资料详细信息编辑器工作正常。
我的路线定义为:
Route::group(array('prefix' => 'profile', 'middleware' => ['auth', 'isVerified']), function(){
Route::post('edit-details-post', array(
'as' => 'profile-edit-details-post',
'uses' => 'ProfileController@update_details'
));
起初我尝试了 PUT,但之后我尝试了 POST - 同样没有结果。
2) 您能否建议一些正确的方法来检查用户个人资料详细信息//7. 修改个人资料详细信息页面 block 开始步骤?
修改 block #2:
我尝试过patch的方法,但是还是不行。 我的 app/Http/Controllers/ProfileController.php 中有调试方法
public function update_details(ProfileUserDetailsRequest $request)
{
方法,当测试运行时,我发现它没有被触发。 我使用相同的 update_details 方法在 brauser 中更新我的表单,它工作正常(我也看到调试信息)。
我想这可能是 csrf 问题,在我的测试文件标题中我写道:
<?php
namespace Tests\Feature;
use Tests\TestCase;
use DB;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\WithoutMiddleware; // Prevent all middleware from being executed for this test class.
public function testProfilePage()
{
$csrf_token = csrf_token();
...
$response = $this->actingAs($newUser)
->withSession($newUserSessionData)
->patch('profile/edit-details-post', [
'first_name' => 'Modified : ' . $newUser->first_name,
'last_name' => 'Modified : ' . $newUser->last_name,
'phone' => 'Modified : ' . $newUser->phone,
'website' => 'Modified : ' . $newUser->website,
// '_token' => $csrf_token / I TRIED TO UNCOMMENT THIS LINE TOO
]);
$response->assertStatus(205); // making this check I see that code 419 was returned
可以决定添加文件 app/Http/Middleware/VerifyCsrfToken.php
class VerifyCsrfToken extends Middleware
{
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected $except = [
// ???
];
如果在命令行中我运行测试
vendor/bin/phpunit tests/Feature/ProfilepageTest.php
除了什么我还需要添加什么?
谢谢!
最佳答案
除了使用 PATCH 而不是 POST 之外,以下是我测试代码的建议。
尝试使用简单的请求,而不是 ProfileUserDetailsRequest。
尝试在更新函数中记录 $request 变量,并检查 _token 和 _method 是否可用,方法应该是 PATCH 并且请求变量是否正确发布。
创建用户表模型并尝试使用该模型更新用户
$user = User::find(Auth::user()->id);
…
$user->save();
session 是通过中间件启动的,我认为如果您禁用中间件, session 将无法工作。
关于测试更新用户配置文件数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52868050/