Skip to content

Commit

Permalink
Fix #1252 - Column not found when extending the User (or Group) model
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Apr 24, 2024
1 parent 3970661 commit d993625
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 5 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.1](https://github.com/userfrosting/sprinkle-account/compare/5.1.0...5.1.1)
- [Fix #1252](https://github.com/userfrosting/UserFrosting/issues/1252) - Column not found when extending the User (or Group) model

## [5.1.0](https://github.com/userfrosting/sprinkle-account/compare/5.0.1...5.1.0)
- Drop PHP 8.1 support, add PHP 8.3 support
- Update to Laravel 10
Expand Down
7 changes: 7 additions & 0 deletions app/src/Database/Models/Interfaces/UserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@ public function passwordResets(): HasMany;
*/
public function permissions(): BelongsToManyThrough;

/**
* Get all verification request for this user.
*
* @return HasMany
*/
public function verifications(): HasMany;

/**
* Get all persistence items for this user.
*
Expand Down
18 changes: 13 additions & 5 deletions app/src/Database/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ public function activities(): HasMany
/** @var string */
$relation = static::$ci?->get(ActivityInterface::class);

return $this->hasMany($relation);
// Define foreign key in case User is extended
return $this->hasMany($relation, 'user_id');
}

/**
Expand Down Expand Up @@ -392,6 +393,7 @@ public function group(): BelongsTo
/** @var string */
$relation = static::$ci?->get(GroupInterface::class);

// Define foreign key in case User is extended
return $this->belongsTo($relation);
}

Expand All @@ -405,7 +407,8 @@ public function passwordResets(): HasMany
/** @var string */
$relation = static::$ci?->get(PasswordResetInterface::class);

return $this->hasMany($relation);
// Define foreign key in case User is extended
return $this->hasMany($relation, 'user_id');
}

/**
Expand All @@ -418,7 +421,8 @@ public function verifications(): HasMany
/** @var string */
$relation = static::$ci?->get(VerificationInterface::class);

return $this->hasMany($relation);
// Define foreign key in case User is extended
return $this->hasMany($relation, 'user_id');
}

/**
Expand All @@ -431,7 +435,8 @@ public function persistences(): HasMany
/** @var string */
$relation = static::$ci?->get(PersistenceInterface::class);

return $this->hasMany($relation);
// Define foreign key in case User is extended
return $this->hasMany($relation, 'user_id');
}

/**
Expand All @@ -447,10 +452,12 @@ public function permissions(): BelongsToManyThrough
/** @var class-string */
$roleRelation = static::$ci?->get(RoleInterface::class);

// Define foreign keys in case User is extended
return $this->belongsToManyThrough(
$permissionRelation,
$roleRelation,
firstJoiningTable: 'role_users',
firstForeignPivotKey: 'user_id',
secondJoiningTable: 'permission_roles',
);
}
Expand All @@ -465,7 +472,8 @@ public function roles(): BelongsToMany
/** @var string */
$relation = static::$ci?->get(RoleInterface::class);

return $this->belongsToMany($relation, 'role_users')
// Define foreign keys in case User is extended
return $this->belongsToMany($relation, 'role_users', 'user_id')
->using(RoleUsers::class)
->withTimestamps();
}
Expand Down
26 changes: 26 additions & 0 deletions app/tests/Database/Models/GroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace UserFrosting\Sprinkle\Account\Tests\Database\Models;

use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\Attributes\TestWith;
use UserFrosting\Sprinkle\Account\Database\Models\Group;
use UserFrosting\Sprinkle\Account\Database\Models\Interfaces\GroupInterface;
use UserFrosting\Sprinkle\Account\Database\Models\User;
Expand Down Expand Up @@ -90,4 +91,29 @@ public function testUserRelation(): void
// Assert reverse relation
$this->assertSame($group->id, $user->group?->id);
}

/**
* Test relations setup are working, even if the class is extended
* @see https://github.com/userfrosting/UserFrosting/issues/1252
*
* @param class-string<GroupInterface> $model
*/
#[TestWith([Group::class])]
#[TestWith([ZeGroup::class])]
public function testRelations(string $model): void
{
$object = new $model([
'slug' => 'testing',
'name' => 'Test Group',
]);
$object->save();

// Fetch each relation - They each will be empty
// A real query is required to trigger a SQL Exception
$this->assertCount(0, $object->users()->get());
}
}

class ZeGroup extends Group
{
}
35 changes: 35 additions & 0 deletions app/tests/Database/Models/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Illuminate\Cache\Repository as Cache;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\Attributes\TestWith;
use UserFrosting\Config\Config;
use UserFrosting\Sprinkle\Account\Database\Models\Interfaces\UserInterface;
use UserFrosting\Sprinkle\Account\Database\Models\User;
Expand Down Expand Up @@ -204,4 +205,38 @@ public function testFindCacheWithNullUser(): void
// N.B.: Laravel cache won't store anything if the value is null.
$this->assertFalse($cache->has($key));
}

/**
* Test relations setup are working, even if the class is extended
* @see https://github.com/userfrosting/UserFrosting/issues/1252
*
* @param class-string<UserInterface> $model
*/
#[TestWith([User::class])]
#[TestWith([Member::class])]
public function testRelations(string $model): void
{
$object = new $model([
'user_name' => 'testing',
'email' => '[email protected]',
'first_name' => 'Test',
'last_name' => 'Ing',
'password' => 'secret',
]);
$object->save();

// Fetch each relation - They each will be empty
// A real query is required to trigger a SQL Exception
$this->assertCount(0, $object->activities()->get());
$this->assertCount(0, $object->group()->get());
$this->assertCount(0, $object->passwordResets()->get());
$this->assertCount(0, $object->verifications()->get());
$this->assertCount(0, $object->persistences()->get());
$this->assertCount(0, $object->permissions()->get());
$this->assertCount(0, $object->roles()->get());
}
}

class Member extends User
{
}

0 comments on commit d993625

Please sign in to comment.