fix: slug transliteration assuming the language is always en (#3387)

* fix: transliteration assuming the language is always `en`
* test: transliteration works with forum locale
This commit is contained in:
Sami Mazouz 2022-04-16 11:31:23 +01:00 committed by GitHub
parent cca97f32c6
commit fb717db57c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 1 deletions

View File

@ -18,6 +18,7 @@ use Flarum\Discussion\Event\Renamed;
use Flarum\Discussion\Event\Restored;
use Flarum\Discussion\Event\Started;
use Flarum\Foundation\EventGeneratorTrait;
use Flarum\Locale\LocaleManager;
use Flarum\Notification\Notification;
use Flarum\Post\MergeableInterface;
use Flarum\Post\Post;
@ -445,6 +446,6 @@ class Discussion extends AbstractModel
protected function setTitleAttribute($title)
{
$this->attributes['title'] = $title;
$this->slug = Str::slug($title);
$this->slug = Str::slug($title, '-', resolve(LocaleManager::class)->getLocale());
}
}

View File

@ -133,6 +133,35 @@ class CreateTest extends TestCase
$this->assertEquals('test - too-obscure', Arr::get($data, 'data.attributes.title'));
}
/**
* @test
*/
public function can_create_discussion_with_current_lang_slug_transliteration()
{
$this->app()->getContainer()->make('flarum.locales')->setLocale('zh');
$response = $this->send(
$this->request('POST', '/api/discussions', [
'authenticatedAs' => 1,
'json' => [
'data' => [
'attributes' => [
'title' => '我是一个土豆',
'content' => 'predetermined content for automated testing',
],
]
],
])
);
$this->assertEquals(201, $response->getStatusCode());
/** @var Discussion $discussion */
$discussion = Discussion::firstOrFail();
$this->assertEquals('wo-shi-yi-ge-tu-dou', $discussion->slug);
}
/**
* @test
*/