2017-09-19 22:04:40 +02:00
< ? php
require_once ( __DIR__ . " /../lib/UloggerAPITestCase.php " );
if ( ! defined ( " ROOT_DIR " )) { define ( " ROOT_DIR " , __DIR__ . " /../.. " ); }
require_once ( ROOT_DIR . " /helpers/config.php " );
2019-02-25 10:04:09 +01:00
require_once ( ROOT_DIR . " /helpers/lang.php " );
2017-09-19 22:04:40 +02:00
class InternalAPITest extends UloggerAPITestCase {
/* getpositions */
public function testGetPositionsAdmin () {
$this -> assertTrue ( $this -> authenticate (), " Authentication failed " );
$trackId = $this -> addTestTrack ( $this -> testUserId );
$this -> addTestPosition ( $this -> testUserId , $trackId , $this -> testTimestamp );
$this -> addTestPosition ( $this -> testUserId , $trackId , $this -> testTimestamp + 1 );
$this -> assertEquals ( 1 , $this -> getConnection () -> getRowCount ( " tracks " ), " Wrong row count " );
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " positions " ), " Wrong row count " );
$options = [
" http_errors " => false ,
2019-01-24 19:07:41 +01:00
" query " => [ " userid " => $this -> testUserId , " trackid " => $trackId ],
2017-09-19 22:04:40 +02:00
];
2019-01-24 19:07:41 +01:00
$response = $this -> http -> get ( " /utils/getpositions.php " , $options );
2017-09-19 22:04:40 +02:00
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals ( count ( $json ), 2 , " Wrong count of positions " );
2017-09-19 22:04:40 +02:00
2020-01-07 17:21:40 +01:00
$position = $json [ 0 ];
$this -> assertEquals (( int ) $position -> id , 1 , " Wrong position id " );
2017-09-19 22:04:40 +02:00
$this -> assertEquals (( float ) $position -> latitude , $this -> testLat , " Wrong latitude " );
$this -> assertEquals (( float ) $position -> longitude , $this -> testLon , " Wrong longitude " );
$this -> assertEquals (( int ) $position -> timestamp , $this -> testTimestamp , " Wrong timestamp " );
$this -> assertEquals (( string ) $position -> username , $this -> testAdminUser , " Wrong username " );
$this -> assertEquals (( string ) $position -> trackname , $this -> testTrackName , " Wrong trackname " );
2020-01-07 17:21:40 +01:00
$position = $json [ 1 ];
$this -> assertEquals (( int ) $position -> id , 2 , " Wrong position id " );
2017-09-19 22:04:40 +02:00
$this -> assertEquals (( float ) $position -> latitude , $this -> testLat , " Wrong latitude " );
$this -> assertEquals (( float ) $position -> longitude , $this -> testLon , " Wrong longitude " );
$this -> assertEquals (( int ) $position -> timestamp , $this -> testTimestamp + 1 , " Wrong timestamp " );
$this -> assertEquals (( string ) $position -> username , $this -> testAdminUser , " Wrong username " );
$this -> assertEquals (( string ) $position -> trackname , $this -> testTrackName , " Wrong trackname " );
}
public function testGetPositionsUser () {
$userId = $this -> addTestUser ( $this -> testUser , password_hash ( $this -> testPass , PASSWORD_DEFAULT ));
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
$this -> assertTrue ( $this -> authenticate ( $this -> testUser , $this -> testPass ), " Authentication failed " );
$trackId = $this -> addTestTrack ( $userId );
$this -> addTestPosition ( $userId , $trackId , $this -> testTimestamp );
$this -> addTestPosition ( $userId , $trackId , $this -> testTimestamp + 1 );
$this -> assertEquals ( 1 , $this -> getConnection () -> getRowCount ( " tracks " ), " Wrong row count " );
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " positions " ), " Wrong row count " );
$options = [
" http_errors " => false ,
2019-01-24 19:07:41 +01:00
" query " => [ " userid " => $userId , " trackid " => $trackId ],
2017-09-19 22:04:40 +02:00
];
2019-01-24 19:07:41 +01:00
$response = $this -> http -> get ( " /utils/getpositions.php " , $options );
2017-09-19 22:04:40 +02:00
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals ( count ( $json ), 2 , " Wrong count of positions " );
2017-09-19 22:04:40 +02:00
2020-01-07 17:21:40 +01:00
$position = $json [ 0 ];
$this -> assertEquals (( int ) $position -> id , 1 , " Wrong position id " );
2017-09-19 22:04:40 +02:00
$this -> assertEquals (( float ) $position -> latitude , $this -> testLat , " Wrong latitude " );
$this -> assertEquals (( float ) $position -> longitude , $this -> testLon , " Wrong longitude " );
$this -> assertEquals (( int ) $position -> timestamp , $this -> testTimestamp , " Wrong timestamp " );
$this -> assertEquals (( string ) $position -> username , $this -> testUser , " Wrong username " );
$this -> assertEquals (( string ) $position -> trackname , $this -> testTrackName , " Wrong trackname " );
2020-01-07 17:21:40 +01:00
$position = $json [ 1 ];
$this -> assertEquals (( int ) $position -> id , 2 , " Wrong position id " );
2017-09-19 22:04:40 +02:00
$this -> assertEquals (( float ) $position -> latitude , $this -> testLat , " Wrong latitude " );
$this -> assertEquals (( float ) $position -> longitude , $this -> testLon , " Wrong longitude " );
$this -> assertEquals (( int ) $position -> timestamp , $this -> testTimestamp + 1 , " Wrong timestamp " );
$this -> assertEquals (( string ) $position -> username , $this -> testUser , " Wrong username " );
$this -> assertEquals (( string ) $position -> trackname , $this -> testTrackName , " Wrong trackname " );
}
public function testGetPositionsOtherUser () {
$userId = $this -> addTestUser ( $this -> testUser , password_hash ( $this -> testPass , PASSWORD_DEFAULT ));
$this -> assertEquals ( 2 , $userId );
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
$this -> assertTrue ( $this -> authenticate ( $this -> testUser , $this -> testPass ), " Authentication failed " );
$trackId = $this -> addTestTrack ( $userId );
$this -> addTestPosition ( $userId , $trackId , $this -> testTimestamp );
$this -> addTestPosition ( $userId , $trackId , $this -> testTimestamp + 1 );
$this -> assertEquals ( 1 , $this -> getConnection () -> getRowCount ( " tracks " ), " Wrong row count " );
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " positions " ), " Wrong row count " );
$options = [
" http_errors " => false ,
2019-01-24 19:07:41 +01:00
" query " => [ " userid " => $this -> testUserId , " trackid " => $trackId ],
2017-09-19 22:04:40 +02:00
];
2019-01-24 19:07:41 +01:00
$response = $this -> http -> get ( " /utils/getpositions.php " , $options );
2017-09-19 22:04:40 +02:00
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals ( count ( $json ), 0 , " Wrong count of positions " );
2017-09-19 22:04:40 +02:00
}
public function testGetPositionsOtherUserByAdmin () {
$userId = $this -> addTestUser ( $this -> testUser , password_hash ( $this -> testPass , PASSWORD_DEFAULT ));
$this -> assertEquals ( 2 , $userId );
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
$this -> assertTrue ( $this -> authenticate (), " Authentication failed " );
$trackId = $this -> addTestTrack ( $userId );
$this -> addTestPosition ( $userId , $trackId , $this -> testTimestamp );
$this -> addTestPosition ( $userId , $trackId , $this -> testTimestamp + 1 );
$this -> assertEquals ( 1 , $this -> getConnection () -> getRowCount ( " tracks " ), " Wrong row count " );
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " positions " ), " Wrong row count " );
$options = [
" http_errors " => false ,
2019-01-24 19:07:41 +01:00
" query " => [ " userid " => $userId , " trackid " => $trackId ],
2017-09-19 22:04:40 +02:00
];
2019-01-24 19:07:41 +01:00
$response = $this -> http -> get ( " /utils/getpositions.php " , $options );
2017-09-19 22:04:40 +02:00
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals ( count ( $json ), 2 , " Wrong count of positions " );
2017-09-19 22:04:40 +02:00
2020-01-07 17:21:40 +01:00
$position = $json [ 0 ];
$this -> assertEquals (( int ) $position -> id , 1 , " Wrong position id " );
2017-09-19 22:04:40 +02:00
$this -> assertEquals (( float ) $position -> latitude , $this -> testLat , " Wrong latitude " );
$this -> assertEquals (( float ) $position -> longitude , $this -> testLon , " Wrong longitude " );
$this -> assertEquals (( int ) $position -> timestamp , $this -> testTimestamp , " Wrong timestamp " );
$this -> assertEquals (( string ) $position -> username , $this -> testUser , " Wrong username " );
$this -> assertEquals (( string ) $position -> trackname , $this -> testTrackName , " Wrong trackname " );
2020-01-07 17:21:40 +01:00
$position = $json [ 1 ];
$this -> assertEquals (( int ) $position -> id , 2 , " Wrong position id " );
2017-09-19 22:04:40 +02:00
$this -> assertEquals (( float ) $position -> latitude , $this -> testLat , " Wrong latitude " );
$this -> assertEquals (( float ) $position -> longitude , $this -> testLon , " Wrong longitude " );
$this -> assertEquals (( int ) $position -> timestamp , $this -> testTimestamp + 1 , " Wrong timestamp " );
$this -> assertEquals (( string ) $position -> username , $this -> testUser , " Wrong username " );
$this -> assertEquals (( string ) $position -> trackname , $this -> testTrackName , " Wrong trackname " );
}
2019-03-02 23:53:45 +01:00
public function testGetPositionsUserLatest () {
2017-09-19 22:04:40 +02:00
$this -> assertTrue ( $this -> authenticate (), " Authentication failed " );
$trackId = $this -> addTestTrack ( $this -> testUserId );
$this -> addTestPosition ( $this -> testUserId , $trackId , $this -> testTimestamp );
2019-03-02 23:53:45 +01:00
$this -> addTestPosition ( $this -> testUserId , $trackId , $this -> testTimestamp + 3 );
2017-09-19 22:04:40 +02:00
$this -> assertEquals ( 1 , $this -> getConnection () -> getRowCount ( " tracks " ), " Wrong row count " );
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " positions " ), " Wrong row count " );
2019-03-02 23:53:45 +01:00
$userId = $this -> addTestUser ( $this -> testUser , password_hash ( $this -> testPass , PASSWORD_DEFAULT ));
$trackId2 = $this -> addTestTrack ( $userId );
$this -> addTestPosition ( $userId , $trackId2 , $this -> testTimestamp + 2 );
$this -> addTestPosition ( $userId , $trackId2 , $this -> testTimestamp + 1 );
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " tracks " ), " Wrong row count " );
$this -> assertEquals ( 4 , $this -> getConnection () -> getRowCount ( " positions " ), " Wrong row count " );
2017-09-19 22:04:40 +02:00
$options = [
" http_errors " => false ,
2019-03-02 23:53:45 +01:00
" query " => [ " userid " => $this -> testUserId , " last " => 1 ],
2017-09-19 22:04:40 +02:00
];
2019-01-24 19:07:41 +01:00
$response = $this -> http -> get ( " /utils/getpositions.php " , $options );
2017-09-19 22:04:40 +02:00
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals ( count ( $json ), 1 , " Wrong count of positions " );
2017-09-19 22:04:40 +02:00
2020-01-07 17:21:40 +01:00
$position = $json [ 0 ];
$this -> assertEquals (( int ) $position -> id , 2 , " Wrong position id " );
2017-09-19 22:04:40 +02:00
$this -> assertEquals (( float ) $position -> latitude , $this -> testLat , " Wrong latitude " );
$this -> assertEquals (( float ) $position -> longitude , $this -> testLon , " Wrong longitude " );
2019-03-02 23:53:45 +01:00
$this -> assertEquals (( int ) $position -> timestamp , $this -> testTimestamp + 3 , " Wrong timestamp " );
2017-09-19 22:04:40 +02:00
$this -> assertEquals (( string ) $position -> username , $this -> testAdminUser , " Wrong username " );
$this -> assertEquals (( string ) $position -> trackname , $this -> testTrackName , " Wrong trackname " );
}
2019-03-02 23:53:45 +01:00
public function testGetPositionsAllUsersLatest () {
$this -> assertTrue ( $this -> authenticate (), " Authentication failed " );
$userId = $this -> addTestUser ( $this -> testUser , password_hash ( $this -> testPass , PASSWORD_DEFAULT ));
$trackId = $this -> addTestTrack ( $this -> testUserId );
$this -> addTestPosition ( $this -> testUserId , $trackId , $this -> testTimestamp );
$this -> addTestPosition ( $this -> testUserId , $trackId , $this -> testTimestamp + 3 );
$this -> assertEquals ( 1 , $this -> getConnection () -> getRowCount ( " tracks " ), " Wrong row count " );
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " positions " ), " Wrong row count " );
$trackName = " Track 2 " ;
$trackId2 = $this -> addTestTrack ( $userId , $trackName );
$this -> addTestPosition ( $userId , $trackId2 , $this -> testTimestamp + 2 );
$this -> addTestPosition ( $userId , $trackId2 , $this -> testTimestamp + 1 );
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " tracks " ), " Wrong row count " );
$this -> assertEquals ( 4 , $this -> getConnection () -> getRowCount ( " positions " ), " Wrong row count " );
$options = [
" http_errors " => false ,
" query " => [ " last " => 1 ],
];
$response = $this -> http -> get ( " /utils/getpositions.php " , $options );
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals ( count ( $json ), 2 , " Wrong count of positions " );
2019-03-02 23:53:45 +01:00
2020-01-07 17:21:40 +01:00
$position = $json [ 0 ];
$this -> assertEquals (( int ) $position -> id , 2 , " Wrong position id " );
2019-03-02 23:53:45 +01:00
$this -> assertEquals (( float ) $position -> latitude , $this -> testLat , " Wrong latitude " );
$this -> assertEquals (( float ) $position -> longitude , $this -> testLon , " Wrong longitude " );
$this -> assertEquals (( int ) $position -> timestamp , $this -> testTimestamp + 3 , " Wrong timestamp " );
$this -> assertEquals (( string ) $position -> username , $this -> testAdminUser , " Wrong username " );
$this -> assertEquals (( string ) $position -> trackname , $this -> testTrackName , " Wrong trackname " );
2020-01-07 17:21:40 +01:00
$position = $json [ 1 ];
$this -> assertEquals (( int ) $position -> id , 3 , " Wrong position id " );
2019-03-02 23:53:45 +01:00
$this -> assertEquals (( float ) $position -> latitude , $this -> testLat , " Wrong latitude " );
$this -> assertEquals (( float ) $position -> longitude , $this -> testLon , " Wrong longitude " );
$this -> assertEquals (( int ) $position -> timestamp , $this -> testTimestamp + 2 , " Wrong timestamp " );
$this -> assertEquals (( string ) $position -> username , $this -> testUser , " Wrong username " );
$this -> assertEquals (( string ) $position -> trackname , $trackName , " Wrong trackname " );
}
public function testGetPositionsNoTrackId () {
$this -> assertTrue ( $this -> authenticate (), " Authentication failed " );
$trackId = $this -> addTestTrack ( $this -> testUserId );
$this -> addTestPosition ( $this -> testUserId , $trackId , $this -> testTimestamp );
$this -> addTestPosition ( $this -> testUserId , $trackId , $this -> testTimestamp + 1 );
$this -> assertEquals ( 1 , $this -> getConnection () -> getRowCount ( " tracks " ), " Wrong row count " );
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " positions " ), " Wrong row count " );
$options = [
" http_errors " => false ,
" query " => [ " userid " => $this -> testUserId ],
];
$response = $this -> http -> get ( " /utils/getpositions.php " , $options );
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertCount ( 0 , $json , " Wrong count of positions " );
2019-03-02 23:53:45 +01:00
}
2017-09-19 22:04:40 +02:00
public function testGetPositionsNoUserId () {
$this -> assertTrue ( $this -> authenticate (), " Authentication failed " );
$trackId = $this -> addTestTrack ( $this -> testUserId );
$this -> addTestPosition ( $this -> testUserId , $trackId , $this -> testTimestamp );
$this -> addTestPosition ( $this -> testUserId , $trackId , $this -> testTimestamp + 1 );
$this -> assertEquals ( 1 , $this -> getConnection () -> getRowCount ( " tracks " ), " Wrong row count " );
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " positions " ), " Wrong row count " );
$options = [
" http_errors " => false ,
2019-01-24 19:07:41 +01:00
" query " => [ " trackid " => $trackId ],
2017-09-19 22:04:40 +02:00
];
2019-01-24 19:07:41 +01:00
$response = $this -> http -> get ( " /utils/getpositions.php " , $options );
2017-09-19 22:04:40 +02:00
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertCount ( 0 , $json , " Wrong count of positions " );
2017-09-19 22:04:40 +02:00
}
public function testGetPositionsNoAuth () {
$trackId = $this -> addTestTrack ( $this -> testUserId );
$this -> addTestPosition ( $this -> testUserId , $trackId , $this -> testTimestamp );
$this -> assertEquals ( 1 , $this -> getConnection () -> getRowCount ( " tracks " ), " Wrong row count " );
$options = [
" http_errors " => false ,
2019-01-24 19:07:41 +01:00
" query " => [ " userid " => $this -> testUserId , " trackid " => $trackId ],
2017-09-19 22:04:40 +02:00
];
2019-01-24 19:07:41 +01:00
$response = $this -> http -> get ( " /utils/getpositions.php " , $options );
2017-09-19 22:04:40 +02:00
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
2017-09-19 22:04:40 +02:00
2020-01-07 17:21:40 +01:00
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals ( count ( $json ), 0 , " Wrong count of positions " );
2017-09-19 22:04:40 +02:00
}
/* gettracks.php */
public function testGetTracksAdmin () {
$this -> assertTrue ( $this -> authenticate (), " Authentication failed " );
$this -> addTestTrack ( $this -> testUserId );
$this -> addTestTrack ( $this -> testUserId , $this -> testTrackName . " 2 " );
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " tracks " ), " Wrong row count " );
$options = [
" http_errors " => false ,
2019-01-24 19:07:41 +01:00
" query " => [ " userid " => $this -> testUserId ],
2017-09-19 22:04:40 +02:00
];
2019-01-24 19:07:41 +01:00
$response = $this -> http -> get ( " /utils/gettracks.php " , $options );
2017-09-19 22:04:40 +02:00
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals ( count ( $json ), 2 , " Wrong count of tracks " );
2017-09-19 22:04:40 +02:00
2020-01-07 17:21:40 +01:00
$track = $json [ 0 ];
$this -> assertEquals (( int ) $track -> id , $this -> testTrackId2 , " Wrong track id " );
$this -> assertEquals (( string ) $track -> name , $this -> testTrackName . " 2 " , " Wrong track name " );
2017-09-19 22:04:40 +02:00
2020-01-07 17:21:40 +01:00
$track = $json [ 1 ];
$this -> assertEquals (( int ) $track -> id , $this -> testTrackId , " Wrong track id " );
$this -> assertEquals (( string ) $track -> name , $this -> testTrackName , " Wrong track name " );
2017-09-19 22:04:40 +02:00
}
public function testGetTracksUser () {
$userId = $this -> addTestUser ( $this -> testUser , password_hash ( $this -> testPass , PASSWORD_DEFAULT ));
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
$this -> assertTrue ( $this -> authenticate ( $this -> testUser , $this -> testPass ), " Authentication failed " );
$this -> addTestTrack ( $userId );
$this -> addTestTrack ( $userId , $this -> testTrackName . " 2 " );
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " tracks " ), " Wrong row count " );
$options = [
" http_errors " => false ,
2019-01-24 19:07:41 +01:00
" query " => [ " userid " => $userId ],
2017-09-19 22:04:40 +02:00
];
2019-01-24 19:07:41 +01:00
$response = $this -> http -> get ( " /utils/gettracks.php " , $options );
2017-09-19 22:04:40 +02:00
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals ( count ( $json ), 2 , " Wrong count of tracks " );
2017-09-19 22:04:40 +02:00
2020-01-07 17:21:40 +01:00
$track = $json [ 0 ];
$this -> assertEquals (( int ) $track -> id , $this -> testTrackId2 , " Wrong track id " );
$this -> assertEquals (( string ) $track -> name , $this -> testTrackName . " 2 " , " Wrong track name " );
2017-09-19 22:04:40 +02:00
2020-01-07 17:21:40 +01:00
$track = $json [ 1 ];
$this -> assertEquals (( int ) $track -> id , $this -> testTrackId , " Wrong track id " );
$this -> assertEquals (( string ) $track -> name , $this -> testTrackName , " Wrong track name " );
}
2017-09-19 22:04:40 +02:00
public function testGetTracksOtherUser () {
$this -> addTestUser ( $this -> testUser , password_hash ( $this -> testPass , PASSWORD_DEFAULT ));
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
$this -> assertTrue ( $this -> authenticate ( $this -> testUser , $this -> testPass ), " Authentication failed " );
$this -> addTestTrack ( $this -> testUserId );
$this -> addTestTrack ( $this -> testUserId , $this -> testTrackName . " 2 " );
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " tracks " ), " Wrong row count " );
$options = [
" http_errors " => false ,
2019-01-24 19:07:41 +01:00
" query " => [ " userid " => $this -> testUserId ],
2017-09-19 22:04:40 +02:00
];
2019-01-24 19:07:41 +01:00
$response = $this -> http -> get ( " /utils/gettracks.php " , $options );
2017-09-19 22:04:40 +02:00
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals ( count ( $json ), 0 , " Wrong count of tracks " );
2017-09-19 22:04:40 +02:00
}
public function testGetTracksNoUserId () {
$this -> addTestUser ( $this -> testUser , password_hash ( $this -> testPass , PASSWORD_DEFAULT ));
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
$this -> assertTrue ( $this -> authenticate ( $this -> testUser , $this -> testPass ), " Authentication failed " );
$this -> addTestTrack ( $this -> testUserId );
$this -> addTestTrack ( $this -> testUserId , $this -> testTrackName . " 2 " );
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " tracks " ), " Wrong row count " );
$options = [
" http_errors " => false ,
];
2019-01-24 19:07:41 +01:00
$response = $this -> http -> get ( " /utils/gettracks.php " , $options );
2017-09-19 22:04:40 +02:00
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals ( count ( $json ), 0 , " Wrong count of tracks " );
2017-09-19 22:04:40 +02:00
}
public function testGetTracksNoAuth () {
$this -> addTestTrack ( $this -> testUserId );
$this -> addTestTrack ( $this -> testUserId , $this -> testTrackName . " 2 " );
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " tracks " ), " Wrong row count " );
$options = [
" http_errors " => false ,
2019-01-24 19:07:41 +01:00
" query " => [ " userid " => $this -> testUserId ],
2017-09-19 22:04:40 +02:00
];
2019-01-24 19:07:41 +01:00
$response = $this -> http -> get ( " /utils/gettracks.php " , $options );
2017-09-19 22:04:40 +02:00
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals ( count ( $json ), 0 , " Wrong count of tracks " );
2017-09-19 22:04:40 +02:00
}
/* changepass.php */
public function testChangePassNoAuth () {
$options = [
" http_errors " => false ,
2019-05-15 11:32:36 +02:00
" form_params " => [
" login " => $this -> testUser ,
" pass " => $this -> testPass ,
" oldpass " => $this -> testPass
],
2017-09-19 22:04:40 +02:00
];
$response = $this -> http -> post ( " /utils/changepass.php " , $options );
$this -> assertEquals ( 401 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals (( int ) $json -> error , 1 , " Wrong error status " );
$this -> assertEquals (( string ) $json -> message , " Unauthorized " , " Wrong error message " );
2017-09-19 22:04:40 +02:00
}
public function testChangePassEmpty () {
$this -> assertTrue ( $this -> authenticate (), " Authentication failed " );
$options = [
" http_errors " => false ,
" form_params " => [ " login " => $this -> testAdminUser ],
];
$response = $this -> http -> post ( " /utils/changepass.php " , $options );
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals (( int ) $json -> error , 1 , " Wrong error status " );
$this -> assertEquals (( string ) $json -> message , " Empty password " , " Wrong error message " );
2017-09-19 22:04:40 +02:00
}
2019-05-15 11:32:36 +02:00
public function testChangePassUserUnknown () {
2017-09-19 22:04:40 +02:00
$this -> assertTrue ( $this -> authenticate (), " Authentication failed " );
$options = [
" http_errors " => false ,
" form_params " => [
" login " => $this -> testUser ,
" pass " => $this -> testPass ,
],
];
$response = $this -> http -> post ( " /utils/changepass.php " , $options );
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals (( int ) $json -> error , 1 , " Wrong error status " );
$this -> assertEquals (( string ) $json -> message , " User unknown " , " Wrong error message " );
2017-09-19 22:04:40 +02:00
}
2019-05-15 11:32:36 +02:00
public function testChangePassEmptyLogin () {
$this -> assertTrue ( $this -> authenticate (), " Authentication failed " );
$options = [
" http_errors " => false ,
" form_params " => [
" pass " => $this -> testPass ,
],
];
$response = $this -> http -> post ( " /utils/changepass.php " , $options );
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals (( int ) $json -> error , 1 , " Wrong error status " );
$this -> assertEquals (( string ) $json -> message , " Empty login " , " Wrong error message " );
2019-05-15 11:32:36 +02:00
}
2017-09-19 22:04:40 +02:00
public function testChangePassWrongOldpass () {
$this -> assertTrue ( $this -> authenticate (), " Authentication failed " );
$options = [
" http_errors " => false ,
" form_params " => [
2019-05-15 11:32:36 +02:00
" login " => $this -> testAdminUser ,
2017-09-19 22:04:40 +02:00
" oldpass " => " badpass " ,
2020-02-19 18:42:44 +01:00
" pass " => " Newpass1234567890 " ,
2017-09-19 22:04:40 +02:00
],
];
$response = $this -> http -> post ( " /utils/changepass.php " , $options );
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals (( int ) $json -> error , 1 , " Wrong error status " );
$this -> assertEquals (( string ) $json -> message , " Wrong old password " , " Wrong error message " );
2017-09-19 22:04:40 +02:00
}
public function testChangePassNoOldpass () {
$this -> assertTrue ( $this -> authenticate (), " Authentication failed " );
$options = [
" http_errors " => false ,
" form_params " => [
2019-05-15 11:32:36 +02:00
" login " => $this -> testAdminUser ,
2020-02-19 18:42:44 +01:00
" pass " => " Newpass1234567890 " ,
2017-09-19 22:04:40 +02:00
],
];
$response = $this -> http -> post ( " /utils/changepass.php " , $options );
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals (( int ) $json -> error , 1 , " Wrong error status " );
$this -> assertEquals (( string ) $json -> message , " Wrong old password " , " Wrong error message " );
2017-09-19 22:04:40 +02:00
}
public function testChangePassSelfAdmin () {
$this -> assertTrue ( $this -> authenticate (), " Authentication failed " );
2020-02-19 18:42:44 +01:00
$newPass = " Newpass1234567890 " ;
2017-09-19 22:04:40 +02:00
$options = [
" http_errors " => false ,
" form_params " => [
2019-05-15 11:32:36 +02:00
" login " => $this -> testAdminUser ,
2017-09-19 22:04:40 +02:00
" oldpass " => $this -> testAdminPass ,
" pass " => $newPass ,
],
];
$response = $this -> http -> post ( " /utils/changepass.php " , $options );
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
2017-09-19 22:04:40 +02:00
$this -> assertTrue ( password_verify ( $newPass , $this -> pdoGetColumn ( " SELECT password FROM users " )), " Wrong actual password hash " );
}
public function testChangePassSelfUser () {
$userId = $this -> addTestUser ( $this -> testUser , password_hash ( $this -> testPass , PASSWORD_DEFAULT ));
$this -> assertTrue ( $this -> authenticate ( $this -> testUser , $this -> testPass ), " Authentication failed " );
2020-02-19 18:42:44 +01:00
$newPass = " Newpass1234567890 " ;
2017-09-19 22:04:40 +02:00
$options = [
" http_errors " => false ,
" form_params " => [
2019-05-15 11:32:36 +02:00
" login " => $this -> testUser ,
2017-09-19 22:04:40 +02:00
" oldpass " => $this -> testPass ,
" pass " => $newPass ,
],
];
$response = $this -> http -> post ( " /utils/changepass.php " , $options );
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
2017-09-19 22:04:40 +02:00
$this -> assertTrue ( password_verify ( $newPass , $this -> pdoGetColumn ( " SELECT password FROM users WHERE id = $userId " )), " Wrong actual password hash " );
}
public function testChangePassOtherAdmin () {
$this -> assertTrue ( $this -> authenticate (), " Authentication failed " );
$userId = $this -> addTestUser ( $this -> testUser , password_hash ( $this -> testPass , PASSWORD_DEFAULT ));
2020-02-19 18:42:44 +01:00
$newPass = " Newpass1234567890 " ;
2017-09-19 22:04:40 +02:00
$options = [
" http_errors " => false ,
" form_params " => [
" login " => $this -> testUser ,
" pass " => $newPass ,
],
];
$response = $this -> http -> post ( " /utils/changepass.php " , $options );
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
2017-09-19 22:04:40 +02:00
$this -> assertTrue ( password_verify ( $newPass , $this -> pdoGetColumn ( " SELECT password FROM users WHERE id = $userId " )), " Wrong actual password hash " );
}
public function testChangePassOtherUser () {
2020-02-19 18:42:44 +01:00
$this -> addTestUser ( $this -> testUser , password_hash ( $this -> testPass , PASSWORD_DEFAULT ));
$this -> addTestUser ( $this -> testUser2 , password_hash ( $this -> testPass , PASSWORD_DEFAULT ));
2017-09-19 22:04:40 +02:00
$this -> assertTrue ( $this -> authenticate ( $this -> testUser , $this -> testPass ), " Authentication failed " );
2020-02-19 18:42:44 +01:00
$newPass = " Newpass1234567890 " ;
2017-09-19 22:04:40 +02:00
$options = [
" http_errors " => false ,
" form_params " => [
" login " => $this -> testUser2 ,
" pass " => $newPass ,
],
];
$response = $this -> http -> post ( " /utils/changepass.php " , $options );
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals (( int ) $json -> error , 1 , " Wrong error status " );
$this -> assertEquals (( string ) $json -> message , " Unauthorized " , " Wrong error message " );
2017-09-19 22:04:40 +02:00
}
/* handletrack.php */
public function testHandleTrackDeleteAdmin () {
$this -> assertTrue ( $this -> authenticate (), " Authentication failed " );
$userId = $this -> addTestUser ( $this -> testUser , password_hash ( $this -> testPass , PASSWORD_DEFAULT ));
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
$trackId = $this -> addTestTrack ( $userId );
$trackId2 = $this -> addTestTrack ( $userId );
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " tracks " ), " Wrong row count " );
$options = [
" http_errors " => false ,
" form_params " => [ " trackid " => $trackId , " action " => " delete " ],
];
$response = $this -> http -> post ( " /utils/handletrack.php " , $options );
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
2017-09-19 22:04:40 +02:00
$this -> assertEquals ( 1 , $this -> getConnection () -> getRowCount ( " tracks " ), " Wrong row count " );
$this -> assertEquals ( $trackId2 , $this -> pdoGetColumn ( " SELECT id FROM tracks WHERE id = $trackId2 " ), " Wrong actual track id " );
}
public function testHandleTrackDeleteSelf () {
$userId = $this -> addTestUser ( $this -> testUser , password_hash ( $this -> testPass , PASSWORD_DEFAULT ));
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
$this -> assertTrue ( $this -> authenticate ( $this -> testUser , $this -> testPass ), " Authentication failed " );
$trackId = $this -> addTestTrack ( $userId );
$trackId2 = $this -> addTestTrack ( $userId );
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " tracks " ), " Wrong row count " );
$options = [
" http_errors " => false ,
" form_params " => [ " trackid " => $trackId , " action " => " delete " ],
];
$response = $this -> http -> post ( " /utils/handletrack.php " , $options );
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
2017-09-19 22:04:40 +02:00
$this -> assertEquals ( 1 , $this -> getConnection () -> getRowCount ( " tracks " ), " Wrong row count " );
$this -> assertEquals ( $trackId2 , $this -> pdoGetColumn ( " SELECT id FROM tracks WHERE id = $trackId2 " ), " Wrong actual track id " );
}
public function testHandleTrackDeleteOtherUser () {
2020-02-20 17:08:47 +01:00
$lang = ( new uLang ( $this -> mockConfig )) -> getStrings ();
2017-09-19 22:04:40 +02:00
$this -> addTestUser ( $this -> testUser , password_hash ( $this -> testPass , PASSWORD_DEFAULT ));
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
$this -> assertTrue ( $this -> authenticate ( $this -> testUser , $this -> testPass ), " Authentication failed " );
$trackId = $this -> addTestTrack ( $this -> testUserId );
$this -> assertEquals ( 1 , $this -> getConnection () -> getRowCount ( " tracks " ), " Wrong row count " );
$options = [
" http_errors " => false ,
" form_params " => [ " trackid " => $trackId , " action " => " delete " ],
];
$response = $this -> http -> post ( " /utils/handletrack.php " , $options );
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals (( int ) $json -> error , 1 , " Wrong error status " );
$this -> assertEquals (( string ) $json -> message , $lang [ " servererror " ], " Wrong error message " );
2017-09-19 22:04:40 +02:00
}
public function testHandleTrackUpdate () {
$newName = " New name " ;
$this -> assertTrue ( $this -> authenticate (), " Authentication failed " );
$userId = $this -> addTestUser ( $this -> testUser , password_hash ( $this -> testPass , PASSWORD_DEFAULT ));
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
$trackId = $this -> addTestTrack ( $userId );
$trackId2 = $this -> addTestTrack ( $userId );
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " tracks " ), " Wrong row count " );
$options = [
" http_errors " => false ,
" form_params " => [ " trackid " => $trackId , " action " => " update " , " trackname " => $newName ],
];
$response = $this -> http -> post ( " /utils/handletrack.php " , $options );
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
2017-09-19 22:04:40 +02:00
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " tracks " ), " Wrong row count " );
$row1 = [
" id " => $trackId2 ,
" user_id " => $userId ,
" name " => $this -> testTrackName ,
" comment " => $this -> testTrackComment
];
$row2 = [
" id " => $trackId ,
" user_id " => $userId ,
" name " => $newName ,
" comment " => $this -> testTrackComment
];
$actual = $this -> getConnection () -> createQueryTable (
" tracks " ,
" SELECT * FROM tracks "
);
$this -> assertTableContains ( $row1 , $actual , " Wrong actual table data " );
$this -> assertTableContains ( $row2 , $actual , " Wrong actual table data " );
}
public function testHandleTrackUpdateEmptyName () {
2020-02-20 17:08:47 +01:00
$lang = ( new uLang ( $this -> mockConfig )) -> getStrings ();
2017-09-19 22:04:40 +02:00
$this -> assertTrue ( $this -> authenticate (), " Authentication failed " );
$userId = $this -> addTestUser ( $this -> testUser , password_hash ( $this -> testPass , PASSWORD_DEFAULT ));
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
$trackId = $this -> addTestTrack ( $userId );
2020-02-19 18:42:44 +01:00
$this -> addTestTrack ( $userId );
2017-09-19 22:04:40 +02:00
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " tracks " ), " Wrong row count " );
$options = [
" http_errors " => false ,
" form_params " => [ " trackid " => $trackId , " action " => " update " , " trackname " => " " ],
];
$response = $this -> http -> post ( " /utils/handletrack.php " , $options );
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals (( int ) $json -> error , 1 , " Wrong error status " );
$this -> assertEquals (( string ) $json -> message , $lang [ " servererror " ], " Wrong error message " );
2017-09-19 22:04:40 +02:00
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " tracks " ), " Wrong row count " );
}
public function testHandleTrackUpdateNonexistantTrack () {
2020-02-20 17:08:47 +01:00
$lang = ( new uLang ( $this -> mockConfig )) -> getStrings ();
2017-09-19 22:04:40 +02:00
$newName = " New name " ;
$this -> assertTrue ( $this -> authenticate (), " Authentication failed " );
$userId = $this -> addTestUser ( $this -> testUser , password_hash ( $this -> testPass , PASSWORD_DEFAULT ));
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
$trackId = $this -> addTestTrack ( $userId );
$nonexistantTrackId = $trackId + 1 ;
$this -> assertEquals ( 1 , $this -> getConnection () -> getRowCount ( " tracks " ), " Wrong row count " );
$this -> assertFalse ( $this -> pdoGetColumn ( " SELECT id FROM tracks WHERE id = $nonexistantTrackId " ), " Nonexistant track exists " );
$options = [
" http_errors " => false ,
" form_params " => [ " trackid " => $nonexistantTrackId , " action " => " update " , " trackname " => $newName ],
];
$response = $this -> http -> post ( " /utils/handletrack.php " , $options );
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals (( int ) $json -> error , 1 , " Wrong error status " );
$this -> assertEquals (( string ) $json -> message , $lang [ " servererror " ], " Wrong error message " );
2017-09-19 22:04:40 +02:00
}
public function testHandleTrackMissingAction () {
2020-02-20 17:08:47 +01:00
$lang = ( new uLang ( $this -> mockConfig )) -> getStrings ();
2017-09-19 22:04:40 +02:00
$this -> assertTrue ( $this -> authenticate (), " Authentication failed " );
$options = [
" http_errors " => false ,
];
$response = $this -> http -> post ( " /utils/handletrack.php " , $options );
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals (( int ) $json -> error , 1 , " Wrong error status " );
$this -> assertEquals (( string ) $json -> message , $lang [ " servererror " ], " Wrong error message " );
2017-09-19 22:04:40 +02:00
}
/* handleuser.php */
public function testHandleUserMissingAction () {
2020-02-20 17:08:47 +01:00
$lang = ( new uLang ( $this -> mockConfig )) -> getStrings ();
2017-09-19 22:04:40 +02:00
$this -> assertTrue ( $this -> authenticate (), " Authentication failed " );
$options = [
" http_errors " => false ,
];
$response = $this -> http -> post ( " /utils/handleuser.php " , $options );
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals (( int ) $json -> error , 1 , " Wrong error status " );
$this -> assertEquals (( string ) $json -> message , $lang [ " servererror " ], " Wrong error message " );
2017-09-19 22:04:40 +02:00
}
public function testHandleUserNonAdmin () {
2020-02-20 17:08:47 +01:00
$lang = ( new uLang ( $this -> mockConfig )) -> getStrings ();
2017-09-19 22:04:40 +02:00
$this -> addTestUser ( $this -> testUser , password_hash ( $this -> testPass , PASSWORD_DEFAULT ));
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
$this -> assertTrue ( $this -> authenticate ( $this -> testUser , $this -> testPass ), " Authentication failed " );
$options = [
" http_errors " => false ,
" form_params " => [ " action " => " delete " , " login " => " test " ],
];
$response = $this -> http -> post ( " /utils/handleuser.php " , $options );
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals (( int ) $json -> error , 1 , " Wrong error status " );
$this -> assertEquals (( string ) $json -> message , $lang [ " servererror " ], " Wrong error message " );
2017-09-19 22:04:40 +02:00
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
}
public function testHandleUserSelf () {
2020-02-20 17:08:47 +01:00
$lang = ( new uLang ( $this -> mockConfig )) -> getStrings ();
2017-09-19 22:04:40 +02:00
$this -> assertTrue ( $this -> authenticate (), " Authentication failed " );
$this -> assertEquals ( 1 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
$options = [
" http_errors " => false ,
" form_params " => [ " action " => " delete " , " login " => $this -> testAdminUser ],
];
$response = $this -> http -> post ( " /utils/handleuser.php " , $options );
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals (( int ) $json -> error , 1 , " Wrong error status " );
$this -> assertEquals (( string ) $json -> message , $lang [ " servererror " ], " Wrong error message " );
2017-09-19 22:04:40 +02:00
$this -> assertEquals ( 1 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
}
public function testHandleUserEmptyLogin () {
2020-02-20 17:08:47 +01:00
$lang = ( new uLang ( $this -> mockConfig )) -> getStrings ();
2017-09-19 22:04:40 +02:00
$this -> assertTrue ( $this -> authenticate (), " Authentication failed " );
$this -> assertEquals ( 1 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
$options = [
" http_errors " => false ,
" form_params " => [ " action " => " delete " , " login " => " " ],
];
$response = $this -> http -> post ( " /utils/handleuser.php " , $options );
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals (( int ) $json -> error , 1 , " Wrong error status " );
$this -> assertEquals (( string ) $json -> message , $lang [ " servererror " ], " Wrong error message " );
2017-09-19 22:04:40 +02:00
$this -> assertEquals ( 1 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
}
public function testHandleUserNoAuth () {
2020-02-20 17:08:47 +01:00
$lang = ( new uLang ( $this -> mockConfig )) -> getStrings ();
2017-09-19 22:04:40 +02:00
$this -> addTestUser ( $this -> testUser , password_hash ( $this -> testPass , PASSWORD_DEFAULT ));
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
$options = [
" http_errors " => false ,
" form_params " => [ " action " => " delete " , " login " => $this -> testUser ],
];
$response = $this -> http -> post ( " /utils/handleuser.php " , $options );
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals (( int ) $json -> error , 1 , " Wrong error status " );
$this -> assertEquals (( string ) $json -> message , $lang [ " servererror " ], " Wrong error message " );
2017-09-19 22:04:40 +02:00
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
}
public function testHandleUserAdd () {
$this -> assertTrue ( $this -> authenticate (), " Authentication failed " );
$this -> assertEquals ( 1 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
$options = [
" http_errors " => false ,
" form_params " => [ " action " => " add " , " login " => $this -> testUser , " pass " => $this -> testPass ],
];
$response = $this -> http -> post ( " /utils/handleuser.php " , $options );
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
2017-09-19 22:04:40 +02:00
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
$expected = [
" login " => $this -> testUser ,
];
$actual = $this -> getConnection () -> createQueryTable (
" users " ,
" SELECT login FROM users "
);
$this -> assertTableContains ( $expected , $actual , " Wrong actual table data " );
$this -> assertTrue ( password_verify ( $this -> testPass , $this -> pdoGetColumn ( " SELECT password FROM users WHERE login = ' $this->testUser ' " )), " Wrong actual password hash " );
}
public function testHandleUserAddSameLogin () {
2020-02-20 17:08:47 +01:00
$lang = ( new uLang ( $this -> mockConfig )) -> getStrings ();
2017-09-19 22:04:40 +02:00
$this -> assertTrue ( $this -> authenticate (), " Authentication failed " );
$this -> addTestUser ( $this -> testUser , password_hash ( $this -> testPass , PASSWORD_DEFAULT ));
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
$options = [
" http_errors " => false ,
" form_params " => [ " action " => " add " , " login " => $this -> testUser , " pass " => $this -> testPass ],
];
$response = $this -> http -> post ( " /utils/handleuser.php " , $options );
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
$this -> assertEquals ( 1 , ( int ) $json -> error , " Wrong error status " );
$this -> assertEquals (( string ) $json -> message , $lang [ " userexists " ], " Wrong error message " );
2017-09-19 22:04:40 +02:00
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
}
public function testHandleUserUpdate () {
$newPass = $this -> testPass . " new " ;
$this -> assertTrue ( $this -> authenticate (), " Authentication failed " );
$this -> addTestUser ( $this -> testUser , password_hash ( $this -> testPass , PASSWORD_DEFAULT ));
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
$options = [
" http_errors " => false ,
" form_params " => [ " action " => " update " , " login " => $this -> testUser , " pass " => $newPass ],
];
$response = $this -> http -> post ( " /utils/handleuser.php " , $options );
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
2017-09-19 22:04:40 +02:00
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
$this -> assertTrue ( password_verify ( $newPass , $this -> pdoGetColumn ( " SELECT password FROM users WHERE login = ' $this->testUser ' " )), " Wrong actual password hash " );
}
public function testHandleUserUpdateEmptyPass () {
$this -> assertTrue ( $this -> authenticate (), " Authentication failed " );
$this -> addTestUser ( $this -> testUser , password_hash ( $this -> testPass , PASSWORD_DEFAULT ));
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
$options = [
" http_errors " => false ,
" form_params " => [ " action " => " update " , " login " => $this -> testUser , " pass " => " " ],
];
$response = $this -> http -> post ( " /utils/handleuser.php " , $options );
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-02-19 18:42:44 +01:00
2017-09-19 22:04:40 +02:00
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
$this -> assertTrue ( password_verify ( $this -> testPass , $this -> pdoGetColumn ( " SELECT password FROM users WHERE login = ' $this->testUser ' " )), " Wrong actual password hash " );
}
public function testHandleUserDelete () {
$this -> assertTrue ( $this -> authenticate (), " Authentication failed " );
$this -> addTestUser ( $this -> testUser , password_hash ( $this -> testPass , PASSWORD_DEFAULT ));
$this -> assertEquals ( 2 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
$options = [
" http_errors " => false ,
" form_params " => [ " action " => " delete " , " login " => $this -> testUser ],
];
$response = $this -> http -> post ( " /utils/handleuser.php " , $options );
$this -> assertEquals ( 200 , $response -> getStatusCode (), " Unexpected status code " );
2020-01-07 17:21:40 +01:00
$json = json_decode ( $response -> getBody ());
$this -> assertNotNull ( $json , " JSON object is null " );
2017-09-19 22:04:40 +02:00
$this -> assertEquals ( 1 , $this -> getConnection () -> getRowCount ( " users " ), " Wrong row count " );
}
}
?>