Skip to content

Commit

Permalink
Merge pull request #471 from creative-commoners/pulls/2/sapphire-test…
Browse files Browse the repository at this point in the history
…-nine

API phpunit 9 support
  • Loading branch information
Maxime Rainville authored Nov 1, 2021
2 parents 0179176 + 13ab072 commit 5049a5e
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 38 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ import:

env:
global:
- COMPOSER_ROOT_VERSION="2.x-dev"
- BEHAT_SUITE="@subsites"
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
}
],
"require": {
"silverstripe/framework": "^4.4@dev",
"php": "^7.3 || ^8.0",
"silverstripe/framework": "^4.10",
"silverstripe/cms": "^4.4@dev",
"silverstripe/admin": "^1.4@dev",
"silverstripe/asset-admin": "^1.4@dev",
"silverstripe/errorpage": "^1.4@dev",
"silverstripe/versioned": "^1.4@dev"
},
"require-dev": {
"sminnee/phpunit": "^5.7",
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.0"
},
"autoload": {
Expand Down
8 changes: 5 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<phpunit bootstrap="vendor/silverstripe/cms/tests/bootstrap.php" colors="true">
<testsuite name="Default">
<directory>tests/php</directory>
</testsuite>
<testsuites>
<testsuite name="Default">
<directory>tests/php</directory>
</testsuite>
</testsuites>

<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
Expand Down
2 changes: 1 addition & 1 deletion tests/php/BaseSubsiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class BaseSubsiteTest extends SapphireTest
{
protected function setUp()
protected function setUp(): void
{
parent::setUp();

Expand Down
4 changes: 2 additions & 2 deletions tests/php/GroupSubsitesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class GroupSubsitesTest extends BaseSubsiteTest

public function testTrivialFeatures()
{
$this->assertInternalType('array', singleton(GroupSubsites::class)->extraStatics());
$this->assertInternalType('array', singleton(GroupSubsites::class)->providePermissions());
$this->assertIsArray(singleton(GroupSubsites::class)->extraStatics());
$this->assertIsArray(singleton(GroupSubsites::class)->providePermissions());
$this->assertInstanceOf(FieldList::class, singleton(Group::class)->getCMSFields());
}

Expand Down
4 changes: 2 additions & 2 deletions tests/php/InitStateMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ class InitStateMiddlewareTest extends BaseSubsiteTest
*/
protected $origServer = [];

protected function setUp()
protected function setUp(): void
{
parent::setUp();

$this->origServer = $_SERVER;
}

protected function tearDown()
protected function tearDown(): void
{
$_SERVER = $this->origServer;

Expand Down
2 changes: 1 addition & 1 deletion tests/php/Service/ThemeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ThemeResolverTest extends SapphireTest
SSViewer::DEFAULT_THEME,
];

protected function setUp()
protected function setUp(): void
{
parent::setUp();

Expand Down
2 changes: 1 addition & 1 deletion tests/php/SiteTreeSubsitesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class SiteTreeSubsitesTest extends BaseSubsiteTest
],
];

protected function setUp()
protected function setUp(): void
{
// We have our own home page fixtures, prevent the default one being created in this test suite.
Config::modify()->set(SiteTree::class, 'create_default_pages', false);
Expand Down
50 changes: 35 additions & 15 deletions tests/php/SubsiteAdminFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SubsiteAdminFunctionalTest extends FunctionalTest

protected $autoFollowRedirection = false;

protected function setUp()
protected function setUp(): void
{
parent::setUp();
// Ensure all pages are published
Expand Down Expand Up @@ -48,14 +48,18 @@ public function testAnonymousIsForbiddenAdminAccess()
$this->logOut();

$response = $this->getAndFollowAll('admin/pages/?SubsiteID=0');
$this->assertContains('Security/login', $this->mainSession->lastUrl(), 'Admin is disallowed');
$this->assertStringContainsString('Security/login', $this->mainSession->lastUrl(), 'Admin is disallowed');

$subsite1 = $this->objFromFixture(Subsite::class, 'subsite1');
$response = $this->getAndFollowAll("admin/pages/?SubsiteID={$subsite1->ID}");
$this->assertContains('Security/login', $this->mainSession->lastUrl(), 'Admin is disallowed');
$this->assertStringContainsString('Security/login', $this->mainSession->lastUrl(), 'Admin is disallowed');

$response = $this->getAndFollowAll('admin/subsite_xhr');
$this->assertContains('Security/login', $this->mainSession->lastUrl(), 'SubsiteXHRController is disallowed');
$this->assertStringContainsString(
'Security/login',
$this->mainSession->lastUrl(),
'SubsiteXHRController is disallowed'
);
}

/**
Expand All @@ -67,17 +71,21 @@ public function testAdminCanAccessAllSubsites()

$this->getAndFollowAll('admin/pages/?SubsiteID=0');
$this->assertEquals(0, $this->session()->get('SubsiteID'), 'Can access main site.');
$this->assertContains('admin/pages', $this->mainSession->lastUrl(), 'Lands on the correct section');
$this->assertStringContainsString('admin/pages', $this->mainSession->lastUrl(), 'Lands on the correct section');

$subsite1 = $this->objFromFixture(Subsite::class, 'subsite1');
$this->getAndFollowAll("admin/pages/?SubsiteID={$subsite1->ID}");

// Check the session manually, since the state is unique to the request, not this test
$this->assertEquals($subsite1->ID, $this->session()->get('SubsiteID'), 'Can access other subsite.');
$this->assertContains('admin/pages', $this->mainSession->lastUrl(), 'Lands on the correct section');
$this->assertStringContainsString('admin/pages', $this->mainSession->lastUrl(), 'Lands on the correct section');

$response = $this->getAndFollowAll('admin/subsite_xhr');
$this->assertNotContains('Security/login', $this->mainSession->lastUrl(), 'SubsiteXHRController is reachable');
$this->assertStringNotContainsString(
'Security/login',
$this->mainSession->lastUrl(),
'SubsiteXHRController is reachable'
);
}

public function testAdminIsRedirectedToObjectsSubsite()
Expand All @@ -92,7 +100,7 @@ public function testAdminIsRedirectedToObjectsSubsite()
$response = $this->get("admin/pages/edit/show/$subsite1Home->ID");

$this->assertEquals(302, $response->getStatusCode());
$this->assertContains(
$this->assertStringContainsString(
'admin/pages/edit/show/' . $subsite1Home->ID . '?SubsiteID=' . $subsite1Home->SubsiteID,
$response->getHeader('Location')
);
Expand All @@ -102,7 +110,7 @@ public function testAdminIsRedirectedToObjectsSubsite()

$response = $this->get("admin/pages/edit/show/$subsite1Home->ID");
$this->assertEquals(302, $response->getStatusCode());
$this->assertContains(
$this->assertStringContainsString(
'admin/pages/edit/show/' . $subsite1Home->ID . '?SubsiteID=' . $subsite1Home->SubsiteID,
$response->getHeader('Location')
);
Expand All @@ -122,15 +130,19 @@ public function testEditorCanAccessAllSubsites()

$this->get('admin/pages/?SubsiteID=0');
$this->assertEquals(0, $this->session()->get('SubsiteID'), 'Can access main site.');
$this->assertContains('admin/pages', $this->mainSession->lastUrl(), 'Lands on the correct section');
$this->assertStringContainsString('admin/pages', $this->mainSession->lastUrl(), 'Lands on the correct section');

$subsite1 = $this->objFromFixture(Subsite::class, 'subsite1');
$this->get("admin/pages/?SubsiteID={$subsite1->ID}");
$this->assertEquals($subsite1->ID, $this->session()->get('SubsiteID'), 'Can access other subsite.');
$this->assertContains('admin/pages', $this->mainSession->lastUrl(), 'Lands on the correct section');
$this->assertStringContainsString('admin/pages', $this->mainSession->lastUrl(), 'Lands on the correct section');

$response = $this->get('admin/subsite_xhr');
$this->assertNotContains('Security/login', $this->mainSession->lastUrl(), 'SubsiteXHRController is reachable');
$this->assertStringNotContainsString(
'Security/login',
$this->mainSession->lastUrl(),
'SubsiteXHRController is reachable'
);
}

/**
Expand All @@ -146,7 +158,11 @@ public function testSubsiteAdmin()
// Check allowed URL.
$this->getAndFollowAll("admin/pages/?SubsiteID={$subsite1->ID}");
$this->assertEquals($subsite1->ID, $this->session()->get('SubsiteID'), 'Can access own subsite.');
$this->assertContains('admin/pages', $this->mainSession->lastUrl(), 'Can access permitted section.');
$this->assertStringContainsString(
'admin/pages',
$this->mainSession->lastUrl(),
'Can access permitted section.'
);

// Check forbidden section in allowed subsite.
$this->getAndFollowAll("admin/assets/?SubsiteID={$subsite1->ID}");
Expand All @@ -172,10 +188,14 @@ public function testSubsiteAdmin()
$subsite1->ID,
'Is redirected to first permitted subsite.'
);
$this->assertNotContains('Security/login', $this->mainSession->lastUrl(), 'Is not denied access');
$this->assertStringNotContainsString('Security/login', $this->mainSession->lastUrl(), 'Is not denied access');

// Check the standalone XHR controller.
$response = $this->getAndFollowAll('admin/subsite_xhr');
$this->assertNotContains('Security/login', $this->mainSession->lastUrl(), 'SubsiteXHRController is reachable');
$this->assertStringNotContainsString(
'Security/login',
$this->mainSession->lastUrl(),
'SubsiteXHRController is reachable'
);
}
}
2 changes: 1 addition & 1 deletion tests/php/SubsiteAdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class SubsiteAdminTest extends FunctionalTest
{
protected static $fixture_file = 'SubsiteTest.yml';

protected function setUp()
protected function setUp(): void
{
parent::setUp();

Expand Down
4 changes: 2 additions & 2 deletions tests/php/SubsiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SubsiteTest extends BaseSubsiteTest
*/
protected $origServer = [];

protected function setUp()
protected function setUp(): void
{
parent::setUp();

Expand All @@ -38,7 +38,7 @@ protected function setUp()
$this->origServer = $_SERVER;
}

protected function tearDown()
protected function tearDown(): void
{
$_SERVER = $this->origServer;

Expand Down
10 changes: 5 additions & 5 deletions tests/php/SubsiteXHRControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ public function testCanView()

$this->assertEquals(200, $result->getStatusCode());
// SilverStripe 4.0-4.2: text/json. >=4.3: application/json
$this->assertContains('json', $result->getHeader('Content-Type'));
$this->assertStringContainsString('json', $result->getHeader('Content-Type'));

$body = $result->getBody();
$this->assertContains('Main site', $body);
$this->assertContains('Test 1', $body);
$this->assertContains('Test 2', $body);
$this->assertContains('Test 3', $body);
$this->assertStringContainsString('Main site', $body);
$this->assertStringContainsString('Test 1', $body);
$this->assertStringContainsString('Test 2', $body);
$this->assertStringContainsString('Test 3', $body);
}
}
4 changes: 2 additions & 2 deletions tests/php/SubsitesVirtualPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SubsitesVirtualPageTest extends BaseSubsiteTest
'SubsitesVirtualPageTest.yml',
];

protected function setUp()
protected function setUp(): void
{
parent::setUp();

Expand All @@ -43,7 +43,7 @@ protected function setUp()
$page->write();
}

public function tearDown()
protected function tearDown(): void
{
TestAssetStore::reset();
parent::tearDown();
Expand Down

0 comments on commit 5049a5e

Please sign in to comment.