Skip to content

Commit

Permalink
Upgrade to CakePHP 5 - Fixing types and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rochamarcelo committed Jan 9, 2023
1 parent f8d2463 commit c827746
Show file tree
Hide file tree
Showing 43 changed files with 78 additions and 71 deletions.
1 change: 0 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<issueHandlers>
<LessSpecificReturnType errorLevel="info" />
<RedundantConditionGivenDocblockType errorLevel="info" />
<TypeCoercion errorLevel="info" />
<DocblockTypeContradiction errorLevel="info" />
</issueHandlers>
</psalm>
2 changes: 1 addition & 1 deletion src/Controller/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class AppController extends BaseController
public function initialize(): void
{
parent::initialize();
$this->loadComponent('Security');
$this->loadComponent('FormProtection');
if ($this->request->getParam('_csrfToken') === false) {
$this->loadComponent('Csrf');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/Traits/PasswordManagementTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,6 @@ public function resetOneTimePasswordAuthenticator($id = null)
}
}

return $this->redirect($this->getRequest()->referer());
return $this->redirect($this->getRequest()->referer() ?? '/');
}
}
5 changes: 3 additions & 2 deletions tests/Fixture/UsersFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ public function init(): void
'type' => 'Webauthn\TrustPath\EmptyTrustPath',
],
'aaguid' => '00000000-0000-0000-0000-000000000000',
'credentialPublicKey' => Base64Url::encode('000000000000000000000000000000000000-9999999999999999999999999999999999999999-XXXXXXXXXXXXX-ZZZZZZZZZZZ'),
'userHandle' => Base64Url::encode('00000000-0000-0000-0000-000000000001'),
//add this when migrated webauthn
// 'credentialPublicKey' => Base64Url::encode('000000000000000000000000000000000000-9999999999999999999999999999999999999999-XXXXXXXXXXXXX-ZZZZZZZZZZZ'),
// 'userHandle' => Base64Url::encode('00000000-0000-0000-0000-000000000001'),
'counter' => 190,
'otherUI' => null,
],
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Authenticator/SocialAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*/
class SocialAuthenticatorTest extends TestCase
{
public $fixtures = [
protected array $fixtures = [
'plugin.CakeDC/Users.Users',
'plugin.CakeDC/Users.SocialAccounts',
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

class SocialPendingEmailAuthenticatorTest extends TestCase
{
public $fixtures = [
protected array $fixtures = [
'plugin.CakeDC/Users.Users',
'plugin.CakeDC/Users.SocialAccounts',
];
Expand Down
14 changes: 7 additions & 7 deletions tests/TestCase/Controller/SocialAccountsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use Cake\Core\Configure;
use Cake\Http\ServerRequest;
use Cake\Mailer\Email;
use Cake\Mailer\Mailer;
use Cake\Mailer\TransportFactory;
use Cake\TestSuite\TestCase;

Expand All @@ -26,7 +26,7 @@ class SocialAccountsControllerTest extends TestCase
*
* @var array
*/
public $fixtures = [
protected array $fixtures = [
'plugin.CakeDC/Users.SocialAccounts',
'plugin.CakeDC/Users.Users',
];
Expand All @@ -46,9 +46,9 @@ public function setUp(): void
Configure::write('Users.RememberMe.active', false);

TransportFactory::setConfig('test', ['className' => 'Debug']);
$this->configEmail = Email::getConfig('default');
Email::drop('default');
Email::setConfig('default', [
$this->configEmail = Mailer::getConfig('default');
Mailer::drop('default');
Mailer::setConfig('default', [
'transport' => 'test',
'from' => '[email protected]',
]);
Expand All @@ -69,9 +69,9 @@ public function setUp(): void
*/
public function tearDown(): void
{
Email::drop('default');
Mailer::drop('default');
TransportFactory::drop('test');
//Email::setConfig('default', $this->configEmail);
//Mailer::setConfig('default', $this->configEmail);

Configure::write('Opauth', $this->configOpauth);
Configure::write('Users.RememberMe.active', $this->configRememberMe);
Expand Down
16 changes: 9 additions & 7 deletions tests/TestCase/Controller/Traits/BaseTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
use Cake\Controller\ComponentRegistry;
use Cake\Controller\Controller;
use Cake\Event\Event;
use Cake\Mailer\Email;
use Cake\Http\ServerRequest;
use Cake\Mailer\Mailer;
use Cake\Mailer\TransportFactory;
use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
Expand All @@ -42,7 +43,7 @@ abstract class BaseTraitTest extends TestCase
*
* @var array
*/
public $fixtures = [
protected array $fixtures = [
'plugin.CakeDC/Users.Users',
];

Expand Down Expand Up @@ -85,6 +86,7 @@ public function setUp(): void
try {
$this->Trait = $this->getMockBuilder($this->traitClassName)
->setMethods($traitMockMethods)
->setConstructorArgs([new ServerRequest()])
->getMock();
$this->Trait->expects($this->any())
->method('getUsersTable')
Expand All @@ -98,9 +100,9 @@ public function setUp(): void
TransportFactory::setConfig('test', [
'className' => 'Debug',
]);
$this->configEmail = Email::getConfig('default');
Email::drop('default');
Email::setConfig('default', [
$this->configEmail = Mailer::getConfig('default');
Mailer::drop('default');
Mailer::setConfig('default', [
'transport' => 'test',
'from' => '[email protected]',
]);
Expand All @@ -116,9 +118,9 @@ public function tearDown(): void
{
unset($this->table, $this->Trait);
if ($this->mockDefaultEmail) {
Email::drop('default');
Mailer::drop('default');
TransportFactory::drop('test');
//Email::setConfig('default', $this->setConfigEmail);
//Mailer::setConfig('default', $this->setConfigEmail);
}
parent::tearDown();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CakeDC\Users\Test\TestCase\Controller\Traits;

use Cake\Http\ServerRequest;
use Cake\ORM\Table;
use Cake\TestSuite\TestCase;

Expand All @@ -23,6 +24,7 @@ public function setUp(): void
parent::setUp();
$this->controller = $this->getMockBuilder('Cake\Controller\Controller')
->setMethods(['header', 'redirect', 'render', '_stop'])
->setConstructorArgs([new ServerRequest()])
->getMock();
$this->controller->Trait = $this->getMockForTrait('CakeDC\Users\Controller\Traits\CustomUsersTableTrait');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class LoginTraitIntegrationTest extends TestCase
*
* @var array
*/
public $fixtures = [
protected array $fixtures = [
'plugin.CakeDC/Users.Users',
];

Expand Down Expand Up @@ -72,6 +72,7 @@ public function testLoginGetRequestNoSocialLogin()
});

$this->get('/login');
file_put_contents(TMP . 'testLoginGetRequestNoSocialLogin.html', $this->_response->getBody());
$this->assertResponseOk();
$this->assertResponseNotContains('Username or password is incorrect');
$this->assertResponseContains('<form method="post" accept-charset="utf-8" action="/login">');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PasswordManagementTraitIntegrationTest extends TestCase
*
* @var array
*/
public $fixtures = [
protected array $fixtures = [
'plugin.CakeDC/Users.Users',
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ProfileTraitIntegrationTest extends TestCase
*
* @var array
*/
public $fixtures = [
protected array $fixtures = [
'plugin.CakeDC/Users.Users',
'plugin.CakeDC/Users.SocialAccounts',
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class RegisterTraitIntegrationTest extends TestCase
*
* @var array
*/
public $fixtures = [
protected array $fixtures = [
'plugin.CakeDC/Users.Users',
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SimpleCrudTraitIntegrationTest extends TestCase
*
* @var array
*/
public $fixtures = [
protected array $fixtures = [
'plugin.CakeDC/Users.Users',
'plugin.CakeDC/Users.SocialAccounts',
];
Expand Down
6 changes: 3 additions & 3 deletions tests/TestCase/Controller/Traits/LinkSocialTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Cake\Http\Response;
use Cake\Http\ServerRequest;
use Cake\Http\ServerRequestFactory;
use Cake\I18n\FrozenTime;
use Cake\I18n\DateTime;
use Cake\ORM\TableRegistry;
use Laminas\Diactoros\Uri;
use League\OAuth2\Client\Provider\FacebookUser;
Expand All @@ -30,7 +30,7 @@ class LinkSocialTraitTest extends BaseTraitTest
*
* @var array
*/
public $fixtures = [
protected array $fixtures = [
'plugin.CakeDC/Users.SocialAccounts',
'plugin.CakeDC/Users.Users',
];
Expand Down Expand Up @@ -274,7 +274,7 @@ public function testCallbackLinkSocialHappy()

$actual = $Table->SocialAccounts->find('all')->where(['reference' => '9999911112255'])->firstOrFail();

$expiresTime = new FrozenTime();
$expiresTime = new DateTime();
$tokenExpires = $expiresTime->setTimestamp($Token->getExpires())->format('Y-m-d H:i:s');

$expected = [
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Controller/Traits/LoginTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Authentication\Authenticator\SessionAuthenticator;
use Authentication\Identifier\IdentifierCollection;
use Authentication\Identifier\PasswordIdentifier;
use Cake\Auth\DefaultPasswordHasher;
use Authentication\PasswordHasher\DefaultPasswordHasher;
use Cake\Controller\ComponentRegistry;
use Cake\Event\Event;
use Cake\Http\Response;
Expand Down Expand Up @@ -134,7 +134,7 @@ public function testLoginHappy()
$this->assertSame($passwordBefore, $passwordAfter);
$lastLoginAfter = $userAfter['last_login'];
$this->assertNotEmpty($lastLoginAfter);
$now = \Cake\I18n\FrozenTime::now();
$now = \Cake\I18n\DateTime::now();
$this->assertEqualsWithDelta($lastLoginAfter->timestamp, $now->timestamp, 2);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function setUp(): void
$request = new ServerRequest();
$this->Trait = $this->getMockBuilder($this->traitClassName)
->setMethods(['dispatchEvent', 'redirect', 'set', 'getUsersTable'])
->setConstructorArgs([new ServerRequest()])
->getMock();

$this->Trait->setRequest($request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace CakeDC\Users\Test\TestCase\Controller\Traits;

use Cake\Auth\PasswordHasherFactory;
use Authentication\PasswordHasher\PasswordHasherFactory;
use Cake\Core\Configure;
use Cake\Event\Event;
use Cake\TestSuite\TestCase;
Expand Down Expand Up @@ -72,7 +72,7 @@ public function testChangePasswordHappy()
->method('success')
->with('Password has been changed successfully');
$this->Trait->changePassword();
$hasher = PasswordHasherFactory::build('Default');
$hasher = PasswordHasherFactory::build('Authentication.Default');
$this->assertTrue($hasher->check('new', $this->table->get('00000000-0000-0000-0000-000000000001')->password));
}

Expand Down Expand Up @@ -130,7 +130,7 @@ public function testChangePasswordWithAfterChangeEvent()
->method('success')
->with('Password has been changed successfully');
$this->Trait->changePassword();
$hasher = PasswordHasherFactory::build('Default');
$hasher = PasswordHasherFactory::build('Authentication.Default');
$this->assertTrue($hasher->check('new', $this->table->get('00000000-0000-0000-0000-000000000001')->password));
}

Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Controller/Traits/ProfileTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ProfileTraitTest extends BaseTraitTest
*
* @var array
*/
public $fixtures = [
protected array $fixtures = [
'plugin.CakeDC/Users.Users',
'plugin.CakeDC/Users.SocialAccounts',
];
Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase/Controller/Traits/ReCaptchaTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace CakeDC\Users\Test\TestCase\Controller\Traits;

use Cake\Core\Configure;
use Cake\Http\ServerRequest;
use Cake\TestSuite\TestCase;
use ReflectionMethod;

Expand All @@ -29,6 +30,7 @@ public function setUp(): void
parent::setUp();
$this->Trait = $this->getMockBuilder('CakeDC\Users\Controller\Traits\ReCaptchaTrait')
->setMethods(['_getReCaptchaInstance'])
->setConstructorArgs([new ServerRequest()])
->getMockForTrait();
}

Expand Down
1 change: 1 addition & 0 deletions tests/TestCase/Controller/Traits/SocialTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function setUp(): void
$request = new ServerRequest();
$this->Trait = $this->getMockBuilder($this->traitClassName)
->setMethods(['dispatchEvent', 'redirect', 'set', 'loadComponent'])
->setConstructorArgs([new ServerRequest()])
->getMock();

$this->Trait->request = $request;
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Controller/Traits/U2fTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class U2fTraitTest extends BaseTraitTest
*
* @var array
*/
public $fixtures = [
protected array $fixtures = [
'plugin.CakeDC/Users.Users',
];

Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Controller/Traits/Webauthn2faTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Webauthn2faTraitTest extends BaseTraitTest
*
* @var array
*/
public $fixtures = [
protected array $fixtures = [
'plugin.CakeDC/Users.Users',
];

Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Controller/UsersControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class UsersControllerTest extends TestCase
*
* @var array
*/
public $fixtures = [
protected array $fixtures = [
'plugin.CakeDC/Users.Users',
];

Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Identifier/SocialIdentifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

class SocialIdentifierTest extends TestCase
{
public $fixtures = [
protected array $fixtures = [
'plugin.CakeDC/Users.Users',
'plugin.CakeDC/Users.SocialAccounts',
];
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Mailer/UsersMailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class UsersMailerTest extends TestCase
*
* @var array
*/
public $fixtures = [
protected array $fixtures = [
'plugin.CakeDC/Users.SocialAccounts',
'plugin.CakeDC/Users.Users',
];
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Middleware/SocialAuthMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

class SocialAuthMiddlewareTest extends TestCase
{
public $fixtures = [
protected array $fixtures = [
'plugin.CakeDC/Users.Users',
'plugin.CakeDC/Users.SocialAccounts',
];
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Middleware/SocialEmailMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

class SocialEmailMiddlewareTest extends TestCase
{
public $fixtures = [
protected array $fixtures = [
'plugin.CakeDC/Users.Users',
'plugin.CakeDC/Users.SocialAccounts',
];
Expand Down
Loading

0 comments on commit c827746

Please sign in to comment.