From 7f1f4da911b68f2cb041acf5a60bae28ec21be50 Mon Sep 17 00:00:00 2001 From: Louis Charette Date: Tue, 19 Nov 2019 20:28:54 -0500 Subject: [PATCH] Deprecated `value` instead of removing it --- CHANGELOG.md | 3 ++ .../admin/src/Controller/UserController.php | 10 ++++-- .../Controller/UserControllerTest.php | 35 +++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c104d1a04..ad4696238 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - User cache not flushed on model save ([#1050]) - Fix "the passwords don't match" error when editing a user password ([#1034], [#1038]) +### Deprecated +`UserController:updateField` now expect the new value as `$_PUT[$fieldName]` (where `$fieldName` is the name of the field you want to update, eg. `$_PUT['password']` for editing `password`) instead of `$_PUT['value']`. This will only affect your code if you're **not** using the [user widjet](https://github.com/userfrosting/UserFrosting/blob/master/app/sprinkles/admin/assets/userfrosting/js/widgets/users.js). + ## [v4.3.1] ### Changed diff --git a/app/sprinkles/admin/src/Controller/UserController.php b/app/sprinkles/admin/src/Controller/UserController.php index 804583f55..6aac3d0da 100644 --- a/app/sprinkles/admin/src/Controller/UserController.php +++ b/app/sprinkles/admin/src/Controller/UserController.php @@ -1323,13 +1323,19 @@ public function updateField(Request $request, Response $response, array $args) // Get PUT parameters: value $put = $request->getParsedBody(); - if (!isset($put[$fieldName])) { + // Make sure data is part of $_PUT data + if (isset($put[$fieldName])) { + $fieldData = $put[$fieldName]; + } elseif (isset($put['value'])) { + /** @deprecated - Fieldname should be used instead of `value` */ + $fieldData = $put['value']; + } else { throw new BadRequestException(); } // Create and validate key -> value pair $params = [ - $fieldName => $put[$fieldName], + $fieldName => $fieldData, ]; // Load the request schema diff --git a/app/sprinkles/admin/tests/Integration/Controller/UserControllerTest.php b/app/sprinkles/admin/tests/Integration/Controller/UserControllerTest.php index 9f39d875a..e3b330231 100644 --- a/app/sprinkles/admin/tests/Integration/Controller/UserControllerTest.php +++ b/app/sprinkles/admin/tests/Integration/Controller/UserControllerTest.php @@ -819,6 +819,41 @@ public function testUpdateField(UserController $controller) $this->assertSame('success', end($messages)['type']); } + /** + * @depends testControllerConstructorWithUser + * @depends testUpdateField + * @param UserController $controller + */ + public function testUpdateFieldWithDeprecatedSupport(UserController $controller) + { + // Create a user + $user = $this->createTestUser(); + + // Set post data + $data = [ + 'value' => 'deprecated', //<-- Use old `value` + ]; + $request = $this->getRequest()->withParsedBody($data); + + // Get controller stuff + $result = $controller->updateField($request, $this->getResponse(), ['user_name' => $user->user_name, 'field' => 'first_name']); + $this->assertSame($result->getStatusCode(), 200); + $this->assertJson((string) $result->getBody()); + $this->assertSame('[]', (string) $result->getBody()); + + // Make sure user was update + $editedUser = User::where('user_name', $user->user_name)->first(); + $this->assertSame('deprecated', $editedUser->first_name); + $this->assertNotSame($user->first_name, $editedUser->first_name); + $this->assertSame($user->last_name, $editedUser->last_name); + + // Test message + /** @var \UserFrosting\Sprinkle\Core\Alert\AlertStream $ms */ + $ms = $this->ci->alerts; + $messages = $ms->getAndClearMessages(); + $this->assertSame('success', end($messages)['type']); + } + /** * @depends testControllerConstructorWithUser * @depends testUpdateField