2017-06-19 57 views
0

我正在使用shibboleth apache模块进行联合单点登录。它将$_SERVER变量设置为来自活动目录的用户权利。在我的laravel应用程序中,我使用自定义身份验证和用户提供程序,它们利用这些权限进行资源授权。

我的简化的用户模型是这样的:

public function isAdmin() 
{ 
    return Request::server('entitlement') === 'admin'; 
} 

但是,我无法弄清楚如何测试这一点,因为Request::server始终没有返回该值。

public function setUp() 
{ 
    $_SERVER['entitlement'] = 'admin'; 
    parent::setUp(); 
} 

public function test_admin_something() 
{ 
    $user = factory(User::class)->create(); 

    $response = $this 
     ->actingAs($user) 
     ->get('/admin/somewhere'); 

    var_dump($_SERVER['entitlement']);  // string(5) "admin" 
    var_dump(Request::server('entitlement')); // NULL 

    $response->assertStatus(200); // always fails 403 
} 

我也试过setUpBeforeClass并检查所有出现在代替自定义制作Request对象的测试过程中被忽略其它服务器变量。我也不能嘲笑请求façade,per the documentation

+1

尝试改变' - >获取( '/管理/某处')'来' - >调用( 'GET', '/管理/某处', ['entitlement'=>'admin']);'。请参阅:https://laravel.com/docs/5.2/testing#custom-http-requests – btl

+0

您正在使用哪个测试套件以及哪个版本。 – Ohgodwhy

+0

@Ohgodwhy phpunit 5.7.20 laravel 5.4.24 –

回答

0

挖掘到the source code揭示了一个未公开的方法withServerVariables

public function test_admin_something() 
{ 
    $user = factory(User::class)->create(); 

    $response = $this 
     ->withServerVariables(['entitlement' => 'admin']) 
     ->actingAs($user) 
     ->get('/admin/somewhere'); 

    $response->assertStatus(200); 
}