2018-05-14 19:32:19 +08:00
< ? php
/*
* This file is part of Flarum .
*
2019-11-28 08:16:50 +08:00
* For detailed copyright and license information , please view the
* LICENSE file that was distributed with this source code .
2018-05-14 19:32:19 +08:00
*/
2020-03-27 20:39:38 +08:00
namespace Flarum\Tests\integration\api\discussions ;
2018-05-14 19:32:19 +08:00
2019-01-31 04:15:27 +08:00
use Carbon\Carbon ;
2020-03-27 20:39:38 +08:00
use Flarum\Tests\integration\RetrievesAuthorizedUsers ;
use Flarum\Tests\integration\TestCase ;
2018-05-14 19:32:19 +08:00
2020-03-27 20:39:38 +08:00
class ListTest extends TestCase
2018-05-14 19:32:19 +08:00
{
2020-03-27 20:39:38 +08:00
use RetrievesAuthorizedUsers ;
2018-05-14 19:32:19 +08:00
2020-03-28 08:26:26 +08:00
protected function setUp () : void
2019-01-31 04:15:27 +08:00
{
parent :: setUp ();
$this -> prepareDatabase ([
'discussions' => [
[ 'id' => 1 , 'title' => __CLASS__ , 'created_at' => Carbon :: now () -> toDateTimeString (), 'user_id' => 2 , 'first_post_id' => 1 , 'comment_count' => 1 ],
],
'posts' => [
[ 'id' => 1 , 'discussion_id' => 1 , 'created_at' => Carbon :: now () -> toDateTimeString (), 'user_id' => 2 , 'type' => 'comment' , 'content' => '<t><p>foo bar</p></t>' ],
],
'users' => [
$this -> normalUser (),
],
'groups' => [
$this -> memberGroup (),
$this -> guestGroup (),
],
'group_permission' => [
[ 'permission' => 'viewDiscussions' , 'group_id' => 2 ],
]
]);
}
2018-05-14 19:32:19 +08:00
/**
* @ test
*/
public function shows_index_for_guest ()
{
2020-03-27 20:39:38 +08:00
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' )
);
2018-05-14 19:32:19 +08:00
$this -> assertEquals ( 200 , $response -> getStatusCode ());
$data = json_decode ( $response -> getBody () -> getContents (), true );
2019-01-31 04:15:27 +08:00
$this -> assertEquals ( 1 , count ( $data [ 'data' ]));
2018-05-14 19:32:19 +08:00
}
2018-10-30 06:01:25 +08:00
/**
* @ test
*/
public function can_search_for_author ()
{
2020-03-27 20:39:38 +08:00
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' )
-> withQueryParams ([
'filter' => [ 'q' => 'author:normal foo' ],
'include' => 'mostRelevantPost' ,
])
);
2018-10-30 06:01:25 +08:00
$this -> assertEquals ( 200 , $response -> getStatusCode ());
}
2019-03-07 07:21:43 +08:00
/**
* @ test
*/
2019-10-26 21:41:39 +08:00
public function can_search_for_word_in_post ()
2019-03-07 07:21:43 +08:00
{
2019-10-27 23:37:55 +08:00
$this -> database () -> table ( 'discussions' ) -> insert ([
[ 'id' => 2 , 'title' => 'lightsail in title' , 'created_at' => Carbon :: now () -> toDateTimeString (), 'user_id' => 2 , 'comment_count' => 1 ],
[ 'id' => 3 , 'title' => 'not in title' , 'created_at' => Carbon :: now () -> toDateTimeString (), 'user_id' => 2 , 'comment_count' => 1 ],
]);
2019-03-07 07:21:43 +08:00
$this -> database () -> table ( 'posts' ) -> insert ([
[ 'id' => 2 , 'discussion_id' => 2 , 'created_at' => Carbon :: now () -> toDateTimeString (), 'user_id' => 2 , 'type' => 'comment' , 'content' => '<t><p>not in text</p></t>' ],
[ 'id' => 3 , 'discussion_id' => 3 , 'created_at' => Carbon :: now () -> toDateTimeString (), 'user_id' => 2 , 'type' => 'comment' , 'content' => '<t><p>lightsail in text</p></t>' ],
]);
2020-03-27 20:39:38 +08:00
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' )
-> withQueryParams ([
'filter' => [ 'q' => 'lightsail' ],
'include' => 'mostRelevantPost' ,
])
);
2019-03-07 07:21:43 +08:00
$data = json_decode ( $response -> getBody () -> getContents (), true );
2019-03-07 07:22:15 +08:00
$ids = array_map ( function ( $row ) {
return $row [ 'id' ];
}, $data [ 'data' ]);
2019-03-07 07:21:43 +08:00
// Order-independent comparison
2019-10-26 21:41:39 +08:00
$this -> assertEquals ([ '3' ], $ids , 'IDs do not match' , 0.0 , 10 , true );
2019-03-07 07:21:43 +08:00
}
2019-07-24 05:55:06 +08:00
/**
* @ test
*/
public function ignores_non_word_characters_when_searching ()
{
2019-10-27 23:37:55 +08:00
$this -> database () -> table ( 'discussions' ) -> insert ([
[ 'id' => 2 , 'title' => 'lightsail in title' , 'created_at' => Carbon :: now () -> toDateTimeString (), 'user_id' => 2 , 'comment_count' => 1 ],
[ 'id' => 3 , 'title' => 'not in title' , 'created_at' => Carbon :: now () -> toDateTimeString (), 'user_id' => 2 , 'comment_count' => 1 ],
]);
2019-07-24 05:55:06 +08:00
$this -> database () -> table ( 'posts' ) -> insert ([
[ 'id' => 2 , 'discussion_id' => 2 , 'created_at' => Carbon :: now () -> toDateTimeString (), 'user_id' => 2 , 'type' => 'comment' , 'content' => '<t><p>not in text</p></t>' ],
[ 'id' => 3 , 'discussion_id' => 3 , 'created_at' => Carbon :: now () -> toDateTimeString (), 'user_id' => 2 , 'type' => 'comment' , 'content' => '<t><p>lightsail in text</p></t>' ],
]);
2020-03-27 20:39:38 +08:00
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' )
-> withQueryParams ([
'filter' => [ 'q' => 'lightsail+' ],
'include' => 'mostRelevantPost' ,
])
);
2019-07-24 05:55:06 +08:00
$data = json_decode ( $response -> getBody () -> getContents (), true );
$ids = array_map ( function ( $row ) {
return $row [ 'id' ];
}, $data [ 'data' ]);
// Order-independent comparison
2019-10-26 21:41:39 +08:00
$this -> assertEquals ([ '3' ], $ids , 'IDs do not match' , 0.0 , 10 , true );
2019-07-24 05:55:06 +08:00
}
/**
* @ test
*/
public function search_for_special_characters_gives_empty_result ()
{
2020-03-27 20:39:38 +08:00
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' )
-> withQueryParams ([
'filter' => [ 'q' => '*' ],
'include' => 'mostRelevantPost' ,
])
);
2019-07-24 05:55:06 +08:00
$data = json_decode ( $response -> getBody () -> getContents (), true );
$this -> assertEquals ([], $data [ 'data' ]);
2020-03-27 20:39:38 +08:00
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' )
-> withQueryParams ([
'filter' => [ 'q' => '@' ],
'include' => 'mostRelevantPost' ,
])
);
2019-07-24 05:55:06 +08:00
$data = json_decode ( $response -> getBody () -> getContents (), true );
$this -> assertEquals ([], $data [ 'data' ]);
}
2018-05-14 19:32:19 +08:00
}