How to test variable from controller

I have function:

    /**
     * Password forgot method
     *
     * @return \Cake\Http\Response|null|void Renders view
     */
    public function passwordForgot()
    {
        $user = new UserPasswordForgotForm();
        if ($this->request->is('post')) {
            if ($user->execute($this->request->getData())) {
                $user_data = $this->Users->find('userData', options: [
                    'column' => 'email',
                    'email' => $this->request->getData('email'),
                ])
                    ->first();

                $user_data = $this->Users->patchEntity($user_data, [
                    'password_validity' => DateTime::now()->addHours(1),
                ]);

                if ($this->Users->save($user_data)) {
                    $this->getMailer('User')->send('passwordForgot', [$user_data]);

                    $validityTime = $user_data->password_validity ?? null;
                    $validityTimeMessage = $validityTime?->format('d.m.Y H:i:s');

                    $this->Flash->success(__x(
                        'Validation',
                        'The password can be changed to {0}.',
                        $validityTimeMessage
                    ));

                    return $this->redirect([
                        'action' => 'login',
                    ]);
                }
            }

            $this->Flash->error(__x('Validation', 'Error.'));
        }

        $this->set(compact('user'));
    }

And test is:

    /**
     * Test passwordForgot method
     *
     * @return void
     * @uses \App\Controller\Pages\Web\AuthController::passwordForgot()
     */
    public function testPasswordForgot(): void
    {
        $this->enableCsrfToken();
        $this->enableSecurityToken();

        // Post data
        $postData = [
            'email' => 'valid@example.com',
        ];

        // Check form post data
        $this->assertTrue($this->UserPasswordForgot->execute($postData));

        // Post
        $this->post([
            'prefix' => 'Pages/Web',
            'controller' => 'Auth',
            'action' => 'passwordForgot',
        ], $postData);

        // Check for a successful response
        $this->assertResponseSuccess();

        // Check send mail to
        $this->assertMailSentTo('valid@example.com');

        // Redirect
        $expectedRedirect = [
            'prefix' => 'Pages/Web',
            'controller' => 'Auth',
            'action' => 'login',
        ];
        $this->assertRedirect($expectedRedirect);
    }

I would also like to add a test of the resulting message - the $validityTimeMessage variable
I would like to test the resulting message - Flash.

Thank you

https://book.cakephp.org/4/en/development/testing.html#testing-flash-messages

1 Like

I looked at that, but I don’t know how to test the time that the controller creates.
Do I have to define that time in the test? Can’t it be taken from the controller?

You also just do

$this->assertResponseContains('The password can be changed to');
1 Like

To do tests using times, see setTestNow.

1 Like