B
B
bearenok2018-09-20 07:55:04
Functional testing
bearenok, 2018-09-20 07:55:04

Why does Yii2 from functional tests not find the user's identity after login?

I'm doing a functional test:

<?php

namespace frontend\tests\functional;

use common\fixtures\UserRequestFixtures;
use frontend\tests\FunctionalTester;

class ConfirmEmailCest
{
    public function _fixtures()
    {
        return [
            'userRequest' => [
                'class' => UserRequestFixtures::class,
                'dataFile' => codecept_data_dir() . 'user_request_data.php'
            ],
        ];
    }

    public function testConfirmEmail(FunctionalTester $I)
    {
        $I->amOnRoute('main/user/confirm-email',['token'=>'BADVYbYFO2HqHADmDXjoGHS4as9Y_Dtq']);
        $I->see('Подтверждение', 'h1');
        $I->see('Продолжить','form button');
        $I->click('Продолжить');
        $I->seeRecord('common\models\User', [
            'email' => '[email protected]',
        ]);
    }
}

During the execution of the script, the user is created (login) and redirected to the main page. But after the redirect, the script crashes with an error because one of the classes in my code tries to get Yii::$app->user->id but gets null.
In the console I see:
1) ConfirmEmailCest: Test confirm email
 Test  tests\functional\ConfirmEmailCest.php:testConfirmEmail

  [yii\base\ErrorException] Trying to get property of non-object

Scenario Steps:

 4. $I->click("Продолжить") at tests\functional\ConfirmEmailCest.php:31
 3. $I->see("Продолжить","form button") at tests\functional\ConfirmEmailCest.php:30
 2. $I->see("Подтверждение","h1") at tests\functional\ConfirmEmailCest.php:29
 1. $I->amOnRoute("main/user/confirm-email",{"token":"BADVYbYFO2HqHADmDXjoGHS4as9Y_Dtq"}) at tests\functional\ConfirmEmailCest.php:28

C:\OSPanel\domains\site.local\frontend\modules\cabinet\models\DocumentsSearch.php:225

C:\OSPanel\domains\site.local\frontend\modules\cabinet\models\DocumentsSearch.php:225 - this is where Yii::$app->user->id is accessed and the error is due to its null values.
Tell me, what could be the problem? What to check in test/app configuration? In another functional test to check application login (standard one from yii2-advanced repository) had the same error, Login occurs. Yii::$app->user->isGuest === false but the user's identity model is not found.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bearenok, 2018-09-20
@bearenok

The test execution log did not fit into the question:

[TransactionForcer] Transaction cancelled; all changes reverted.                                                                                
[TransactionForcer] no longer watching new connections                                                                                          
[ConnectionWatcher] no longer watching new connections                                                                                          
[ConnectionWatcher] closing all (2) connections                                                                                                 
[yii\db\Connection::open] 'Opening DB connection: mysql:host=localhost;dbname=test'                                                    
Destroying application                                                                                                                          
nfirmEmailCest: Test confirm email                                                                                                              
gnature: frontend\tests\functional\ConfirmEmailCest:testConfirmEmail                                                                            
st: tests\functional\ConfirmEmailCest.php:testConfirmEmail                                                                                      
enario --                                                                                                                                       
Destroying application                                                                                                                          
Starting application                                                                                                                            
[ConnectionWatcher] watching new connections                                                                                                    
[Fixtures] Loading fixtures                                                                                                                     
[ConnectionWatcher] watching new connections                                                                                                    
[yii\db\Connection::open] 'Opening DB connection: mysql:host=localhost;dbname=test'                                                    
[ConnectionWatcher] Connection opened!                                                                                                          
[ConnectionWatcher] Connection opened!                                                                                                          
[ConnectionWatcher] no longer watching new connections                                                                                          
[ConnectionWatcher] closing all (1) connections                                                                                                 
[Fixtures] Done                                                                                                                                 
[TransactionForcer] watching new connections                                                                                                    
 am on route "main/user/confirm-email",{"token":"BADVYbYFO2HqHADmDXjoGHS4as9Y_Dtq"}                                                             
[Request Headers] []                                                                                                                            
[yii\db\Connection::open] 'Opening DB connection: mysql:host=localhost;dbname=test'                                                    
[ConnectionWatcher] Connection opened!                                                                                                          
[TransactionForcer] Connection opened!                                                                                                          
[TransactionForcer] Transaction started for: mysql:host=localhost;dbname=test                                                          
[yii\web\Session::open] 'Session started'                                                                                                       
[Page] /index-test.php?r=main%2Fuser%2Fconfirm-email&token=BADVYbYFO2HqHADmDXjoGHS4as9Y_Dtq                                                     
[Response] 200                                                                                                                                  
[Request Cookies] []                                                                                                                            
[Response Headers] {"content-type":["text/html; charset=UTF-8"]}                                                                                
 see "Подтверждение","h1"                                                                                                                       
 see "Продолжить","form button"                                                                                                                 
 click "Продолжить"                                                                                                                             
[Uri] http://localhost/index-test.php?r=main%2Fuser%2Fconfirm-email&token=BADVYbYFO2HqHADmDXjoGHS4as9Y_Dtq                                      
[Method] POST                                                                                                                                   
[Parameters] {"_csrf-frontend":"cw5_rV1Ujp8_PwGr3WNtq8-7F0-VMWKDMflEj_Z7l90mQybEN3n-0W1oZZyvOTvHvPAgYsJlJMlaoBDdr0r1sw=="}                      
[Request Headers] []                                                                                                                            
[yii\mail\BaseMailer::send] 'Sending email "Зрагеистрировался новый ползователь" to "[email protected]"'                                          
[yii\mail\BaseMailer::send] 'Sending email "Вы успешно зарегистрировались на сайте" to "[email protected]"'                     
[yii\web\User::login] 'User \'26\' logged in from  with duration 0.'                                                                            
[Redirect with headers]Array                                                                                                                    
(                                                                                                                                               
    [location] => Array                                                                                                                         
        (                                                                                                                                       
            [0] => http://localhost/index-test.php?r=cabinet%2Fdefault%2Findex                                                                  
        )                                                                                                                                       
                                                                                                                                                
    [content-type] => Array                                                                                                                     
        (                                                                                                                                       
            [0] => text/html; charset=UTF-8                                                                                                     
        )                                                                                                                                       
                                                                                                                                                
)                                                                                                                                               
                                                                                                                                                
[Page] http://localhost/index-test.php?r=main%2Fuser%2Fconfirm-email&token=BADVYbYFO2HqHADmDXjoGHS4as9Y_Dtq                                     
[Response] 302                                                                                                                                  
[Request Cookies] {"utms":"2f9c854af2d11befdcaab97edb9740fa46f410a99721dbbbcc97fa601f01be71a:2:{i:0;s:4:"utms";i:1;s:206:"a:1:{s:178:"a:6:{i:0;s
tp://localhost/index-test.php?r=main%2Fuser%2Fconfirm-email&token=BADVYbYFO2HqHADmDXjoGHS4as9Y_Dtq";}";i:1537418982;}";}","_csrf-frontend":"1143
0;s:14:"_csrf-frontend";i:1;s:32:"UMYij-pNRWd7rZVlsK7-WTFJkYTRY1bn";}"}                                                                         
[Response Headers] {"location":["http://localhost/index-test.php?r=cabinet%2Fdefault%2Findex"],"content-type":["text/html; charset=UTF-8"]}     
[Redirecting to] http://localhost/index-test.php?r=cabinet%2Fdefault%2Findex                                                                    
RROR

well, as usual, I decided it myself:
the problem was in the call to Yii::$app->user->id in the init() method of one of the plug-in components. It turns out that when moving between pages, the application instance seems to remain unchanged and the components are not re-initialized, well, or otherwise, correct it if you are not too lazy. Well, I went to study the process of performing functional tests ..

Similar questions

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question