Skip to content

Commit 91544b1

Browse files
authored
Merge pull request #13 from samsonasik/permission-test
[wip] Add PermissionsTest
2 parents d36f59a + 5af4d5c commit 91544b1

File tree

7 files changed

+331
-39
lines changed

7 files changed

+331
-39
lines changed

composer.json

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,47 +18,30 @@
1818
"role": "Developer"
1919
}
2020
],
21-
"repositories": [
22-
{
23-
"type": "vcs",
24-
"url": "https://github.com/codeigniter4/CodeIgniter4",
25-
"no-api": true
26-
},
27-
{
28-
"type": "vcs",
29-
"url": "https://github.com/lonnieezell/myth-auth",
30-
"no-api": true
31-
},
32-
{
33-
"type": "composer",
34-
"url": "https://asset-packagist.org"
35-
}
36-
],
37-
"minimum-stability": "dev",
38-
"prefer-stable": true,
3921
"require": {
40-
"php" : ">=7.2",
22+
"php": ">=7.2",
23+
"components/jquery": "^3.3",
24+
"enyo/dropzone": "^5.7",
25+
"fortawesome/font-awesome": "^5.8",
4126
"tatter/alerts": "^2.0",
4227
"tatter/assets": "^2.2",
4328
"tatter/audits": "^1.0",
4429
"tatter/exports": "^2.0",
4530
"tatter/permits": "^2.0",
4631
"tatter/settings": "^1.0",
4732
"tatter/thumbnails": "^1.2",
48-
"components/jquery": "^3.3",
49-
"enyo/dropzone": "^5.7",
50-
"fortawesome/font-awesome": "^5.8",
5133
"twbs/bootstrap": "^4.5"
5234
},
5335
"require-dev": {
36+
"antecedent/patchwork": "^2.1",
5437
"codeigniter4/codeigniter4": "dev-develop",
55-
"myth/auth": "dev-develop",
38+
"codeigniter4/codeigniter4-standard": "^1.0",
5639
"fzaninotto/faker": "^1.9@dev",
5740
"mikey179/vfsstream": "^1.6",
58-
"phpunit/phpunit": "^8.5",
41+
"myth/auth": "dev-develop",
5942
"phpstan/phpstan": "^0.12",
60-
"squizlabs/php_codesniffer": "^3.5",
61-
"codeigniter4/codeigniter4-standard": "^1.0"
43+
"phpunit/phpunit": "^8.5",
44+
"squizlabs/php_codesniffer": "^3.5"
6245
},
6346
"autoload": {
6447
"psr-4": {
@@ -70,12 +53,30 @@
7053
"Tests\\Support\\": "tests/_support"
7154
}
7255
},
56+
"repositories": [
57+
{
58+
"type": "vcs",
59+
"url": "https://github.com/codeigniter4/CodeIgniter4",
60+
"no-api": true
61+
},
62+
{
63+
"type": "vcs",
64+
"url": "https://github.com/lonnieezell/myth-auth",
65+
"no-api": true
66+
},
67+
{
68+
"type": "composer",
69+
"url": "https://asset-packagist.org"
70+
}
71+
],
72+
"minimum-stability": "dev",
73+
"prefer-stable": true,
7374
"scripts": {
74-
"analyze": "phpstan analyze",
75-
"style": "phpcbf --standard=./vendor/codeigniter4/codeigniter4-standard/CodeIgniter4 src/ tests/",
76-
"test": "phpunit",
7775
"post-update-cmd": [
7876
"composer dump-autoload"
79-
]
77+
],
78+
"analyze": "phpstan analyze",
79+
"style": "phpcbf --standard=./vendor/codeigniter4/codeigniter4-standard/CodeIgniter4 src/ tests/",
80+
"test": "phpunit"
8081
}
8182
}

phpstan.neon.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ parameters:
99
excludes_analyse:
1010
- src/Config/Routes.php
1111
- src/Views/*
12+
- tests/_support/Models/UserModel.php
1213
ignoreErrors:
1314
- '#Access to an undefined property [A-Za-z\\]+\\Entities\\[A-Za-z]+::\$[a-z]+#'
1415
- '#Cannot access property \$[a-z]+ on array\|object#'
1516
- '#Cannot call method [A-Za-z]+\(\) on array\|object#'
1617
- '#Unsafe usage of new static\(\)*#'
18+
- '#Call to an undefined static method Config\\Services::authentication\(\)#'
19+
- '#Function Patchwork\\redefine not found#'
1720
scanDirectories:
1821
- vendor/codeigniter4/codeigniter4/system/Helpers
1922
- vendor/myth/auth/src/Helpers

src/Controllers/Files.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,12 @@ public function bulk(): RedirectResponse
363363
*/
364364
public function upload()
365365
{
366+
// Check for create permission
367+
if (! $this->model->mayCreate())
368+
{
369+
return $this->failure(403, lang('Permits.notPermitted'));
370+
}
371+
366372
// Verify upload succeeded
367373
$upload = $this->request->getFile('file');
368374
if (empty($upload))

tests/_support/FeatureTestCase.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<?php namespace Tests\Support;
22

3-
use App\Entities\Card;
4-
use App\Models\CardModel;
5-
use CodeIgniter\Test\Fabricator;
3+
use CodeIgniter\Config\Factories;
64
use Config\Services;
5+
use Tatter\Files\Models\FileModel;
76

87
class FeatureTestCase extends FilesTestCase
98
{
@@ -39,4 +38,17 @@ protected function setUp(): void
3938

4039
$this->resetAuthServices();
4140
}
41+
42+
/**
43+
* Injects a permission mode into the shared FileModel.
44+
*
45+
* @param int $mode Octal mode
46+
*/
47+
protected function setMode(int $mode)
48+
{
49+
$model = new FileModel();
50+
$model->setMode($mode);
51+
52+
Factories::injectMock('models', FileModel::class, $model);
53+
}
4254
}

tests/_support/FilesTestCase.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<?php namespace Tests\Support;
22

3-
use CodeIgniter\Config\Config;
4-
use CodeIgniter\Database\ModelFactory;
3+
use CodeIgniter\Config\Factories;
54
use CodeIgniter\Test\CIDatabaseTestCase;
5+
use Config\Services;
66
use Myth\Auth\Entities\User;
77
use Myth\Auth\Test\Fakers\UserFaker;
88
use Tatter\Files\Config\Files;
99
use Tests\Support\Fakers\FileFaker;
10+
use Tests\Support\Models;
1011
use org\bovigo\vfs\vfsStream;
1112
use org\bovigo\vfs\vfsStreamDirectory;
1213

@@ -66,8 +67,7 @@ class FilesTestCase extends CIDatabaseTestCase
6667
protected function setUp(): void
6768
{
6869
parent::setUp();
69-
Config::reset();
70-
ModelFactory::reset();
70+
7171
helper('auth');
7272
$this->resetAuthServices();
7373
$_REQUEST = [];
@@ -81,7 +81,7 @@ protected function setUp(): void
8181
// Force our config to the virtual path
8282
$this->config = new Files();
8383
$this->config->storagePath = $this->root->url() . '/storage/';
84-
Config::injectMock('Files', $this->config);
84+
Factories::injectMock('config', 'Files', $this->config);
8585

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

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

110+
$auth = Services::authentication();
111+
$auth->login($user);
112+
Services::injectMock('authentication', $auth);
113+
110114
return $user;
111115
}
112116

tests/_support/Models/UserModel.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php namespace Tests\Support\Models;
2+
3+
use Myth\Auth\Models\UserModel as MythModel;
4+
use Tatter\Permits\Interfaces\PermitsUserModelInterface;
5+
6+
/**
7+
* An extension of Myth's UserModel that is
8+
* compatible with Tatter\Permits.
9+
*/
10+
class UserModel extends MythModel implements PermitsUserModelInterface
11+
{
12+
/**
13+
* Returns an empty array since groups are
14+
* not currently implemented.
15+
*
16+
* @param mixed $userId = null
17+
*
18+
* @return array
19+
*/
20+
public function groups($userId = null): array
21+
{
22+
return [];
23+
}
24+
}

0 commit comments

Comments
 (0)