Skip to content

Commit

Permalink
Merge pull request #13 from samsonasik/permission-test
Browse files Browse the repository at this point in the history
[wip] Add PermissionsTest
  • Loading branch information
MGatner authored Oct 16, 2020
2 parents d36f59a + 5af4d5c commit 91544b1
Show file tree
Hide file tree
Showing 7 changed files with 331 additions and 39 deletions.
61 changes: 31 additions & 30 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,30 @@
"role": "Developer"
}
],
"repositories": [
{
"type": "vcs",
"url": "https://github.com/codeigniter4/CodeIgniter4",
"no-api": true
},
{
"type": "vcs",
"url": "https://github.com/lonnieezell/myth-auth",
"no-api": true
},
{
"type": "composer",
"url": "https://asset-packagist.org"
}
],
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php" : ">=7.2",
"php": ">=7.2",
"components/jquery": "^3.3",
"enyo/dropzone": "^5.7",
"fortawesome/font-awesome": "^5.8",
"tatter/alerts": "^2.0",
"tatter/assets": "^2.2",
"tatter/audits": "^1.0",
"tatter/exports": "^2.0",
"tatter/permits": "^2.0",
"tatter/settings": "^1.0",
"tatter/thumbnails": "^1.2",
"components/jquery": "^3.3",
"enyo/dropzone": "^5.7",
"fortawesome/font-awesome": "^5.8",
"twbs/bootstrap": "^4.5"
},
"require-dev": {
"antecedent/patchwork": "^2.1",
"codeigniter4/codeigniter4": "dev-develop",
"myth/auth": "dev-develop",
"codeigniter4/codeigniter4-standard": "^1.0",
"fzaninotto/faker": "^1.9@dev",
"mikey179/vfsstream": "^1.6",
"phpunit/phpunit": "^8.5",
"myth/auth": "dev-develop",
"phpstan/phpstan": "^0.12",
"squizlabs/php_codesniffer": "^3.5",
"codeigniter4/codeigniter4-standard": "^1.0"
"phpunit/phpunit": "^8.5",
"squizlabs/php_codesniffer": "^3.5"
},
"autoload": {
"psr-4": {
Expand All @@ -70,12 +53,30 @@
"Tests\\Support\\": "tests/_support"
}
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/codeigniter4/CodeIgniter4",
"no-api": true
},
{
"type": "vcs",
"url": "https://github.com/lonnieezell/myth-auth",
"no-api": true
},
{
"type": "composer",
"url": "https://asset-packagist.org"
}
],
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"analyze": "phpstan analyze",
"style": "phpcbf --standard=./vendor/codeigniter4/codeigniter4-standard/CodeIgniter4 src/ tests/",
"test": "phpunit",
"post-update-cmd": [
"composer dump-autoload"
]
],
"analyze": "phpstan analyze",
"style": "phpcbf --standard=./vendor/codeigniter4/codeigniter4-standard/CodeIgniter4 src/ tests/",
"test": "phpunit"
}
}
3 changes: 3 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ parameters:
excludes_analyse:
- src/Config/Routes.php
- src/Views/*
- tests/_support/Models/UserModel.php
ignoreErrors:
- '#Access to an undefined property [A-Za-z\\]+\\Entities\\[A-Za-z]+::\$[a-z]+#'
- '#Cannot access property \$[a-z]+ on array\|object#'
- '#Cannot call method [A-Za-z]+\(\) on array\|object#'
- '#Unsafe usage of new static\(\)*#'
- '#Call to an undefined static method Config\\Services::authentication\(\)#'
- '#Function Patchwork\\redefine not found#'
scanDirectories:
- vendor/codeigniter4/codeigniter4/system/Helpers
- vendor/myth/auth/src/Helpers
Expand Down
6 changes: 6 additions & 0 deletions src/Controllers/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,12 @@ public function bulk(): RedirectResponse
*/
public function upload()
{
// Check for create permission
if (! $this->model->mayCreate())
{
return $this->failure(403, lang('Permits.notPermitted'));
}

// Verify upload succeeded
$upload = $this->request->getFile('file');
if (empty($upload))
Expand Down
18 changes: 15 additions & 3 deletions tests/_support/FeatureTestCase.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php namespace Tests\Support;

use App\Entities\Card;
use App\Models\CardModel;
use CodeIgniter\Test\Fabricator;
use CodeIgniter\Config\Factories;
use Config\Services;
use Tatter\Files\Models\FileModel;

class FeatureTestCase extends FilesTestCase
{
Expand Down Expand Up @@ -39,4 +38,17 @@ protected function setUp(): void

$this->resetAuthServices();
}

/**
* Injects a permission mode into the shared FileModel.
*
* @param int $mode Octal mode
*/
protected function setMode(int $mode)
{
$model = new FileModel();
$model->setMode($mode);

Factories::injectMock('models', FileModel::class, $model);
}
}
16 changes: 10 additions & 6 deletions tests/_support/FilesTestCase.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php namespace Tests\Support;

use CodeIgniter\Config\Config;
use CodeIgniter\Database\ModelFactory;
use CodeIgniter\Config\Factories;
use CodeIgniter\Test\CIDatabaseTestCase;
use Config\Services;
use Myth\Auth\Entities\User;
use Myth\Auth\Test\Fakers\UserFaker;
use Tatter\Files\Config\Files;
use Tests\Support\Fakers\FileFaker;
use Tests\Support\Models;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;

Expand Down Expand Up @@ -66,8 +67,7 @@ class FilesTestCase extends CIDatabaseTestCase
protected function setUp(): void
{
parent::setUp();
Config::reset();
ModelFactory::reset();

helper('auth');
$this->resetAuthServices();
$_REQUEST = [];
Expand All @@ -81,7 +81,7 @@ protected function setUp(): void
// Force our config to the virtual path
$this->config = new Files();
$this->config->storagePath = $this->root->url() . '/storage/';
Config::injectMock('Files', $this->config);
Factories::injectMock('config', 'Files', $this->config);

$this->testPath = $this->config->storagePath . 'image.jpg';
}
Expand All @@ -103,10 +103,14 @@ protected function tearDown(): void
protected function login(int $userId = null): User
{
// Get or create the user
$user = $userId ? model('UserModel')->find($userId) : fake(UserFaker::class);
$user = $userId ? model(Models\UserModel::class)->find($userId) : fake(UserFaker::class);

$_SESSION['logged_in'] = $user->id;

$auth = Services::authentication();
$auth->login($user);
Services::injectMock('authentication', $auth);

return $user;
}

Expand Down
24 changes: 24 additions & 0 deletions tests/_support/Models/UserModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php namespace Tests\Support\Models;

use Myth\Auth\Models\UserModel as MythModel;
use Tatter\Permits\Interfaces\PermitsUserModelInterface;

/**
* An extension of Myth's UserModel that is
* compatible with Tatter\Permits.
*/
class UserModel extends MythModel implements PermitsUserModelInterface
{
/**
* Returns an empty array since groups are
* not currently implemented.
*
* @param mixed $userId = null
*
* @return array
*/
public function groups($userId = null): array
{
return [];
}
}
Loading

0 comments on commit 91544b1

Please sign in to comment.