2021-01-08 06:23:56 +08:00
< ? 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\api\discussions ;
use Carbon\Carbon ;
2021-03-08 05:32:41 +08:00
use Flarum\Testing\integration\RetrievesAuthorizedUsers ;
use Flarum\Testing\integration\TestCase ;
2021-01-08 06:23:56 +08:00
2021-04-12 10:21:56 +08:00
class ListWithFulltextSearchTest extends TestCase
2021-01-08 06:23:56 +08:00
{
use RetrievesAuthorizedUsers ;
/**
* @ inheritDoc
*/
protected function setUp () : void
{
parent :: setUp ();
$this -> database () -> rollBack ();
// We need to insert these outside of a transaction, because FULLTEXT indexing,
// which is needed for search, doesn't happen in transactions.
// 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 ],
2021-04-21 07:16:59 +08:00
[ 'id' => 2 , 'title' => 'lightsail in title too' , 'created_at' => Carbon :: createFromDate ( 2020 , 01 , 01 ) -> toDateTimeString (), 'user_id' => 1 , 'comment_count' => 1 ],
2021-04-12 10:21:56 +08:00
[ 'id' => 3 , 'title' => 'not in title either' , 'created_at' => Carbon :: now () -> toDateTimeString (), 'user_id' => 1 , 'comment_count' => 1 ],
2021-04-21 07:16:59 +08:00
[ 'id' => 4 , 'title' => 'not in title or text' , 'created_at' => Carbon :: now () -> toDateTimeString (), 'user_id' => 1 , 'comment_count' => 1 ],
2021-01-08 06:23:56 +08:00
]);
$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>' ],
2021-04-12 10:21:56 +08:00
[ '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>' ],
2021-04-21 07:16:59 +08:00
[ 'id' => 5 , 'discussion_id' => 4 , 'created_at' => Carbon :: now () -> toDateTimeString (), 'user_id' => 1 , 'type' => 'comment' , 'content' => '<t><p>not in title or text</p></t>' ],
2021-01-08 06:23:56 +08:00
]);
// We need to call these again, since we rolled back the transaction started by `::app()`.
$this -> database () -> beginTransaction ();
$this -> populateDatabase ();
}
/**
* @ inheritDoc
*/
protected function tearDown () : void
{
parent :: tearDown ();
2021-04-21 07:16:59 +08:00
$this -> database () -> table ( 'discussions' ) -> delete ();
$this -> database () -> table ( 'posts' ) -> delete ();
2021-01-08 06:23:56 +08:00
}
/**
* @ test
*/
2021-04-21 07:16:59 +08:00
public function can_search_for_word_or_title_in_post ()
2021-01-08 06:23:56 +08:00
{
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' )
-> withQueryParams ([
'filter' => [ 'q' => 'lightsail' ],
'include' => 'mostRelevantPost' ,
])
);
$data = json_decode ( $response -> getBody () -> getContents (), true );
$ids = array_map ( function ( $row ) {
return $row [ 'id' ];
}, $data [ 'data' ]);
2021-04-21 07:16:59 +08:00
$this -> assertEquals ([ '2' , '1' , '3' ], $ids , 'IDs do not match' );
2021-01-08 06:23:56 +08:00
}
/**
* @ test
*/
public function ignores_non_word_characters_when_searching ()
{
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' )
-> withQueryParams ([
'filter' => [ 'q' => 'lightsail+' ],
'include' => 'mostRelevantPost' ,
])
);
$data = json_decode ( $response -> getBody () -> getContents (), true );
$ids = array_map ( function ( $row ) {
return $row [ 'id' ];
}, $data [ 'data' ]);
2021-04-21 07:16:59 +08:00
$this -> assertEquals ([ '2' , '1' , '3' ], $ids , 'IDs do not match' );
2021-01-08 06:23:56 +08:00
}
/**
* @ test
*/
public function search_for_special_characters_gives_empty_result ()
{
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' )
-> withQueryParams ([
'filter' => [ 'q' => '*' ],
'include' => 'mostRelevantPost' ,
])
);
$data = json_decode ( $response -> getBody () -> getContents (), true );
$this -> assertEquals ([], $data [ 'data' ]);
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' )
-> withQueryParams ([
'filter' => [ 'q' => '@' ],
'include' => 'mostRelevantPost' ,
])
);
$data = json_decode ( $response -> getBody () -> getContents (), true );
$this -> assertEquals ([], $data [ 'data' ]);
}
}