mirror of
https://github.com/flarum/core.git
synced 2025-08-06 16:36:47 +02:00
Fix Callables for Extenders (#2423)
- Standardize signatures and variable names for extenders that take callbacks - Adjust model extender docblock to clarify that default calue can't be an invokable class. - Make invokable classes provided to Model->relationship - Add integration tests to ensure Model->relationship and User->groupProcessor extenders accept callbacks - Extract code for wrapping callbacks into central util
This commit is contained in:
committed by
GitHub
parent
c10cc92deb
commit
47d2eee9ce
@@ -134,6 +134,23 @@ class ModelTest extends TestCase
|
||||
$this->assertEquals([], $user->customRelation()->get()->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function custom_relationship_can_be_invokable_class()
|
||||
{
|
||||
$this->extend(
|
||||
(new Extend\Model(User::class))
|
||||
->relationship('customRelation', CustomRelationClass::class)
|
||||
);
|
||||
|
||||
$this->prepDB();
|
||||
|
||||
$user = User::find(1);
|
||||
|
||||
$this->assertEquals([], $user->customRelation()->get()->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
@@ -423,3 +440,11 @@ class ModelTestCustomPost extends AbstractEventPost
|
||||
*/
|
||||
public static $type = 'customPost';
|
||||
}
|
||||
|
||||
class CustomRelationClass
|
||||
{
|
||||
public function __invoke(User $user)
|
||||
{
|
||||
return $user->hasMany(Discussion::class, 'user_id');
|
||||
}
|
||||
}
|
||||
|
@@ -88,6 +88,19 @@ class UserTest extends TestCase
|
||||
|
||||
$this->assertNotContains('viewUserList', $user->getPermissions());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function processor_can_be_invokable_class()
|
||||
{
|
||||
$this->extend((new Extend\User)->permissionGroups(CustomGroupProcessorClass::class));
|
||||
|
||||
$this->prepDb();
|
||||
$user = User::find(2);
|
||||
|
||||
$this->assertNotContains('viewUserList', $user->getPermissions());
|
||||
}
|
||||
}
|
||||
|
||||
class CustomDisplayNameDriver implements DriverInterface
|
||||
@@ -97,3 +110,13 @@ class CustomDisplayNameDriver implements DriverInterface
|
||||
return $user->email.'$$$suffix';
|
||||
}
|
||||
}
|
||||
|
||||
class CustomGroupProcessorClass
|
||||
{
|
||||
public function __invoke(User $user, array $groupIds)
|
||||
{
|
||||
return array_filter($groupIds, function ($id) {
|
||||
return $id != 3;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user