Skip to content

Commit e256cca

Browse files
committed
Started UserController Tests
1 parent 5700e60 commit e256cca

4 files changed

Lines changed: 538 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,4 +755,4 @@ See [http://learn.userfrosting.com/upgrading/40-to-41](Upgrading 4.0.x to 4.1.x
755755
[#968]: https://github.com/userfrosting/UserFrosting/issues/968
756756

757757
[v4.2.0]: https://github.com/userfrosting/UserFrosting/compare/v4.1.22...v4.2.0
758-
[v4.2.1]: https://github.com/userfrosting/UserFrosting/compare/v4.2.0...v4.2.1
758+
[v4.2.1]: https://github.com/userfrosting/UserFrosting/compare/v4.2.0...v.4.2.1

app/sprinkles/admin/src/Controller/UserController.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ public function create(Request $request, Response $response, $args)
9999
/** @var \UserFrosting\Sprinkle\Core\Util\ClassMapper $classMapper */
100100
$classMapper = $this->ci->classMapper;
101101

102-
// Check if username or email already exists
103-
if ($classMapper->staticMethod('user', 'findUnique', $data['user_name'], 'user_name')) {
102+
// Check if username or email already exists. If not set, validator will pick it up
103+
if (isset($data['user_name']) && $classMapper->staticMethod('user', 'findUnique', $data['user_name'], 'user_name')) {
104104
$ms->addMessageTranslated('danger', 'USERNAME.IN_USE', $data);
105105
$error = true;
106106
}
107107

108-
if ($classMapper->staticMethod('user', 'findUnique', $data['email'], 'email')) {
108+
if (isset($data['email']) && $classMapper->staticMethod('user', 'findUnique', $data['email'], 'email')) {
109109
$ms->addMessageTranslated('danger', 'EMAIL.IN_USE', $data);
110110
$error = true;
111111
}
@@ -555,9 +555,6 @@ public function getModalConfirmDelete(Request $request, Response $response, $arg
555555
*/
556556
public function getModalCreate(Request $request, Response $response, $args)
557557
{
558-
// GET parameters
559-
$params = $request->getQueryParams();
560-
561558
/** @var \UserFrosting\Sprinkle\Account\Authorize\AuthorizationManager $authorizer */
562559
$authorizer = $this->ci->authorizer;
563560

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
/*
4+
* UserFrosting (http://www.userfrosting.com)
5+
*
6+
* @link https://github.com/userfrosting/UserFrosting
7+
* @copyright Copyright (c) 2019 Alexander Weissman
8+
* @license https://github.com/userfrosting/UserFrosting/blob/master/LICENSE.md (MIT License)
9+
*/
10+
11+
namespace UserFrosting\Sprinkle\Admin\Tests\Integration\Controller;
12+
13+
use UserFrosting\Sprinkle\Account\Tests\withTestUser;
14+
use UserFrosting\Sprinkle\Admin\Controller\UserController;
15+
use UserFrosting\Sprinkle\Core\Tests\RefreshDatabase;
16+
use UserFrosting\Sprinkle\Core\Tests\TestDatabase;
17+
use UserFrosting\Sprinkle\Core\Tests\withController;
18+
use UserFrosting\Support\Exception\ForbiddenException;
19+
use UserFrosting\Tests\TestCase;
20+
21+
/**
22+
* Tests UserController
23+
*/
24+
class UserControllerGuestTest extends TestCase
25+
{
26+
use TestDatabase;
27+
use RefreshDatabase;
28+
use withTestUser;
29+
use withController;
30+
31+
/**
32+
* @var bool DB is initialized for normal db
33+
*/
34+
protected static $initialized = false;
35+
36+
/**
37+
* Setup test database for controller tests
38+
*/
39+
public function setUp()
40+
{
41+
parent::setUp();
42+
$this->setupTestDatabase();
43+
44+
if ($this->usingInMemoryDatabase()) {
45+
46+
// Setup database, then setup User & default role
47+
$this->refreshDatabase();
48+
$this->setupUser();
49+
50+
} else if (!static::$initialized) {
51+
52+
// Only refresh db once
53+
$this->refreshDatabase();
54+
static::$initialized = true;
55+
}
56+
}
57+
58+
/**
59+
*/
60+
public function testControllerConstructor()
61+
{
62+
$controller = $this->getController();
63+
$this->assertInstanceOf(UserController::class, $controller);
64+
}
65+
66+
/**
67+
* @depends testControllerConstructor
68+
* @return UserController
69+
*/
70+
public function testControllerConstructorWithUser()
71+
{
72+
// Skip user setup if using in-memory db
73+
if (!$this->usingInMemoryDatabase()) {
74+
$this->setupUser();
75+
}
76+
77+
$controller = $this->getController();
78+
$this->assertInstanceOf(UserController::class, $controller);
79+
80+
return $controller;
81+
}
82+
83+
/**
84+
* @depends testControllerConstructorWithUser
85+
* @param UserController $controller
86+
*/
87+
public function testCreateWithNoPermissions(UserController $controller)
88+
{
89+
$this->expectException(ForbiddenException::class);
90+
$controller->create($this->getRequest(), $this->getResponse(), []);
91+
}
92+
93+
/**
94+
* @depends testControllerConstructorWithUser
95+
* @param UserController $controller
96+
*/
97+
public function testDeleteWithNoPermission(UserController $controller)
98+
{
99+
$this->expectException(ForbiddenException::class);
100+
$controller->delete($this->getRequest(), $this->getResponse(), ['user_name' => 'userfoo']);
101+
}
102+
103+
/**
104+
* @depends testControllerConstructorWithUser
105+
* @param UserController $controller
106+
*/
107+
public function testGetListWithNoPermission(UserController $controller)
108+
{
109+
$this->expectException(ForbiddenException::class);
110+
$controller->getList($this->getRequest(), $this->getResponse(), []);
111+
}
112+
113+
/**
114+
* @return UserController
115+
*/
116+
private function getController()
117+
{
118+
return new UserController($this->ci);
119+
}
120+
121+
/**
122+
*/
123+
private function setupUser()
124+
{
125+
// Admin user, WILL have access
126+
$testUser = $this->createTestUser(false, true);
127+
128+
// Create test user
129+
$fm = $this->ci->factory;
130+
$user = $fm->create('UserFrosting\Sprinkle\Account\Database\Models\User', [
131+
'id' => '9999',
132+
'user_name' => 'userfoo',
133+
'email' => '[email protected]'
134+
]);
135+
}
136+
}

0 commit comments

Comments
 (0)