mirror of
https://github.com/flarum/framework.git
synced 2024-12-04 16:23:37 +08:00
ae280016e7
Previously, the `prepareDatabase` method would directly modify the database, booting the app in the process. This would prevent any extenders from being applied, since `->extend()` has no effect once the app is booted. Since the new implementation of `prepareDatabase` simply registers seed data to be applied during app boot, the workaround of sticking this seed data into `prepDb` is no longer necessary, and seed data common to all test cases in a class can be provided in `setUp`. When needed, app boot is explicitly triggered in individual test cases by calling `$this->app()`.
83 lines
2.1 KiB
PHP
83 lines
2.1 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of Flarum.
|
|
*
|
|
* For detailed copyright and license information, please view the
|
|
* LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Flarum\Tests\integration\extenders;
|
|
|
|
use Flarum\Database\AbstractModel;
|
|
use Flarum\Extend;
|
|
use Flarum\Http\SlugDriverInterface;
|
|
use Flarum\Http\SlugManager;
|
|
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
|
|
use Flarum\Tests\integration\TestCase;
|
|
use Flarum\User\User;
|
|
|
|
class ModelUrlTest extends TestCase
|
|
{
|
|
use RetrievesAuthorizedUsers;
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$userClass = User::class;
|
|
$this->prepareDatabase([
|
|
'users' => [
|
|
$this->normalUser(),
|
|
],
|
|
'settings' => [
|
|
['key' => "slug_driver_$userClass", 'value' => 'testDriver'],
|
|
]
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function uses_default_driver_by_default()
|
|
{
|
|
$slugManager = $this->app()->getContainer()->make(SlugManager::class);
|
|
|
|
$testUser = User::find(1);
|
|
|
|
$this->assertEquals('admin', $slugManager->forResource(User::class)->toSlug($testUser));
|
|
$this->assertEquals('1', $slugManager->forResource(User::class)->fromSlug('admin', $testUser)->id);
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function custom_slug_driver_has_effect_if_added()
|
|
{
|
|
$this->extend((new Extend\ModelUrl(User::class))->addSlugDriver('testDriver', TestSlugDriver::class));
|
|
|
|
$slugManager = $this->app()->getContainer()->make(SlugManager::class);
|
|
|
|
$testUser = User::find(1);
|
|
|
|
$this->assertEquals('test-slug', $slugManager->forResource(User::class)->toSlug($testUser));
|
|
$this->assertEquals('1', $slugManager->forResource(User::class)->fromSlug('random-gibberish', $testUser)->id);
|
|
}
|
|
}
|
|
|
|
class TestSlugDriver implements SlugDriverInterface
|
|
{
|
|
public function toSlug(AbstractModel $instance): string
|
|
{
|
|
return 'test-slug';
|
|
}
|
|
|
|
public function fromSlug(string $slug, User $actor): AbstractModel
|
|
{
|
|
return User::find(1);
|
|
}
|
|
}
|