Skip to content

Commit

Permalink
Fixed withTrashed not available when SoftDeletes is not used
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Feb 27, 2020
1 parent 73d6e4a commit f96b27b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Improved Bakery debug command output
- Improve ordering by activity date ([#1061] & [#1062]; Thanks @ktecho!)
- Updated Vagrant config and documentation
- Fixed a bug where `withTrashed` in `findUnique` was not available when `SoftDeletes` trait is not included in a model.

### Removed
- `localePathBuilder` service removed. Task now handled by the `locale` and `translator` services.
Expand Down
63 changes: 63 additions & 0 deletions app/sprinkles/account/tests/Integration/FindUniqueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/*
* UserFrosting (http://www.userfrosting.com)
*
* @link https://github.com/userfrosting/UserFrosting
* @copyright Copyright (c) 2019 Alexander Weissman
* @license https://github.com/userfrosting/UserFrosting/blob/master/LICENSE.md (MIT License)
*/

namespace UserFrosting\Sprinkle\Account\Tests\Integration;

use UserFrosting\Sprinkle\Account\Database\Models\User;
use UserFrosting\Sprinkle\Account\Database\Models\Group;
use UserFrosting\Sprinkle\Account\Tests\withTestUser;
use UserFrosting\Sprinkle\Core\Tests\TestDatabase;
use UserFrosting\Sprinkle\Core\Tests\RefreshDatabase;
use UserFrosting\Tests\TestCase;

/**
* Test for bug with `withTrashed` in `findUnique` not available when `SoftDeletes` trait is not included in a model.
* @see https://chat.userfrosting.com/channel/support?msg=aAYvdwczSvBMzriJ6
*/
class FindUniqueTest extends TestCase
{
use TestDatabase;
use RefreshDatabase;
use withTestUser;

/**
* Setup the database schema.
*/
public function setUp()
{
parent::setUp();

// Setup test database
$this->setupTestDatabase();
$this->refreshDatabase();
}

/**
* User Model does have the soft Delete
*/
public function testUserFindUnique(): void
{
$user = $this->createTestUser();
$resultA = User::findUnique($user->user_name, 'user_name', true);
$resultB = $this->ci->classMapper->staticMethod('user', 'findUnique', $user->user_name, 'user_name');
$this->assertEquals($resultA, $resultB);
}

/**
* Group model doesn't have the soft delete
*/
public function testGroupFindUnique(): void
{
$group = $this->ci->factory->create(Group::class);
$resultA = Group::findUnique($group->name, 'name', true);
$resultB = $this->ci->classMapper->staticMethod('group', 'findUnique', $group->name, 'name');
$this->assertEquals($resultA, $resultB);
}
}
2 changes: 1 addition & 1 deletion app/sprinkles/core/src/Database/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static function findUnique($value, $identifier, $checkDeleted = true)
{
$query = static::whereRaw("LOWER($identifier) = ?", [mb_strtolower($value)]);

if ($checkDeleted) {
if ($checkDeleted && method_exists($query, 'withTrashed')) {
$query = $query->withTrashed();
}

Expand Down

0 comments on commit f96b27b

Please sign in to comment.