-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from dnadesign/feature/unit-tests
Add unit test
- Loading branch information
Showing
6 changed files
with
165 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ | |
}, | ||
"autoload": { | ||
"psr-4": { | ||
"DNADesign\\Tagurit\\": "src/" | ||
"DNADesign\\IdleLock\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace DNADesign\IdleLock\Tasks; | ||
|
||
use SilverStripe\Dev\BuildTask; | ||
use SilverStripe\ORM\FieldType\DBDatetime; | ||
use SilverStripe\Security\Member; | ||
|
||
/** | ||
* This is in lieu of proper unit test as I can't seem to be able to make them work. | ||
* We'll revisit when we ge the time. | ||
*/ | ||
class UnitTestTask extends BuildTask | ||
{ | ||
private static $segment = 'idlelock-unittest'; | ||
|
||
protected $enabled = false; | ||
|
||
public function run($request) | ||
{ | ||
echo 'testDefaultThreshold => '.$this->testDefaultThreshold().PHP_EOL; | ||
echo 'testNotLockedOut => '.$this->testNotLockedOut().PHP_EOL; | ||
echo 'testLockedOut => '.$this->testLockedOut().PHP_EOL; | ||
|
||
echo 'Done.'; | ||
} | ||
|
||
private function testDefaultThreshold() | ||
{ | ||
$threshold = (int) Member::config()->get('lockout_threshold_days'); | ||
return 30 === $threshold; | ||
} | ||
|
||
private function testNotLockedOut() | ||
{ | ||
DBDatetime::set_mock_now('2024-02-01 10:00:00'); | ||
|
||
$member = new Member(); | ||
$member->LastEdited = '2024-01-15 10:00:00'; | ||
|
||
return $member->shouldBeLockedOut() === false; | ||
} | ||
|
||
private function testLockedOut() | ||
{ | ||
DBDatetime::set_mock_now('2024-02-01 10:00:00'); | ||
|
||
$member = new Member(); | ||
$member->LastEdited = '2023-01-01 10:00:00'; | ||
|
||
return $member->shouldBeLockedOut() === true; | ||
} | ||
|
||
// NOTE: cannot test the group threshold as it would require | ||
// writing in the database. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
namespace DNADesign\IdleLock\Tests; | ||
|
||
use SilverStripe\Dev\SapphireTest; | ||
use SilverStripe\ORM\FieldType\DBDatetime; | ||
use SilverStripe\Security\Member; | ||
|
||
/** | ||
* NOTE: at time of writing, I couldn't get the test to run at all | ||
* Seems to be a widespread issue. | ||
*/ | ||
class IdleLockTest extends SapphireTest | ||
{ | ||
/** | ||
* Defines the fixture file to use for this test class | ||
* @var string $fixture_file | ||
*/ | ||
protected static $fixture_file = 'dnadesign/silverstripe-idlelock:tests/fixtures.yml'; | ||
|
||
protected function setUp() : void | ||
{ | ||
DBDatetime::set_mock_now('2024-02-01 10:00:00'); | ||
} | ||
|
||
public function testDefaultThreshold() | ||
{ | ||
$threshold = (int) Member::config()->get('lockout_threshold_days'); | ||
$this->assertEquals(30, $threshold); | ||
} | ||
|
||
// TODO: Add other tests to check every functions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
SilverStripe\Security\Group: | ||
lowThreshold: | ||
Title: Low threshold | ||
LockoutThresholdDays: 10 | ||
|
||
SilverStripe\Security\Member: | ||
user1: | ||
FirstName: unlocked | ||
Email: [email protected] | ||
LastEdited: '2024-02-01 10:00:00' | ||
user2: | ||
FirstName: locked | ||
Email: [email protected] | ||
LastEdited: '2024-02-01 10:00:00' | ||
user3: | ||
FirstName: groupLocked | ||
Email: [email protected] | ||
LastEdited: '2024-02-01 10:00:00' | ||
Groups: =>SilverStripe\Security\Group.lowThreshold | ||
|
||
SilverStripe\SessionManager\Models\LoginSession: | ||
user2session: | ||
LastAccessed: '2023-02-01 10:00:00' | ||
Member: =>SilverStripe\Security\Member.user2 |