1
0
mirror of https://github.com/flarum/core.git synced 2025-08-06 16:36:47 +02:00

Fix relevance sort (#2773)

- Adds a field to QueryCriteria that determines whether the sort provided is the controller's default sort
- Set this field to true iff sort not in query params. Default it to false
- Override $sort if a new default sort has been set on search state, and the param is true.
- Add tests!
This commit is contained in:
Alexander Skvortsov
2021-04-11 22:21:56 -04:00
committed by GitHub
parent 598bb94657
commit 0fb3548f46
9 changed files with 42 additions and 17 deletions

View File

@@ -13,7 +13,7 @@ use Carbon\Carbon;
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
use Flarum\Testing\integration\TestCase;
class ListTest extends TestCase
class ListWithFulltextSearchTest extends TestCase
{
use RetrievesAuthorizedUsers;
@@ -31,12 +31,15 @@ class ListTest extends TestCase
// We clean it up explcitly at the end.
$this->database()->table('discussions')->insert([
['id' => 1, 'title' => 'lightsail in title', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'comment_count' => 1],
['id' => 2, 'title' => 'not in title', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'comment_count' => 1],
['id' => 2, 'title' => 'not in title and older', 'created_at' => Carbon::createFromDate(2020, 01, 01)->toDateTimeString(), 'user_id' => 1, 'comment_count' => 1],
['id' => 3, 'title' => 'not in title either', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'comment_count' => 1],
]);
$this->database()->table('posts')->insert([
['id' => 1, 'discussion_id' => 1, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '<t><p>not in text</p></t>'],
['id' => 2, 'discussion_id' => 2, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '<t><p>lightsail in text</p></t>'],
['id' => 3, 'discussion_id' => 2, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '<t><p>another lightsail for discussion 2!</p></t>'],
['id' => 4, 'discussion_id' => 3, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '<t><p>just one lightsail for discussion 3.</p></t>'],
]);
// We need to call these again, since we rolled back the transaction started by `::app()`.
@@ -52,8 +55,8 @@ class ListTest extends TestCase
{
parent::tearDown();
$this->database()->table('discussions')->whereIn('id', [1, 2])->delete();
$this->database()->table('posts')->whereIn('id', [1, 2])->delete();
$this->database()->table('discussions')->whereIn('id', [1, 2, 3])->delete();
$this->database()->table('posts')->whereIn('id', [1, 2, 3, 4])->delete();
}
/**
@@ -74,8 +77,7 @@ class ListTest extends TestCase
return $row['id'];
}, $data['data']);
// Order-independent comparison
$this->assertEquals(['3'], $ids, 'IDs do not match');
$this->assertEquals(['2', '3'], $ids, 'IDs do not match');
}
/**
@@ -96,8 +98,7 @@ class ListTest extends TestCase
return $row['id'];
}, $data['data']);
// Order-independent comparison
$this->assertEquals(['3'], $ids, 'IDs do not match');
$this->assertEquals(['2', '3'], $ids, 'IDs do not match');
}
/**