Merge pull request #2682 from creative-commoners/pulls/4/fix-unit-tests

MNT Use logInAs() for unit tests
This commit is contained in:
Maxime Rainville 2021-08-19 13:49:13 +12:00 committed by GitHub
commit 5634173d9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 18 deletions

View File

@ -287,7 +287,7 @@ class CMSMainTest extends FunctionalTest
$this->assertEquals(403, $response->getStatusCode(), 'Add TopLevel page must fail for normal user'); $this->assertEquals(403, $response->getStatusCode(), 'Add TopLevel page must fail for normal user');
// with correct permissions // with correct permissions
Security::setCurrentUser($rootEditUser); $this->logInAs($rootEditUser);
$response = $this->get('admin/pages/add'); $response = $this->get('admin/pages/add');
$response = $this->post( $response = $this->post(
@ -307,8 +307,7 @@ class CMSMainTest extends FunctionalTest
$location = $response->getHeader('X-ControllerURL'); $location = $response->getHeader('X-ControllerURL');
$this->assertNotEmpty($location, 'Must be a redirect on success'); $this->assertNotEmpty($location, 'Must be a redirect on success');
$this->assertContains('/show/', $location, 'Must redirect to /show/ the new page'); $this->assertContains('/show/', $location, 'Must redirect to /show/ the new page');
// TODO Logout $this->logOut();
Security::setCurrentUser(null);
$this->autoFollowRedirection = $origFollow; $this->autoFollowRedirection = $origFollow;
} }
@ -318,8 +317,7 @@ class CMSMainTest extends FunctionalTest
$origFollow = $this->autoFollowRedirection; $origFollow = $this->autoFollowRedirection;
$this->autoFollowRedirection = false; $this->autoFollowRedirection = false;
$adminUser = $this->objFromFixture(Member::class, 'admin'); $this->logInAs('admin');
Security::setCurrentUser($adminUser);
// Create toplevel page // Create toplevel page
$this->get('admin/pages/add'); $this->get('admin/pages/add');
@ -393,8 +391,7 @@ class CMSMainTest extends FunctionalTest
{ {
$page3 = $this->objFromFixture(Page::class, 'page3'); $page3 = $this->objFromFixture(Page::class, 'page3');
$page31 = $this->objFromFixture(Page::class, 'page31'); $page31 = $this->objFromFixture(Page::class, 'page31');
$adminuser = $this->objFromFixture(Member::class, 'admin'); $this->logInAs('admin');
Security::setCurrentUser($adminuser);
$response = $this->get('admin/pages/edit/show/' . $page31->ID); $response = $this->get('admin/pages/edit/show/' . $page31->ID);
$parser = new CSSContentParser($response->getBody()); $parser = new CSSContentParser($response->getBody());

View File

@ -211,7 +211,7 @@ class SiteTreePermissionsTest extends FunctionalTest
$page->canView(false), $page->canView(false),
'Unauthenticated members cant view a page marked as "Viewable for any logged in users"' 'Unauthenticated members cant view a page marked as "Viewable for any logged in users"'
); );
Security::setCurrentUser(null); $this->logOut();
$response = $this->get($page->RelativeLink()); $response = $this->get($page->RelativeLink());
$this->assertEquals( $this->assertEquals(
$response->getStatusCode(), $response->getStatusCode(),
@ -225,14 +225,14 @@ class SiteTreePermissionsTest extends FunctionalTest
$page->canView($websiteuser), $page->canView($websiteuser),
'Authenticated members can view a page marked as "Viewable for any logged in users" even if they dont have access to the CMS' 'Authenticated members can view a page marked as "Viewable for any logged in users" even if they dont have access to the CMS'
); );
Security::setCurrentUser($websiteuser); $this->logInAs($websiteuser);
$response = $this->get($page->RelativeLink()); $response = $this->get($page->RelativeLink());
$this->assertEquals( $this->assertEquals(
$response->getStatusCode(), $response->getStatusCode(),
200, 200,
'Authenticated members can view a page marked as "Viewable for any logged in users" even if they dont have access to the CMS' 'Authenticated members can view a page marked as "Viewable for any logged in users" even if they dont have access to the CMS'
); );
Security::setCurrentUser(null); $this->logOut();
} }
public function testRestrictedViewOnlyTheseUsers() public function testRestrictedViewOnlyTheseUsers()
@ -244,7 +244,7 @@ class SiteTreePermissionsTest extends FunctionalTest
$page->canView(false), $page->canView(false),
'Unauthenticated members cant view a page marked as "Viewable by these groups"' 'Unauthenticated members cant view a page marked as "Viewable by these groups"'
); );
Security::setCurrentUser(null); $this->logOut();
$response = $this->get($page->RelativeLink()); $response = $this->get($page->RelativeLink());
$this->assertEquals( $this->assertEquals(
$response->getStatusCode(), $response->getStatusCode(),
@ -258,14 +258,14 @@ class SiteTreePermissionsTest extends FunctionalTest
$page->canView($subadminuser), $page->canView($subadminuser),
'Authenticated members cant view a page marked as "Viewable by these groups" if theyre not in the listed groups' 'Authenticated members cant view a page marked as "Viewable by these groups" if theyre not in the listed groups'
); );
Security::setCurrentUser($subadminuser); $this->LogInAs($subadminuser);
$response = $this->get($page->RelativeLink()); $response = $this->get($page->RelativeLink());
$this->assertEquals( $this->assertEquals(
$response->getStatusCode(), $response->getStatusCode(),
403, 403,
'Authenticated members cant view a page marked as "Viewable by these groups" if theyre not in the listed groups' 'Authenticated members cant view a page marked as "Viewable by these groups" if theyre not in the listed groups'
); );
Security::setCurrentUser(null); $this->logOut();
// website users // website users
$websiteuser = $this->objFromFixture(Member::class, 'websiteuser'); $websiteuser = $this->objFromFixture(Member::class, 'websiteuser');
@ -273,14 +273,14 @@ class SiteTreePermissionsTest extends FunctionalTest
$page->canView($websiteuser), $page->canView($websiteuser),
'Authenticated members can view a page marked as "Viewable by these groups" if theyre in the listed groups' 'Authenticated members can view a page marked as "Viewable by these groups" if theyre in the listed groups'
); );
Security::setCurrentUser($websiteuser); $this->logInAs($websiteuser);
$response = $this->get($page->RelativeLink()); $response = $this->get($page->RelativeLink());
$this->assertEquals( $this->assertEquals(
$response->getStatusCode(), $response->getStatusCode(),
200, 200,
'Authenticated members can view a page marked as "Viewable by these groups" if theyre in the listed groups' 'Authenticated members can view a page marked as "Viewable by these groups" if theyre in the listed groups'
); );
Security::setCurrentUser(null); $this->logOut();
} }
public function testRestrictedEditLoggedInUsers() public function testRestrictedEditLoggedInUsers()
@ -344,7 +344,7 @@ class SiteTreePermissionsTest extends FunctionalTest
$childPage->canView(false), $childPage->canView(false),
'Unauthenticated members cant view a page marked as "Viewable by these groups" by inherited permission' 'Unauthenticated members cant view a page marked as "Viewable by these groups" by inherited permission'
); );
Security::setCurrentUser(null); $this->logOut();
$response = $this->get($childPage->RelativeLink()); $response = $this->get($childPage->RelativeLink());
$this->assertEquals( $this->assertEquals(
$response->getStatusCode(), $response->getStatusCode(),
@ -358,14 +358,14 @@ class SiteTreePermissionsTest extends FunctionalTest
$childPage->canView($subadminuser), $childPage->canView($subadminuser),
'Authenticated members can view a page marked as "Viewable by these groups" if theyre in the listed groups by inherited permission' 'Authenticated members can view a page marked as "Viewable by these groups" if theyre in the listed groups by inherited permission'
); );
Security::setCurrentUser($subadminuser); $this->logInAs($subadminuser);
$response = $this->get($childPage->RelativeLink()); $response = $this->get($childPage->RelativeLink());
$this->assertEquals( $this->assertEquals(
$response->getStatusCode(), $response->getStatusCode(),
200, 200,
'Authenticated members can view a page marked as "Viewable by these groups" if theyre in the listed groups by inherited permission' 'Authenticated members can view a page marked as "Viewable by these groups" if theyre in the listed groups by inherited permission'
); );
Security::setCurrentUser(null); $this->logOut();
} }
public function testRestrictedEditInheritance() public function testRestrictedEditInheritance()