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 ;
2021-03-08 05:32:41 +08:00
use Flarum\Testing\integration\RetrievesAuthorizedUsers ;
use Flarum\Testing\integration\TestCase ;
2021-02-25 00:17:40 +08:00
use Flarum\User\User ;
use Illuminate\Support\Arr ;
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
2021-01-07 11:16:26 +08:00
/**
* @ inheritDoc
*/
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' => [
2021-02-25 00:17:40 +08:00
[ 'id' => 1 , 'title' => __CLASS__ , 'created_at' => Carbon :: createFromDate ( 1975 , 5 , 21 ) -> toDateTimeString (), 'last_posted_at' => Carbon :: createFromDate ( 1975 , 5 , 21 ) -> toDateTimeString (), 'user_id' => 1 , 'first_post_id' => 1 , 'comment_count' => 1 ],
[ 'id' => 2 , 'title' => 'lightsail in title' , 'created_at' => Carbon :: createFromDate ( 1985 , 5 , 21 ) -> toDateTimeString (), 'last_posted_at' => Carbon :: createFromDate ( 1985 , 5 , 21 ) -> toDateTimeString (), 'user_id' => 2 , 'comment_count' => 1 ],
[ 'id' => 3 , 'title' => 'not in title' , 'created_at' => Carbon :: createFromDate ( 1995 , 5 , 21 ) -> toDateTimeString (), 'last_posted_at' => Carbon :: createFromDate ( 1995 , 5 , 21 ) -> toDateTimeString (), 'user_id' => 2 , 'comment_count' => 1 ],
[ 'id' => 4 , 'title' => 'hidden' , 'created_at' => Carbon :: createFromDate ( 2005 , 5 , 21 ) -> toDateTimeString (), 'last_posted_at' => Carbon :: createFromDate ( 2005 , 5 , 21 ) -> toDateTimeString (), 'hidden_at' => Carbon :: now () -> toDateTimeString (), 'user_id' => 1 , 'comment_count' => 1 ],
2019-01-31 04:15:27 +08:00
],
'posts' => [
2021-02-25 00:17:40 +08:00
[ 'id' => 1 , 'discussion_id' => 1 , 'created_at' => Carbon :: createFromDate ( 1975 , 5 , 21 ) -> toDateTimeString (), 'user_id' => 1 , 'type' => 'comment' , 'content' => '<t><p>foo bar</p></t>' ],
[ 'id' => 2 , 'discussion_id' => 2 , 'created_at' => Carbon :: createFromDate ( 1985 , 5 , 21 ) -> toDateTimeString (), 'user_id' => 2 , 'type' => 'comment' , 'content' => '<t><p>not in text</p></t>' ],
[ 'id' => 3 , 'discussion_id' => 3 , 'created_at' => Carbon :: createFromDate ( 1995 , 5 , 21 ) -> toDateTimeString (), 'user_id' => 2 , 'type' => 'comment' , 'content' => '<t><p>lightsail in text</p></t>' ],
[ 'id' => 4 , 'discussion_id' => 4 , 'created_at' => Carbon :: createFromDate ( 2005 , 5 , 21 ) -> toDateTimeString (), 'user_id' => 1 , 'type' => 'comment' , 'content' => '<t><p>lightsail in text</p></t>' ],
2019-01-31 04:15:27 +08:00
],
'users' => [
$this -> normalUser (),
]
]);
}
2021-02-25 00:17:40 +08:00
/**
* Mark some discussions , but not others , as read to test that filter / gambit .
*/
protected function read ()
{
$user = User :: find ( 2 );
$user -> marked_all_as_read_at = Carbon :: createFromDate ( 1990 , 0 , 0 ) -> toDateTimeString ();
$user -> save ();
}
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 );
2021-02-25 00:17:40 +08:00
$this -> assertEquals ( 3 , count ( $data [ 'data' ]));
}
/**
* @ test
*/
public function author_filter_works ()
{
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' )
-> withQueryParams ([
'filter' => [ 'author' => 'normal' ],
'include' => 'mostRelevantPost' ,
])
);
$data = json_decode ( $response -> getBody () -> getContents (), true )[ 'data' ];
// Order-independent comparison
2021-03-05 22:43:35 +08:00
$this -> assertEqualsCanonicalizing ([ '2' , '3' ], Arr :: pluck ( $data , 'id' ), 'IDs do not match' );
2021-02-25 00:17:40 +08:00
}
/**
* @ test
*/
public function author_filter_works_negated ()
{
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' )
-> withQueryParams ([
'filter' => [ '-author' => 'normal' ],
'include' => 'mostRelevantPost' ,
])
);
$data = json_decode ( $response -> getBody () -> getContents (), true )[ 'data' ];
// Order-independent comparison
2021-03-05 22:43:35 +08:00
$this -> assertEquals ([ '1' ], Arr :: pluck ( $data , 'id' ), 'IDs do not match' );
2021-02-25 00:17:40 +08:00
}
/**
* @ test
*/
public function created_filter_works_with_date ()
{
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' )
-> withQueryParams ([
'filter' => [ 'created' => '1995-05-21' ],
'include' => 'mostRelevantPost' ,
])
);
$data = json_decode ( $response -> getBody () -> getContents (), true )[ 'data' ];
// Order-independent comparison
2021-03-05 22:43:35 +08:00
$this -> assertEquals ([ '3' ], Arr :: pluck ( $data , 'id' ), 'IDs do not match' );
2021-02-25 00:17:40 +08:00
}
/**
* @ test
*/
public function created_filter_works_negated_with_date ()
{
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' )
-> withQueryParams ([
'filter' => [ '-created' => '1995-05-21' ],
'include' => 'mostRelevantPost' ,
])
);
$data = json_decode ( $response -> getBody () -> getContents (), true )[ 'data' ];
// Order-independent comparison
2021-03-05 22:43:35 +08:00
$this -> assertEqualsCanonicalizing ([ '1' , '2' ], Arr :: pluck ( $data , 'id' ), 'IDs do not match' );
2021-02-25 00:17:40 +08:00
}
/**
* @ test
*/
public function created_filter_works_with_range ()
{
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' )
-> withQueryParams ([
'filter' => [ 'created' => '1980-05-21..2000-05-21' ],
'include' => 'mostRelevantPost' ,
])
);
$data = json_decode ( $response -> getBody () -> getContents (), true )[ 'data' ];
// Order-independent comparison
2021-03-05 22:43:35 +08:00
$this -> assertEqualsCanonicalizing ([ '2' , '3' ], Arr :: pluck ( $data , 'id' ), 'IDs do not match' );
2018-05-14 19:32:19 +08:00
}
2018-10-30 06:01:25 +08:00
/**
* @ test
*/
2021-02-25 00:17:40 +08:00
public function created_filter_works_negated_with_range ()
2018-10-30 06:01:25 +08:00
{
2020-03-27 20:39:38 +08:00
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' )
2021-02-25 00:17:40 +08:00
-> withQueryParams ([
'filter' => [ '-created' => '1980-05-21..2000-05-21' ],
'include' => 'mostRelevantPost' ,
])
);
$data = json_decode ( $response -> getBody () -> getContents (), true )[ 'data' ];
// Order-independent comparison
2021-03-05 22:43:35 +08:00
$this -> assertEquals ([ '1' ], Arr :: pluck ( $data , 'id' ), 'IDs do not match' );
2021-02-25 00:17:40 +08:00
}
/**
* @ test
*/
public function hidden_filter_works ()
{
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' , [ 'authenticatedAs' => 1 ])
-> withQueryParams ([
'filter' => [ 'hidden' => '' ],
'include' => 'mostRelevantPost' ,
])
);
$data = json_decode ( $response -> getBody () -> getContents (), true )[ 'data' ];
// Order-independent comparison
2021-03-05 22:43:35 +08:00
$this -> assertEquals ([ '4' ], Arr :: pluck ( $data , 'id' ), 'IDs do not match' );
2021-02-25 00:17:40 +08:00
}
/**
* @ test
*/
public function hidden_filter_works_negated ()
{
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' , [ 'authenticatedAs' => 1 ])
-> withQueryParams ([
'filter' => [ '-hidden' => '' ],
'include' => 'mostRelevantPost' ,
])
);
$data = json_decode ( $response -> getBody () -> getContents (), true )[ 'data' ];
// Order-independent comparison
2021-03-05 22:43:35 +08:00
$this -> assertEqualsCanonicalizing ([ '1' , '2' , '3' ], Arr :: pluck ( $data , 'id' ), 'IDs do not match' );
2021-02-25 00:17:40 +08:00
}
/**
* @ test
*/
public function unread_filter_works ()
{
$this -> app ();
$this -> read ();
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' , [ 'authenticatedAs' => 2 ])
2020-03-27 20:39:38 +08:00
-> withQueryParams ([
2021-02-25 00:17:40 +08:00
'filter' => [ 'unread' => '' ],
2020-03-27 20:39:38 +08:00
'include' => 'mostRelevantPost' ,
])
);
2018-10-30 06:01:25 +08:00
2021-02-25 00:17:40 +08:00
$data = json_decode ( $response -> getBody () -> getContents (), true )[ 'data' ];
// Order-independent comparison
2021-03-05 22:43:35 +08:00
$this -> assertEquals ([ '3' ], Arr :: pluck ( $data , 'id' ), 'IDs do not match' );
2021-02-25 00:17:40 +08:00
}
/**
* @ test
*/
public function unread_filter_works_when_negated ()
{
$this -> app ();
$this -> read ();
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' , [ 'authenticatedAs' => 2 ])
-> withQueryParams ([
'filter' => [ '-unread' => '' ],
'include' => 'mostRelevantPost' ,
])
);
$data = json_decode ( $response -> getBody () -> getContents (), true )[ 'data' ];
// Order-independent comparison
2021-03-05 22:43:35 +08:00
$this -> assertEqualsCanonicalizing ([ '1' , '2' ], Arr :: pluck ( $data , 'id' ), 'IDs do not match' );
2021-02-25 00:17:40 +08:00
}
/**
* @ test
*/
public function author_gambit_works ()
{
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' )
-> withQueryParams ([
'filter' => [ 'q' => 'author:normal' ],
'include' => 'mostRelevantPost' ,
])
);
$data = json_decode ( $response -> getBody () -> getContents (), true )[ 'data' ];
// Order-independent comparison
2021-03-05 22:43:35 +08:00
$this -> assertEqualsCanonicalizing ([ '2' , '3' ], Arr :: pluck ( $data , 'id' ), 'IDs do not match' );
2021-02-25 00:17:40 +08:00
}
/**
* @ test
*/
public function author_gambit_works_negated ()
{
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' )
-> withQueryParams ([
'filter' => [ 'q' => '-author:normal' ],
'include' => 'mostRelevantPost' ,
])
);
$data = json_decode ( $response -> getBody () -> getContents (), true )[ 'data' ];
// Order-independent comparison
2021-03-05 22:43:35 +08:00
$this -> assertEquals ([ '1' ], Arr :: pluck ( $data , 'id' ), 'IDs do not match' );
2021-02-25 00:17:40 +08:00
}
/**
* @ test
*/
public function created_gambit_works_with_date ()
{
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' )
-> withQueryParams ([
'filter' => [ 'q' => 'created:1995-05-21' ],
'include' => 'mostRelevantPost' ,
])
);
$data = json_decode ( $response -> getBody () -> getContents (), true )[ 'data' ];
// Order-independent comparison
2021-03-05 22:43:35 +08:00
$this -> assertEquals ([ '3' ], Arr :: pluck ( $data , 'id' ), 'IDs do not match' );
2021-02-25 00:17:40 +08:00
}
/**
* @ test
*/
public function created_gambit_works_negated_with_date ()
{
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' )
-> withQueryParams ([
'filter' => [ 'q' => '-created:1995-05-21' ],
'include' => 'mostRelevantPost' ,
])
);
$data = json_decode ( $response -> getBody () -> getContents (), true )[ 'data' ];
// Order-independent comparison
2021-03-05 22:43:35 +08:00
$this -> assertEqualsCanonicalizing ([ '1' , '2' ], Arr :: pluck ( $data , 'id' ), 'IDs do not match' );
2021-02-25 00:17:40 +08:00
}
/**
* @ test
*/
public function created_gambit_works_with_range ()
{
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' )
-> withQueryParams ([
'filter' => [ 'q' => 'created:1980-05-21..2000-05-21' ],
'include' => 'mostRelevantPost' ,
])
);
$data = json_decode ( $response -> getBody () -> getContents (), true )[ 'data' ];
// Order-independent comparison
2021-03-05 22:43:35 +08:00
$this -> assertEqualsCanonicalizing ([ '2' , '3' ], Arr :: pluck ( $data , 'id' ), 'IDs do not match' );
2021-02-25 00:17:40 +08:00
}
/**
* @ test
*/
public function created_gambit_works_negated_with_range ()
{
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' )
-> withQueryParams ([
'filter' => [ 'q' => '-created:1980-05-21..2000-05-21' ],
'include' => 'mostRelevantPost' ,
])
);
$data = json_decode ( $response -> getBody () -> getContents (), true )[ 'data' ];
// Order-independent comparison
2021-03-05 22:43:35 +08:00
$this -> assertEquals ([ '1' ], Arr :: pluck ( $data , 'id' ), 'IDs do not match' );
2021-02-25 00:17:40 +08:00
}
/**
* @ test
*/
public function hidden_gambit_works ()
{
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' , [ 'authenticatedAs' => 1 ])
-> withQueryParams ([
'filter' => [ 'q' => 'is:hidden' ],
'include' => 'mostRelevantPost' ,
])
);
$data = json_decode ( $response -> getBody () -> getContents (), true )[ 'data' ];
// Order-independent comparison
2021-03-05 22:43:35 +08:00
$this -> assertEquals ([ '4' ], Arr :: pluck ( $data , 'id' ), 'IDs do not match' );
2021-02-25 00:17:40 +08:00
}
/**
* @ test
*/
public function hidden_gambit_works_negated ()
{
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' , [ 'authenticatedAs' => 1 ])
-> withQueryParams ([
'filter' => [ 'q' => '-is:hidden' ],
'include' => 'mostRelevantPost' ,
])
);
$data = json_decode ( $response -> getBody () -> getContents (), true )[ 'data' ];
// Order-independent comparison
2021-03-05 22:43:35 +08:00
$this -> assertEqualsCanonicalizing ([ '1' , '2' , '3' ], Arr :: pluck ( $data , 'id' ), 'IDs do not match' );
2021-02-25 00:17:40 +08:00
}
/**
* @ test
*/
public function unread_gambit_works ()
{
$this -> app ();
$this -> read ();
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' , [ 'authenticatedAs' => 2 ])
-> withQueryParams ([
'filter' => [ 'q' => 'is:unread' ],
'include' => 'mostRelevantPost' ,
])
);
$data = json_decode ( $response -> getBody () -> getContents (), true )[ 'data' ];
// Order-independent comparison
2021-03-05 22:43:35 +08:00
$this -> assertEquals ([ '3' ], Arr :: pluck ( $data , 'id' ), 'IDs do not match' );
2021-02-25 00:17:40 +08:00
}
/**
* @ test
*/
public function unread_gambit_works_when_negated ()
{
$this -> app ();
$this -> read ();
$response = $this -> send (
$this -> request ( 'GET' , '/api/discussions' , [ 'authenticatedAs' => 2 ])
-> withQueryParams ([
'filter' => [ 'q' => '-is:unread' ],
'include' => 'mostRelevantPost' ,
])
);
$data = json_decode ( $response -> getBody () -> getContents (), true )[ 'data' ];
// Order-independent comparison
2021-03-05 22:43:35 +08:00
$this -> assertEqualsCanonicalizing ([ '1' , '2' ], Arr :: pluck ( $data , 'id' ), 'IDs do not match' );
2018-10-30 06:01:25 +08:00
}
2018-05-14 19:32:19 +08:00
}