###前提・実現したいこと
- Cakephp3.4.9
- PHPUnit 6.3.1
usersテーブルに保存されているユーザーデータ(id,email,password)を持ってきて、
ログインしていないとログイン画面にリダイレクトされるテストをしたい。
###発生している問題・エラーメッセージ
php
1 // テスト 2 $this->session( 3 [ 4 'Auth' => [ 5 'User' => [ 6 'id' => 1, 7 'user_id' => 'test', 8 'email' => 'test@test.com', 9 'password' => 'test' 10 ] 11 ] 12 ] 13 ); 14 $this->get('/test/test'); 15 $this->assertResponseOk();
で、どんなデータを入れてもログインに成功(リダイレクトされず、assertResponseOkが通る)してしまう。
実際のブラウザでは当然間違ったログインは弾かれるようにしています。
php
1 // コントローラ 2 $this->loadComponent('Auth',[ 3 'authorize' => 'Controller', 4 'authenticate' => [ 5 'Form' => [ 6 'fields' => [ 7 'username' => 'email', 8 'password' => 'password' 9 ] 10 ] 11 ], 12 'loginAction' => [ 13 'controller' => 'CONTROLLER', 14 'action' => 'login' 15 ], 16 'loginRedirect' => [ 17 'controller' => 'CONTROLLER', 18 'action' => 'index' 19 ], 20 'logoutRedirect' => [ 21 'controller' => 'CONTROLLER', 22 'action' => 'login', 23 ] 24 ]);
追記
UsersFixture
php
1 public $records = [ 2 [ 3 'id' => 1, 4 'user_id' => 'test', 5 'email' => 'test@test.co.jp', 6 'password' => 'test', 7 'deleted' => 0 8 ], 9 ];
test
php
1 public $fixtures = [ 2 'app.users' 3 ];
loginAction
php
1 public function login() 2 { 3 if($this->request->is('post')){ 4 $user = $this->Auth->identify(); 5 if($user){ 6 $this->Auth->setUser($user); 7 return $this->redirect($this->Auth->redirectUrl()); 8 } 9 $this->Flash->error('ユーザー名かパスワードが間違ってます.'); 10 } 11 }
と読み込んでおり、ログイン画面にPostしたところ、リダイレクトは通ります($this->assertResponseSuccessで判定、実際はログイン成功時はindexへ、失敗時はloginへ)、assertSessionの値が合いません。
つまりログインできていないのでしょうか?
また、
php
1$this->assertRedirect(['controller' => 'CONTROLLER', 'action' => 'index']);
がNo location header set.となってしまうため、リダイレクト先が正しいかの判断ができません。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/28 04:52
2017/09/28 04:55 編集
2017/09/28 05:43
2017/09/28 05:48
2017/09/28 05:55