Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Oct 5, 2024
1 parent 614345e commit 43ef82b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [5.1.2](https://github.com/userfrosting/sprinkle-admin/compare/5.1.1...5.1.2)
- Fix Unable to create a user without a group on MySQL (Fix [#1273](https://github.com/userfrosting/UserFrosting/issues/1273))

## [5.1.1](https://github.com/userfrosting/sprinkle-admin/compare/5.1.0...5.1.1)
- Fix issue when a Group Administrator without the `create_user_field` permission creates a new user, the new user SHOULD inherit the admin's group (Fix [#1256](https://github.com/userfrosting/UserFrosting/issues/1256))

Expand Down
5 changes: 5 additions & 0 deletions app/src/Controller/User/UserCreateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ protected function handle(Request $request): void
$data['group_id'] = $currentUser->group_id;
}

// If group id is zero, then it's no group
if ($data['group_id'] === 0) {
$data['group_id'] = null;
}

// Now that we check the form, we can try to register the actual user
$user = new $this->userModel($data);

Expand Down
51 changes: 51 additions & 0 deletions app/tests/Controller/User/UserCreateActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,57 @@ public function testPostForGroupIsSetAs(): void
$this->assertSame('success', array_reverse($messages)[0]['type']);
}

public function testPostForNoGroup(): void
{
/** @var User */
$user = User::factory()->create();
$this->actAsUser($user, isMaster: true);

/** @var Config */
$config = $this->ci->get(Config::class);

// Force locale config.
$config->set('site.registration.user_defaults.locale', 'en_US');
$config->set('site.locales.available', ['en_US' => true]);

/** @var Mailer */
$mailer = Mockery::mock(Mailer::class)
->makePartial()
->shouldReceive('send')->once()
->getMock();
$this->ci->set(Mailer::class, $mailer);

// Set post payload
$data = [
'user_name' => 'foo',
'first_name' => 'Foo',
'last_name' => 'Bar',
'email' => '[email protected]',
'locale' => 'en_US',
'group_id' => 0,
];

// Create request with method and url and fetch response
$request = $this->createJsonRequest('POST', '/api/users', $data);
$response = $this->handleRequest($request);

// Assert response status & body
$this->assertJsonResponse([], $response);
$this->assertResponseStatus(200, $response);

// Make sure the user is added to the db by querying it
/** @var User */
$user = User::where('email', '[email protected]')->first();
$this->assertSame('foo', $user['user_name']);
$this->assertSame('en_US', $user['locale']);

// Test message
/** @var AlertStream */
$ms = $this->ci->get(AlertStream::class);
$messages = $ms->getAndClearMessages();
$this->assertSame('success', array_reverse($messages)[0]['type']);
}

/**
* @depends testPost
*/
Expand Down

0 comments on commit 43ef82b

Please sign in to comment.