Skip to content

Commit 8ad9b1b

Browse files
committed
intial tests
* travis updates for SilverStripe 4 * scrutinizer updates * codecov setup
1 parent 7145ab1 commit 8ad9b1b

File tree

12 files changed

+371
-28
lines changed

12 files changed

+371
-28
lines changed

.scrutinizer.yml

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
11
inherit: true
22

3-
checks:
3+
#Copied from https://www.adayinthelifeof.nl/2013/11/20/external-code-coverage-with-travis-scrutinizer/
4+
tools:
5+
external_code_coverage:
6+
timeout: 600
7+
php_code_sniffer:
8+
config:
9+
standard: PSR2
10+
php_cs_fixer:
11+
extensions:
12+
# Default:
13+
- php
14+
fixers: []
15+
enabled: false
16+
filter:
17+
paths: [tests/*,src/*]
18+
excluded_paths: []
19+
coding_style:
420
php:
5-
code_rating: true
6-
duplication: true
21+
indentation:
22+
general:
23+
use_tabs: false
24+
25+
checks:
26+
php:
27+
code_rating: true
28+
duplication: true
729

830
filter:
9-
paths: [code/*, tests/*]
31+
paths: [tests/*,src/*]

.travis.yml

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,54 @@
1-
# See https://github.com/silverstripe-labs/silverstripe-travis-support for setup details
1+
2+
language: php
23

34
sudo: false
45

5-
language: php
6+
addons:
7+
apt:
8+
packages:
9+
- tidy
610

7-
php:
8-
- 5.3
9-
- 5.4
10-
- 5.5
11-
- 5.6
12-
- 7.0
11+
before_install:
12+
- pip install --user codecov
1313

1414
env:
15-
- DB=MYSQL CORE_RELEASE=3.2
15+
global:
16+
- DB=MYSQL CORE_RELEASE=4.0
17+
- MODULE_PATH=blocks
18+
- COVERAGE=0
1619

1720
matrix:
1821
include:
22+
- php: 7.0
23+
env: DB=SQLITE
24+
- php: 7.0
25+
env: DB=PGSQL
26+
- php: 7.0
27+
env: COVERAGE=1
1928
- php: 5.6
20-
env: DB=MYSQL CORE_RELEASE=3
21-
- php: 5.6
22-
env: DB=MYSQL CORE_RELEASE=3.1
23-
- php: 5.6
24-
env: DB=PGSQL CORE_RELEASE=3.2
2529
allow_failures:
2630
- php: 7.0
31+
env: DB=SQLITE
32+
2733

2834
before_script:
35+
- phpenv rehash
2936
- composer self-update || true
3037
- git clone git://github.com/silverstripe-labs/silverstripe-travis-support.git ~/travis-support
3138
- php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss
3239
- cd ~/builds/ss
33-
- composer install
40+
- mv "$MODULE_PATH/phpunit.xml.dist" .
3441

3542
script:
36-
- vendor/bin/phpunit silverstripe-blocks/tests
43+
# Execute tests with no coverage. This is the fastest option
44+
- "if [ \"$COVERAGE\" = \"0\" ]; then vendor/bin/phpunit $MODULE_PATH/tests/; fi"
45+
46+
# Execute tests with coverage. Do this for a small
47+
- "if [ \"$COVERAGE\" = \"1\" ]; then vendor/bin/phpunit --coverage-clover=coverage.clover $MODULE_PATH/tests/; fi"
48+
49+
after_script:
50+
- "if [ \"$COVERAGE\" = \"1\" ]; then mv coverage.clover ~/build/$TRAVIS_REPO_SLUG/; fi"
51+
- cd ~/build/$TRAVIS_REPO_SLUG
52+
- wget https://scrutinizer-ci.com/ocular.phar
53+
- "if [ \"$COVERAGE\" = \"1\" ]; then travis_retry codecov && travis_retry php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi"
54+

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# SilverStripe Blocks
22

3+
[![Build Status](https://travis-ci.org/sheadawson/silverstripe-blocks.svg?branch=master)](https://travis-ci.org/sheadawson/silverstripe-blocks)
34
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/sheadawson/silverstripe-blocks/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/sheadawson/silverstripe-blocks/?branch=master)
5+
[![codecov](https://codecov.io/gh/sheadawson/silverstripe-blocks/branch/master/graph/badge.svg)](https://codecov.io/gh/sheadawson/silverstripe-blocks)
46

57
The Blocks modules aims to provide developers with a flexible foundation for defining reusable blocks of content or widgets that can be managed in the CMS.
68

phpunit.xml.dist

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<phpunit bootstrap="framework/tests/bootstrap.php" colors="true">
2+
<testsuite name="blocks">
3+
<directory>blocks/tests</directory>
4+
</testsuite>
5+
6+
<filter>
7+
<whitelist addUncoveredFilesFromWhitelist="true">
8+
<directory suffix=".php">blocks/src/</directory>
9+
<exclude>
10+
<directory suffix=".php">blocks/tests/</directory>
11+
</exclude>
12+
</whitelist>
13+
</filter>
14+
</phpunit>

src/model/Block.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use SheaDawson\Blocks\BlockManager;
66
use SilverStripe\ORM\DataObject;
7+
use SilverStripe\Security\Security;
78
use SilverStripe\Versioned\Versioned;
89
use SilverStripe\ORM\FieldType\DBBoolean;
910
use SilverStripe\ORM\FieldType\DBHTMLText;
@@ -288,7 +289,7 @@ public function onAfterDuplicate()
288289
public function canView($member = null)
289290
{
290291
if (!$member || !(is_a($member, 'Member')) || is_numeric($member)) {
291-
$member = Member::currentUserID();
292+
$member = Security::getCurrentUser()->ID;
292293
}
293294

294295
// admin override
@@ -325,22 +326,22 @@ public function canView($member = null)
325326

326327
public function canEdit($member = null)
327328
{
328-
return Permission::check('ADMIN') || Permission::check('BLOCK_EDIT');
329+
return Permission::check('ADMIN', 'any', $member) || Permission::check('BLOCK_EDIT', 'any', $member);
329330
}
330331

331332
public function canDelete($member = null)
332333
{
333-
return Permission::check('ADMIN') || Permission::check('BLOCK_DELETE');
334+
return Permission::check('ADMIN', 'any', $member) || Permission::check('BLOCK_DELETE', 'any', $member);
334335
}
335336

336337
public function canCreate($member = null, $context = [])
337338
{
338-
return Permission::check('ADMIN') || Permission::check('BLOCK_CREATE');
339+
return Permission::check('ADMIN', 'any', $member) || Permission::check('BLOCK_CREATE', 'any', $member);
339340
}
340341

341342
public function canPublish($member = null)
342343
{
343-
return Permission::check('ADMIN') || Permission::check('BLOCK_PUBLISH');
344+
return Permission::check('ADMIN', 'any', $member) || Permission::check('BLOCK_PUBLISH', 'any', $member);
344345
}
345346

346347
public function providePermissions()

src/model/BlockSet.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,17 @@ public function canView($member = null)
151151

152152
public function canEdit($member = null)
153153
{
154-
return Permission::check('ADMIN') || Permission::check('BLOCK_EDIT');
154+
return Permission::check('ADMIN', 'any', $member) || Permission::check('BLOCK_EDIT', 'any', $member);
155155
}
156156

157157
public function canDelete($member = null)
158158
{
159-
return Permission::check('ADMIN') || Permission::check('BLOCK_DELETE');
159+
return Permission::check('ADMIN', 'any', $member) || Permission::check('BLOCK_DELETE', 'any', $member);
160160
}
161161

162162
public function canCreate($member = null, $context = [])
163163
{
164-
return Permission::check('ADMIN') || Permission::check('BLOCK_CREATE');
164+
return Permission::check('ADMIN', 'any', $member) || Permission::check('BLOCK_CREATE', 'any', $member);
165165
}
166166

167167
public function providePermissions()

tests/BlockSetTest.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace SheaDawson\Blocks\Test;
4+
5+
use SheaDawson\Blocks\Model\BlockSet;
6+
use SilverStripe\Dev\SapphireTest;
7+
use SilverStripe\Forms\FieldList;
8+
use SilverStripe\Security\Member;
9+
10+
class BlockSetTest extends SapphireTest
11+
{
12+
/**
13+
* @var string
14+
*/
15+
protected static $fixture_file = 'fixtures.yml';
16+
17+
/**
18+
*
19+
*/
20+
public function testGetCMSFields()
21+
{
22+
$object = $this->objFromFixture(BlockSet::class, 'default');
23+
$fields = $object->getCMSFields();
24+
$this->assertInstanceOf(FieldList::class, $fields);
25+
}
26+
27+
/**
28+
*
29+
*/
30+
public function testCanView()
31+
{
32+
$object = $this->objFromFixture(BlockSet::class, 'default');
33+
$admin = $this->objFromFixture(Member::class, 'admin');
34+
$this->assertTrue($object->canView($admin));
35+
$member = $this->objFromFixture(Member::class, 'default');
36+
$this->assertTrue($object->canView($member));
37+
}
38+
39+
/**
40+
*
41+
*/
42+
public function testCanEdit()
43+
{
44+
$object = $this->objFromFixture(BlockSet::class, 'default');
45+
$admin = $this->objFromFixture(Member::class, 'admin');
46+
$this->assertTrue($object->canEdit($admin));
47+
$member = $this->objFromFixture(Member::class, 'default');
48+
$this->assertFalse($object->canEdit($member));
49+
}
50+
51+
/**
52+
*
53+
*/
54+
public function testCanDelete()
55+
{
56+
$object = $this->objFromFixture(BlockSet::class, 'default');
57+
$admin = $this->objFromFixture(Member::class, 'admin');
58+
$this->assertTrue($object->canDelete($admin));
59+
$member = $this->objFromFixture(Member::class, 'default');
60+
$this->assertFalse($object->canDelete($member));
61+
}
62+
63+
/**
64+
*
65+
*/
66+
public function testCanCreate()
67+
{
68+
$object = $this->objFromFixture(BlockSet::class, 'default');
69+
$admin = $this->objFromFixture(Member::class, 'admin');
70+
$this->assertTrue($object->canCreate($admin));
71+
$member = $this->objFromFixture(Member::class, 'default');
72+
$this->assertFalse($object->canCreate($member));
73+
}
74+
75+
/**
76+
*
77+
*/
78+
public function testProvidePermissions()
79+
{
80+
$object = singleton(BlockSet::class);
81+
$expected = [
82+
'BLOCKSET_EDIT' => [
83+
'name' => _t('BlockSet.EditBlockSet','Edit a Block Set'),
84+
'category' => _t('Block.PermissionCategory', 'Blocks'),
85+
],
86+
'BLOCKSET_DELETE' => [
87+
'name' => _t('BlockSet.DeleteBlockSet','Delete a Block Set'),
88+
'category' => _t('Block.PermissionCategory', 'Blocks'),
89+
],
90+
'BLOCKSET_CREATE' => [
91+
'name' => _t('BlockSet.CreateBlockSet','Create a Block Set'),
92+
'category' => _t('Block.PermissionCategory', 'Blocks'),
93+
],
94+
];
95+
96+
$this->assertEquals($expected, $object->providePermissions());
97+
}
98+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace SheaDawson\Blocks\Test;
4+
5+
use SheaDawson\Blocks\Extensions\BlockSiteConfigExtension;
6+
use SilverStripe\Dev\SapphireTest;
7+
use SilverStripe\Forms\FieldList;
8+
use SilverStripe\SiteConfig\SiteConfig;
9+
10+
class BlockSiteConfigExtensionTest extends SapphireTest
11+
{
12+
/**
13+
*
14+
*/
15+
public function testUpdateCMSFields()
16+
{
17+
$object = SiteConfig::current_site_config();
18+
$fields = $object->getCMSFields();
19+
$extension = new BlockSiteConfigExtension();
20+
$extension->updateCMSFields($fields);
21+
22+
$this->assertInstanceOf(FieldList::class, $fields);
23+
$this->assertNull($fields->dataFieldByName('Blocks'));
24+
}
25+
}

0 commit comments

Comments
 (0)