Skip to content

Commit

Permalink
feat(tests): logged in user is now always logged out by base test class
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalsem committed Nov 5, 2021
1 parent 3ec2d61 commit c1a9736
Show file tree
Hide file tree
Showing 73 changed files with 25 additions and 400 deletions.
3 changes: 3 additions & 0 deletions engine/tests/classes/Elgg/BaseTestCase.php
Expand Up @@ -182,6 +182,9 @@ protected function tearDown(): void {
$this->assertFalse((bool) _elgg_services()->session->getDisabledEntityVisibility(), __METHOD__ . ': hidden entities not reset');

// Tests should run without a logged in user
if (_elgg_services()->session->isLoggedIn()) {
_elgg_services()->session->removeLoggedInUser();
}
$this->assertFalse((bool) _elgg_services()->session->isLoggedIn(), __METHOD__ . ': there should be no logged in user');

// cleanup admin user
Expand Down
Expand Up @@ -31,22 +31,6 @@ public function up() {

$this->group = $this->createGroup();
}

/**
* {@inheritDoc}
* @see \Elgg\BaseTestCase::down()
*/
public function down() {
if ($this->group) {
$this->group->delete();
}

if ($this->owner) {
$this->owner->delete();
}

elgg()->session->removeLoggedInUser();
}

/**
* Get the type of the content being gated behind the group tool option
Expand Down Expand Up @@ -88,8 +72,6 @@ public function testUserCantWriteContentWithToolEnabled() {
$this->assertTrue($this->group->enableTool($this->getGroupToolOption()));

$this->assertFalse($this->group->canWriteToContainer($user->guid, $this->getContentType(), $this->getContentSubtype()));

$user->delete();
}

public function testUserCantWriteContentWithToolDisabled() {
Expand All @@ -98,8 +80,6 @@ public function testUserCantWriteContentWithToolDisabled() {
$this->assertTrue($this->group->disableTool($this->getGroupToolOption()));

$this->assertFalse($this->group->canWriteToContainer($user->guid, $this->getContentType(), $this->getContentSubtype()));

$user->delete();
}

public function testGroupMemberCanWriteContentWithToolEnabled() {
Expand All @@ -109,8 +89,6 @@ public function testGroupMemberCanWriteContentWithToolEnabled() {
$this->assertTrue($this->group->enableTool($this->getGroupToolOption()));

$this->assertTrue($this->group->canWriteToContainer($user->guid, $this->getContentType(), $this->getContentSubtype()));

$user->delete();
}

public function testGroupMemberCantWriteContentWithToolDisabled() {
Expand All @@ -120,8 +98,6 @@ public function testGroupMemberCantWriteContentWithToolDisabled() {
$this->assertTrue($this->group->disableTool($this->getGroupToolOption()));

$this->assertFalse($this->group->canWriteToContainer($user->guid, $this->getContentType(), $this->getContentSubtype()));

$user->delete();
}

public function testAdminCanWriteContentWithToolEnabled() {
Expand All @@ -130,8 +106,6 @@ public function testAdminCanWriteContentWithToolEnabled() {
$this->assertTrue($this->group->enableTool($this->getGroupToolOption()));

$this->assertTrue($this->group->canWriteToContainer($user->guid, $this->getContentType(), $this->getContentSubtype()));

$user->delete();
}

public function testAdminCantWriteContentWithToolDisabled() {
Expand All @@ -140,8 +114,6 @@ public function testAdminCantWriteContentWithToolDisabled() {
$this->assertTrue($this->group->disableTool($this->getGroupToolOption()));

$this->assertFalse($this->group->canWriteToContainer($user->guid, $this->getContentType(), $this->getContentSubtype()));

$user->delete();
}

public function testAdminMemberCantWriteContentWithToolDisabled() {
Expand All @@ -151,7 +123,5 @@ public function testAdminMemberCantWriteContentWithToolDisabled() {
$this->assertTrue($this->group->disableTool($this->getGroupToolOption()));

$this->assertFalse($this->group->canWriteToContainer($user->guid, $this->getContentType(), $this->getContentSubtype()));

$user->delete();
}
}
Expand Up @@ -16,10 +16,6 @@ public function up() {
_elgg_services()->session->setLoggedInUser($this->getAdmin());
}

public function down() {
_elgg_services()->session->removeLoggedInUser();
}

public function testDeletesSingleAdminNotice() {

elgg_delete_admin_notice('zen');
Expand Down Expand Up @@ -64,5 +60,4 @@ public function testDeletesBatchAdminNotices() {

$this->assertEquals(0, $count_zens());
}

}
}
Expand Up @@ -17,11 +17,7 @@ class UpgradeTest extends ActionResponseTestCase {
public function up() {
_elgg_services()->session->setLoggedInUser($this->getAdmin());
}

public function down() {
_elgg_services()->session->removeLoggedInUser();
}


public function testUpgradeFailsWithInvalidUpgradeEntity() {
$this->expectException(EntityNotFoundException::class);
$this->executeAction('admin/upgrade', [
Expand Down
Expand Up @@ -31,13 +31,6 @@ public function up() {
_elgg_services()->session->setLoggedInUser($this->user);
}

/**
* {@inheritDoc}
*/
public function down() {
_elgg_services()->session->removeLoggedInUser();
}

public function testFailsWithMissingEntityGUID() {
$response = $this->executeAction('entity/mute');

Expand Down
Expand Up @@ -33,13 +33,6 @@ public function up() {
_elgg_services()->session->setLoggedInUser($this->user);
}

/**
* {@inheritDoc}
*/
public function down() {
_elgg_services()->session->removeLoggedInUser();
}

public function testFailsWithMissingEntityGUID() {
$response = $this->executeAction('entity/subscribe');

Expand Down
Expand Up @@ -30,14 +30,7 @@ public function up() {

_elgg_services()->session->setLoggedInUser($this->user);
}

/**
* {@inheritDoc}
*/
public function down() {
_elgg_services()->session->removeLoggedInUser();
}


public function testFailsWithMissingEntityGUID() {
$response = $this->executeAction('entity/unmute');

Expand Down
Expand Up @@ -31,13 +31,6 @@ public function up() {
_elgg_services()->session->setLoggedInUser($this->user);
}

/**
* {@inheritDoc}
*/
public function down() {
_elgg_services()->session->removeLoggedInUser();
}

public function testFailsWithMissingEntityGUID() {
$response = $this->executeAction('entity/unsubscribe');

Expand Down
Expand Up @@ -51,8 +51,6 @@ public function testLoginWithUsernameAndPassword() {
$this->assertEquals(elgg_echo('loginok', [], $user->language), array_shift($messages['success']));

$this->assertEquals($user, _elgg_services()->session->getLoggedInUser());

_elgg_services()->session->removeLoggedInUser();
}

public function testLoginWithEmailAndPassword() {
Expand All @@ -72,8 +70,6 @@ public function testLoginWithEmailAndPassword() {
$this->assertInstanceOf(OkResponse::class, $response);

$this->assertEquals($user, _elgg_services()->session->getLoggedInUser());

_elgg_services()->session->removeLoggedInUser();
}

public function testLoginFailsWithEmptyPassword() {
Expand Down Expand Up @@ -328,6 +324,5 @@ public function testRespectsLastForwardFrom() {
_elgg_services()->session->removeLoggedInUser();

elgg_unregister_plugin_hook_handler('login:forward', 'user', $forward_handler);

}
}
Expand Up @@ -269,11 +269,8 @@ public function testRegistrationSucceeds() {
$this->assertInstanceOf(\ElggUser::class, $user);

$this->assertEquals($user->guid, elgg_get_logged_in_user_guid());

_elgg_services()->session->removeLoggedInUser();
}


public function testRegistrationSucceedsButExceptionThrownFromHook() {

$hook = $this->registerTestingHook('register', 'user', function () {
Expand Down Expand Up @@ -357,6 +354,5 @@ public function testRegisterWithoutValidation() {
$this->assertTrue($user->isEnabled());

$this->assertNotEmpty(elgg_get_logged_in_user_entity());
logout($user);
}
}
Expand Up @@ -9,17 +9,9 @@
class ValidateIntegrationTest extends ActionResponseTestCase {

public function up() {

$session = elgg_get_session();
$session->setLoggedInUser($this->getAdmin());
}

public function down() {

$session = elgg_get_session();
$session->removeLoggedInUser();
_elgg_services()->session->setLoggedInUser($this->getAdmin());
}

public function testValidateSingleUserWithoutParams() {

$response = $this->executeAction('admin/user/validate');
Expand Down
Expand Up @@ -59,8 +59,6 @@ public function testPasswordChangeFailsWithoutValidCurrentPassword() {
$this->assertInstanceOf(OkResponse::class, $response);

$this->assertErrorMessageEmitted(elgg_echo('LoginException:ChangePasswordFailure'));

_elgg_services()->session->removeLoggedInUser();
}

public function testPasswordChangeFailsWithInvalidNewPassword() {
Expand All @@ -81,8 +79,6 @@ public function testPasswordChangeFailsWithInvalidNewPassword() {
$this->assertInstanceOf(OkResponse::class, $response);

$this->assertErrorMessageEmitted(elgg_echo('Security:InvalidPasswordLengthException', [elgg()->config->min_password_length]));

_elgg_services()->session->removeLoggedInUser();
}

public function testPasswordChangeFailsWithMismatchingNewPassword() {
Expand All @@ -103,8 +99,6 @@ public function testPasswordChangeFailsWithMismatchingNewPassword() {
$this->assertInstanceOf(OkResponse::class, $response);

$this->assertErrorMessageEmitted(elgg_echo('RegistrationException:PasswordMismatch'));

_elgg_services()->session->removeLoggedInUser();
}

public function testPasswordChangeSucceeds() {
Expand All @@ -131,8 +125,6 @@ public function testPasswordChangeSucceeds() {
$this->assertSystemMessageEmitted(elgg_echo('user:password:success'));

$this->assertNotEquals($user->password_hash, $old_hash);

_elgg_services()->session->removeLoggedInUser();
}

public function testDisplayNameChangeFails() {
Expand All @@ -152,8 +144,6 @@ public function testDisplayNameChangeFails() {
$this->assertErrorMessageEmitted(elgg_echo('user:name:fail'));

$this->assertEquals($user->name, $name);

_elgg_services()->session->removeLoggedInUser();
}

public function testDisplayNameChangeSucceeds() {
Expand All @@ -173,8 +163,6 @@ public function testDisplayNameChangeSucceeds() {
$this->assertSystemMessageEmitted(elgg_echo('user:name:success'));

$this->assertEquals($new_name, $user->name);

_elgg_services()->session->removeLoggedInUser();
}

public function testUsernameChangeFails() {
Expand All @@ -199,8 +187,6 @@ public function testUsernameChangeFails() {
$this->assertErrorMessageEmitted(elgg_echo('registration:userexists'));

$this->assertEquals($user->username, $username);

_elgg_services()->session->removeLoggedInUser();
}

public function testUsernameChangeSucceeds() {
Expand All @@ -224,8 +210,6 @@ public function testUsernameChangeSucceeds() {
$this->assertSystemMessageEmitted(elgg_echo('user:username:success'));

$this->assertEquals($new_username, $user->username);

_elgg_services()->session->removeLoggedInUser();
}

public function testLanguageChangeSucceeds() {
Expand Down Expand Up @@ -260,8 +244,6 @@ public function testLanguageChangeSucceeds() {
$this->assertSystemMessageEmitted(elgg_echo('user:language:success'));

$this->assertEquals($new_language, $user->getLanguage());

_elgg_services()->session->removeLoggedInUser();
}

public function testEmailChangeFailsWithExistingEmail() {
Expand All @@ -283,8 +265,6 @@ public function testEmailChangeFailsWithExistingEmail() {
$this->assertErrorMessageEmitted(elgg_echo('registration:dupeemail'));

$this->assertEquals($email, $user->email);

_elgg_services()->session->removeLoggedInUser();
}

public function testEmailChangeFailsWithWrongPassword() {
Expand All @@ -308,8 +288,6 @@ public function testEmailChangeFailsWithWrongPassword() {
$this->assertErrorMessageEmitted(elgg_echo('email:save:fail:password'));

$this->assertEquals($email, $user->email);

_elgg_services()->session->removeLoggedInUser();
}

public function testEmailChangeSucceeds() {
Expand Down Expand Up @@ -337,8 +315,6 @@ public function testEmailChangeSucceeds() {
$this->assertEquals($new_email, $user->email);

$this->assertSystemMessageEmitted(elgg_echo('email:save:success'));

_elgg_services()->session->removeLoggedInUser();
}

public function testDefaultAccessChangeSucceeds() {
Expand All @@ -360,7 +336,5 @@ public function testDefaultAccessChangeSucceeds() {
$this->assertSystemMessageEmitted(elgg_echo('user:default_access:success'));

$this->assertEquals(ACCESS_PRIVATE, get_default_access($user));

_elgg_services()->session->removeLoggedInUser();
}
}
}
Expand Up @@ -35,7 +35,6 @@ public function up() {
}

public function down() {
_elgg_services()->session->removeLoggedInUser();
_elgg_services()->hooks->restore();
}

Expand Down
Expand Up @@ -14,8 +14,6 @@ class AdminPluginsRefreshIntegrationTest extends IntegrationTestCase {
*/
public function down() {
elgg_unregister_route('admin_plugins_refresh');

elgg()->session->removeLoggedInUser();
}

protected function createService(Request $request) {
Expand Down
Expand Up @@ -43,13 +43,6 @@ public function up() {
'access_id' => ACCESS_PUBLIC,
]);
}

/**
* {@inheritDoc}
*/
public function down() {
elgg()->session->removeLoggedInUser();
}

protected function createService(Request $request) {
$request->_integration_testing = true;
Expand Down

0 comments on commit c1a9736

Please sign in to comment.