2020-01-13 21:46:09 +13:00
< ? php
2022-06-22 22:51:49 +12:00
namespace Tests\E2E\Services\Databases ;
2020-01-13 21:46:09 +13:00
2023-01-20 13:36:17 +13:00
use Appwrite\Extend\Exception ;
2020-01-13 21:46:09 +13:00
use Tests\E2E\Client ;
2023-03-17 05:28:08 +13:00
use Utopia\Database\Database ;
2023-01-20 13:36:17 +13:00
use Utopia\Database\DateTime ;
2023-12-20 23:55:09 +13:00
use Utopia\Database\Document ;
2022-12-15 04:42:25 +13:00
use Utopia\Database\Helpers\ID ;
2022-12-15 05:04:06 +13:00
use Utopia\Database\Helpers\Permission ;
use Utopia\Database\Helpers\Role ;
2023-12-11 08:05:33 +13:00
use Utopia\Database\Query ;
2023-03-13 03:04:18 +13:00
use Utopia\Database\Validator\Datetime as DatetimeValidator ;
2024-01-22 05:22:02 +13:00
use Utopia\Validator\JSON ;
2023-03-27 19:03:00 +13:00
2022-06-22 22:51:49 +12:00
trait DatabasesBase
2020-01-13 21:46:09 +13:00
{
2022-06-22 22:51:49 +12:00
public function testCreateDatabase () : array
2020-01-13 21:46:09 +13:00
{
/**
* Test for SUCCESS
*/
2022-06-22 22:51:49 +12:00
$database = $this -> client -> call ( Client :: METHOD_POST , '/databases' , [
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
], [
2022-08-14 22:33:36 +12:00
'databaseId' => ID :: unique (),
2022-06-22 22:51:49 +12:00
'name' => 'Test Database'
]);
$this -> assertNotEmpty ( $database [ 'body' ][ '$id' ]);
$this -> assertEquals ( 201 , $database [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'Test Database' , $database [ 'body' ][ 'name' ]);
2023-12-20 23:55:09 +13:00
return [ 'databaseId' => $database [ 'body' ][ '$id' ]];
2022-06-22 22:51:49 +12:00
}
/**
* @ depends testCreateDatabase
*/
public function testCreateCollection ( array $data ) : array
{
$databaseId = $data [ 'databaseId' ];
/**
* Test for SUCCESS
*/
$movies = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections' , array_merge ([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json' ,
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2020-01-13 21:46:09 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2022-08-14 22:33:36 +12:00
'collectionId' => ID :: unique (),
2021-06-16 01:38:24 +12:00
'name' => 'Movies' ,
2022-08-25 01:32:52 +12:00
'documentSecurity' => true ,
2022-08-14 02:10:28 +12:00
'permissions' => [
2022-08-27 20:29:32 +12:00
Permission :: create ( Role :: user ( $this -> getUser ()[ '$id' ])),
2022-08-14 02:10:28 +12:00
],
2020-01-13 21:46:09 +13:00
]);
2022-08-08 23:00:03 +12:00
$this -> assertEquals ( 201 , $movies [ 'headers' ][ 'status-code' ]);
2021-06-12 08:06:54 +12:00
$this -> assertEquals ( $movies [ 'body' ][ 'name' ], 'Movies' );
2020-01-13 21:46:09 +13:00
2023-03-25 16:29:40 +13:00
$actors = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'collectionId' => ID :: unique (),
'name' => 'Actors' ,
'documentSecurity' => true ,
'permissions' => [
Permission :: create ( Role :: user ( $this -> getUser ()[ '$id' ])),
],
]);
$this -> assertEquals ( 201 , $actors [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $actors [ 'body' ][ 'name' ], 'Actors' );
return [
'databaseId' => $databaseId ,
'moviesId' => $movies [ 'body' ][ '$id' ],
'actorsId' => $actors [ 'body' ][ '$id' ],
];
2021-06-12 08:06:54 +12:00
}
2021-12-14 02:23:12 +13:00
/**
* @ depends testCreateCollection
*/
public function testDisableCollection ( array $data ) : void
{
2022-06-22 22:51:49 +12:00
$databaseId = $data [ 'databaseId' ];
2021-12-14 02:23:12 +13:00
/**
* Test for SUCCESS
*/
2022-06-22 22:51:49 +12:00
$response = $this -> client -> call ( Client :: METHOD_PUT , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ], array_merge ([
2021-12-14 02:23:12 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'name' => 'Movies' ,
'enabled' => false ,
2022-08-03 16:17:49 +12:00
'documentSecurity' => true ,
2021-12-14 02:23:12 +13:00
]);
2022-08-08 23:00:03 +12:00
$this -> assertEquals ( 200 , $response [ 'headers' ][ 'status-code' ]);
2021-12-14 02:23:12 +13:00
$this -> assertFalse ( $response [ 'body' ][ 'enabled' ]);
if ( $this -> getSide () === 'client' ) {
2022-06-22 22:51:49 +12:00
$responseCreateDocument = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2021-12-14 02:23:12 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2022-08-14 22:33:36 +12:00
'documentId' => ID :: unique (),
2021-12-14 02:23:12 +13:00
'data' => [
'title' => 'Captain America' ,
],
2022-08-03 16:17:49 +12:00
'permissions' => [
2022-08-15 23:24:31 +12:00
Permission :: read ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: update ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: delete ( Role :: user ( $this -> getUser ()[ '$id' ])),
2022-08-03 16:17:49 +12:00
],
2021-12-14 02:23:12 +13:00
]);
2022-08-25 01:32:52 +12:00
$this -> assertEquals ( 404 , $responseCreateDocument [ 'headers' ][ 'status-code' ]);
2022-06-22 22:51:49 +12:00
$responseListDocument = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2021-12-14 02:23:12 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
2022-08-25 01:32:52 +12:00
$this -> assertEquals ( 404 , $responseListDocument [ 'headers' ][ 'status-code' ]);
2022-06-22 22:51:49 +12:00
$responseGetDocument = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents/someID' , array_merge ([
2021-12-14 02:23:12 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
2022-08-08 23:00:03 +12:00
$this -> assertEquals ( 404 , $responseGetDocument [ 'headers' ][ 'status-code' ]);
2021-12-14 02:23:12 +13:00
}
2022-06-22 22:51:49 +12:00
$response = $this -> client -> call ( Client :: METHOD_PUT , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ], array_merge ([
2021-12-14 02:23:12 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'name' => 'Movies' ,
'enabled' => true ,
2022-08-03 16:17:49 +12:00
'documentSecurity' => true ,
2021-12-14 02:23:12 +13:00
]);
2022-08-08 23:00:03 +12:00
$this -> assertEquals ( 200 , $response [ 'headers' ][ 'status-code' ]);
2021-12-14 02:23:12 +13:00
$this -> assertTrue ( $response [ 'body' ][ 'enabled' ]);
}
2021-06-12 08:06:54 +12:00
/**
* @ depends testCreateCollection
*/
public function testCreateAttributes ( array $data ) : array
{
2022-06-22 22:51:49 +12:00
$databaseId = $data [ 'databaseId' ];
2023-05-30 14:52:59 +12:00
2022-06-22 22:51:49 +12:00
$title = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/attributes/string' , array_merge ([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json' ,
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2020-01-13 21:46:09 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2021-12-17 04:04:30 +13:00
'key' => 'title' ,
2021-06-12 08:06:54 +12:00
'size' => 256 ,
'required' => true ,
2020-01-13 21:46:09 +13:00
]);
2023-05-30 14:52:59 +12:00
$description = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/attributes/string' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'description' ,
2023-06-15 17:28:35 +12:00
'size' => 512 ,
'required' => false ,
'default' => '' ,
]);
$tagline = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/attributes/string' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'tagline' ,
'size' => 512 ,
2023-05-30 14:52:59 +12:00
'required' => false ,
'default' => '' ,
]);
2022-06-22 22:51:49 +12:00
$releaseYear = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/attributes/integer' , array_merge ([
2021-06-12 08:06:54 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2021-12-17 04:04:30 +13:00
'key' => 'releaseYear' ,
2021-06-12 08:06:54 +12:00
'required' => true ,
2023-03-25 16:29:40 +13:00
'min' => 1900 ,
'max' => 2200 ,
2021-06-12 08:06:54 +12:00
]);
2020-01-13 21:46:09 +13:00
2022-06-22 22:51:49 +12:00
$duration = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/attributes/integer' , array_merge ([
2022-06-10 03:15:34 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'duration' ,
'required' => false ,
2023-03-25 16:29:40 +13:00
'min' => 60 ,
2022-06-10 03:15:34 +12:00
]);
2022-06-22 22:51:49 +12:00
$actors = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/attributes/string' , array_merge ([
2021-06-12 08:06:54 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2021-12-17 04:04:30 +13:00
'key' => 'actors' ,
2021-06-12 08:06:54 +12:00
'size' => 256 ,
'required' => false ,
'array' => true ,
]);
2022-07-25 20:53:41 +12:00
$datetime = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/attributes/datetime' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'birthDay' ,
'required' => false ,
]);
2023-03-25 16:29:40 +13:00
$relationship = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/attributes/relationship' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'relatedCollectionId' => $data [ 'actorsId' ],
'type' => 'oneToMany' ,
'twoWay' => true ,
'key' => 'starringActors' ,
'twoWayKey' => 'movie'
]);
2024-01-21 21:10:18 +13:00
$integers = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/attributes/integer' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'integers' ,
'required' => false ,
'array' => true ,
2024-01-22 05:22:02 +13:00
'min' => 10 ,
'max' => 99 ,
2024-01-21 21:10:18 +13:00
]);
2022-08-13 00:54:27 +12:00
$this -> assertEquals ( 202 , $title [ 'headers' ][ 'status-code' ]);
2021-08-23 02:06:59 +12:00
$this -> assertEquals ( $title [ 'body' ][ 'key' ], 'title' );
2021-06-15 07:54:19 +12:00
$this -> assertEquals ( $title [ 'body' ][ 'type' ], 'string' );
$this -> assertEquals ( $title [ 'body' ][ 'size' ], 256 );
$this -> assertEquals ( $title [ 'body' ][ 'required' ], true );
2023-05-30 14:52:59 +12:00
$this -> assertEquals ( 202 , $description [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $description [ 'body' ][ 'key' ], 'description' );
$this -> assertEquals ( $description [ 'body' ][ 'type' ], 'string' );
2023-06-15 17:28:35 +12:00
$this -> assertEquals ( $description [ 'body' ][ 'size' ], 512 );
2023-05-30 14:52:59 +12:00
$this -> assertEquals ( $description [ 'body' ][ 'required' ], false );
$this -> assertEquals ( $description [ 'body' ][ 'default' ], '' );
2023-06-15 17:28:35 +12:00
$this -> assertEquals ( 202 , $tagline [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $tagline [ 'body' ][ 'key' ], 'tagline' );
$this -> assertEquals ( $tagline [ 'body' ][ 'type' ], 'string' );
$this -> assertEquals ( $tagline [ 'body' ][ 'size' ], 512 );
$this -> assertEquals ( $tagline [ 'body' ][ 'required' ], false );
$this -> assertEquals ( $tagline [ 'body' ][ 'default' ], '' );
2022-08-13 00:54:27 +12:00
$this -> assertEquals ( 202 , $releaseYear [ 'headers' ][ 'status-code' ]);
2021-08-23 02:06:59 +12:00
$this -> assertEquals ( $releaseYear [ 'body' ][ 'key' ], 'releaseYear' );
2021-06-15 07:54:19 +12:00
$this -> assertEquals ( $releaseYear [ 'body' ][ 'type' ], 'integer' );
$this -> assertEquals ( $releaseYear [ 'body' ][ 'required' ], true );
2022-08-13 00:54:27 +12:00
$this -> assertEquals ( 202 , $duration [ 'headers' ][ 'status-code' ]);
2022-06-10 03:15:34 +12:00
$this -> assertEquals ( $duration [ 'body' ][ 'key' ], 'duration' );
$this -> assertEquals ( $duration [ 'body' ][ 'type' ], 'integer' );
$this -> assertEquals ( $duration [ 'body' ][ 'required' ], false );
2022-08-13 00:54:27 +12:00
$this -> assertEquals ( 202 , $actors [ 'headers' ][ 'status-code' ]);
2021-08-23 02:06:59 +12:00
$this -> assertEquals ( $actors [ 'body' ][ 'key' ], 'actors' );
2021-06-15 07:54:19 +12:00
$this -> assertEquals ( $actors [ 'body' ][ 'type' ], 'string' );
$this -> assertEquals ( $actors [ 'body' ][ 'size' ], 256 );
$this -> assertEquals ( $actors [ 'body' ][ 'required' ], false );
$this -> assertEquals ( $actors [ 'body' ][ 'array' ], true );
2021-06-12 08:06:54 +12:00
2022-08-12 11:53:52 +12:00
$this -> assertEquals ( $datetime [ 'headers' ][ 'status-code' ], 202 );
2022-07-29 03:24:17 +12:00
$this -> assertEquals ( $datetime [ 'body' ][ 'key' ], 'birthDay' );
$this -> assertEquals ( $datetime [ 'body' ][ 'type' ], 'datetime' );
$this -> assertEquals ( $datetime [ 'body' ][ 'required' ], false );
2023-03-25 16:29:40 +13:00
$this -> assertEquals ( $relationship [ 'headers' ][ 'status-code' ], 202 );
$this -> assertEquals ( $relationship [ 'body' ][ 'key' ], 'starringActors' );
$this -> assertEquals ( $relationship [ 'body' ][ 'type' ], 'relationship' );
$this -> assertEquals ( $relationship [ 'body' ][ 'relatedCollection' ], $data [ 'actorsId' ]);
$this -> assertEquals ( $relationship [ 'body' ][ 'relationType' ], 'oneToMany' );
$this -> assertEquals ( $relationship [ 'body' ][ 'twoWay' ], true );
$this -> assertEquals ( $relationship [ 'body' ][ 'twoWayKey' ], 'movie' );
2024-01-21 21:10:18 +13:00
$this -> assertEquals ( 202 , $integers [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $integers [ 'body' ][ 'key' ], 'integers' );
$this -> assertEquals ( $integers [ 'body' ][ 'type' ], 'integer' );
$this -> assertArrayNotHasKey ( 'size' , $integers [ 'body' ]);
$this -> assertEquals ( $integers [ 'body' ][ 'required' ], false );
$this -> assertEquals ( $integers [ 'body' ][ 'array' ], true );
2021-06-19 05:09:07 +12:00
// wait for database worker to create attributes
2021-08-23 16:06:53 +12:00
sleep ( 2 );
2021-06-19 05:09:07 +12:00
2022-06-22 22:51:49 +12:00
$movies = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ], array_merge ([
2021-06-19 05:09:07 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
2023-06-15 17:28:35 +12:00
]));
2021-06-19 05:09:07 +12:00
$this -> assertIsArray ( $movies [ 'body' ][ 'attributes' ]);
2024-01-22 05:22:02 +13:00
$this -> assertCount ( 9 , $movies [ 'body' ][ 'attributes' ]);
2021-08-23 02:06:59 +12:00
$this -> assertEquals ( $movies [ 'body' ][ 'attributes' ][ 0 ][ 'key' ], $title [ 'body' ][ 'key' ]);
2023-05-30 14:52:59 +12:00
$this -> assertEquals ( $movies [ 'body' ][ 'attributes' ][ 1 ][ 'key' ], $description [ 'body' ][ 'key' ]);
2023-06-15 17:28:35 +12:00
$this -> assertEquals ( $movies [ 'body' ][ 'attributes' ][ 2 ][ 'key' ], $tagline [ 'body' ][ 'key' ]);
$this -> assertEquals ( $movies [ 'body' ][ 'attributes' ][ 3 ][ 'key' ], $releaseYear [ 'body' ][ 'key' ]);
$this -> assertEquals ( $movies [ 'body' ][ 'attributes' ][ 4 ][ 'key' ], $duration [ 'body' ][ 'key' ]);
$this -> assertEquals ( $movies [ 'body' ][ 'attributes' ][ 5 ][ 'key' ], $actors [ 'body' ][ 'key' ]);
$this -> assertEquals ( $movies [ 'body' ][ 'attributes' ][ 6 ][ 'key' ], $datetime [ 'body' ][ 'key' ]);
$this -> assertEquals ( $movies [ 'body' ][ 'attributes' ][ 7 ][ 'key' ], $relationship [ 'body' ][ 'key' ]);
2024-01-22 05:22:02 +13:00
$this -> assertEquals ( $movies [ 'body' ][ 'attributes' ][ 8 ][ 'key' ], $integers [ 'body' ][ 'key' ]);
2021-06-19 05:09:07 +12:00
2021-06-12 08:06:54 +12:00
return $data ;
2020-01-13 21:46:09 +13:00
}
2023-07-21 06:50:53 +12:00
/**
* @ depends testCreateAttributes
*/
public function testListAttributes ( array $data ) : void
{
$databaseId = $data [ 'databaseId' ];
2023-08-10 06:41:18 +12:00
$response = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/attributes' , array_merge ([
2023-07-21 06:50:53 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
]), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: equal ( 'type' , [ 'string' ]) -> toString (),
Query :: limit ( 2 ) -> toString (),
Query :: cursorAfter ( new Document ([ '$id' => 'title' ])) -> toString ()
2024-01-09 13:56:01 +13:00
],
2023-07-21 06:50:53 +12:00
]);
2023-08-10 06:41:18 +12:00
$this -> assertEquals ( 200 , $response [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 2 , \count ( $response [ 'body' ][ 'attributes' ]));
$response = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/attributes' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
]), [
2023-12-20 23:55:09 +13:00
'queries' => [ Query :: select ([ 'key' ]) -> toString ()],
2023-08-10 06:41:18 +12:00
]);
$this -> assertEquals ( Exception :: GENERAL_ARGUMENT_INVALID , $response [ 'body' ][ 'type' ]);
$this -> assertEquals ( 400 , $response [ 'headers' ][ 'status-code' ]);
2023-07-21 06:50:53 +12:00
}
2023-08-22 15:25:55 +12:00
public function testUpdateAttributeEnum () : void
{
$database = $this -> client -> call ( Client :: METHOD_POST , '/databases' , [
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
], [
'databaseId' => ID :: unique (),
'name' => 'Test Database 2'
]);
$players = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $database [ 'body' ][ '$id' ] . '/collections' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'collectionId' => ID :: unique (),
'name' => 'Players' ,
'documentSecurity' => true ,
'permissions' => [
Permission :: create ( Role :: user ( $this -> getUser ()[ '$id' ])),
],
]);
// Create enum attribute
$attribute = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $database [ 'body' ][ '$id' ] . '/collections/' . $players [ 'body' ][ '$id' ] . '/attributes/enum' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
]), [
'key' => 'position' ,
'elements' => [ 'goalkeeper' , 'defender' , 'midfielder' , 'forward' ],
'required' => true ,
'array' => false ,
]);
$this -> assertEquals ( 202 , $attribute [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $attribute [ 'body' ][ 'key' ], 'position' );
$this -> assertEquals ( $attribute [ 'body' ][ 'elements' ], [ 'goalkeeper' , 'defender' , 'midfielder' , 'forward' ]);
\sleep ( 2 );
// Update enum attribute
$attribute = $this -> client -> call ( Client :: METHOD_PATCH , '/databases/' . $database [ 'body' ][ '$id' ] . '/collections/' . $players [ 'body' ][ '$id' ] . '/attributes/enum/' . $attribute [ 'body' ][ 'key' ], array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
]), [
'elements' => [ 'goalkeeper' , 'defender' , 'midfielder' , 'forward' , 'coach' ],
'required' => true ,
'default' => null
]);
$this -> assertEquals ( 200 , $attribute [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $attribute [ 'body' ][ 'elements' ], [ 'goalkeeper' , 'defender' , 'midfielder' , 'forward' , 'coach' ]);
}
2023-03-17 05:28:08 +13:00
/**
2023-03-24 18:56:49 +13:00
* @ depends testCreateAttributes
2023-03-17 05:28:08 +13:00
*/
2023-03-24 18:56:49 +13:00
public function testAttributeResponseModels ( array $data ) : array
2023-03-17 05:28:08 +13:00
{
$databaseId = $data [ 'databaseId' ];
2023-03-24 18:56:49 +13:00
$collection = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections' , array_merge ([
2023-03-17 05:28:08 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2023-03-24 18:56:49 +13:00
'collectionId' => ID :: unique (),
'name' => 'Response Models' ,
// 'permissions' missing on purpose to make sure it's optional
2023-03-17 05:28:08 +13:00
'documentSecurity' => true ,
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 201 , $collection [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $collection [ 'body' ][ 'name' ], 'Response Models' );
2023-03-20 06:09:17 +13:00
2023-03-24 18:56:49 +13:00
$collectionId = $collection [ 'body' ][ '$id' ];
$attributesPath = " /databases/ " . $databaseId . " /collections/ { $collectionId } /attributes " ;
$string = $this -> client -> call ( Client :: METHOD_POST , $attributesPath . '/string' , array_merge ([
2023-03-17 05:28:08 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2023-03-24 18:56:49 +13:00
'key' => 'string' ,
'size' => 16 ,
'required' => false ,
'default' => 'default' ,
2023-03-17 05:28:08 +13:00
]);
2023-03-24 18:56:49 +13:00
$email = $this -> client -> call ( Client :: METHOD_POST , $attributesPath . '/email' , array_merge ([
2023-03-24 03:08:38 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2023-03-24 18:56:49 +13:00
'key' => 'email' ,
2023-03-24 03:08:38 +13:00
'required' => false ,
2023-03-24 18:56:49 +13:00
'default' => 'default@example.com' ,
2023-03-24 03:08:38 +13:00
]);
2023-03-24 18:56:49 +13:00
$enum = $this -> client -> call ( Client :: METHOD_POST , $attributesPath . '/enum' , array_merge ([
2023-03-17 05:28:08 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2023-03-24 18:56:49 +13:00
'key' => 'enum' ,
'elements' => [ 'yes' , 'no' , 'maybe' ],
'required' => false ,
'default' => 'maybe' ,
2023-03-17 05:28:08 +13:00
]);
2023-03-24 18:56:49 +13:00
$ip = $this -> client -> call ( Client :: METHOD_POST , $attributesPath . '/ip' , array_merge ([
2023-03-17 05:28:08 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2023-03-24 18:56:49 +13:00
'key' => 'ip' ,
'required' => false ,
'default' => '192.0.2.0' ,
2023-03-17 05:28:08 +13:00
]);
2023-03-24 18:56:49 +13:00
$url = $this -> client -> call ( Client :: METHOD_POST , $attributesPath . '/url' , array_merge ([
2023-03-17 05:28:08 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
2023-03-24 18:56:49 +13:00
]), [
'key' => 'url' ,
'required' => false ,
'default' => 'http://example.com' ,
]);
2023-03-20 06:09:17 +13:00
2023-03-24 18:56:49 +13:00
$integer = $this -> client -> call ( Client :: METHOD_POST , $attributesPath . '/integer' , array_merge ([
2023-03-20 06:09:17 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'integer' ,
'required' => false ,
'min' => 1 ,
'max' => 5 ,
'default' => 3
2023-03-20 06:09:17 +13:00
]);
2023-03-24 18:56:49 +13:00
$float = $this -> client -> call ( Client :: METHOD_POST , $attributesPath . '/float' , array_merge ([
2023-03-20 06:09:17 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'float' ,
'required' => false ,
'min' => 1.5 ,
'max' => 5.5 ,
'default' => 3.5
2023-03-17 05:28:08 +13:00
]);
2023-03-24 18:56:49 +13:00
$boolean = $this -> client -> call ( Client :: METHOD_POST , $attributesPath . '/boolean' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'boolean' ,
'required' => false ,
'default' => true ,
]);
2023-03-20 06:09:17 +13:00
2023-03-24 18:56:49 +13:00
$datetime = $this -> client -> call ( Client :: METHOD_POST , $attributesPath . '/datetime' , array_merge ([
2023-03-22 01:32:05 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'datetime' ,
'required' => false ,
'default' => null ,
2023-03-22 01:32:05 +13:00
]);
2023-03-25 16:29:40 +13:00
$relationship = $this -> client -> call ( Client :: METHOD_POST , $attributesPath . '/relationship' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'relatedCollectionId' => $data [ 'actorsId' ],
'type' => 'oneToMany' ,
'twoWay' => true ,
'key' => 'relationship' ,
'twoWayKey' => 'twoWayKey'
]);
2024-01-21 21:10:18 +13:00
$strings = $this -> client -> call ( Client :: METHOD_POST , $attributesPath . '/string' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'names' ,
'size' => 512 ,
'required' => false ,
'array' => true ,
]);
$integers = $this -> client -> call ( Client :: METHOD_POST , $attributesPath . '/integer' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'numbers' ,
'required' => false ,
'array' => true ,
'min' => 1 ,
'max' => 999 ,
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 202 , $string [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'string' , $string [ 'body' ][ 'key' ]);
$this -> assertEquals ( 'string' , $string [ 'body' ][ 'type' ]);
$this -> assertEquals ( false , $string [ 'body' ][ 'required' ]);
$this -> assertEquals ( false , $string [ 'body' ][ 'array' ]);
$this -> assertEquals ( 16 , $string [ 'body' ][ 'size' ]);
$this -> assertEquals ( 'default' , $string [ 'body' ][ 'default' ]);
2023-03-20 06:09:17 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 202 , $email [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'email' , $email [ 'body' ][ 'key' ]);
$this -> assertEquals ( 'string' , $email [ 'body' ][ 'type' ]);
$this -> assertEquals ( false , $email [ 'body' ][ 'required' ]);
$this -> assertEquals ( false , $email [ 'body' ][ 'array' ]);
$this -> assertEquals ( 'email' , $email [ 'body' ][ 'format' ]);
$this -> assertEquals ( 'default@example.com' , $email [ 'body' ][ 'default' ]);
$this -> assertEquals ( 202 , $enum [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'enum' , $enum [ 'body' ][ 'key' ]);
$this -> assertEquals ( 'string' , $enum [ 'body' ][ 'type' ]);
$this -> assertEquals ( false , $enum [ 'body' ][ 'required' ]);
$this -> assertEquals ( false , $enum [ 'body' ][ 'array' ]);
$this -> assertEquals ( 'enum' , $enum [ 'body' ][ 'format' ]);
$this -> assertEquals ( 'maybe' , $enum [ 'body' ][ 'default' ]);
$this -> assertIsArray ( $enum [ 'body' ][ 'elements' ]);
$this -> assertEquals ([ 'yes' , 'no' , 'maybe' ], $enum [ 'body' ][ 'elements' ]);
$this -> assertEquals ( 202 , $ip [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'ip' , $ip [ 'body' ][ 'key' ]);
$this -> assertEquals ( 'string' , $ip [ 'body' ][ 'type' ]);
$this -> assertEquals ( false , $ip [ 'body' ][ 'required' ]);
$this -> assertEquals ( false , $ip [ 'body' ][ 'array' ]);
$this -> assertEquals ( 'ip' , $ip [ 'body' ][ 'format' ]);
$this -> assertEquals ( '192.0.2.0' , $ip [ 'body' ][ 'default' ]);
$this -> assertEquals ( 202 , $url [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'url' , $url [ 'body' ][ 'key' ]);
$this -> assertEquals ( 'string' , $url [ 'body' ][ 'type' ]);
$this -> assertEquals ( false , $url [ 'body' ][ 'required' ]);
$this -> assertEquals ( false , $url [ 'body' ][ 'array' ]);
$this -> assertEquals ( 'url' , $url [ 'body' ][ 'format' ]);
$this -> assertEquals ( 'http://example.com' , $url [ 'body' ][ 'default' ]);
$this -> assertEquals ( 202 , $integer [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'integer' , $integer [ 'body' ][ 'key' ]);
$this -> assertEquals ( 'integer' , $integer [ 'body' ][ 'type' ]);
$this -> assertEquals ( false , $integer [ 'body' ][ 'required' ]);
$this -> assertEquals ( false , $integer [ 'body' ][ 'array' ]);
$this -> assertEquals ( 1 , $integer [ 'body' ][ 'min' ]);
$this -> assertEquals ( 5 , $integer [ 'body' ][ 'max' ]);
$this -> assertEquals ( 3 , $integer [ 'body' ][ 'default' ]);
$this -> assertEquals ( 202 , $float [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'float' , $float [ 'body' ][ 'key' ]);
$this -> assertEquals ( 'double' , $float [ 'body' ][ 'type' ]);
$this -> assertEquals ( false , $float [ 'body' ][ 'required' ]);
$this -> assertEquals ( false , $float [ 'body' ][ 'array' ]);
$this -> assertEquals ( 1.5 , $float [ 'body' ][ 'min' ]);
$this -> assertEquals ( 5.5 , $float [ 'body' ][ 'max' ]);
$this -> assertEquals ( 3.5 , $float [ 'body' ][ 'default' ]);
$this -> assertEquals ( 202 , $boolean [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'boolean' , $boolean [ 'body' ][ 'key' ]);
$this -> assertEquals ( 'boolean' , $boolean [ 'body' ][ 'type' ]);
$this -> assertEquals ( false , $boolean [ 'body' ][ 'required' ]);
$this -> assertEquals ( false , $boolean [ 'body' ][ 'array' ]);
$this -> assertEquals ( true , $boolean [ 'body' ][ 'default' ]);
$this -> assertEquals ( 202 , $datetime [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'datetime' , $datetime [ 'body' ][ 'key' ]);
$this -> assertEquals ( 'datetime' , $datetime [ 'body' ][ 'type' ]);
$this -> assertEquals ( false , $datetime [ 'body' ][ 'required' ]);
$this -> assertEquals ( false , $datetime [ 'body' ][ 'array' ]);
$this -> assertEquals ( null , $datetime [ 'body' ][ 'default' ]);
2023-03-25 16:29:40 +13:00
$this -> assertEquals ( 202 , $relationship [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'relationship' , $relationship [ 'body' ][ 'key' ]);
$this -> assertEquals ( 'relationship' , $relationship [ 'body' ][ 'type' ]);
$this -> assertEquals ( false , $relationship [ 'body' ][ 'required' ]);
$this -> assertEquals ( false , $relationship [ 'body' ][ 'array' ]);
$this -> assertEquals ( $data [ 'actorsId' ], $relationship [ 'body' ][ 'relatedCollection' ]);
$this -> assertEquals ( 'oneToMany' , $relationship [ 'body' ][ 'relationType' ]);
$this -> assertEquals ( true , $relationship [ 'body' ][ 'twoWay' ]);
$this -> assertEquals ( 'twoWayKey' , $relationship [ 'body' ][ 'twoWayKey' ]);
2024-01-21 21:10:18 +13:00
$this -> assertEquals ( 202 , $strings [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'names' , $strings [ 'body' ][ 'key' ]);
$this -> assertEquals ( 'string' , $strings [ 'body' ][ 'type' ]);
$this -> assertEquals ( false , $strings [ 'body' ][ 'required' ]);
$this -> assertEquals ( true , $strings [ 'body' ][ 'array' ]);
$this -> assertEquals ( null , $strings [ 'body' ][ 'default' ]);
$this -> assertEquals ( 202 , $integers [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'numbers' , $integers [ 'body' ][ 'key' ]);
$this -> assertEquals ( 'integer' , $integers [ 'body' ][ 'type' ]);
$this -> assertEquals ( false , $integers [ 'body' ][ 'required' ]);
$this -> assertEquals ( true , $integers [ 'body' ][ 'array' ]);
$this -> assertEquals ( 1 , $integers [ 'body' ][ 'min' ]);
$this -> assertEquals ( 999 , $integers [ 'body' ][ 'max' ]);
$this -> assertEquals ( null , $integers [ 'body' ][ 'default' ]);
2023-03-25 16:29:40 +13:00
// Wait for database worker to create attributes
sleep ( 5 );
2023-03-24 18:56:49 +13:00
$stringResponse = $this -> client -> call ( Client :: METHOD_GET , $attributesPath . '/' . $string [ 'body' ][ 'key' ], array_merge ([
2023-03-20 06:31:15 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]));
2023-03-20 06:09:17 +13:00
2023-03-24 18:56:49 +13:00
$emailResponse = $this -> client -> call ( Client :: METHOD_GET , $attributesPath . '/' . $email [ 'body' ][ 'key' ], array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]));
2023-03-23 18:34:13 +13:00
2023-03-24 18:56:49 +13:00
$enumResponse = $this -> client -> call ( Client :: METHOD_GET , $attributesPath . '/' . $enum [ 'body' ][ 'key' ], array_merge ([
2023-03-22 01:32:05 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]));
2023-03-23 18:34:13 +13:00
2023-03-24 18:56:49 +13:00
$ipResponse = $this -> client -> call ( Client :: METHOD_GET , $attributesPath . '/' . $ip [ 'body' ][ 'key' ], array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]));
2023-03-20 06:09:17 +13:00
2023-03-24 18:56:49 +13:00
$urlResponse = $this -> client -> call ( Client :: METHOD_GET , $attributesPath . '/' . $url [ 'body' ][ 'key' ], array_merge ([
2023-03-23 18:34:13 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]));
2023-03-23 18:34:13 +13:00
2023-03-24 18:56:49 +13:00
$integerResponse = $this -> client -> call ( Client :: METHOD_GET , $attributesPath . '/' . $integer [ 'body' ][ 'key' ], array_merge ([
2023-03-22 06:52:25 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
2023-03-24 18:56:49 +13:00
]));
2023-03-22 06:52:25 +13:00
2023-03-24 18:56:49 +13:00
$floatResponse = $this -> client -> call ( Client :: METHOD_GET , $attributesPath . '/' . $float [ 'body' ][ 'key' ], array_merge ([
2023-03-22 06:52:25 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]));
2023-03-24 18:56:49 +13:00
$booleanResponse = $this -> client -> call ( Client :: METHOD_GET , $attributesPath . '/' . $boolean [ 'body' ][ 'key' ], array_merge ([
2023-03-23 04:49:08 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]));
2023-03-24 16:48:50 +13:00
2023-03-24 18:56:49 +13:00
$datetimeResponse = $this -> client -> call ( Client :: METHOD_GET , $attributesPath . '/' . $datetime [ 'body' ][ 'key' ], array_merge ([
2023-03-23 04:49:08 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]));
2023-03-23 04:49:08 +13:00
2023-03-25 16:29:40 +13:00
$relationshipResponse = $this -> client -> call ( Client :: METHOD_GET , $attributesPath . '/' . $relationship [ 'body' ][ 'key' ], array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]));
2024-01-21 21:10:18 +13:00
$stringsResponse = $this -> client -> call ( Client :: METHOD_GET , $attributesPath . '/' . $strings [ 'body' ][ 'key' ], array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]));
$integersResponse = $this -> client -> call ( Client :: METHOD_GET , $attributesPath . '/' . $integers [ 'body' ][ 'key' ], array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]));
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $stringResponse [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $string [ 'body' ][ 'key' ], $stringResponse [ 'body' ][ 'key' ]);
$this -> assertEquals ( $string [ 'body' ][ 'type' ], $stringResponse [ 'body' ][ 'type' ]);
$this -> assertEquals ( 'available' , $stringResponse [ 'body' ][ 'status' ]);
$this -> assertEquals ( $string [ 'body' ][ 'required' ], $stringResponse [ 'body' ][ 'required' ]);
$this -> assertEquals ( $string [ 'body' ][ 'array' ], $stringResponse [ 'body' ][ 'array' ]);
$this -> assertEquals ( 16 , $stringResponse [ 'body' ][ 'size' ]);
$this -> assertEquals ( $string [ 'body' ][ 'default' ], $stringResponse [ 'body' ][ 'default' ]);
2023-03-23 04:49:08 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $emailResponse [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $email [ 'body' ][ 'key' ], $emailResponse [ 'body' ][ 'key' ]);
$this -> assertEquals ( $email [ 'body' ][ 'type' ], $emailResponse [ 'body' ][ 'type' ]);
$this -> assertEquals ( 'available' , $emailResponse [ 'body' ][ 'status' ]);
$this -> assertEquals ( $email [ 'body' ][ 'required' ], $emailResponse [ 'body' ][ 'required' ]);
$this -> assertEquals ( $email [ 'body' ][ 'array' ], $emailResponse [ 'body' ][ 'array' ]);
$this -> assertEquals ( $email [ 'body' ][ 'format' ], $emailResponse [ 'body' ][ 'format' ]);
$this -> assertEquals ( $email [ 'body' ][ 'default' ], $emailResponse [ 'body' ][ 'default' ]);
2023-03-23 04:49:08 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $enumResponse [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $enum [ 'body' ][ 'key' ], $enumResponse [ 'body' ][ 'key' ]);
$this -> assertEquals ( $enum [ 'body' ][ 'type' ], $enumResponse [ 'body' ][ 'type' ]);
$this -> assertEquals ( 'available' , $enumResponse [ 'body' ][ 'status' ]);
$this -> assertEquals ( $enum [ 'body' ][ 'required' ], $enumResponse [ 'body' ][ 'required' ]);
$this -> assertEquals ( $enum [ 'body' ][ 'array' ], $enumResponse [ 'body' ][ 'array' ]);
$this -> assertEquals ( $enum [ 'body' ][ 'format' ], $enumResponse [ 'body' ][ 'format' ]);
$this -> assertEquals ( $enum [ 'body' ][ 'default' ], $enumResponse [ 'body' ][ 'default' ]);
$this -> assertEquals ( $enum [ 'body' ][ 'elements' ], $enumResponse [ 'body' ][ 'elements' ]);
2023-03-23 04:49:08 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $ipResponse [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $ip [ 'body' ][ 'key' ], $ipResponse [ 'body' ][ 'key' ]);
$this -> assertEquals ( $ip [ 'body' ][ 'type' ], $ipResponse [ 'body' ][ 'type' ]);
$this -> assertEquals ( 'available' , $ipResponse [ 'body' ][ 'status' ]);
$this -> assertEquals ( $ip [ 'body' ][ 'required' ], $ipResponse [ 'body' ][ 'required' ]);
$this -> assertEquals ( $ip [ 'body' ][ 'array' ], $ipResponse [ 'body' ][ 'array' ]);
$this -> assertEquals ( $ip [ 'body' ][ 'format' ], $ipResponse [ 'body' ][ 'format' ]);
$this -> assertEquals ( $ip [ 'body' ][ 'default' ], $ipResponse [ 'body' ][ 'default' ]);
2023-03-23 04:49:08 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $urlResponse [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $url [ 'body' ][ 'key' ], $urlResponse [ 'body' ][ 'key' ]);
$this -> assertEquals ( $url [ 'body' ][ 'type' ], $urlResponse [ 'body' ][ 'type' ]);
$this -> assertEquals ( 'available' , $urlResponse [ 'body' ][ 'status' ]);
$this -> assertEquals ( $url [ 'body' ][ 'required' ], $urlResponse [ 'body' ][ 'required' ]);
$this -> assertEquals ( $url [ 'body' ][ 'array' ], $urlResponse [ 'body' ][ 'array' ]);
$this -> assertEquals ( $url [ 'body' ][ 'format' ], $urlResponse [ 'body' ][ 'format' ]);
$this -> assertEquals ( $url [ 'body' ][ 'default' ], $urlResponse [ 'body' ][ 'default' ]);
2023-03-23 18:34:13 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $integerResponse [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $integer [ 'body' ][ 'key' ], $integerResponse [ 'body' ][ 'key' ]);
$this -> assertEquals ( $integer [ 'body' ][ 'type' ], $integerResponse [ 'body' ][ 'type' ]);
$this -> assertEquals ( 'available' , $integerResponse [ 'body' ][ 'status' ]);
$this -> assertEquals ( $integer [ 'body' ][ 'required' ], $integerResponse [ 'body' ][ 'required' ]);
$this -> assertEquals ( $integer [ 'body' ][ 'array' ], $integerResponse [ 'body' ][ 'array' ]);
$this -> assertEquals ( $integer [ 'body' ][ 'min' ], $integerResponse [ 'body' ][ 'min' ]);
$this -> assertEquals ( $integer [ 'body' ][ 'max' ], $integerResponse [ 'body' ][ 'max' ]);
$this -> assertEquals ( $integer [ 'body' ][ 'default' ], $integerResponse [ 'body' ][ 'default' ]);
2023-03-23 05:44:12 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $floatResponse [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $float [ 'body' ][ 'key' ], $floatResponse [ 'body' ][ 'key' ]);
$this -> assertEquals ( $float [ 'body' ][ 'type' ], $floatResponse [ 'body' ][ 'type' ]);
$this -> assertEquals ( 'available' , $floatResponse [ 'body' ][ 'status' ]);
$this -> assertEquals ( $float [ 'body' ][ 'required' ], $floatResponse [ 'body' ][ 'required' ]);
$this -> assertEquals ( $float [ 'body' ][ 'array' ], $floatResponse [ 'body' ][ 'array' ]);
$this -> assertEquals ( $float [ 'body' ][ 'min' ], $floatResponse [ 'body' ][ 'min' ]);
$this -> assertEquals ( $float [ 'body' ][ 'max' ], $floatResponse [ 'body' ][ 'max' ]);
$this -> assertEquals ( $float [ 'body' ][ 'default' ], $floatResponse [ 'body' ][ 'default' ]);
2023-03-23 05:44:12 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $booleanResponse [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $boolean [ 'body' ][ 'key' ], $booleanResponse [ 'body' ][ 'key' ]);
$this -> assertEquals ( $boolean [ 'body' ][ 'type' ], $booleanResponse [ 'body' ][ 'type' ]);
$this -> assertEquals ( 'available' , $booleanResponse [ 'body' ][ 'status' ]);
$this -> assertEquals ( $boolean [ 'body' ][ 'required' ], $booleanResponse [ 'body' ][ 'required' ]);
$this -> assertEquals ( $boolean [ 'body' ][ 'array' ], $booleanResponse [ 'body' ][ 'array' ]);
$this -> assertEquals ( $boolean [ 'body' ][ 'default' ], $booleanResponse [ 'body' ][ 'default' ]);
2023-03-24 16:48:50 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $datetimeResponse [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $datetime [ 'body' ][ 'key' ], $datetimeResponse [ 'body' ][ 'key' ]);
$this -> assertEquals ( $datetime [ 'body' ][ 'type' ], $datetimeResponse [ 'body' ][ 'type' ]);
$this -> assertEquals ( 'available' , $datetimeResponse [ 'body' ][ 'status' ]);
$this -> assertEquals ( $datetime [ 'body' ][ 'required' ], $datetimeResponse [ 'body' ][ 'required' ]);
$this -> assertEquals ( $datetime [ 'body' ][ 'array' ], $datetimeResponse [ 'body' ][ 'array' ]);
$this -> assertEquals ( $datetime [ 'body' ][ 'default' ], $datetimeResponse [ 'body' ][ 'default' ]);
2023-03-24 16:48:50 +13:00
2023-03-25 16:29:40 +13:00
$this -> assertEquals ( 200 , $relationshipResponse [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $relationship [ 'body' ][ 'key' ], $relationshipResponse [ 'body' ][ 'key' ]);
$this -> assertEquals ( $relationship [ 'body' ][ 'type' ], $relationshipResponse [ 'body' ][ 'type' ]);
$this -> assertEquals ( 'available' , $relationshipResponse [ 'body' ][ 'status' ]);
$this -> assertEquals ( $relationship [ 'body' ][ 'required' ], $relationshipResponse [ 'body' ][ 'required' ]);
$this -> assertEquals ( $relationship [ 'body' ][ 'array' ], $relationshipResponse [ 'body' ][ 'array' ]);
$this -> assertEquals ( $relationship [ 'body' ][ 'relatedCollection' ], $relationshipResponse [ 'body' ][ 'relatedCollection' ]);
$this -> assertEquals ( $relationship [ 'body' ][ 'relationType' ], $relationshipResponse [ 'body' ][ 'relationType' ]);
$this -> assertEquals ( $relationship [ 'body' ][ 'twoWay' ], $relationshipResponse [ 'body' ][ 'twoWay' ]);
$this -> assertEquals ( $relationship [ 'body' ][ 'twoWayKey' ], $relationshipResponse [ 'body' ][ 'twoWayKey' ]);
2023-03-24 18:56:49 +13:00
$attributes = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes' , array_merge ([
2023-03-24 16:48:50 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
2023-03-24 18:56:49 +13:00
]));
2023-03-24 16:48:50 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $attributes [ 'headers' ][ 'status-code' ]);
2024-01-21 21:10:18 +13:00
$this -> assertEquals ( 12 , $attributes [ 'body' ][ 'total' ]);
2023-03-24 16:48:50 +13:00
2023-03-24 18:56:49 +13:00
$attributes = $attributes [ 'body' ][ 'attributes' ];
$this -> assertIsArray ( $attributes );
2024-01-21 21:10:18 +13:00
$this -> assertCount ( 12 , $attributes );
2023-03-24 16:48:50 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( $stringResponse [ 'body' ][ 'key' ], $attributes [ 0 ][ 'key' ]);
$this -> assertEquals ( $stringResponse [ 'body' ][ 'type' ], $attributes [ 0 ][ 'type' ]);
$this -> assertEquals ( $stringResponse [ 'body' ][ 'status' ], $attributes [ 0 ][ 'status' ]);
$this -> assertEquals ( $stringResponse [ 'body' ][ 'required' ], $attributes [ 0 ][ 'required' ]);
$this -> assertEquals ( $stringResponse [ 'body' ][ 'array' ], $attributes [ 0 ][ 'array' ]);
$this -> assertEquals ( $stringResponse [ 'body' ][ 'size' ], $attributes [ 0 ][ 'size' ]);
$this -> assertEquals ( $stringResponse [ 'body' ][ 'default' ], $attributes [ 0 ][ 'default' ]);
$this -> assertEquals ( $emailResponse [ 'body' ][ 'key' ], $attributes [ 1 ][ 'key' ]);
$this -> assertEquals ( $emailResponse [ 'body' ][ 'type' ], $attributes [ 1 ][ 'type' ]);
$this -> assertEquals ( $emailResponse [ 'body' ][ 'status' ], $attributes [ 1 ][ 'status' ]);
$this -> assertEquals ( $emailResponse [ 'body' ][ 'required' ], $attributes [ 1 ][ 'required' ]);
$this -> assertEquals ( $emailResponse [ 'body' ][ 'array' ], $attributes [ 1 ][ 'array' ]);
$this -> assertEquals ( $emailResponse [ 'body' ][ 'default' ], $attributes [ 1 ][ 'default' ]);
$this -> assertEquals ( $emailResponse [ 'body' ][ 'format' ], $attributes [ 1 ][ 'format' ]);
$this -> assertEquals ( $enumResponse [ 'body' ][ 'key' ], $attributes [ 2 ][ 'key' ]);
$this -> assertEquals ( $enumResponse [ 'body' ][ 'type' ], $attributes [ 2 ][ 'type' ]);
$this -> assertEquals ( $enumResponse [ 'body' ][ 'status' ], $attributes [ 2 ][ 'status' ]);
$this -> assertEquals ( $enumResponse [ 'body' ][ 'required' ], $attributes [ 2 ][ 'required' ]);
$this -> assertEquals ( $enumResponse [ 'body' ][ 'array' ], $attributes [ 2 ][ 'array' ]);
$this -> assertEquals ( $enumResponse [ 'body' ][ 'default' ], $attributes [ 2 ][ 'default' ]);
$this -> assertEquals ( $enumResponse [ 'body' ][ 'format' ], $attributes [ 2 ][ 'format' ]);
$this -> assertEquals ( $enumResponse [ 'body' ][ 'elements' ], $attributes [ 2 ][ 'elements' ]);
$this -> assertEquals ( $ipResponse [ 'body' ][ 'key' ], $attributes [ 3 ][ 'key' ]);
$this -> assertEquals ( $ipResponse [ 'body' ][ 'type' ], $attributes [ 3 ][ 'type' ]);
$this -> assertEquals ( $ipResponse [ 'body' ][ 'status' ], $attributes [ 3 ][ 'status' ]);
$this -> assertEquals ( $ipResponse [ 'body' ][ 'required' ], $attributes [ 3 ][ 'required' ]);
$this -> assertEquals ( $ipResponse [ 'body' ][ 'array' ], $attributes [ 3 ][ 'array' ]);
$this -> assertEquals ( $ipResponse [ 'body' ][ 'default' ], $attributes [ 3 ][ 'default' ]);
$this -> assertEquals ( $ipResponse [ 'body' ][ 'format' ], $attributes [ 3 ][ 'format' ]);
$this -> assertEquals ( $urlResponse [ 'body' ][ 'key' ], $attributes [ 4 ][ 'key' ]);
$this -> assertEquals ( $urlResponse [ 'body' ][ 'type' ], $attributes [ 4 ][ 'type' ]);
$this -> assertEquals ( $urlResponse [ 'body' ][ 'status' ], $attributes [ 4 ][ 'status' ]);
$this -> assertEquals ( $urlResponse [ 'body' ][ 'required' ], $attributes [ 4 ][ 'required' ]);
$this -> assertEquals ( $urlResponse [ 'body' ][ 'array' ], $attributes [ 4 ][ 'array' ]);
$this -> assertEquals ( $urlResponse [ 'body' ][ 'default' ], $attributes [ 4 ][ 'default' ]);
$this -> assertEquals ( $urlResponse [ 'body' ][ 'format' ], $attributes [ 4 ][ 'format' ]);
$this -> assertEquals ( $integerResponse [ 'body' ][ 'key' ], $attributes [ 5 ][ 'key' ]);
$this -> assertEquals ( $integerResponse [ 'body' ][ 'type' ], $attributes [ 5 ][ 'type' ]);
$this -> assertEquals ( $integerResponse [ 'body' ][ 'status' ], $attributes [ 5 ][ 'status' ]);
$this -> assertEquals ( $integerResponse [ 'body' ][ 'required' ], $attributes [ 5 ][ 'required' ]);
$this -> assertEquals ( $integerResponse [ 'body' ][ 'array' ], $attributes [ 5 ][ 'array' ]);
$this -> assertEquals ( $integerResponse [ 'body' ][ 'default' ], $attributes [ 5 ][ 'default' ]);
$this -> assertEquals ( $integerResponse [ 'body' ][ 'min' ], $attributes [ 5 ][ 'min' ]);
$this -> assertEquals ( $integerResponse [ 'body' ][ 'max' ], $attributes [ 5 ][ 'max' ]);
$this -> assertEquals ( $floatResponse [ 'body' ][ 'key' ], $attributes [ 6 ][ 'key' ]);
$this -> assertEquals ( $floatResponse [ 'body' ][ 'type' ], $attributes [ 6 ][ 'type' ]);
$this -> assertEquals ( $floatResponse [ 'body' ][ 'status' ], $attributes [ 6 ][ 'status' ]);
$this -> assertEquals ( $floatResponse [ 'body' ][ 'required' ], $attributes [ 6 ][ 'required' ]);
$this -> assertEquals ( $floatResponse [ 'body' ][ 'array' ], $attributes [ 6 ][ 'array' ]);
$this -> assertEquals ( $floatResponse [ 'body' ][ 'default' ], $attributes [ 6 ][ 'default' ]);
$this -> assertEquals ( $floatResponse [ 'body' ][ 'min' ], $attributes [ 6 ][ 'min' ]);
$this -> assertEquals ( $floatResponse [ 'body' ][ 'max' ], $attributes [ 6 ][ 'max' ]);
$this -> assertEquals ( $booleanResponse [ 'body' ][ 'key' ], $attributes [ 7 ][ 'key' ]);
$this -> assertEquals ( $booleanResponse [ 'body' ][ 'type' ], $attributes [ 7 ][ 'type' ]);
$this -> assertEquals ( $booleanResponse [ 'body' ][ 'status' ], $attributes [ 7 ][ 'status' ]);
$this -> assertEquals ( $booleanResponse [ 'body' ][ 'required' ], $attributes [ 7 ][ 'required' ]);
$this -> assertEquals ( $booleanResponse [ 'body' ][ 'array' ], $attributes [ 7 ][ 'array' ]);
$this -> assertEquals ( $booleanResponse [ 'body' ][ 'default' ], $attributes [ 7 ][ 'default' ]);
$this -> assertEquals ( $datetimeResponse [ 'body' ][ 'key' ], $attributes [ 8 ][ 'key' ]);
$this -> assertEquals ( $datetimeResponse [ 'body' ][ 'type' ], $attributes [ 8 ][ 'type' ]);
$this -> assertEquals ( $datetimeResponse [ 'body' ][ 'status' ], $attributes [ 8 ][ 'status' ]);
$this -> assertEquals ( $datetimeResponse [ 'body' ][ 'required' ], $attributes [ 8 ][ 'required' ]);
$this -> assertEquals ( $datetimeResponse [ 'body' ][ 'array' ], $attributes [ 8 ][ 'array' ]);
$this -> assertEquals ( $datetimeResponse [ 'body' ][ 'default' ], $attributes [ 8 ][ 'default' ]);
2023-03-25 16:29:40 +13:00
$this -> assertEquals ( $relationshipResponse [ 'body' ][ 'key' ], $attributes [ 9 ][ 'key' ]);
$this -> assertEquals ( $relationshipResponse [ 'body' ][ 'type' ], $attributes [ 9 ][ 'type' ]);
$this -> assertEquals ( $relationshipResponse [ 'body' ][ 'status' ], $attributes [ 9 ][ 'status' ]);
$this -> assertEquals ( $relationshipResponse [ 'body' ][ 'required' ], $attributes [ 9 ][ 'required' ]);
$this -> assertEquals ( $relationshipResponse [ 'body' ][ 'array' ], $attributes [ 9 ][ 'array' ]);
$this -> assertEquals ( $relationshipResponse [ 'body' ][ 'relatedCollection' ], $attributes [ 9 ][ 'relatedCollection' ]);
$this -> assertEquals ( $relationshipResponse [ 'body' ][ 'relationType' ], $attributes [ 9 ][ 'relationType' ]);
$this -> assertEquals ( $relationshipResponse [ 'body' ][ 'twoWay' ], $attributes [ 9 ][ 'twoWay' ]);
$this -> assertEquals ( $relationshipResponse [ 'body' ][ 'twoWayKey' ], $attributes [ 9 ][ 'twoWayKey' ]);
2024-01-21 21:10:18 +13:00
$this -> assertEquals ( $stringsResponse [ 'body' ][ 'key' ], $attributes [ 10 ][ 'key' ]);
$this -> assertEquals ( $stringsResponse [ 'body' ][ 'type' ], $attributes [ 10 ][ 'type' ]);
$this -> assertEquals ( $stringsResponse [ 'body' ][ 'status' ], $attributes [ 10 ][ 'status' ]);
$this -> assertEquals ( $stringsResponse [ 'body' ][ 'required' ], $attributes [ 10 ][ 'required' ]);
$this -> assertEquals ( $stringsResponse [ 'body' ][ 'array' ], $attributes [ 10 ][ 'array' ]);
$this -> assertEquals ( $stringsResponse [ 'body' ][ 'default' ], $attributes [ 10 ][ 'default' ]);
$this -> assertEquals ( $integersResponse [ 'body' ][ 'key' ], $attributes [ 11 ][ 'key' ]);
$this -> assertEquals ( $integersResponse [ 'body' ][ 'type' ], $attributes [ 11 ][ 'type' ]);
$this -> assertEquals ( $integersResponse [ 'body' ][ 'status' ], $attributes [ 11 ][ 'status' ]);
$this -> assertEquals ( $integersResponse [ 'body' ][ 'required' ], $attributes [ 11 ][ 'required' ]);
$this -> assertEquals ( $integersResponse [ 'body' ][ 'array' ], $attributes [ 11 ][ 'array' ]);
$this -> assertEquals ( $integersResponse [ 'body' ][ 'default' ], $attributes [ 11 ][ 'default' ]);
$this -> assertEquals ( $integersResponse [ 'body' ][ 'min' ], $attributes [ 11 ][ 'min' ]);
$this -> assertEquals ( $integersResponse [ 'body' ][ 'max' ], $attributes [ 11 ][ 'max' ]);
2023-03-24 18:56:49 +13:00
$collection = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $collectionId , array_merge ([
2023-03-24 16:48:50 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
2023-03-24 18:56:49 +13:00
]));
2023-03-24 16:48:50 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $collection [ 'headers' ][ 'status-code' ]);
2023-03-24 16:48:50 +13:00
2023-03-24 18:56:49 +13:00
$attributes = $collection [ 'body' ][ 'attributes' ];
2023-03-24 16:48:50 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertIsArray ( $attributes );
2024-01-21 21:10:18 +13:00
$this -> assertCount ( 12 , $attributes );
2023-03-24 16:48:50 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( $stringResponse [ 'body' ][ 'key' ], $attributes [ 0 ][ 'key' ]);
$this -> assertEquals ( $stringResponse [ 'body' ][ 'type' ], $attributes [ 0 ][ 'type' ]);
$this -> assertEquals ( $stringResponse [ 'body' ][ 'status' ], $attributes [ 0 ][ 'status' ]);
$this -> assertEquals ( $stringResponse [ 'body' ][ 'required' ], $attributes [ 0 ][ 'required' ]);
$this -> assertEquals ( $stringResponse [ 'body' ][ 'array' ], $attributes [ 0 ][ 'array' ]);
$this -> assertEquals ( $stringResponse [ 'body' ][ 'size' ], $attributes [ 0 ][ 'size' ]);
$this -> assertEquals ( $stringResponse [ 'body' ][ 'default' ], $attributes [ 0 ][ 'default' ]);
2023-03-24 16:48:50 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( $emailResponse [ 'body' ][ 'key' ], $attributes [ 1 ][ 'key' ]);
$this -> assertEquals ( $emailResponse [ 'body' ][ 'type' ], $attributes [ 1 ][ 'type' ]);
$this -> assertEquals ( $emailResponse [ 'body' ][ 'status' ], $attributes [ 1 ][ 'status' ]);
$this -> assertEquals ( $emailResponse [ 'body' ][ 'required' ], $attributes [ 1 ][ 'required' ]);
$this -> assertEquals ( $emailResponse [ 'body' ][ 'array' ], $attributes [ 1 ][ 'array' ]);
$this -> assertEquals ( $emailResponse [ 'body' ][ 'default' ], $attributes [ 1 ][ 'default' ]);
$this -> assertEquals ( $emailResponse [ 'body' ][ 'format' ], $attributes [ 1 ][ 'format' ]);
2023-03-24 16:48:50 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( $enumResponse [ 'body' ][ 'key' ], $attributes [ 2 ][ 'key' ]);
$this -> assertEquals ( $enumResponse [ 'body' ][ 'type' ], $attributes [ 2 ][ 'type' ]);
$this -> assertEquals ( $enumResponse [ 'body' ][ 'status' ], $attributes [ 2 ][ 'status' ]);
$this -> assertEquals ( $enumResponse [ 'body' ][ 'required' ], $attributes [ 2 ][ 'required' ]);
$this -> assertEquals ( $enumResponse [ 'body' ][ 'array' ], $attributes [ 2 ][ 'array' ]);
$this -> assertEquals ( $enumResponse [ 'body' ][ 'default' ], $attributes [ 2 ][ 'default' ]);
$this -> assertEquals ( $enumResponse [ 'body' ][ 'format' ], $attributes [ 2 ][ 'format' ]);
$this -> assertEquals ( $enumResponse [ 'body' ][ 'elements' ], $attributes [ 2 ][ 'elements' ]);
2023-03-24 16:48:50 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( $ipResponse [ 'body' ][ 'key' ], $attributes [ 3 ][ 'key' ]);
$this -> assertEquals ( $ipResponse [ 'body' ][ 'type' ], $attributes [ 3 ][ 'type' ]);
$this -> assertEquals ( $ipResponse [ 'body' ][ 'status' ], $attributes [ 3 ][ 'status' ]);
$this -> assertEquals ( $ipResponse [ 'body' ][ 'required' ], $attributes [ 3 ][ 'required' ]);
$this -> assertEquals ( $ipResponse [ 'body' ][ 'array' ], $attributes [ 3 ][ 'array' ]);
$this -> assertEquals ( $ipResponse [ 'body' ][ 'default' ], $attributes [ 3 ][ 'default' ]);
$this -> assertEquals ( $ipResponse [ 'body' ][ 'format' ], $attributes [ 3 ][ 'format' ]);
2023-03-24 16:48:50 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( $urlResponse [ 'body' ][ 'key' ], $attributes [ 4 ][ 'key' ]);
$this -> assertEquals ( $urlResponse [ 'body' ][ 'type' ], $attributes [ 4 ][ 'type' ]);
$this -> assertEquals ( $urlResponse [ 'body' ][ 'status' ], $attributes [ 4 ][ 'status' ]);
$this -> assertEquals ( $urlResponse [ 'body' ][ 'required' ], $attributes [ 4 ][ 'required' ]);
$this -> assertEquals ( $urlResponse [ 'body' ][ 'array' ], $attributes [ 4 ][ 'array' ]);
$this -> assertEquals ( $urlResponse [ 'body' ][ 'default' ], $attributes [ 4 ][ 'default' ]);
$this -> assertEquals ( $urlResponse [ 'body' ][ 'format' ], $attributes [ 4 ][ 'format' ]);
$this -> assertEquals ( $integerResponse [ 'body' ][ 'key' ], $attributes [ 5 ][ 'key' ]);
$this -> assertEquals ( $integerResponse [ 'body' ][ 'type' ], $attributes [ 5 ][ 'type' ]);
$this -> assertEquals ( $integerResponse [ 'body' ][ 'status' ], $attributes [ 5 ][ 'status' ]);
$this -> assertEquals ( $integerResponse [ 'body' ][ 'required' ], $attributes [ 5 ][ 'required' ]);
$this -> assertEquals ( $integerResponse [ 'body' ][ 'array' ], $attributes [ 5 ][ 'array' ]);
$this -> assertEquals ( $integerResponse [ 'body' ][ 'default' ], $attributes [ 5 ][ 'default' ]);
$this -> assertEquals ( $integerResponse [ 'body' ][ 'min' ], $attributes [ 5 ][ 'min' ]);
$this -> assertEquals ( $integerResponse [ 'body' ][ 'max' ], $attributes [ 5 ][ 'max' ]);
$this -> assertEquals ( $floatResponse [ 'body' ][ 'key' ], $attributes [ 6 ][ 'key' ]);
$this -> assertEquals ( $floatResponse [ 'body' ][ 'type' ], $attributes [ 6 ][ 'type' ]);
$this -> assertEquals ( $floatResponse [ 'body' ][ 'status' ], $attributes [ 6 ][ 'status' ]);
$this -> assertEquals ( $floatResponse [ 'body' ][ 'required' ], $attributes [ 6 ][ 'required' ]);
$this -> assertEquals ( $floatResponse [ 'body' ][ 'array' ], $attributes [ 6 ][ 'array' ]);
$this -> assertEquals ( $floatResponse [ 'body' ][ 'default' ], $attributes [ 6 ][ 'default' ]);
$this -> assertEquals ( $floatResponse [ 'body' ][ 'min' ], $attributes [ 6 ][ 'min' ]);
$this -> assertEquals ( $floatResponse [ 'body' ][ 'max' ], $attributes [ 6 ][ 'max' ]);
$this -> assertEquals ( $booleanResponse [ 'body' ][ 'key' ], $attributes [ 7 ][ 'key' ]);
$this -> assertEquals ( $booleanResponse [ 'body' ][ 'type' ], $attributes [ 7 ][ 'type' ]);
$this -> assertEquals ( $booleanResponse [ 'body' ][ 'status' ], $attributes [ 7 ][ 'status' ]);
$this -> assertEquals ( $booleanResponse [ 'body' ][ 'required' ], $attributes [ 7 ][ 'required' ]);
$this -> assertEquals ( $booleanResponse [ 'body' ][ 'array' ], $attributes [ 7 ][ 'array' ]);
$this -> assertEquals ( $booleanResponse [ 'body' ][ 'default' ], $attributes [ 7 ][ 'default' ]);
$this -> assertEquals ( $datetimeResponse [ 'body' ][ 'key' ], $attributes [ 8 ][ 'key' ]);
$this -> assertEquals ( $datetimeResponse [ 'body' ][ 'type' ], $attributes [ 8 ][ 'type' ]);
$this -> assertEquals ( $datetimeResponse [ 'body' ][ 'status' ], $attributes [ 8 ][ 'status' ]);
$this -> assertEquals ( $datetimeResponse [ 'body' ][ 'required' ], $attributes [ 8 ][ 'required' ]);
$this -> assertEquals ( $datetimeResponse [ 'body' ][ 'array' ], $attributes [ 8 ][ 'array' ]);
$this -> assertEquals ( $datetimeResponse [ 'body' ][ 'default' ], $attributes [ 8 ][ 'default' ]);
2023-03-25 16:29:40 +13:00
$this -> assertEquals ( $relationshipResponse [ 'body' ][ 'key' ], $attributes [ 9 ][ 'key' ]);
$this -> assertEquals ( $relationshipResponse [ 'body' ][ 'type' ], $attributes [ 9 ][ 'type' ]);
$this -> assertEquals ( $relationshipResponse [ 'body' ][ 'status' ], $attributes [ 9 ][ 'status' ]);
$this -> assertEquals ( $relationshipResponse [ 'body' ][ 'required' ], $attributes [ 9 ][ 'required' ]);
$this -> assertEquals ( $relationshipResponse [ 'body' ][ 'array' ], $attributes [ 9 ][ 'array' ]);
$this -> assertEquals ( $relationshipResponse [ 'body' ][ 'relatedCollection' ], $attributes [ 9 ][ 'relatedCollection' ]);
$this -> assertEquals ( $relationshipResponse [ 'body' ][ 'relationType' ], $attributes [ 9 ][ 'relationType' ]);
$this -> assertEquals ( $relationshipResponse [ 'body' ][ 'twoWay' ], $attributes [ 9 ][ 'twoWay' ]);
$this -> assertEquals ( $relationshipResponse [ 'body' ][ 'twoWayKey' ], $attributes [ 9 ][ 'twoWayKey' ]);
2024-01-21 21:10:18 +13:00
$this -> assertEquals ( $stringsResponse [ 'body' ][ 'key' ], $attributes [ 10 ][ 'key' ]);
$this -> assertEquals ( $stringsResponse [ 'body' ][ 'type' ], $attributes [ 10 ][ 'type' ]);
$this -> assertEquals ( $stringsResponse [ 'body' ][ 'status' ], $attributes [ 10 ][ 'status' ]);
$this -> assertEquals ( $stringsResponse [ 'body' ][ 'required' ], $attributes [ 10 ][ 'required' ]);
$this -> assertEquals ( $stringsResponse [ 'body' ][ 'array' ], $attributes [ 10 ][ 'array' ]);
$this -> assertEquals ( $stringsResponse [ 'body' ][ 'default' ], $attributes [ 10 ][ 'default' ]);
$this -> assertEquals ( $integersResponse [ 'body' ][ 'key' ], $attributes [ 11 ][ 'key' ]);
$this -> assertEquals ( $integersResponse [ 'body' ][ 'type' ], $attributes [ 11 ][ 'type' ]);
$this -> assertEquals ( $integersResponse [ 'body' ][ 'status' ], $attributes [ 11 ][ 'status' ]);
$this -> assertEquals ( $integersResponse [ 'body' ][ 'required' ], $attributes [ 11 ][ 'required' ]);
$this -> assertEquals ( $integersResponse [ 'body' ][ 'array' ], $attributes [ 11 ][ 'array' ]);
$this -> assertEquals ( $integersResponse [ 'body' ][ 'default' ], $attributes [ 11 ][ 'default' ]);
$this -> assertEquals ( $integersResponse [ 'body' ][ 'min' ], $attributes [ 11 ][ 'min' ]);
$this -> assertEquals ( $integersResponse [ 'body' ][ 'max' ], $attributes [ 11 ][ 'max' ]);
2023-03-24 18:56:49 +13:00
/**
* Test for FAILURE
*/
$badEnum = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/enum' , array_merge ([
2023-03-24 16:48:50 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'enum' ,
'elements' => [ 'yes' , 'no' , '' ],
'required' => false ,
'default' => 'maybe' ,
]);
2023-03-24 16:48:50 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 400 , $badEnum [ 'headers' ][ 'status-code' ]);
2024-01-25 03:56:43 +13:00
$this -> assertEquals ( 'Invalid `elements` param: Value must a valid array no longer than 100 items and Value must be a valid string and at least 1 chars and no longer than 255 chars' , $badEnum [ 'body' ][ 'message' ]);
2023-03-24 16:48:50 +13:00
2023-03-24 18:56:49 +13:00
return $data ;
2023-03-24 16:48:50 +13:00
}
/**
2023-03-24 18:56:49 +13:00
* @ depends testCreateAttributes
2023-03-24 16:48:50 +13:00
*/
2023-03-24 18:56:49 +13:00
public function testCreateIndexes ( array $data ) : array
2023-03-24 16:48:50 +13:00
{
$databaseId = $data [ 'databaseId' ];
2023-06-15 17:28:35 +12:00
2023-03-24 18:56:49 +13:00
$titleIndex = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/indexes' , array_merge ([
2023-03-24 16:48:50 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2023-03-24 18:56:49 +13:00
'key' => 'titleIndex' ,
'type' => 'fulltext' ,
'attributes' => [ 'title' ],
2023-03-24 16:48:50 +13:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 202 , $titleIndex [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'titleIndex' , $titleIndex [ 'body' ][ 'key' ]);
$this -> assertEquals ( 'fulltext' , $titleIndex [ 'body' ][ 'type' ]);
$this -> assertCount ( 1 , $titleIndex [ 'body' ][ 'attributes' ]);
$this -> assertEquals ( 'title' , $titleIndex [ 'body' ][ 'attributes' ][ 0 ]);
2023-03-24 16:48:50 +13:00
2023-03-24 18:56:49 +13:00
$releaseYearIndex = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/indexes' , array_merge ([
2023-03-24 16:48:50 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2023-03-24 18:56:49 +13:00
'key' => 'releaseYear' ,
'type' => 'key' ,
'attributes' => [ 'releaseYear' ],
2023-03-24 16:48:50 +13:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 202 , $releaseYearIndex [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'releaseYear' , $releaseYearIndex [ 'body' ][ 'key' ]);
$this -> assertEquals ( 'key' , $releaseYearIndex [ 'body' ][ 'type' ]);
$this -> assertCount ( 1 , $releaseYearIndex [ 'body' ][ 'attributes' ]);
$this -> assertEquals ( 'releaseYear' , $releaseYearIndex [ 'body' ][ 'attributes' ][ 0 ]);
2023-03-24 16:48:50 +13:00
2024-02-09 02:01:20 +13:00
$releaseWithDate1 = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/indexes' , array_merge ([
2023-03-24 16:48:50 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2023-03-24 18:56:49 +13:00
'key' => 'releaseYearDated' ,
'type' => 'key' ,
'attributes' => [ 'releaseYear' , '$createdAt' , '$updatedAt' ],
2023-03-24 16:48:50 +13:00
]);
2024-02-09 02:01:20 +13:00
$this -> assertEquals ( 202 , $releaseWithDate1 [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'releaseYearDated' , $releaseWithDate1 [ 'body' ][ 'key' ]);
$this -> assertEquals ( 'key' , $releaseWithDate1 [ 'body' ][ 'type' ]);
$this -> assertCount ( 3 , $releaseWithDate1 [ 'body' ][ 'attributes' ]);
$this -> assertEquals ( 'releaseYear' , $releaseWithDate1 [ 'body' ][ 'attributes' ][ 0 ]);
$this -> assertEquals ( '$createdAt' , $releaseWithDate1 [ 'body' ][ 'attributes' ][ 1 ]);
$this -> assertEquals ( '$updatedAt' , $releaseWithDate1 [ 'body' ][ 'attributes' ][ 2 ]);
2023-03-24 16:48:50 +13:00
2024-02-09 02:01:20 +13:00
$releaseWithDate2 = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/indexes' , array_merge ([
2023-03-24 16:48:50 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'birthDay' ,
'type' => 'key' ,
'attributes' => [ 'birthDay' ],
]);
2023-03-24 16:48:50 +13:00
2024-02-09 02:01:20 +13:00
$this -> assertEquals ( 202 , $releaseWithDate2 [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'birthDay' , $releaseWithDate2 [ 'body' ][ 'key' ]);
$this -> assertEquals ( 'key' , $releaseWithDate2 [ 'body' ][ 'type' ]);
$this -> assertCount ( 1 , $releaseWithDate2 [ 'body' ][ 'attributes' ]);
$this -> assertEquals ( 'birthDay' , $releaseWithDate2 [ 'body' ][ 'attributes' ][ 0 ]);
2023-03-24 16:48:50 +13:00
2023-06-15 17:28:35 +12:00
// Test for failure
$fulltextReleaseYear = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/indexes' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'releaseYearDated' ,
'type' => 'fulltext' ,
'attributes' => [ 'releaseYear' ],
]);
$this -> assertEquals ( 400 , $fulltextReleaseYear [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $fulltextReleaseYear [ 'body' ][ 'message' ], 'Attribute "releaseYear" cannot be part of a FULLTEXT index, must be of type string' );
$noAttributes = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/indexes' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'none' ,
'type' => 'key' ,
'attributes' => [],
]);
$this -> assertEquals ( 400 , $noAttributes [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $noAttributes [ 'body' ][ 'message' ], 'No attributes provided for index' );
$duplicates = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/indexes' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'duplicate' ,
'type' => 'fulltext' ,
'attributes' => [ 'releaseYear' , 'releaseYear' ],
]);
$this -> assertEquals ( 400 , $duplicates [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $duplicates [ 'body' ][ 'message' ], 'Duplicate attributes provided' );
$tooLong = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/indexes' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
]), [
'key' => 'tooLong' ,
'type' => 'key' ,
'attributes' => [ 'description' , 'tagline' ],
]);
$this -> assertEquals ( 400 , $tooLong [ 'headers' ][ 'status-code' ]);
$this -> assertStringContainsString ( 'Index length is longer than the maximum' , $tooLong [ 'body' ][ 'message' ]);
2024-01-21 21:10:18 +13:00
$fulltextArray = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/indexes' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
]), [
'key' => 'ft' ,
'type' => 'fulltext' ,
2024-01-22 05:22:02 +13:00
'attributes' => [ 'actors' ],
2024-01-21 21:10:18 +13:00
]);
2024-01-22 05:22:02 +13:00
2024-01-21 21:10:18 +13:00
$this -> assertEquals ( 400 , $fulltextArray [ 'headers' ][ 'status-code' ]);
2024-01-22 05:22:02 +13:00
$this -> assertEquals ( '"Fulltext" index is forbidden on array attributes' , $fulltextArray [ 'body' ][ 'message' ]);
$actorsArray = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/indexes' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
]), [
'key' => 'index-actors' ,
'type' => 'key' ,
'attributes' => [ 'actors' ],
]);
$this -> assertEquals ( 202 , $actorsArray [ 'headers' ][ 'status-code' ]);
$twoLevelsArray = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/indexes' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
]), [
'key' => 'index-ip-actors' ,
'type' => 'key' ,
'attributes' => [ 'releaseYear' , 'actors' ], // 2 levels
2024-02-09 02:01:20 +13:00
'orders' => [ 'DESC' , 'DESC' ],
2024-01-22 05:22:02 +13:00
]);
$this -> assertEquals ( 202 , $twoLevelsArray [ 'headers' ][ 'status-code' ]);
2024-02-09 02:01:20 +13:00
$this -> assertEquals ( 'DESC' , $twoLevelsArray [ 'body' ][ 'orders' ][ 0 ]);
$this -> assertEquals ( null , $twoLevelsArray [ 'body' ][ 'orders' ][ 1 ]); // Overwrite by API (array)
2024-01-22 05:22:02 +13:00
$unknown = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/indexes' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
]), [
'key' => 'index-unknown' ,
'type' => 'key' ,
'attributes' => [ 'Unknown' ],
]);
$this -> assertEquals ( 400 , $unknown [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'Unknown attribute: Unknown' , $unknown [ 'body' ][ 'message' ]);
2024-01-21 21:10:18 +13:00
2024-02-09 02:01:20 +13:00
$index1 = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/indexes' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
]), [
'key' => 'integers-order' ,
'type' => 'key' ,
'attributes' => [ 'integers' ], // array attribute
'orders' => [ 'DESC' ], // Check order is removed in API
]);
$this -> assertEquals ( 202 , $index1 [ 'headers' ][ 'status-code' ]);
$index2 = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/indexes' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
]), [
'key' => 'integers-size' ,
'type' => 'key' ,
'attributes' => [ 'integers' ], // array attribute
]);
$this -> assertEquals ( 202 , $index2 [ 'headers' ][ 'status-code' ]);
/**
* Create Indexes by worker
*/
sleep ( 2 );
$movies = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ], array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), []);
$this -> assertIsArray ( $movies [ 'body' ][ 'indexes' ]);
$this -> assertCount ( 8 , $movies [ 'body' ][ 'indexes' ]);
$this -> assertEquals ( $titleIndex [ 'body' ][ 'key' ], $movies [ 'body' ][ 'indexes' ][ 0 ][ 'key' ]);
$this -> assertEquals ( $releaseYearIndex [ 'body' ][ 'key' ], $movies [ 'body' ][ 'indexes' ][ 1 ][ 'key' ]);
$this -> assertEquals ( $releaseWithDate1 [ 'body' ][ 'key' ], $movies [ 'body' ][ 'indexes' ][ 2 ][ 'key' ]);
$this -> assertEquals ( $releaseWithDate2 [ 'body' ][ 'key' ], $movies [ 'body' ][ 'indexes' ][ 3 ][ 'key' ]);
foreach ( $movies [ 'body' ][ 'indexes' ] as $index ) {
$this -> assertEquals ( 'available' , $index [ 'status' ]);
}
2023-03-24 18:56:49 +13:00
return $data ;
2023-03-24 03:08:38 +13:00
}
/**
2023-03-24 18:56:49 +13:00
* @ depends testCreateIndexes
2023-07-21 23:10:44 +12:00
*/
public function testListIndexes ( array $data ) : void
{
$databaseId = $data [ 'databaseId' ];
2023-08-10 06:41:18 +12:00
$response = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/indexes' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
]), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: equal ( 'type' , [ 'key' ]) -> toString (),
Query :: limit ( 2 ) -> toString ()
],
2023-08-10 06:41:18 +12:00
]);
$this -> assertEquals ( 200 , $response [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 2 , \count ( $response [ 'body' ][ 'indexes' ]));
$response = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/indexes' , array_merge ([
2023-07-21 23:10:44 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
]), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: select ([ 'key' ]) -> toString (),
],
2023-07-21 23:10:44 +12:00
]);
2023-08-10 06:41:18 +12:00
$this -> assertEquals ( Exception :: GENERAL_ARGUMENT_INVALID , $response [ 'body' ][ 'type' ]);
$this -> assertEquals ( 400 , $response [ 'headers' ][ 'status-code' ]);
2023-07-21 23:10:44 +12:00
}
2023-03-24 03:08:38 +13:00
/**
2023-03-24 18:56:49 +13:00
* @ depends testCreateIndexes
2023-03-24 03:08:38 +13:00
*/
2023-03-24 18:56:49 +13:00
public function testCreateDocument ( array $data ) : array
2023-03-24 03:08:38 +13:00
{
2023-03-24 18:56:49 +13:00
$databaseId = $data [ 'databaseId' ];
$document1 = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2023-03-24 03:08:38 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-03-24 18:56:49 +13:00
'documentId' => ID :: unique (),
'data' => [
'title' => 'Captain America' ,
'releaseYear' => 1944 ,
'birthDay' => '1975-06-12 14:12:55+02:00' ,
'actors' => [
'Chris Evans' ,
'Samuel Jackson' ,
]
2023-03-24 16:48:50 +13:00
],
2023-03-24 18:56:49 +13:00
'permissions' => [
Permission :: read ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: update ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: delete ( Role :: user ( $this -> getUser ()[ '$id' ])),
]
2023-03-24 16:48:50 +13:00
]);
2023-03-24 18:56:49 +13:00
$document2 = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2023-03-24 16:48:50 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-03-24 18:56:49 +13:00
'documentId' => ID :: unique (),
'data' => [
'title' => 'Spider-Man: Far From Home' ,
'releaseYear' => 2019 ,
'birthDay' => null ,
'actors' => [
'Tom Holland' ,
'Zendaya Maree Stoermer' ,
'Samuel Jackson' ,
2024-01-22 05:22:02 +13:00
],
'integers' => [ 50 , 60 ]
2023-03-24 03:08:38 +13:00
],
2023-03-24 18:56:49 +13:00
'permissions' => [
Permission :: read ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: update ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: delete ( Role :: user ( $this -> getUser ()[ '$id' ])),
]
2023-03-24 03:08:38 +13:00
]);
2023-03-24 18:56:49 +13:00
$document3 = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2023-03-24 03:08:38 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-03-24 18:56:49 +13:00
'documentId' => ID :: unique (),
'data' => [
'title' => 'Spider-Man: Homecoming' ,
'releaseYear' => 2017 ,
'birthDay' => '1975-06-12 14:12:55 America/New_York' ,
2023-03-25 16:29:40 +13:00
'duration' => 65 ,
2023-03-24 18:56:49 +13:00
'actors' => [
'Tom Holland' ,
'Zendaya Maree Stoermer' ,
],
2024-01-22 05:22:02 +13:00
'integers' => [ 50 ]
2023-03-24 03:08:38 +13:00
],
2023-03-24 18:56:49 +13:00
'permissions' => [
Permission :: read ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: update ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: delete ( Role :: user ( $this -> getUser ()[ '$id' ])),
]
2023-03-24 03:08:38 +13:00
]);
2023-03-24 18:56:49 +13:00
$document4 = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2023-03-24 03:08:38 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-03-24 18:56:49 +13:00
'documentId' => ID :: unique (),
'data' => [
'releaseYear' => 2020 , // Missing title, expect an 400 error
2023-03-24 03:08:38 +13:00
],
2023-03-24 18:56:49 +13:00
'permissions' => [
Permission :: read ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: update ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: delete ( Role :: user ( $this -> getUser ()[ '$id' ])),
]
2023-03-24 03:08:38 +13:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 201 , $document1 [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $data [ 'moviesId' ], $document1 [ 'body' ][ '$collectionId' ]);
$this -> assertArrayNotHasKey ( '$collection' , $document1 [ 'body' ]);
$this -> assertEquals ( $databaseId , $document1 [ 'body' ][ '$databaseId' ]);
$this -> assertEquals ( $document1 [ 'body' ][ 'title' ], 'Captain America' );
$this -> assertEquals ( $document1 [ 'body' ][ 'releaseYear' ], 1944 );
$this -> assertIsArray ( $document1 [ 'body' ][ '$permissions' ]);
$this -> assertCount ( 3 , $document1 [ 'body' ][ '$permissions' ]);
$this -> assertCount ( 2 , $document1 [ 'body' ][ 'actors' ]);
$this -> assertEquals ( $document1 [ 'body' ][ 'actors' ][ 0 ], 'Chris Evans' );
$this -> assertEquals ( $document1 [ 'body' ][ 'actors' ][ 1 ], 'Samuel Jackson' );
$this -> assertEquals ( $document1 [ 'body' ][ 'birthDay' ], '1975-06-12T12:12:55.000+00:00' );
2023-03-24 03:08:38 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 201 , $document2 [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $data [ 'moviesId' ], $document2 [ 'body' ][ '$collectionId' ]);
$this -> assertArrayNotHasKey ( '$collection' , $document2 [ 'body' ]);
$this -> assertEquals ( $databaseId , $document2 [ 'body' ][ '$databaseId' ]);
$this -> assertEquals ( $document2 [ 'body' ][ 'title' ], 'Spider-Man: Far From Home' );
$this -> assertEquals ( $document2 [ 'body' ][ 'releaseYear' ], 2019 );
$this -> assertEquals ( $document2 [ 'body' ][ 'duration' ], null );
$this -> assertIsArray ( $document2 [ 'body' ][ '$permissions' ]);
$this -> assertCount ( 3 , $document2 [ 'body' ][ '$permissions' ]);
$this -> assertCount ( 3 , $document2 [ 'body' ][ 'actors' ]);
$this -> assertEquals ( $document2 [ 'body' ][ 'actors' ][ 0 ], 'Tom Holland' );
$this -> assertEquals ( $document2 [ 'body' ][ 'actors' ][ 1 ], 'Zendaya Maree Stoermer' );
$this -> assertEquals ( $document2 [ 'body' ][ 'actors' ][ 2 ], 'Samuel Jackson' );
$this -> assertEquals ( $document2 [ 'body' ][ 'birthDay' ], null );
2024-01-22 05:22:02 +13:00
$this -> assertEquals ( $document2 [ 'body' ][ 'integers' ][ 0 ], 50 );
$this -> assertEquals ( $document2 [ 'body' ][ 'integers' ][ 1 ], 60 );
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 201 , $document3 [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $data [ 'moviesId' ], $document3 [ 'body' ][ '$collectionId' ]);
$this -> assertArrayNotHasKey ( '$collection' , $document3 [ 'body' ]);
$this -> assertEquals ( $databaseId , $document3 [ 'body' ][ '$databaseId' ]);
$this -> assertEquals ( $document3 [ 'body' ][ 'title' ], 'Spider-Man: Homecoming' );
$this -> assertEquals ( $document3 [ 'body' ][ 'releaseYear' ], 2017 );
2023-03-25 16:29:40 +13:00
$this -> assertEquals ( $document3 [ 'body' ][ 'duration' ], 65 );
2023-03-24 18:56:49 +13:00
$this -> assertIsArray ( $document3 [ 'body' ][ '$permissions' ]);
$this -> assertCount ( 3 , $document3 [ 'body' ][ '$permissions' ]);
$this -> assertCount ( 2 , $document3 [ 'body' ][ 'actors' ]);
$this -> assertEquals ( $document3 [ 'body' ][ 'actors' ][ 0 ], 'Tom Holland' );
$this -> assertEquals ( $document3 [ 'body' ][ 'actors' ][ 1 ], 'Zendaya Maree Stoermer' );
$this -> assertEquals ( $document3 [ 'body' ][ 'birthDay' ], '1975-06-12T18:12:55.000+00:00' ); // UTC for NY
$this -> assertEquals ( 400 , $document4 [ 'headers' ][ 'status-code' ]);
2023-05-30 14:07:12 +12:00
// Delete document 4 with incomplete path
$this -> assertEquals ( 404 , $this -> client -> call ( Client :: METHOD_DELETE , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents/' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()))[ 'headers' ][ 'status-code' ]);
2023-03-24 18:56:49 +13:00
return $data ;
2023-03-17 05:28:08 +13:00
}
2021-08-28 11:45:18 +12:00
/**
2023-03-24 18:56:49 +13:00
* @ depends testCreateDocument
2021-08-28 11:45:18 +12:00
*/
2023-03-24 18:56:49 +13:00
public function testListDocuments ( array $data ) : array
2021-08-25 06:02:33 +12:00
{
2022-06-22 22:51:49 +12:00
$databaseId = $data [ 'databaseId' ];
2023-03-24 18:56:49 +13:00
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2021-08-25 06:02:33 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: orderAsc ( 'releaseYear' ) -> toString (),
],
2021-08-25 06:02:33 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $documents [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 1944 , $documents [ 'body' ][ 'documents' ][ 0 ][ 'releaseYear' ]);
$this -> assertEquals ( 2017 , $documents [ 'body' ][ 'documents' ][ 1 ][ 'releaseYear' ]);
$this -> assertEquals ( 2019 , $documents [ 'body' ][ 'documents' ][ 2 ][ 'releaseYear' ]);
$this -> assertFalse ( array_key_exists ( '$internalId' , $documents [ 'body' ][ 'documents' ][ 0 ]));
$this -> assertFalse ( array_key_exists ( '$internalId' , $documents [ 'body' ][ 'documents' ][ 1 ]));
$this -> assertFalse ( array_key_exists ( '$internalId' , $documents [ 'body' ][ 'documents' ][ 2 ]));
$this -> assertCount ( 3 , $documents [ 'body' ][ 'documents' ]);
2021-08-25 06:02:33 +12:00
2023-03-24 18:56:49 +13:00
foreach ( $documents [ 'body' ][ 'documents' ] as $document ) {
$this -> assertEquals ( $data [ 'moviesId' ], $document [ '$collectionId' ]);
$this -> assertArrayNotHasKey ( '$collection' , $document );
$this -> assertEquals ( $databaseId , $document [ '$databaseId' ]);
}
2022-06-22 22:51:49 +12:00
2023-03-24 18:56:49 +13:00
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2021-08-25 06:02:33 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: orderDesc ( 'releaseYear' ) -> toString (),
],
2021-08-25 06:02:33 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $documents [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 1944 , $documents [ 'body' ][ 'documents' ][ 2 ][ 'releaseYear' ]);
$this -> assertEquals ( 2017 , $documents [ 'body' ][ 'documents' ][ 1 ][ 'releaseYear' ]);
$this -> assertEquals ( 2019 , $documents [ 'body' ][ 'documents' ][ 0 ][ 'releaseYear' ]);
$this -> assertCount ( 3 , $documents [ 'body' ][ 'documents' ]);
return [ 'documents' => $documents [ 'body' ][ 'documents' ], 'databaseId' => $databaseId ];
}
/**
* @ depends testListDocuments
*/
public function testGetDocument ( array $data ) : void
{
$databaseId = $data [ 'databaseId' ];
foreach ( $data [ 'documents' ] as $document ) {
$response = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $document [ '$collectionId' ] . '/documents/' . $document [ '$id' ], array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( 200 , $response [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $response [ 'body' ][ '$id' ], $document [ '$id' ]);
$this -> assertEquals ( $document [ '$collectionId' ], $response [ 'body' ][ '$collectionId' ]);
$this -> assertArrayNotHasKey ( '$collection' , $response [ 'body' ]);
$this -> assertEquals ( $document [ '$databaseId' ], $response [ 'body' ][ '$databaseId' ]);
$this -> assertEquals ( $response [ 'body' ][ 'title' ], $document [ 'title' ]);
$this -> assertEquals ( $response [ 'body' ][ 'releaseYear' ], $document [ 'releaseYear' ]);
$this -> assertEquals ( $response [ 'body' ][ '$permissions' ], $document [ '$permissions' ]);
$this -> assertEquals ( $response [ 'body' ][ 'birthDay' ], $document [ 'birthDay' ]);
$this -> assertFalse ( array_key_exists ( '$internalId' , $response [ 'body' ]));
}
}
/**
* @ depends testListDocuments
*/
public function testGetDocumentWithQueries ( array $data ) : void
{
$databaseId = $data [ 'databaseId' ];
$document = $data [ 'documents' ][ 0 ];
$response = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $document [ '$collectionId' ] . '/documents/' . $document [ '$id' ], array_merge ([
2021-08-25 06:02:33 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
'queries' => [
2023-12-20 23:55:09 +13:00
Query :: select ([ 'title' , 'releaseYear' , '$id' ]) -> toString (),
2023-03-24 18:56:49 +13:00
],
2021-08-25 06:02:33 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $response [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $document [ 'title' ], $response [ 'body' ][ 'title' ]);
$this -> assertEquals ( $document [ 'releaseYear' ], $response [ 'body' ][ 'releaseYear' ]);
$this -> assertArrayNotHasKey ( 'birthDay' , $response [ 'body' ]);
}
/**
* @ depends testCreateDocument
*/
public function testListDocumentsAfterPagination ( array $data ) : array
{
$databaseId = $data [ 'databaseId' ];
/**
* Test after without order .
*/
$base = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2021-08-25 06:02:33 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()));
2021-08-25 06:02:33 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $base [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'Captain America' , $base [ 'body' ][ 'documents' ][ 0 ][ 'title' ]);
$this -> assertEquals ( 'Spider-Man: Far From Home' , $base [ 'body' ][ 'documents' ][ 1 ][ 'title' ]);
$this -> assertEquals ( 'Spider-Man: Homecoming' , $base [ 'body' ][ 'documents' ][ 2 ][ 'title' ]);
$this -> assertCount ( 3 , $base [ 'body' ][ 'documents' ]);
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2021-08-25 06:02:33 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: cursorAfter ( new Document ([ '$id' => $base [ 'body' ][ 'documents' ][ 0 ][ '$id' ]])) -> toString ()
],
2021-08-25 06:02:33 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $documents [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $base [ 'body' ][ 'documents' ][ 1 ][ '$id' ], $documents [ 'body' ][ 'documents' ][ 0 ][ '$id' ]);
$this -> assertEquals ( $base [ 'body' ][ 'documents' ][ 2 ][ '$id' ], $documents [ 'body' ][ 'documents' ][ 1 ][ '$id' ]);
$this -> assertCount ( 2 , $documents [ 'body' ][ 'documents' ]);
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2021-08-25 06:02:33 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: cursorAfter ( new Document ([ '$id' => $base [ 'body' ][ 'documents' ][ 2 ][ '$id' ]])) -> toString ()
],
2021-08-25 06:02:33 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $documents [ 'headers' ][ 'status-code' ]);
$this -> assertEmpty ( $documents [ 'body' ][ 'documents' ]);
/**
* Test with ASC order and after .
*/
$base = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2022-07-28 22:26:22 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: orderAsc ( 'releaseYear' ) -> toString ()
],
2022-07-28 22:26:22 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $base [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 1944 , $base [ 'body' ][ 'documents' ][ 0 ][ 'releaseYear' ]);
$this -> assertEquals ( 2017 , $base [ 'body' ][ 'documents' ][ 1 ][ 'releaseYear' ]);
$this -> assertEquals ( 2019 , $base [ 'body' ][ 'documents' ][ 2 ][ 'releaseYear' ]);
$this -> assertCount ( 3 , $base [ 'body' ][ 'documents' ]);
2021-08-25 06:02:33 +12:00
2023-03-24 18:56:49 +13:00
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: cursorAfter ( new Document ([ '$id' => $base [ 'body' ][ 'documents' ][ 1 ][ '$id' ]])) -> toString (),
Query :: orderAsc ( 'releaseYear' ) -> toString ()
],
2023-03-24 18:56:49 +13:00
]);
2021-08-25 06:02:33 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $documents [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $base [ 'body' ][ 'documents' ][ 2 ][ '$id' ], $documents [ 'body' ][ 'documents' ][ 0 ][ '$id' ]);
$this -> assertCount ( 1 , $documents [ 'body' ][ 'documents' ]);
2021-09-11 08:14:31 +12:00
2023-03-24 18:56:49 +13:00
/**
* Test with DESC order and after .
*/
$base = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: orderDesc ( 'releaseYear' ) -> toString ()
],
2023-03-24 18:56:49 +13:00
]);
2021-08-25 06:02:33 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $base [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 1944 , $base [ 'body' ][ 'documents' ][ 2 ][ 'releaseYear' ]);
$this -> assertEquals ( 2017 , $base [ 'body' ][ 'documents' ][ 1 ][ 'releaseYear' ]);
$this -> assertEquals ( 2019 , $base [ 'body' ][ 'documents' ][ 0 ][ 'releaseYear' ]);
$this -> assertCount ( 3 , $base [ 'body' ][ 'documents' ]);
2021-08-25 06:02:33 +12:00
2023-03-24 18:56:49 +13:00
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: cursorAfter ( new Document ([ '$id' => $base [ 'body' ][ 'documents' ][ 1 ][ '$id' ]])) -> toString (),
Query :: orderDesc ( 'releaseYear' ) -> toString ()
],
2023-03-24 18:56:49 +13:00
]);
2021-08-25 06:02:33 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $documents [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $base [ 'body' ][ 'documents' ][ 2 ][ '$id' ], $documents [ 'body' ][ 'documents' ][ 0 ][ '$id' ]);
$this -> assertCount ( 1 , $documents [ 'body' ][ 'documents' ]);
2021-08-25 06:02:33 +12:00
2023-03-24 18:56:49 +13:00
/**
* Test after with unknown document .
*/
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: cursorAfter ( new Document ([ '$id' => 'unknown' ])) -> toString (),
],
2023-03-24 18:56:49 +13:00
]);
2021-08-25 06:02:33 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 400 , $documents [ 'headers' ][ 'status-code' ]);
2022-07-28 22:26:22 +12:00
2023-03-24 18:56:49 +13:00
return [];
}
2021-08-25 06:02:33 +12:00
2023-03-24 18:56:49 +13:00
/**
* @ depends testCreateDocument
*/
public function testListDocumentsBeforePagination ( array $data ) : array
{
$databaseId = $data [ 'databaseId' ];
/**
* Test before without order .
*/
$base = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2021-08-26 07:37:21 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()));
2021-08-26 07:37:21 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $base [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'Captain America' , $base [ 'body' ][ 'documents' ][ 0 ][ 'title' ]);
$this -> assertEquals ( 'Spider-Man: Far From Home' , $base [ 'body' ][ 'documents' ][ 1 ][ 'title' ]);
$this -> assertEquals ( 'Spider-Man: Homecoming' , $base [ 'body' ][ 'documents' ][ 2 ][ 'title' ]);
$this -> assertCount ( 3 , $base [ 'body' ][ 'documents' ]);
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2021-08-26 07:37:21 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: cursorBefore ( new Document ([ '$id' => $base [ 'body' ][ 'documents' ][ 2 ][ '$id' ]])) -> toString (),
],
2023-03-24 18:56:49 +13:00
]);
2021-08-26 07:37:21 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $documents [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $base [ 'body' ][ 'documents' ][ 0 ][ '$id' ], $documents [ 'body' ][ 'documents' ][ 0 ][ '$id' ]);
$this -> assertEquals ( $base [ 'body' ][ 'documents' ][ 1 ][ '$id' ], $documents [ 'body' ][ 'documents' ][ 1 ][ '$id' ]);
$this -> assertCount ( 2 , $documents [ 'body' ][ 'documents' ]);
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2021-09-11 08:14:31 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: cursorBefore ( new Document ([ '$id' => $base [ 'body' ][ 'documents' ][ 0 ][ '$id' ]])) -> toString (),
],
2023-03-24 18:56:49 +13:00
]);
2021-09-11 08:14:31 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $documents [ 'headers' ][ 'status-code' ]);
$this -> assertEmpty ( $documents [ 'body' ][ 'documents' ]);
/**
* Test with ASC order and after .
*/
$base = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2021-08-26 07:37:21 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: orderAsc ( 'releaseYear' ) -> toString (),
],
2023-03-24 18:56:49 +13:00
]);
2021-08-26 07:37:21 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $base [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 1944 , $base [ 'body' ][ 'documents' ][ 0 ][ 'releaseYear' ]);
$this -> assertEquals ( 2017 , $base [ 'body' ][ 'documents' ][ 1 ][ 'releaseYear' ]);
$this -> assertEquals ( 2019 , $base [ 'body' ][ 'documents' ][ 2 ][ 'releaseYear' ]);
$this -> assertCount ( 3 , $base [ 'body' ][ 'documents' ]);
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2021-08-26 07:37:21 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: cursorBefore ( new Document ([ '$id' => $base [ 'body' ][ 'documents' ][ 1 ][ '$id' ]])) -> toString (),
Query :: orderAsc ( 'releaseYear' ) -> toString (),
],
2023-03-24 18:56:49 +13:00
]);
2021-08-26 07:37:21 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $documents [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $base [ 'body' ][ 'documents' ][ 0 ][ '$id' ], $documents [ 'body' ][ 'documents' ][ 0 ][ '$id' ]);
$this -> assertCount ( 1 , $documents [ 'body' ][ 'documents' ]);
/**
* Test with DESC order and after .
*/
$base = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2021-08-26 07:37:21 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: orderDesc ( 'releaseYear' ) -> toString (),
],
2023-03-24 18:56:49 +13:00
]);
2021-08-26 07:37:21 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $base [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 1944 , $base [ 'body' ][ 'documents' ][ 2 ][ 'releaseYear' ]);
$this -> assertEquals ( 2017 , $base [ 'body' ][ 'documents' ][ 1 ][ 'releaseYear' ]);
$this -> assertEquals ( 2019 , $base [ 'body' ][ 'documents' ][ 0 ][ 'releaseYear' ]);
$this -> assertCount ( 3 , $base [ 'body' ][ 'documents' ]);
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2021-08-26 07:37:21 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: cursorBefore ( new Document ([ '$id' => $base [ 'body' ][ 'documents' ][ 1 ][ '$id' ]])) -> toString (),
Query :: orderDesc ( 'releaseYear' ) -> toString (),
],
2023-03-24 18:56:49 +13:00
]);
2021-08-26 07:37:21 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $documents [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $base [ 'body' ][ 'documents' ][ 0 ][ '$id' ], $documents [ 'body' ][ 'documents' ][ 0 ][ '$id' ]);
$this -> assertCount ( 1 , $documents [ 'body' ][ 'documents' ]);
return [];
}
/**
* @ depends testCreateDocument
*/
public function testListDocumentsLimitAndOffset ( array $data ) : array
{
$databaseId = $data [ 'databaseId' ];
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2021-08-26 07:37:21 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: orderAsc ( 'releaseYear' ) -> toString (),
Query :: limit ( 1 ) -> toString (),
],
2023-03-24 18:56:49 +13:00
]);
2021-08-26 07:37:21 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $documents [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 1944 , $documents [ 'body' ][ 'documents' ][ 0 ][ 'releaseYear' ]);
$this -> assertCount ( 1 , $documents [ 'body' ][ 'documents' ]);
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2022-07-28 22:26:22 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: orderAsc ( 'releaseYear' ) -> toString (),
Query :: limit ( 2 ) -> toString (),
Query :: offset ( 1 ) -> toString (),
],
2023-03-24 18:56:49 +13:00
]);
2021-08-26 07:37:21 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $documents [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 2017 , $documents [ 'body' ][ 'documents' ][ 0 ][ 'releaseYear' ]);
$this -> assertEquals ( 2019 , $documents [ 'body' ][ 'documents' ][ 1 ][ 'releaseYear' ]);
$this -> assertCount ( 2 , $documents [ 'body' ][ 'documents' ]);
2021-08-26 07:37:21 +12:00
2023-03-24 18:56:49 +13:00
return [];
}
2022-07-28 22:26:22 +12:00
2023-03-24 18:56:49 +13:00
/**
* @ depends testCreateDocument
*/
public function testDocumentsListQueries ( array $data ) : array
{
$databaseId = $data [ 'databaseId' ];
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2021-08-25 06:02:33 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: search ( 'title' , 'Captain America' ) -> toString (),
],
2023-03-24 18:56:49 +13:00
]);
2021-08-28 08:30:26 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $documents [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 1944 , $documents [ 'body' ][ 'documents' ][ 0 ][ 'releaseYear' ]);
$this -> assertCount ( 1 , $documents [ 'body' ][ 'documents' ]);
2022-07-28 22:26:22 +12:00
2023-03-24 18:56:49 +13:00
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2021-08-28 08:30:26 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: equal ( '$id' , [ $documents [ 'body' ][ 'documents' ][ 0 ][ '$id' ]]) -> toString (),
],
2023-03-24 18:56:49 +13:00
]);
2021-09-11 08:14:31 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $documents [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 1944 , $documents [ 'body' ][ 'documents' ][ 0 ][ 'releaseYear' ]);
$this -> assertCount ( 1 , $documents [ 'body' ][ 'documents' ]);
2021-09-11 08:14:31 +12:00
2023-03-24 18:56:49 +13:00
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: search ( 'title' , 'Homecoming' ) -> toString (),
],
2023-03-24 18:56:49 +13:00
]);
2021-09-11 08:14:31 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $documents [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 2017 , $documents [ 'body' ][ 'documents' ][ 0 ][ 'releaseYear' ]);
$this -> assertCount ( 1 , $documents [ 'body' ][ 'documents' ]);
2021-09-11 08:14:31 +12:00
2023-03-24 18:56:49 +13:00
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: search ( 'title' , 'spider' ) -> toString (),
],
2023-03-24 18:56:49 +13:00
]);
2021-08-25 06:02:33 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $documents [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 2019 , $documents [ 'body' ][ 'documents' ][ 0 ][ 'releaseYear' ]);
$this -> assertEquals ( 2017 , $documents [ 'body' ][ 'documents' ][ 1 ][ 'releaseYear' ]);
$this -> assertCount ( 2 , $documents [ 'body' ][ 'documents' ]);
2022-07-29 03:24:17 +12:00
2024-02-09 05:10:25 +13:00
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'queries' => [
'{"method":"contains","attribute":"title","values":[bad]}'
],
]);
$this -> assertEquals ( 400 , $documents [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'Invalid query: Syntax error' , $documents [ 'body' ][ 'message' ]);
2024-01-22 05:22:02 +13:00
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'queries' => [
Query :: contains ( 'title' , [ 'spi' ]) -> toString (), // like query
],
]);
$this -> assertEquals ( 200 , $documents [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 2 , $documents [ 'body' ][ 'total' ]);
2023-03-24 18:56:49 +13:00
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2021-10-08 07:30:52 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: equal ( 'releaseYear' , [ 1944 ]) -> toString (),
],
2021-10-08 07:30:52 +13:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertCount ( 1 , $documents [ 'body' ][ 'documents' ]);
$this -> assertEquals ( 'Captain America' , $documents [ 'body' ][ 'documents' ][ 0 ][ 'title' ]);
2021-08-25 06:02:33 +12:00
2023-03-24 18:56:49 +13:00
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2021-06-15 07:54:49 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: notEqual ( 'releaseYear' , 1944 ) -> toString (),
],
2021-06-15 07:54:49 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertCount ( 2 , $documents [ 'body' ][ 'documents' ]);
$this -> assertEquals ( 'Spider-Man: Far From Home' , $documents [ 'body' ][ 'documents' ][ 0 ][ 'title' ]);
$this -> assertEquals ( 'Spider-Man: Homecoming' , $documents [ 'body' ][ 'documents' ][ 1 ][ 'title' ]);
2021-08-23 04:36:26 +12:00
2023-03-24 18:56:49 +13:00
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2021-11-04 04:02:41 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: greaterThan ( '$createdAt' , '1976-06-12' ) -> toString (),
],
2021-11-04 04:02:41 +13:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertCount ( 3 , $documents [ 'body' ][ 'documents' ]);
2021-08-23 04:36:26 +12:00
2023-03-24 18:56:49 +13:00
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2022-06-28 22:53:16 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: lessThan ( '$createdAt' , '1976-06-12' ) -> toString (),
],
2022-06-28 22:53:16 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertCount ( 0 , $documents [ 'body' ][ 'documents' ]);
2021-08-23 04:36:26 +12:00
2023-03-24 18:56:49 +13:00
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2021-08-23 04:36:26 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
2024-01-22 05:22:02 +13:00
Query :: contains ( 'actors' , [ 'Tom Holland' , 'Samuel Jackson' ]) -> toString (),
],
]);
$this -> assertEquals ( 200 , $documents [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 3 , $documents [ 'body' ][ 'total' ]);
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'queries' => [
Query :: contains ( 'actors' , [ 'Tom' ]) -> toString (), // Full-match not like
2023-12-20 23:55:09 +13:00
],
2023-03-24 18:56:49 +13:00
]);
2024-01-22 05:22:02 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $documents [ 'headers' ][ 'status-code' ]);
2024-01-22 05:22:02 +13:00
$this -> assertEquals ( 0 , $documents [ 'body' ][ 'total' ]);
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'queries' => [
Query :: greaterThan ( 'birthDay' , '16/01/2024 12:00:00AM' ) -> toString (),
],
]);
$this -> assertEquals ( 400 , $documents [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'Invalid query: Query value is invalid for attribute "birthDay"' , $documents [ 'body' ][ 'message' ]);
2021-08-23 04:36:26 +12:00
2023-03-30 19:57:56 +13:00
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: greaterThan ( 'birthDay' , '1960-01-01 10:10:10+02:30' ) -> toString (),
],
2023-03-30 19:57:56 +13:00
]);
$this -> assertEquals ( 200 , $documents [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( '1975-06-12T12:12:55.000+00:00' , $documents [ 'body' ][ 'documents' ][ 0 ][ 'birthDay' ]);
$this -> assertEquals ( '1975-06-12T18:12:55.000+00:00' , $documents [ 'body' ][ 'documents' ][ 1 ][ 'birthDay' ]);
$this -> assertCount ( 2 , $documents [ 'body' ][ 'documents' ]);
2024-01-22 05:22:02 +13:00
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'queries' => [
Query :: isNull ( 'integers' ) -> toString (),
],
]);
$this -> assertEquals ( 200 , $documents [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 1 , $documents [ 'body' ][ 'total' ]);
2023-03-24 18:56:49 +13:00
/**
* Test for Failure
*/
$conditions = [];
2021-06-19 05:18:27 +12:00
2023-03-24 18:56:49 +13:00
for ( $i = 0 ; $i < 101 ; $i ++ ) {
$conditions [] = $i ;
}
2022-07-31 22:07:15 +12:00
2023-03-24 18:56:49 +13:00
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2022-07-31 22:07:15 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: equal ( 'releaseYear' , $conditions ) -> toString (),
],
2022-07-31 22:07:15 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 400 , $documents [ 'headers' ][ 'status-code' ]);
2023-12-20 23:55:09 +13:00
$this -> assertEquals ( 'Invalid query: Query on attribute has greater than 100 values: releaseYear' , $documents [ 'body' ][ 'message' ]);
2022-07-31 22:07:15 +12:00
2023-12-20 23:55:09 +13:00
$value = '' ;
2021-06-15 07:54:49 +12:00
2023-03-24 18:56:49 +13:00
for ( $i = 0 ; $i < 101 ; $i ++ ) {
2023-12-20 23:55:09 +13:00
$value .= " [ " . $i . " ] Too long title to cross 2k chars query limit " ;
2023-03-24 18:56:49 +13:00
}
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json' ,
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2020-01-13 21:46:09 +13:00
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: search ( 'title' , $value ) -> toString (),
],
2020-01-13 21:46:09 +13:00
]);
2024-01-22 05:22:02 +13:00
// Todo: Not sure what to do we with Query length Test VS old? JSON validator will fails if query string will be truncated?
2023-12-20 23:55:09 +13:00
//$this->assertEquals(400, $documents['headers']['status-code']);
2023-03-24 18:56:49 +13:00
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json' ,
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2020-01-13 21:46:09 +13:00
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: search ( 'actors' , 'Tom' ) -> toString (),
],
2020-01-13 21:46:09 +13:00
]);
2023-03-30 19:57:56 +13:00
$this -> assertEquals ( 400 , $documents [ 'headers' ][ 'status-code' ]);
2024-01-22 05:22:02 +13:00
$this -> assertEquals ( 'Invalid query: Cannot query search on attribute "actors" because it is an array.' , $documents [ 'body' ][ 'message' ]);
2023-03-24 18:56:49 +13:00
return [];
}
/**
* @ depends testCreateDocument
*/
public function testUpdateDocument ( array $data ) : array
{
$databaseId = $data [ 'databaseId' ];
$document = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json' ,
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2020-01-13 21:46:09 +13:00
], $this -> getHeaders ()), [
2022-08-14 22:33:36 +12:00
'documentId' => ID :: unique (),
2020-01-13 21:46:09 +13:00
'data' => [
2023-03-24 18:56:49 +13:00
'title' => 'Thor: Ragnaroc' ,
2020-01-13 21:46:09 +13:00
'releaseYear' => 2017 ,
2023-03-24 18:56:49 +13:00
'birthDay' => '1976-06-12 14:12:55' ,
'actors' => [],
'$createdAt' => 5 // Should be ignored
2020-01-14 06:53:22 +13:00
],
2022-08-03 16:17:49 +12:00
'permissions' => [
2022-08-15 23:24:31 +12:00
Permission :: read ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: update ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: delete ( Role :: user ( $this -> getUser ()[ '$id' ])),
2020-01-14 06:53:22 +13:00
],
2020-01-13 21:46:09 +13:00
]);
2023-03-24 18:56:49 +13:00
$id = $document [ 'body' ][ '$id' ];
2020-01-13 21:46:09 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 201 , $document [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $data [ 'moviesId' ], $document [ 'body' ][ '$collectionId' ]);
$this -> assertArrayNotHasKey ( '$collection' , $document [ 'body' ]);
$this -> assertEquals ( $databaseId , $document [ 'body' ][ '$databaseId' ]);
$this -> assertEquals ( $document [ 'body' ][ 'title' ], 'Thor: Ragnaroc' );
$this -> assertEquals ( $document [ 'body' ][ 'releaseYear' ], 2017 );
$dateValidator = new DatetimeValidator ();
$this -> assertEquals ( true , $dateValidator -> isValid ( $document [ 'body' ][ '$createdAt' ]));
$this -> assertEquals ( true , $dateValidator -> isValid ( $document [ 'body' ][ 'birthDay' ]));
$this -> assertContains ( Permission :: read ( Role :: user ( $this -> getUser ()[ '$id' ])), $document [ 'body' ][ '$permissions' ]);
$this -> assertContains ( Permission :: update ( Role :: user ( $this -> getUser ()[ '$id' ])), $document [ 'body' ][ '$permissions' ]);
$this -> assertContains ( Permission :: delete ( Role :: user ( $this -> getUser ()[ '$id' ])), $document [ 'body' ][ '$permissions' ]);
2020-01-13 21:46:09 +13:00
2023-03-24 18:56:49 +13:00
$document = $this -> client -> call ( Client :: METHOD_PATCH , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents/' . $id , array_merge ([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json' ,
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2020-01-13 21:46:09 +13:00
], $this -> getHeaders ()), [
2023-03-24 18:56:49 +13:00
'data' => [
'title' => 'Thor: Ragnarok' ,
],
'permissions' => [
Permission :: read ( Role :: users ()),
Permission :: update ( Role :: users ()),
Permission :: delete ( Role :: users ()),
],
2020-01-13 21:46:09 +13:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $document [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $document [ 'body' ][ '$id' ], $id );
$this -> assertEquals ( $data [ 'moviesId' ], $document [ 'body' ][ '$collectionId' ]);
$this -> assertArrayNotHasKey ( '$collection' , $document [ 'body' ]);
$this -> assertEquals ( $databaseId , $document [ 'body' ][ '$databaseId' ]);
$this -> assertEquals ( $document [ 'body' ][ 'title' ], 'Thor: Ragnarok' );
$this -> assertEquals ( $document [ 'body' ][ 'releaseYear' ], 2017 );
$this -> assertContains ( Permission :: read ( Role :: users ()), $document [ 'body' ][ '$permissions' ]);
$this -> assertContains ( Permission :: update ( Role :: users ()), $document [ 'body' ][ '$permissions' ]);
$this -> assertContains ( Permission :: delete ( Role :: users ()), $document [ 'body' ][ '$permissions' ]);
2021-12-27 23:45:24 +13:00
2023-03-24 18:56:49 +13:00
$document = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents/' . $id , array_merge ([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json' ,
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()));
2020-01-13 21:46:09 +13:00
2023-03-24 18:56:49 +13:00
$id = $document [ 'body' ][ '$id' ];
2020-01-13 21:46:09 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $document [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $data [ 'moviesId' ], $document [ 'body' ][ '$collectionId' ]);
$this -> assertArrayNotHasKey ( '$collection' , $document [ 'body' ]);
$this -> assertEquals ( $databaseId , $document [ 'body' ][ '$databaseId' ]);
$this -> assertEquals ( $document [ 'body' ][ 'title' ], 'Thor: Ragnarok' );
$this -> assertEquals ( $document [ 'body' ][ 'releaseYear' ], 2017 );
2021-12-27 23:45:24 +13:00
2023-03-24 18:56:49 +13:00
$response = $this -> client -> call ( Client :: METHOD_PATCH , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents/' . $id , array_merge ([
2022-07-06 11:13:48 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-timestamp' => DateTime :: formatTz ( DateTime :: now ()),
], $this -> getHeaders ()), [
'data' => [
'title' => 'Thor: Ragnarok' ,
],
2022-07-06 11:13:48 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $response [ 'headers' ][ 'status-code' ]);
2022-07-06 11:13:48 +12:00
/**
2023-03-24 18:56:49 +13:00
* Test for failure
2022-07-06 11:13:48 +12:00
*/
2023-03-24 18:56:49 +13:00
$response = $this -> client -> call ( Client :: METHOD_PATCH , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents/' . $id , array_merge ([
2022-07-06 11:13:48 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-timestamp' => 'invalid' ,
], $this -> getHeaders ()), [
'data' => [
'title' => 'Thor: Ragnarok' ,
],
2022-07-06 11:13:48 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 400 , $response [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'Invalid X-Appwrite-Timestamp header value' , $response [ 'body' ][ 'message' ]);
$this -> assertEquals ( Exception :: GENERAL_ARGUMENT_INVALID , $response [ 'body' ][ 'type' ]);
2022-07-06 11:13:48 +12:00
2023-03-24 18:56:49 +13:00
$response = $this -> client -> call ( Client :: METHOD_PATCH , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents/' . $id , array_merge ([
2022-07-06 11:13:48 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-timestamp' => DateTime :: formatTz ( DateTime :: addSeconds ( new \DateTime (), - 1000 )),
], $this -> getHeaders ()), [
'data' => [
'title' => 'Thor: Ragnarok' ,
],
]);
2022-07-06 11:13:48 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 409 , $response [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'Remote document is newer than local.' , $response [ 'body' ][ 'message' ]);
$this -> assertEquals ( Exception :: DOCUMENT_UPDATE_CONFLICT , $response [ 'body' ][ 'type' ]);
2022-07-06 11:13:48 +12:00
return [];
}
2021-12-27 23:45:24 +13:00
/**
2023-03-24 18:56:49 +13:00
* @ depends testCreateDocument
2023-03-23 10:10:47 +13:00
*/
2023-03-24 18:56:49 +13:00
public function testDeleteDocument ( array $data ) : array
2023-03-23 10:10:47 +13:00
{
$databaseId = $data [ 'databaseId' ];
2023-03-24 18:56:49 +13:00
$document = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2023-03-24 03:08:38 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-03-24 18:56:49 +13:00
'documentId' => ID :: unique (),
'data' => [
'title' => 'Thor: Ragnarok' ,
'releaseYear' => 2017 ,
'birthDay' => '1975-06-12 14:12:55' ,
'actors' => [],
2023-03-24 03:08:38 +13:00
],
2023-03-24 18:56:49 +13:00
'permissions' => [
Permission :: read ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: update ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: delete ( Role :: user ( $this -> getUser ()[ '$id' ])),
]
2023-03-24 03:08:38 +13:00
]);
2023-03-23 10:10:47 +13:00
2023-03-24 18:56:49 +13:00
$id = $document [ 'body' ][ '$id' ];
2020-01-13 21:46:09 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 201 , $document [ 'headers' ][ 'status-code' ]);
$document = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents/' . $id , array_merge ([
2021-08-06 07:01:00 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $document [ 'headers' ][ 'status-code' ]);
2021-08-06 07:01:00 +12:00
2023-03-24 18:56:49 +13:00
$document = $this -> client -> call ( Client :: METHOD_DELETE , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents/' . $id , array_merge ([
2021-08-06 07:01:00 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()));
$this -> assertEquals ( 204 , $document [ 'headers' ][ 'status-code' ]);
$document = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents/' . $id , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( 404 , $document [ 'headers' ][ 'status-code' ]);
return $data ;
}
public function testInvalidDocumentStructure ()
{
$database = $this -> client -> call ( Client :: METHOD_POST , '/databases' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'databaseId' => ID :: unique (),
'name' => 'InvalidDocumentDatabase' ,
]);
$this -> assertEquals ( 201 , $database [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'InvalidDocumentDatabase' , $database [ 'body' ][ 'name' ]);
$databaseId = $database [ 'body' ][ '$id' ];
$collection = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'collectionId' => ID :: unique (),
'name' => 'invalidDocumentStructure' ,
'permissions' => [
Permission :: create ( Role :: any ()),
Permission :: read ( Role :: any ()),
],
'documentSecurity' => true ,
2021-08-06 07:01:00 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 201 , $collection [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'invalidDocumentStructure' , $collection [ 'body' ][ 'name' ]);
$collectionId = $collection [ 'body' ][ '$id' ];
2021-08-06 07:01:00 +12:00
2023-03-24 18:56:49 +13:00
$email = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/email' , array_merge ([
2021-08-06 07:01:00 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'email' ,
'required' => false ,
2021-08-06 07:01:00 +12:00
]);
2023-03-24 18:56:49 +13:00
$enum = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/enum' , array_merge ([
2021-08-06 07:01:00 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'enum' ,
'elements' => [ 'yes' , 'no' , 'maybe' ],
'required' => false ,
2021-08-06 07:01:00 +12:00
]);
2023-03-24 18:56:49 +13:00
$ip = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/ip' , array_merge ([
2021-08-06 07:01:00 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'ip' ,
'required' => false ,
2021-08-06 07:01:00 +12:00
]);
2023-03-24 18:56:49 +13:00
$url = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/url' , array_merge ([
2021-08-06 07:01:00 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'url' ,
'size' => 256 ,
'required' => false ,
2021-08-06 07:01:00 +12:00
]);
2023-03-24 18:56:49 +13:00
$range = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/integer' , array_merge ([
2021-08-06 07:01:00 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'range' ,
'required' => false ,
'min' => 1 ,
'max' => 10 ,
2021-08-06 07:01:00 +12:00
]);
2023-03-24 18:56:49 +13:00
// TODO@kodumbeats min and max are rounded in error message
$floatRange = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/float' , array_merge ([
2021-08-06 07:01:00 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'floatRange' ,
'required' => false ,
'min' => 1.1 ,
'max' => 1.4 ,
2021-08-06 07:01:00 +12:00
]);
2023-03-24 18:56:49 +13:00
$probability = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/float' , array_merge ([
2021-10-05 23:30:33 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'probability' ,
'required' => false ,
'default' => 0 ,
'min' => 0 ,
'max' => 1 ,
]);
2021-10-05 23:30:33 +13:00
2023-03-24 18:56:49 +13:00
$upperBound = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/integer' , array_merge ([
2021-10-05 23:30:33 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'upperBound' ,
'required' => false ,
'max' => 10 ,
2021-10-05 23:30:33 +13:00
]);
2023-03-24 18:56:49 +13:00
$lowerBound = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/integer' , array_merge ([
2021-10-05 23:30:33 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'lowerBound' ,
'required' => false ,
'min' => 5 ,
2021-10-05 23:30:33 +13:00
]);
/**
2023-03-24 18:56:49 +13:00
* Test for failure
2021-10-05 23:30:33 +13:00
*/
2023-03-24 18:56:49 +13:00
$invalidRange = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/integer' , array_merge ([
'content-type' => 'application/json' , 'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'invalidRange' ,
'required' => false ,
'min' => 4 ,
'max' => 3 ,
2021-10-05 23:30:33 +13:00
]);
2023-03-24 18:56:49 +13:00
$defaultArray = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/integer' , array_merge ([
'content-type' => 'application/json' , 'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'defaultArray' ,
'required' => false ,
'default' => 42 ,
'array' => true ,
]);
2021-10-05 23:30:33 +13:00
2023-03-24 18:56:49 +13:00
$defaultRequired = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/integer' , array_merge ([
2021-10-05 23:30:33 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'attributeId' => ID :: custom ( 'defaultRequired' ),
'required' => true ,
'default' => 12
2021-10-05 23:30:33 +13:00
]);
2023-03-24 18:56:49 +13:00
$enumDefault = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/enum' , array_merge ([
2021-10-05 23:30:33 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'attributeId' => ID :: custom ( 'enumDefault' ),
'elements' => [ 'north' , 'west' ],
'default' => 'south'
2021-10-05 23:30:33 +13:00
]);
2023-03-24 18:56:49 +13:00
$enumDefaultStrict = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/enum' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'attributeId' => ID :: custom ( 'enumDefault' ),
'elements' => [ 'north' , 'west' ],
'default' => 'NORTH'
]);
2021-10-05 23:30:33 +13:00
2023-03-24 18:56:49 +13:00
$goodDatetime = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/datetime' , array_merge ([
2021-10-05 23:30:33 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'birthDay' ,
'required' => false ,
'default' => null
2021-10-05 23:30:33 +13:00
]);
2023-03-24 18:56:49 +13:00
$datetimeDefault = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/datetime' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'badBirthDay' ,
'required' => false ,
'default' => 'bad'
]);
2021-10-05 23:30:33 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 202 , $email [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 202 , $ip [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 202 , $url [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 202 , $range [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 202 , $floatRange [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 202 , $probability [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 202 , $upperBound [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 202 , $lowerBound [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 202 , $enum [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 202 , $goodDatetime [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 400 , $invalidRange [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 400 , $defaultArray [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 400 , $defaultRequired [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 400 , $enumDefault [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 400 , $enumDefaultStrict [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'Minimum value must be lesser than maximum value' , $invalidRange [ 'body' ][ 'message' ]);
$this -> assertEquals ( 'Cannot set default value for array attributes' , $defaultArray [ 'body' ][ 'message' ]);
$this -> assertEquals ( 400 , $datetimeDefault [ 'headers' ][ 'status-code' ]);
// wait for worker to add attributes
sleep ( 3 );
2021-10-05 23:30:33 +13:00
2023-03-24 18:56:49 +13:00
$collection = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $collectionId , array_merge ([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json' ,
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
]), []);
2020-01-13 21:46:09 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertCount ( 10 , $collection [ 'body' ][ 'attributes' ]);
2020-01-13 21:46:09 +13:00
2023-03-24 18:56:49 +13:00
/**
* Test for successful validation
*/
$goodEmail = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , array_merge ([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json' ,
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2020-01-13 21:46:09 +13:00
], $this -> getHeaders ()), [
2023-03-24 18:56:49 +13:00
'documentId' => ID :: unique (),
'data' => [
'email' => 'user@example.com' ,
],
'permissions' => [
Permission :: read ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: update ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: delete ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
]
2020-01-13 21:46:09 +13:00
]);
2023-03-24 18:56:49 +13:00
$goodEnum = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , array_merge ([
2021-11-04 04:02:41 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-03-24 18:56:49 +13:00
'documentId' => ID :: unique (),
'data' => [
'enum' => 'yes' ,
],
'permissions' => [
Permission :: read ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: update ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: delete ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
]
2021-11-04 04:02:41 +13:00
]);
2020-01-13 21:46:09 +13:00
2023-03-24 18:56:49 +13:00
$goodIp = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , array_merge ([
2021-12-30 03:06:44 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-03-24 18:56:49 +13:00
'documentId' => ID :: unique (),
'data' => [
'ip' => '1.1.1.1' ,
],
'permissions' => [
Permission :: read ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: update ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: delete ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
]
2021-12-30 03:06:44 +13:00
]);
2023-03-24 18:56:49 +13:00
$goodUrl = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , array_merge ([
2021-11-04 04:02:41 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-03-24 18:56:49 +13:00
'documentId' => ID :: unique (),
'data' => [
'url' => 'http://www.example.com' ,
],
'permissions' => [
Permission :: read ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: update ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: delete ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
]
2021-11-04 04:02:41 +13:00
]);
2023-03-24 18:56:49 +13:00
$goodRange = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , array_merge ([
2021-11-04 04:02:41 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-03-24 18:56:49 +13:00
'documentId' => ID :: unique (),
'data' => [
'range' => 3 ,
],
'permissions' => [
Permission :: read ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: update ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: delete ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
]
2021-11-04 04:02:41 +13:00
]);
2023-03-24 18:56:49 +13:00
$goodFloatRange = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , array_merge ([
2021-11-04 04:02:41 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-03-24 18:56:49 +13:00
'documentId' => ID :: unique (),
'data' => [
'floatRange' => 1.4 ,
],
'permissions' => [
Permission :: read ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: update ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: delete ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
]
2021-11-04 04:02:41 +13:00
]);
2023-03-24 18:56:49 +13:00
$goodProbability = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , array_merge ([
2021-11-04 04:02:41 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-03-24 18:56:49 +13:00
'documentId' => ID :: unique (),
'data' => [
'probability' => 0.99999 ,
],
'permissions' => [
Permission :: read ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: update ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: delete ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
]
2021-11-04 04:02:41 +13:00
]);
2023-03-24 18:56:49 +13:00
$notTooHigh = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , array_merge ([
2022-06-28 08:08:23 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-03-24 18:56:49 +13:00
'documentId' => ID :: unique (),
'data' => [
'upperBound' => 8 ,
],
'permissions' => [
Permission :: read ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: update ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: delete ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
]
2022-06-28 08:08:23 +12:00
]);
2023-03-24 18:56:49 +13:00
$notTooLow = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , array_merge ([
2022-06-28 08:08:23 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-03-24 18:56:49 +13:00
'documentId' => ID :: unique (),
'data' => [
'lowerBound' => 8 ,
],
'permissions' => [
Permission :: read ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: update ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: delete ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
]
2022-06-28 08:08:23 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 201 , $goodEmail [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 201 , $goodEnum [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 201 , $goodIp [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 201 , $goodUrl [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 201 , $goodRange [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 201 , $goodFloatRange [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 201 , $goodProbability [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 201 , $notTooHigh [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 201 , $notTooLow [ 'headers' ][ 'status-code' ]);
2022-06-28 08:08:23 +12:00
2023-03-24 18:56:49 +13:00
/*
* Test that custom validators reject documents
*/
$badEmail = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , array_merge ([
2023-02-28 16:52:11 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-03-24 18:56:49 +13:00
'documentId' => ID :: unique (),
'data' => [
'email' => 'user@@example.com' ,
],
'permissions' => [
Permission :: read ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: update ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: delete ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
]
2023-02-28 16:52:11 +13:00
]);
2021-12-29 07:16:22 +13:00
2023-03-24 18:56:49 +13:00
$badEnum = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , array_merge ([
2021-12-29 07:16:22 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-03-24 18:56:49 +13:00
'documentId' => ID :: unique (),
'data' => [
'enum' => 'badEnum' ,
],
'permissions' => [
Permission :: read ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: update ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: delete ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
]
2021-12-29 07:16:22 +13:00
]);
2023-03-24 18:56:49 +13:00
$badIp = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , array_merge ([
2022-06-22 19:47:02 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-03-24 18:56:49 +13:00
'documentId' => ID :: unique (),
'data' => [
'ip' => '1.1.1.1.1' ,
],
'permissions' => [
Permission :: read ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: update ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: delete ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
]
2022-06-22 19:47:02 +12:00
]);
2023-03-24 18:56:49 +13:00
$badUrl = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , array_merge ([
2022-07-29 03:24:17 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-03-24 18:56:49 +13:00
'documentId' => ID :: unique (),
'data' => [
'url' => 'example...com' ,
],
'permissions' => [
Permission :: read ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: update ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: delete ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
]
2022-07-29 03:24:17 +12:00
]);
2022-07-31 22:07:15 +12:00
2023-03-24 18:56:49 +13:00
$badRange = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , array_merge ([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json' ,
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2020-01-13 21:46:09 +13:00
], $this -> getHeaders ()), [
2022-08-14 22:33:36 +12:00
'documentId' => ID :: unique (),
2020-01-13 21:46:09 +13:00
'data' => [
2023-03-24 18:56:49 +13:00
'range' => 11 ,
2020-01-14 06:53:22 +13:00
],
2022-08-02 21:18:49 +12:00
'permissions' => [
2023-03-24 18:56:49 +13:00
Permission :: read ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: update ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: delete ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
]
2020-01-13 21:46:09 +13:00
]);
2023-03-24 18:56:49 +13:00
$badFloatRange = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , array_merge ([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json' ,
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2020-01-13 21:46:09 +13:00
], $this -> getHeaders ()), [
2023-03-24 18:56:49 +13:00
'documentId' => ID :: unique (),
2020-01-13 21:46:09 +13:00
'data' => [
2023-03-24 18:56:49 +13:00
'floatRange' => 2.5 ,
2021-03-16 00:44:11 +13:00
],
2022-08-02 21:18:49 +12:00
'permissions' => [
2023-03-24 18:56:49 +13:00
Permission :: read ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: update ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: delete ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
]
2020-01-13 21:46:09 +13:00
]);
2023-03-24 18:56:49 +13:00
$badProbability = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , array_merge ([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json' ,
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
'documentId' => ID :: unique (),
'data' => [
'probability' => 1.1 ,
],
'permissions' => [
Permission :: read ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: update ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: delete ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
]
]);
2020-01-13 21:46:09 +13:00
2023-03-24 18:56:49 +13:00
$tooHigh = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , array_merge ([
2023-01-20 13:36:17 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-03-24 18:56:49 +13:00
'documentId' => ID :: unique (),
2023-01-20 13:36:17 +13:00
'data' => [
2023-03-24 18:56:49 +13:00
'upperBound' => 11 ,
2023-01-20 13:36:17 +13:00
],
2023-03-24 18:56:49 +13:00
'permissions' => [
Permission :: read ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: update ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: delete ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
]
2023-01-20 13:36:17 +13:00
]);
2023-03-24 18:56:49 +13:00
$tooLow = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , array_merge ([
2023-01-20 13:36:17 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-03-24 18:56:49 +13:00
'documentId' => ID :: unique (),
2023-01-20 13:36:17 +13:00
'data' => [
2023-03-24 18:56:49 +13:00
'lowerBound' => 3 ,
2023-01-20 13:36:17 +13:00
],
2023-03-24 18:56:49 +13:00
'permissions' => [
Permission :: read ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: update ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: delete ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
]
2023-01-20 13:36:17 +13:00
]);
2023-03-24 18:56:49 +13:00
$badTime = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , array_merge ([
2023-01-20 13:36:17 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-03-24 18:56:49 +13:00
'documentId' => 'unique()' ,
2023-01-20 13:36:17 +13:00
'data' => [
2023-03-24 18:56:49 +13:00
'birthDay' => '2020-10-10 27:30:10+01:00' ,
2023-01-20 13:36:17 +13:00
],
2023-03-24 18:56:49 +13:00
'read' => [ 'user:' . $this -> getUser ()[ '$id' ]],
'write' => [ 'user:' . $this -> getUser ()[ '$id' ]],
2023-01-20 13:36:17 +13:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 400 , $badEmail [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 400 , $badEnum [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 400 , $badIp [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 400 , $badUrl [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 400 , $badRange [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 400 , $badFloatRange [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 400 , $badProbability [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 400 , $tooHigh [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 400 , $tooLow [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 400 , $badTime [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'Invalid document structure: Attribute "email" has invalid format. Value must be a valid email address' , $badEmail [ 'body' ][ 'message' ]);
$this -> assertEquals ( 'Invalid document structure: Attribute "enum" has invalid format. Value must be one of (yes, no, maybe)' , $badEnum [ 'body' ][ 'message' ]);
$this -> assertEquals ( 'Invalid document structure: Attribute "ip" has invalid format. Value must be a valid IP address' , $badIp [ 'body' ][ 'message' ]);
$this -> assertEquals ( 'Invalid document structure: Attribute "url" has invalid format. Value must be a valid URL' , $badUrl [ 'body' ][ 'message' ]);
$this -> assertEquals ( 'Invalid document structure: Attribute "range" has invalid format. Value must be a valid range between 1 and 10' , $badRange [ 'body' ][ 'message' ]);
$this -> assertEquals ( 'Invalid document structure: Attribute "floatRange" has invalid format. Value must be a valid range between 1 and 1' , $badFloatRange [ 'body' ][ 'message' ]);
$this -> assertEquals ( 'Invalid document structure: Attribute "probability" has invalid format. Value must be a valid range between 0 and 1' , $badProbability [ 'body' ][ 'message' ]);
$this -> assertEquals ( 'Invalid document structure: Attribute "upperBound" has invalid format. Value must be a valid range between -9,223,372,036,854,775,808 and 10' , $tooHigh [ 'body' ][ 'message' ]);
2024-02-24 01:55:00 +13:00
$this -> assertEquals ( 'Invalid document structure: Attribute "lowerBound" has invalid format. Value must be a valid range between 5 and 9,223,372,036,854,775,807' , $tooLow [ 'body' ][ 'message' ]);
2020-01-13 21:46:09 +13:00
}
/**
2023-03-24 18:56:49 +13:00
* @ depends testDeleteDocument
2020-01-13 21:46:09 +13:00
*/
2023-03-24 18:56:49 +13:00
public function testDefaultPermissions ( array $data ) : array
2020-01-13 21:46:09 +13:00
{
2022-06-22 22:51:49 +12:00
$databaseId = $data [ 'databaseId' ];
$document = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json' ,
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2020-01-13 21:46:09 +13:00
], $this -> getHeaders ()), [
2022-08-14 22:33:36 +12:00
'documentId' => ID :: unique (),
2020-01-13 21:46:09 +13:00
'data' => [
2023-03-24 18:56:49 +13:00
'title' => 'Captain America' ,
'releaseYear' => 1944 ,
2021-06-16 01:38:24 +12:00
'actors' => [],
2020-01-14 06:53:22 +13:00
],
2020-01-13 21:46:09 +13:00
]);
2020-02-17 20:16:11 +13:00
$id = $document [ 'body' ][ '$id' ];
2020-01-13 21:46:09 +13:00
2022-08-08 23:00:03 +12:00
$this -> assertEquals ( 201 , $document [ 'headers' ][ 'status-code' ]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( $document [ 'body' ][ 'title' ], 'Captain America' );
$this -> assertEquals ( $document [ 'body' ][ 'releaseYear' ], 1944 );
$this -> assertIsArray ( $document [ 'body' ][ '$permissions' ]);
if ( $this -> getSide () == 'client' ) {
$this -> assertCount ( 3 , $document [ 'body' ][ '$permissions' ]);
$this -> assertContains ( Permission :: read ( Role :: user ( $this -> getUser ()[ '$id' ])), $document [ 'body' ][ '$permissions' ]);
$this -> assertContains ( Permission :: update ( Role :: user ( $this -> getUser ()[ '$id' ])), $document [ 'body' ][ '$permissions' ]);
$this -> assertContains ( Permission :: delete ( Role :: user ( $this -> getUser ()[ '$id' ])), $document [ 'body' ][ '$permissions' ]);
}
if ( $this -> getSide () == 'server' ) {
$this -> assertCount ( 0 , $document [ 'body' ][ '$permissions' ]);
$this -> assertEquals ([], $document [ 'body' ][ '$permissions' ]);
}
// Updated Permissions
$document = $this -> client -> call ( Client :: METHOD_PATCH , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents/' . $id , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'data' => [
'title' => 'Captain America 2' ,
'releaseYear' => 1945 ,
'actors' => [],
],
'permissions' => [
Permission :: read ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: update ( Role :: user ( $this -> getUser ()[ '$id' ]))
],
]);
$this -> assertEquals ( 200 , $document [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $document [ 'body' ][ 'title' ], 'Captain America 2' );
$this -> assertEquals ( $document [ 'body' ][ 'releaseYear' ], 1945 );
// This differs from the old permissions model because we don't inherit
// existing document permissions on update, unless none were supplied,
// so that specific types can be removed if wanted.
$this -> assertCount ( 2 , $document [ 'body' ][ '$permissions' ]);
$this -> assertEquals ([
Permission :: read ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: update ( Role :: user ( $this -> getUser ()[ '$id' ])),
], $document [ 'body' ][ '$permissions' ]);
2020-01-13 21:46:09 +13:00
2022-06-22 22:51:49 +12:00
$document = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents/' . $id , array_merge ([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json' ,
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2020-01-13 21:46:09 +13:00
], $this -> getHeaders ()));
2022-08-08 23:00:03 +12:00
$this -> assertEquals ( 200 , $document [ 'headers' ][ 'status-code' ]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( $document [ 'body' ][ 'title' ], 'Captain America 2' );
$this -> assertEquals ( $document [ 'body' ][ 'releaseYear' ], 1945 );
2020-01-13 21:46:09 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertCount ( 2 , $document [ 'body' ][ '$permissions' ]);
$this -> assertEquals ([
Permission :: read ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: update ( Role :: user ( $this -> getUser ()[ '$id' ])),
], $document [ 'body' ][ '$permissions' ]);
// Reset Permissions
$document = $this -> client -> call ( Client :: METHOD_PATCH , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents/' . $id , array_merge ([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json' ,
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
'data' => [
'title' => 'Captain America 3' ,
'releaseYear' => 1946 ,
'actors' => [],
],
'permissions' => [],
]);
2020-01-13 21:46:09 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $document [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $document [ 'body' ][ 'title' ], 'Captain America 3' );
$this -> assertEquals ( $document [ 'body' ][ 'releaseYear' ], 1946 );
$this -> assertCount ( 0 , $document [ 'body' ][ '$permissions' ]);
$this -> assertEquals ([], $document [ 'body' ][ '$permissions' ]);
2020-01-13 21:46:09 +13:00
2023-03-24 18:56:49 +13:00
// Check client side can no longer read the document.
2022-06-22 22:51:49 +12:00
$document = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents/' . $id , array_merge ([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json' ,
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2020-01-13 21:46:09 +13:00
], $this -> getHeaders ()));
2023-03-24 18:56:49 +13:00
switch ( $this -> getSide ()) {
case 'client' :
$this -> assertEquals ( 404 , $document [ 'headers' ][ 'status-code' ]);
break ;
case 'server' :
$this -> assertEquals ( 200 , $document [ 'headers' ][ 'status-code' ]);
break ;
}
2021-12-27 23:45:24 +13:00
2021-03-22 20:34:51 +13:00
return $data ;
}
2023-03-24 18:56:49 +13:00
public function testEnforceCollectionAndDocumentPermissions () : void
2021-07-23 08:39:52 +12:00
{
2022-06-22 22:51:49 +12:00
$database = $this -> client -> call ( Client :: METHOD_POST , '/databases' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2022-08-14 22:33:36 +12:00
'databaseId' => ID :: unique (),
2023-03-24 18:56:49 +13:00
'name' => 'EnforceCollectionAndDocumentPermissions' ,
2022-06-22 22:51:49 +12:00
]);
$this -> assertEquals ( 201 , $database [ 'headers' ][ 'status-code' ]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 'EnforceCollectionAndDocumentPermissions' , $database [ 'body' ][ 'name' ]);
2022-06-22 22:51:49 +12:00
$databaseId = $database [ 'body' ][ '$id' ];
2023-03-24 18:56:49 +13:00
$user = $this -> getUser ()[ '$id' ];
2022-06-22 22:51:49 +12:00
$collection = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections' , array_merge ([
2021-07-23 08:39:52 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2022-08-14 22:33:36 +12:00
'collectionId' => ID :: unique (),
2023-03-24 18:56:49 +13:00
'name' => 'enforceCollectionAndDocumentPermissions' ,
'documentSecurity' => true ,
2022-08-14 02:10:28 +12:00
'permissions' => [
2023-03-24 18:56:49 +13:00
Permission :: read ( Role :: user ( $user )),
Permission :: create ( Role :: user ( $user )),
Permission :: update ( Role :: user ( $user )),
Permission :: delete ( Role :: user ( $user )),
2022-08-14 02:10:28 +12:00
],
2021-07-23 08:39:52 +12:00
]);
2021-07-24 03:04:31 +12:00
$this -> assertEquals ( 201 , $collection [ 'headers' ][ 'status-code' ]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( $collection [ 'body' ][ 'name' ], 'enforceCollectionAndDocumentPermissions' );
$this -> assertEquals ( $collection [ 'body' ][ 'documentSecurity' ], true );
2021-07-23 08:39:52 +12:00
$collectionId = $collection [ 'body' ][ '$id' ];
2023-03-24 18:56:49 +13:00
sleep ( 2 );
2021-07-23 08:39:52 +12:00
2023-03-24 18:56:49 +13:00
$attribute = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/string' , array_merge ([
2021-09-11 08:25:16 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2023-03-24 18:56:49 +13:00
'key' => 'attribute' ,
'size' => 64 ,
'required' => true ,
2021-09-11 08:25:16 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 202 , $attribute [ 'headers' ][ 'status-code' ], 202 );
$this -> assertEquals ( 'attribute' , $attribute [ 'body' ][ 'key' ]);
2021-07-23 08:39:52 +12:00
2023-03-24 18:56:49 +13:00
// wait for db to add attribute
sleep ( 2 );
2021-07-23 08:39:52 +12:00
2023-03-24 18:56:49 +13:00
$index = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/indexes' , array_merge ([
2021-07-27 13:00:36 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2021-07-24 09:38:31 +12:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2023-03-24 18:56:49 +13:00
'key' => 'key_attribute' ,
'type' => 'key' ,
'attributes' => [ $attribute [ 'body' ][ 'key' ]],
2021-07-24 09:38:31 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 202 , $index [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'key_attribute' , $index [ 'body' ][ 'key' ]);
2021-07-24 09:38:31 +12:00
2023-03-24 18:56:49 +13:00
// wait for db to add attribute
sleep ( 2 );
2021-07-28 07:12:24 +12:00
2023-03-24 18:56:49 +13:00
$document1 = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , array_merge ([
2021-07-27 13:00:36 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
'documentId' => ID :: unique (),
'data' => [
'attribute' => 'one' ,
],
'permissions' => [
Permission :: read ( Role :: user ( $user )),
Permission :: update ( Role :: user ( $user )),
Permission :: delete ( Role :: user ( $user )),
]
2021-07-24 09:38:31 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 201 , $document1 [ 'headers' ][ 'status-code' ]);
$document2 = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , array_merge ([
2021-07-27 13:00:36 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
'documentId' => ID :: unique (),
'data' => [
'attribute' => 'one' ,
],
'permissions' => [
Permission :: update ( Role :: user ( $user )),
Permission :: delete ( Role :: user ( $user )),
]
2021-07-24 09:38:31 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 201 , $document2 [ 'headers' ][ 'status-code' ]);
2021-07-24 09:38:31 +12:00
2023-03-24 18:56:49 +13:00
$document3 = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , [
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2021-10-26 09:10:39 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
2023-03-24 18:56:49 +13:00
], [
'documentId' => ID :: unique (),
'data' => [
'attribute' => 'one' ,
],
'permissions' => [
Permission :: read ( Role :: user ( ID :: custom ( 'other' ))),
Permission :: update ( Role :: user ( ID :: custom ( 'other' ))),
],
2021-10-26 09:10:39 +13:00
]);
2021-07-24 09:38:31 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 201 , $document3 [ 'headers' ][ 'status-code' ]);
2021-10-27 09:14:25 +13:00
2023-03-24 18:56:49 +13:00
$documentsUser1 = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , array_merge ([
2021-12-16 23:15:55 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()));
2021-12-16 23:15:55 +13:00
2023-03-24 18:56:49 +13:00
// Current user has read permission on the collection so can get any document
$this -> assertEquals ( 3 , $documentsUser1 [ 'body' ][ 'total' ]);
$this -> assertCount ( 3 , $documentsUser1 [ 'body' ][ 'documents' ]);
$document3GetWithCollectionRead = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents/' . $document3 [ 'body' ][ '$id' ], array_merge ([
2021-12-16 23:15:55 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()));
2021-12-16 23:15:55 +13:00
2023-03-24 18:56:49 +13:00
// Current user has read permission on the collection so can get any document
$this -> assertEquals ( 200 , $document3GetWithCollectionRead [ 'headers' ][ 'status-code' ]);
$email = uniqid () . 'user@localhost.test' ;
$password = 'password' ;
$name = 'User Name' ;
$this -> client -> call ( Client :: METHOD_POST , '/account' , [
'origin' => 'http://localhost' ,
2021-12-17 02:06:32 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], [
'userId' => ID :: custom ( 'other' ),
'email' => $email ,
'password' => $password ,
'name' => $name ,
2021-12-17 02:06:32 +13:00
]);
2023-03-24 18:56:49 +13:00
$session2 = $this -> client -> call ( Client :: METHOD_POST , '/account/sessions/email' , [
'origin' => 'http://localhost' ,
2022-08-01 00:32:41 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], [
'email' => $email ,
'password' => $password ,
2022-08-01 00:32:41 +12:00
]);
2023-12-09 12:27:35 +13:00
$session2 = $session2 [ 'cookies' ][ 'a_session_' . $this -> getProject ()[ '$id' ]];
2022-08-01 00:32:41 +12:00
2023-03-24 18:56:49 +13:00
$document3GetWithDocumentRead = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents/' . $document3 [ 'body' ][ '$id' ], [
'origin' => 'http://localhost' ,
2022-08-01 00:32:41 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'cookie' => 'a_session_' . $this -> getProject ()[ '$id' ] . '=' . $session2 ,
2022-08-01 00:32:41 +12:00
]);
2023-03-24 18:56:49 +13:00
// Current user has no collection permissions but has read permission for this document
$this -> assertEquals ( 200 , $document3GetWithDocumentRead [ 'headers' ][ 'status-code' ]);
2021-07-23 08:39:52 +12:00
2023-03-24 18:56:49 +13:00
$document2GetFailure = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents/' . $document2 [ 'body' ][ '$id' ], [
'origin' => 'http://localhost' ,
2021-07-24 09:38:31 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'cookie' => 'a_session_' . $this -> getProject ()[ '$id' ] . '=' . $session2 ,
]);
2021-07-24 09:38:31 +12:00
2023-03-24 18:56:49 +13:00
// Current user has no collection or document permissions for this document
$this -> assertEquals ( 404 , $document2GetFailure [ 'headers' ][ 'status-code' ]);
$documentsUser2 = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , [
'origin' => 'http://localhost' ,
2021-07-24 09:38:31 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'cookie' => 'a_session_' . $this -> getProject ()[ '$id' ] . '=' . $session2 ,
2021-07-24 09:38:31 +12:00
]);
2023-03-24 18:56:49 +13:00
// Current user has no collection permissions but has read permission for one document
$this -> assertEquals ( 1 , $documentsUser2 [ 'body' ][ 'total' ]);
$this -> assertCount ( 1 , $documentsUser2 [ 'body' ][ 'documents' ]);
}
public function testEnforceCollectionPermissions ()
{
$database = $this -> client -> call ( Client :: METHOD_POST , '/databases' , array_merge ([
2021-09-11 08:25:16 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'databaseId' => ID :: unique (),
'name' => 'EnforceCollectionPermissions' ,
2021-09-11 08:25:16 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 201 , $database [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'EnforceCollectionPermissions' , $database [ 'body' ][ 'name' ]);
2021-09-11 08:25:16 +12:00
2023-03-24 18:56:49 +13:00
$databaseId = $database [ 'body' ][ '$id' ];
$user = $this -> getUser ()[ '$id' ];
$collection = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections' , array_merge ([
2021-07-24 09:38:31 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'collectionId' => ID :: unique (),
'name' => 'enforceCollectionPermissions' ,
2022-08-03 16:17:49 +12:00
'permissions' => [
2023-03-24 18:56:49 +13:00
Permission :: read ( Role :: user ( $user )),
Permission :: create ( Role :: user ( $user )),
Permission :: update ( Role :: user ( $user )),
Permission :: delete ( Role :: user ( $user )),
],
2021-07-24 09:38:31 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 201 , $collection [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $collection [ 'body' ][ 'name' ], 'enforceCollectionPermissions' );
$this -> assertEquals ( $collection [ 'body' ][ 'documentSecurity' ], false );
$collectionId = $collection [ 'body' ][ '$id' ];
$attribute = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/string' , array_merge ([
2021-07-24 09:38:31 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'attribute' ,
'size' => 64 ,
'required' => true ,
2021-07-24 09:38:31 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 202 , $attribute [ 'headers' ][ 'status-code' ], 202 );
$this -> assertEquals ( 'attribute' , $attribute [ 'body' ][ 'key' ]);
2023-08-22 15:25:55 +12:00
\sleep ( 2 );
2023-03-24 18:56:49 +13:00
$index = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/indexes' , array_merge ([
2021-07-24 09:38:31 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'key_attribute' ,
'type' => 'key' ,
'attributes' => [ $attribute [ 'body' ][ 'key' ]],
2021-07-24 09:38:31 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 202 , $index [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'key_attribute' , $index [ 'body' ][ 'key' ]);
2023-08-22 15:25:55 +12:00
\sleep ( 2 );
2023-03-24 18:56:49 +13:00
$document1 = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , array_merge ([
2021-07-24 09:38:31 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2022-08-14 22:33:36 +12:00
'documentId' => ID :: unique (),
2021-07-24 09:38:31 +12:00
'data' => [
2023-03-24 18:56:49 +13:00
'attribute' => 'one' ,
2021-07-24 09:38:31 +12:00
],
2022-08-03 16:17:49 +12:00
'permissions' => [
2023-03-24 18:56:49 +13:00
Permission :: read ( Role :: user ( $user )),
Permission :: update ( Role :: user ( $user )),
Permission :: delete ( Role :: user ( $user )),
2022-08-03 16:17:49 +12:00
]
2021-07-24 09:38:31 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 201 , $document1 [ 'headers' ][ 'status-code' ]);
$document2 = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , array_merge ([
2021-10-26 09:00:33 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2022-08-14 22:33:36 +12:00
'documentId' => ID :: unique (),
2021-10-26 09:00:33 +13:00
'data' => [
2023-03-24 18:56:49 +13:00
'attribute' => 'one' ,
2021-10-26 09:00:33 +13:00
],
2022-08-03 16:17:49 +12:00
'permissions' => [
2023-03-24 18:56:49 +13:00
Permission :: update ( Role :: user ( $user )),
Permission :: delete ( Role :: user ( $user )),
2022-08-03 16:17:49 +12:00
]
2021-10-26 09:00:33 +13:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 201 , $document2 [ 'headers' ][ 'status-code' ]);
$document3 = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , [
2021-07-24 09:38:31 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
], [
2022-08-14 22:33:36 +12:00
'documentId' => ID :: unique (),
2021-07-24 09:38:31 +12:00
'data' => [
2023-03-24 18:56:49 +13:00
'attribute' => 'one' ,
2021-07-24 09:38:31 +12:00
],
2022-08-03 16:17:49 +12:00
'permissions' => [
2023-03-24 18:56:49 +13:00
Permission :: read ( Role :: user ( ID :: custom ( 'other2' ))),
Permission :: update ( Role :: user ( ID :: custom ( 'other2' ))),
],
2021-07-24 09:38:31 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 201 , $document3 [ 'headers' ][ 'status-code' ]);
$documentsUser1 = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , array_merge ([
2021-07-24 09:38:31 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()));
2021-07-24 09:38:31 +12:00
2023-03-24 18:56:49 +13:00
// Current user has read permission on the collection so can get any document
$this -> assertEquals ( 3 , $documentsUser1 [ 'body' ][ 'total' ]);
$this -> assertCount ( 3 , $documentsUser1 [ 'body' ][ 'documents' ]);
2021-07-24 09:38:31 +12:00
2023-03-24 18:56:49 +13:00
$document3GetWithCollectionRead = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents/' . $document3 [ 'body' ][ '$id' ], array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
2021-07-24 09:38:31 +12:00
2023-03-24 18:56:49 +13:00
// Current user has read permission on the collection so can get any document
$this -> assertEquals ( 200 , $document3GetWithCollectionRead [ 'headers' ][ 'status-code' ]);
$email = uniqid () . 'user2@localhost.test' ;
$password = 'password' ;
$name = 'User Name' ;
$this -> client -> call ( Client :: METHOD_POST , '/account' , [
'origin' => 'http://localhost' ,
2021-07-23 08:39:52 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], [
'userId' => ID :: custom ( 'other2' ),
'email' => $email ,
'password' => $password ,
'name' => $name ,
]);
$session2 = $this -> client -> call ( Client :: METHOD_POST , '/account/sessions/email' , [
'origin' => 'http://localhost' ,
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], [
'email' => $email ,
'password' => $password ,
2021-07-23 08:39:52 +12:00
]);
2023-12-09 12:48:34 +13:00
$session2 = $session2 [ 'cookies' ][ 'a_session_' . $this -> getProject ()[ '$id' ]];
2021-07-23 08:39:52 +12:00
2023-03-24 18:56:49 +13:00
$document3GetWithDocumentRead = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents/' . $document3 [ 'body' ][ '$id' ], [
'origin' => 'http://localhost' ,
2021-09-11 08:25:16 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'cookie' => 'a_session_' . $this -> getProject ()[ '$id' ] . '=' . $session2 ,
2021-09-11 08:25:16 +12:00
]);
2023-08-22 15:25:55 +12:00
// other2 has no collection permissions and document permissions are disabled
2023-03-28 22:08:36 +13:00
$this -> assertEquals ( 404 , $document3GetWithDocumentRead [ 'headers' ][ 'status-code' ]);
2023-03-24 18:56:49 +13:00
$documentsUser2 = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , [
'origin' => 'http://localhost' ,
2021-07-23 08:39:52 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'cookie' => 'a_session_' . $this -> getProject ()[ '$id' ] . '=' . $session2 ,
2021-07-23 08:39:52 +12:00
]);
2023-08-22 15:25:55 +12:00
// other2 has no collection permissions and document permissions are disabled
$this -> assertEquals ( 401 , $documentsUser2 [ 'headers' ][ 'status-code' ]);
2023-03-24 18:56:49 +13:00
// Enable document permissions
$collection = $this -> client -> call ( CLient :: METHOD_PUT , '/databases/' . $databaseId . '/collections/' . $collectionId , [
2021-07-23 08:39:52 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
], [
'name' => $collection [ 'body' ][ 'name' ],
'documentSecurity' => true ,
2021-07-23 08:39:52 +12:00
]);
2023-03-24 18:56:49 +13:00
$documentsUser2 = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents' , [
'origin' => 'http://localhost' ,
2021-07-24 09:38:31 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'cookie' => 'a_session_' . $this -> getProject ()[ '$id' ] . '=' . $session2 ,
2021-07-24 09:38:31 +12:00
]);
2023-03-24 18:56:49 +13:00
// Current user has no collection permissions read access to one document
$this -> assertEquals ( 1 , $documentsUser2 [ 'body' ][ 'total' ]);
$this -> assertCount ( 1 , $documentsUser2 [ 'body' ][ 'documents' ]);
}
/**
* @ depends testDefaultPermissions
*/
public function testUniqueIndexDuplicate ( array $data ) : array
{
$databaseId = $data [ 'databaseId' ];
$uniqueIndex = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/indexes' , array_merge ([
2021-07-24 09:38:31 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'unique_title' ,
'type' => 'unique' ,
'attributes' => [ 'title' ],
2021-07-24 09:38:31 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 202 , $uniqueIndex [ 'headers' ][ 'status-code' ]);
sleep ( 2 );
// test for failure
$duplicate = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2021-10-26 09:00:33 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2022-08-14 22:33:36 +12:00
'documentId' => ID :: unique (),
2021-10-26 09:00:33 +13:00
'data' => [
2023-03-24 18:56:49 +13:00
'title' => 'Captain America' ,
'releaseYear' => 1944 ,
'actors' => [
'Chris Evans' ,
'Samuel Jackson' ,
]
2021-10-26 09:00:33 +13:00
],
2022-08-03 16:17:49 +12:00
'permissions' => [
2022-08-14 22:33:36 +12:00
Permission :: read ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: update ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: delete ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
2022-08-03 16:17:49 +12:00
]
2021-10-26 09:00:33 +13:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 409 , $duplicate [ 'headers' ][ 'status-code' ]);
// Test for exception when updating document to conflict
$document = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents' , array_merge ([
2021-07-24 09:38:31 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2022-08-14 22:33:36 +12:00
'documentId' => ID :: unique (),
2021-07-24 09:38:31 +12:00
'data' => [
2023-03-24 18:56:49 +13:00
'title' => 'Captain America 5' ,
'releaseYear' => 1944 ,
'actors' => [
'Chris Evans' ,
'Samuel Jackson' ,
]
2021-07-24 09:38:31 +12:00
],
2022-08-03 16:17:49 +12:00
'permissions' => [
2022-08-14 22:33:36 +12:00
Permission :: read ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: update ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: delete ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
2022-08-03 16:17:49 +12:00
]
2021-07-24 09:38:31 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 201 , $document [ 'headers' ][ 'status-code' ]);
// Test for exception when updating document to conflict
$duplicate = $this -> client -> call ( Client :: METHOD_PATCH , '/databases/' . $databaseId . '/collections/' . $data [ 'moviesId' ] . '/documents/' . $document [ 'body' ][ '$id' ], array_merge ([
2021-07-24 09:38:31 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2022-08-14 22:33:36 +12:00
'documentId' => ID :: unique (),
2021-07-24 09:38:31 +12:00
'data' => [
2023-03-24 18:56:49 +13:00
'title' => 'Captain America' ,
'releaseYear' => 1944 ,
'actors' => [
'Chris Evans' ,
'Samuel Jackson' ,
]
2021-07-24 09:38:31 +12:00
],
2022-08-03 16:17:49 +12:00
'permissions' => [
2022-08-14 22:33:36 +12:00
Permission :: read ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: update ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: delete ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
2022-08-03 16:17:49 +12:00
]
2021-07-24 09:38:31 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 409 , $duplicate [ 'headers' ][ 'status-code' ]);
2022-08-01 00:32:41 +12:00
2023-03-24 18:56:49 +13:00
return $data ;
2021-07-23 08:39:52 +12:00
}
2021-03-22 20:34:51 +13:00
/**
2023-03-24 18:56:49 +13:00
* @ depends testUniqueIndexDuplicate
2021-03-22 20:34:51 +13:00
*/
2023-03-24 18:56:49 +13:00
public function testPersistantCreatedAt ( array $data ) : array
2021-03-22 20:34:51 +13:00
{
2023-03-24 18:56:49 +13:00
$headers = $this -> getSide () === 'client' ? array_merge ([
2021-03-22 20:34:51 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()) : [
2021-03-22 20:34:51 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
];
$document = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $data [ 'databaseId' ] . '/collections/' . $data [ 'moviesId' ] . '/documents' , $headers , [
'documentId' => ID :: unique (),
2021-03-22 20:34:51 +13:00
'data' => [
2023-03-24 18:56:49 +13:00
'title' => 'Creation Date Test' ,
'releaseYear' => 2000
]
2021-03-22 20:34:51 +13:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( $document [ 'body' ][ 'title' ], 'Creation Date Test' );
2021-03-22 20:34:51 +13:00
2023-03-24 18:56:49 +13:00
$documentId = $document [ 'body' ][ '$id' ];
$createdAt = $document [ 'body' ][ '$createdAt' ];
$updatedAt = $document [ 'body' ][ '$updatedAt' ];
2021-03-22 20:34:51 +13:00
2023-03-24 18:56:49 +13:00
\sleep ( 1 );
2021-03-22 20:34:51 +13:00
2023-03-24 18:56:49 +13:00
$document = $this -> client -> call ( Client :: METHOD_PATCH , '/databases/' . $data [ 'databaseId' ] . '/collections/' . $data [ 'moviesId' ] . '/documents/' . $documentId , $headers , [
2021-03-22 20:34:51 +13:00
'data' => [
2023-03-24 18:56:49 +13:00
'title' => 'Updated Date Test' ,
]
2021-03-22 20:34:51 +13:00
]);
2023-03-24 18:56:49 +13:00
$updatedAtSecond = $document [ 'body' ][ '$updatedAt' ];
2021-03-22 20:34:51 +13:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( $document [ 'body' ][ 'title' ], 'Updated Date Test' );
$this -> assertEquals ( $document [ 'body' ][ '$createdAt' ], $createdAt );
$this -> assertNotEquals ( $document [ 'body' ][ '$updatedAt' ], $updatedAt );
2022-08-25 01:32:52 +12:00
2023-03-24 18:56:49 +13:00
\sleep ( 1 );
$document = $this -> client -> call ( Client :: METHOD_PATCH , '/databases/' . $data [ 'databaseId' ] . '/collections/' . $data [ 'moviesId' ] . '/documents/' . $documentId , $headers , [
'data' => [
'title' => 'Again Updated Date Test' ,
'$createdAt' => '2022-08-01 13:09:23.040' , // $createdAt is not updatable
'$updatedAt' => '2022-08-01 13:09:23.050' // system will update it not api
]
]);
$this -> assertEquals ( $document [ 'body' ][ 'title' ], 'Again Updated Date Test' );
$this -> assertEquals ( $document [ 'body' ][ '$createdAt' ], $createdAt );
$this -> assertNotEquals ( $document [ 'body' ][ '$createdAt' ], '2022-08-01 13:09:23.040' );
$this -> assertNotEquals ( $document [ 'body' ][ '$updatedAt' ], $updatedAt );
$this -> assertNotEquals ( $document [ 'body' ][ '$updatedAt' ], $updatedAtSecond );
$this -> assertNotEquals ( $document [ 'body' ][ '$updatedAt' ], '2022-08-01 13:09:23.050' );
2022-08-25 01:32:52 +12:00
2021-03-22 20:34:51 +13:00
return $data ;
2020-01-13 21:46:09 +13:00
}
2021-08-19 06:49:57 +12:00
2023-03-24 18:56:49 +13:00
public function testUpdatePermissionsWithEmptyPayload () : array
2021-08-12 13:07:46 +12:00
{
2023-03-24 18:56:49 +13:00
// Create Database
2022-06-22 22:51:49 +12:00
$database = $this -> client -> call ( Client :: METHOD_POST , '/databases' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2022-08-14 22:33:36 +12:00
'databaseId' => ID :: unique (),
2023-03-24 18:56:49 +13:00
'name' => 'Empty Permissions' ,
2022-06-22 22:51:49 +12:00
]);
$this -> assertEquals ( 201 , $database [ 'headers' ][ 'status-code' ]);
$databaseId = $database [ 'body' ][ '$id' ];
2023-03-24 18:56:49 +13:00
// Create collection
2023-05-30 18:26:17 +12:00
$movies = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections' , array_merge ([
2021-08-12 13:07:46 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2022-08-14 22:33:36 +12:00
'collectionId' => ID :: unique (),
2023-03-24 18:56:49 +13:00
'name' => 'Movies' ,
2022-08-03 16:17:49 +12:00
'permissions' => [
2023-03-24 18:56:49 +13:00
Permission :: create ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: read ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: update ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: delete ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
2022-08-03 16:17:49 +12:00
],
2023-03-24 18:56:49 +13:00
'documentSecurity' => true ,
2021-08-12 13:07:46 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 201 , $movies [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $movies [ 'body' ][ 'name' ], 'Movies' );
2021-08-12 13:07:46 +12:00
2023-03-24 18:56:49 +13:00
$moviesId = $movies [ 'body' ][ '$id' ];
2021-08-14 08:20:54 +12:00
2023-03-24 18:56:49 +13:00
// create attribute
$title = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $moviesId . '/attributes/string' , array_merge ([
2021-08-12 13:07:46 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2023-03-24 18:56:49 +13:00
'key' => 'title' ,
'size' => 256 ,
2021-08-12 13:07:46 +12:00
'required' => true ,
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 202 , $title [ 'headers' ][ 'status-code' ]);
2021-08-12 13:07:46 +12:00
2023-03-24 18:56:49 +13:00
// wait for database worker to create attributes
2021-08-14 08:20:54 +12:00
sleep ( 2 );
2021-08-12 13:07:46 +12:00
2023-03-24 18:56:49 +13:00
// add document
$document = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $moviesId . '/documents' , array_merge ([
2021-09-02 02:57:20 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2022-08-14 22:33:36 +12:00
'documentId' => ID :: unique (),
2021-08-12 13:07:46 +12:00
'data' => [
2023-03-24 18:56:49 +13:00
'title' => 'Captain America' ,
2021-08-12 13:07:46 +12:00
],
2022-08-03 16:17:49 +12:00
'permissions' => [
2023-03-24 18:56:49 +13:00
Permission :: read ( Role :: any ()),
Permission :: update ( Role :: any ()),
Permission :: delete ( Role :: any ()),
],
2021-08-12 13:07:46 +12:00
]);
2023-03-24 18:56:49 +13:00
$id = $document [ 'body' ][ '$id' ];
2021-08-12 13:07:46 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 201 , $document [ 'headers' ][ 'status-code' ]);
$this -> assertCount ( 3 , $document [ 'body' ][ '$permissions' ]);
$this -> assertContains ( Permission :: read ( Role :: any ()), $document [ 'body' ][ '$permissions' ]);
$this -> assertContains ( Permission :: update ( Role :: any ()), $document [ 'body' ][ '$permissions' ]);
$this -> assertContains ( Permission :: delete ( Role :: any ()), $document [ 'body' ][ '$permissions' ]);
// Send only read permission
$document = $this -> client -> call ( Client :: METHOD_PATCH , '/databases/' . $databaseId . '/collections/' . $moviesId . '/documents/' . $id , array_merge ([
2021-12-17 07:12:06 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2022-08-03 16:17:49 +12:00
'permissions' => [
2023-03-24 18:56:49 +13:00
Permission :: read ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
2022-08-03 16:17:49 +12:00
]
2021-12-17 07:12:06 +13:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $document [ 'headers' ][ 'status-code' ]);
$this -> assertCount ( 1 , $document [ 'body' ][ '$permissions' ]);
2021-12-17 07:12:06 +13:00
2023-03-24 18:56:49 +13:00
// Send only mutation permissions
$document = $this -> client -> call ( Client :: METHOD_PATCH , '/databases/' . $databaseId . '/collections/' . $moviesId . '/documents/' . $id , array_merge ([
2021-12-17 07:12:06 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
2022-08-08 23:00:03 +12:00
'permissions' => [
2023-03-24 18:56:49 +13:00
Permission :: update ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
Permission :: delete ( Role :: user ( ID :: custom ( $this -> getUser ()[ '$id' ]))),
2022-08-08 23:00:03 +12:00
],
2021-12-17 07:12:06 +13:00
]);
2023-03-24 18:56:49 +13:00
if ( $this -> getSide () == 'server' ) {
$this -> assertEquals ( 200 , $document [ 'headers' ][ 'status-code' ]);
$this -> assertCount ( 2 , $document [ 'body' ][ '$permissions' ]);
$this -> assertContains ( Permission :: update ( Role :: user ( $this -> getUser ()[ '$id' ])), $document [ 'body' ][ '$permissions' ]);
$this -> assertContains ( Permission :: delete ( Role :: user ( $this -> getUser ()[ '$id' ])), $document [ 'body' ][ '$permissions' ]);
}
2021-12-17 07:12:06 +13:00
2023-03-24 18:56:49 +13:00
// remove collection
$this -> client -> call ( Client :: METHOD_DELETE , '/databases/' . $databaseId . '/collections/' . $moviesId , array_merge ([
2021-08-12 13:07:46 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
2023-03-24 18:56:49 +13:00
return [];
}
2021-08-14 08:20:54 +12:00
2023-03-24 18:56:49 +13:00
/**
* @ depends testCreateDatabase
*/
public function testAttributeBooleanDefault ( array $data ) : void
{
$databaseId = $data [ 'databaseId' ];
2021-08-14 08:20:54 +12:00
2023-03-24 18:56:49 +13:00
/**
* Test for SUCCESS
*/
$collection = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections' , array_merge ([
2021-08-14 08:20:54 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'collectionId' => ID :: unique (),
'name' => 'Boolean'
2022-08-08 23:00:03 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 201 , $collection [ 'headers' ][ 'status-code' ]);
$collectionId = $collection [ 'body' ][ '$id' ];
$true = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/boolean' , array_merge ([
2022-08-08 23:00:03 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'true' ,
'required' => false ,
'default' => true
2021-08-14 08:20:54 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 202 , $true [ 'headers' ][ 'status-code' ]);
$false = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/boolean' , array_merge ([
2022-08-08 23:00:03 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'false' ,
'required' => false ,
'default' => false
2022-08-08 23:00:03 +12:00
]);
2021-08-14 08:20:54 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 202 , $false [ 'headers' ][ 'status-code' ]);
}
2021-08-14 08:20:54 +12:00
2023-03-24 18:56:49 +13:00
/**
* @ depends testCreateDatabase
*/
public function testOneToOneRelationship ( array $data ) : array
{
$databaseId = $data [ 'databaseId' ];
$person = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections' , array_merge ([
2021-08-14 08:20:54 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'collectionId' => 'person' ,
'name' => 'person' ,
'permissions' => [
Permission :: read ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: update ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: delete ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: create ( Role :: user ( $this -> getUser ()[ '$id' ])),
],
'documentSecurity' => true ,
2021-08-14 08:20:54 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 201 , $person [ 'headers' ][ 'status-code' ]);
2022-08-25 01:32:52 +12:00
2023-03-24 18:56:49 +13:00
$library = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections' , array_merge ([
2022-08-25 01:32:52 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'collectionId' => 'library' ,
'name' => 'library' ,
'permissions' => [
Permission :: read ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: update ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: create ( Role :: user ( $this -> getUser ()[ '$id' ])),
],
'documentSecurity' => true ,
2022-08-25 01:32:52 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 201 , $library [ 'headers' ][ 'status-code' ]);
2022-08-25 01:32:52 +12:00
2023-03-24 18:56:49 +13:00
$this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $person [ 'body' ][ '$id' ] . '/attributes/string' , array_merge ([
2022-08-25 01:32:52 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2023-03-24 18:56:49 +13:00
'key' => 'fullName' ,
'size' => 255 ,
'required' => false ,
2022-08-25 01:32:52 +12:00
]);
2023-03-24 18:56:49 +13:00
sleep ( 1 ); // Wait for worker
$relation = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $person [ 'body' ][ '$id' ] . '/attributes/relationship' , array_merge ([
2022-08-25 01:32:52 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2023-03-24 18:56:49 +13:00
'relatedCollectionId' => 'library' ,
'type' => Database :: RELATION_ONE_TO_ONE ,
'key' => 'library' ,
2023-03-31 06:36:24 +13:00
'twoWay' => true ,
2023-03-24 18:56:49 +13:00
'onDelete' => Database :: RELATION_MUTATE_CASCADE ,
2022-08-25 01:32:52 +12:00
]);
2023-03-24 18:56:49 +13:00
sleep ( 1 ); // Wait for worker
2022-08-25 01:32:52 +12:00
2023-03-24 18:56:49 +13:00
$libraryName = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $library [ 'body' ][ '$id' ] . '/attributes/string' , array_merge ([
2022-08-25 01:32:52 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2023-03-24 18:56:49 +13:00
'key' => 'libraryName' ,
'size' => 255 ,
2022-08-25 01:32:52 +12:00
'required' => true ,
]);
2023-03-24 18:56:49 +13:00
sleep ( 1 ); // Wait for worker
2022-08-25 01:32:52 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 202 , $libraryName [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 202 , $relation [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'library' , $relation [ 'body' ][ 'key' ]);
$this -> assertEquals ( 'relationship' , $relation [ 'body' ][ 'type' ]);
$this -> assertEquals ( 'processing' , $relation [ 'body' ][ 'status' ]);
2022-08-25 01:32:52 +12:00
2023-03-28 04:24:02 +13:00
$attributes = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $person [ 'body' ][ '$id' ] . '/attributes' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]));
$this -> assertEquals ( 200 , $attributes [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 2 , $attributes [ 'body' ][ 'total' ]);
$attributes = $attributes [ 'body' ][ 'attributes' ];
$this -> assertEquals ( 'library' , $attributes [ 1 ][ 'relatedCollection' ]);
$this -> assertEquals ( 'oneToOne' , $attributes [ 1 ][ 'relationType' ]);
2023-03-31 06:36:24 +13:00
$this -> assertEquals ( true , $attributes [ 1 ][ 'twoWay' ]);
2023-03-28 04:24:02 +13:00
$this -> assertEquals ( 'person' , $attributes [ 1 ][ 'twoWayKey' ]);
$this -> assertEquals ( Database :: RELATION_MUTATE_CASCADE , $attributes [ 1 ][ 'onDelete' ]);
2023-03-24 18:56:49 +13:00
$attribute = $this -> client -> call ( Client :: METHOD_GET , " /databases/ { $databaseId } /collections/ { $person [ 'body' ][ '$id' ] } /attributes/library " , array_merge ([
2022-08-25 01:32:52 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
2023-03-24 18:56:49 +13:00
]));
2022-08-25 01:32:52 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $attribute [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'available' , $attribute [ 'body' ][ 'status' ]);
$this -> assertEquals ( 'library' , $attribute [ 'body' ][ 'key' ]);
$this -> assertEquals ( 'relationship' , $attribute [ 'body' ][ 'type' ]);
$this -> assertEquals ( false , $attribute [ 'body' ][ 'required' ]);
$this -> assertEquals ( false , $attribute [ 'body' ][ 'array' ]);
$this -> assertEquals ( 'oneToOne' , $attribute [ 'body' ][ 'relationType' ]);
2023-03-31 06:36:24 +13:00
$this -> assertEquals ( true , $attribute [ 'body' ][ 'twoWay' ]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 'person' , $attribute [ 'body' ][ 'twoWayKey' ]);
$this -> assertEquals ( Database :: RELATION_MUTATE_CASCADE , $attribute [ 'body' ][ 'onDelete' ]);
2022-08-25 01:32:52 +12:00
2023-03-24 18:56:49 +13:00
$person1 = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $person [ 'body' ][ '$id' ] . '/documents' , array_merge ([
2022-08-25 01:32:52 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'documentId' => ID :: unique (),
'data' => [
2023-03-24 18:56:49 +13:00
'library' => [
'$id' => 'library1' ,
'$permissions' => [
Permission :: read ( Role :: any ()),
],
'libraryName' => 'Library 1' ,
],
2022-08-25 01:32:52 +12:00
],
'permissions' => [
2023-03-24 18:56:49 +13:00
Permission :: read ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: update ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: delete ( Role :: user ( $this -> getUser ()[ '$id' ])),
2022-08-25 01:32:52 +12:00
]
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 'Library 1' , $person1 [ 'body' ][ 'library' ][ 'libraryName' ]);
2022-08-25 01:32:52 +12:00
2023-04-13 15:59:57 +12:00
// Create without nested ID
$person2 = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $person [ 'body' ][ '$id' ] . '/documents' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'documentId' => ID :: unique (),
'data' => [
'library' => [
'libraryName' => 'Library 2' ,
],
],
'permissions' => [
Permission :: read ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: update ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: delete ( Role :: user ( $this -> getUser ()[ '$id' ])),
]
]);
$this -> assertEquals ( 'Library 2' , $person2 [ 'body' ][ 'library' ][ 'libraryName' ]);
2023-03-27 15:42:36 +13:00
// Ensure IDs were set and internal IDs removed
$this -> assertEquals ( $databaseId , $person1 [ 'body' ][ '$databaseId' ]);
$this -> assertEquals ( $databaseId , $person1 [ 'body' ][ 'library' ][ '$databaseId' ]);
$this -> assertEquals ( $person [ 'body' ][ '$id' ], $person1 [ 'body' ][ '$collectionId' ]);
2023-03-27 19:03:00 +13:00
$this -> assertEquals ( $library [ 'body' ][ '$id' ], $person1 [ 'body' ][ 'library' ][ '$collectionId' ]);
2023-03-27 15:42:36 +13:00
$this -> assertArrayNotHasKey ( '$collection' , $person1 [ 'body' ]);
$this -> assertArrayNotHasKey ( '$collection' , $person1 [ 'body' ][ 'library' ]);
$this -> assertArrayNotHasKey ( '$internalId' , $person1 [ 'body' ]);
$this -> assertArrayNotHasKey ( '$internalId' , $person1 [ 'body' ][ 'library' ]);
2023-03-24 18:56:49 +13:00
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $person [ 'body' ][ '$id' ] . '/documents' , array_merge ([
2022-08-25 01:32:52 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-03-24 18:56:49 +13:00
'queries' => [
2023-12-20 23:55:09 +13:00
Query :: select ([ 'fullName' , 'library.*' ]) -> toString (),
Query :: equal ( 'library' , [ 'library1' ]) -> toString (),
],
2022-08-25 01:32:52 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 1 , $documents [ 'body' ][ 'total' ]);
$this -> assertEquals ( 'Library 1' , $documents [ 'body' ][ 'documents' ][ 0 ][ 'library' ][ 'libraryName' ]);
$this -> assertArrayHasKey ( 'fullName' , $documents [ 'body' ][ 'documents' ][ 0 ]);
2022-08-25 01:32:52 +12:00
2023-03-24 18:56:49 +13:00
$documents = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $person [ 'body' ][ '$id' ] . '/documents' , array_merge ([
2022-08-25 01:32:52 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
'queries' => [
2023-12-20 23:55:09 +13:00
Query :: equal ( 'library.libraryName' , [ 'Library 1' ]) -> toString (),
2022-08-25 01:32:52 +12:00
],
]);
2023-04-11 16:21:22 +12:00
$this -> assertEquals ( 400 , $documents [ 'headers' ][ 'status-code' ]);
2023-06-15 17:28:35 +12:00
$this -> assertEquals ( 'Invalid query: Cannot query nested attribute on: library' , $documents [ 'body' ][ 'message' ]);
2022-08-25 01:32:52 +12:00
2023-03-24 18:56:49 +13:00
$response = $this -> client -> call ( Client :: METHOD_DELETE , '/databases/' . $databaseId . '/collections/' . $person [ 'body' ][ '$id' ] . '/attributes/library' , array_merge ([
2022-08-25 01:32:52 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]));
2022-08-25 01:32:52 +12:00
2023-03-31 06:36:24 +13:00
sleep ( 2 );
2022-08-25 01:32:52 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 204 , $response [ 'headers' ][ 'status-code' ]);
$attribute = $this -> client -> call ( Client :: METHOD_GET , " /databases/ { $databaseId } /collections/ { $person [ 'body' ][ '$id' ] } /attributes/library " , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]));
$this -> assertEquals ( 404 , $attribute [ 'headers' ][ 'status-code' ]);
$person1 = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $person [ 'body' ][ '$id' ] . '/documents/' . $person1 [ 'body' ][ '$id' ], array_merge ([
2022-08-25 01:32:52 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
2023-03-24 18:56:49 +13:00
$this -> assertArrayNotHasKey ( 'library' , $person1 [ 'body' ]);
2022-08-25 01:32:52 +12:00
2023-03-31 06:36:24 +13:00
//Test Deletion of related twoKey
$attributes = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $library [ 'body' ][ '$id' ] . '/attributes' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]));
$this -> assertEquals ( 200 , $attributes [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 1 , $attributes [ 'body' ][ 'total' ]);
$this -> assertEquals ( 'libraryName' , $attributes [ 'body' ][ 'attributes' ][ 0 ][ 'key' ]);
2023-03-24 18:56:49 +13:00
return [
'databaseId' => $databaseId ,
'personCollection' => $person [ 'body' ][ '$id' ],
'libraryCollection' => $library [ 'body' ][ '$id' ],
];
}
/**
* @ depends testOneToOneRelationship
*/
public function testOneToManyRelationship ( array $data ) : array
{
$databaseId = $data [ 'databaseId' ];
$personCollection = $data [ 'personCollection' ];
$libraryCollection = $data [ 'libraryCollection' ];
// One person can own several libraries
$this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $personCollection . '/attributes/relationship' , array_merge ([
2022-08-25 01:32:52 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'relatedCollectionId' => 'library' ,
'type' => Database :: RELATION_ONE_TO_MANY ,
'twoWay' => true ,
'key' => 'libraries' ,
2023-03-31 06:36:24 +13:00
'twoWayKey' => 'person_one_to_many' ,
2022-08-25 01:32:52 +12:00
]);
2023-03-24 18:56:49 +13:00
sleep ( 1 );
2023-03-30 04:34:49 +13:00
$libraryAttributesResponse = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $libraryCollection . '/attributes' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]));
$this -> assertIsArray ( $libraryAttributesResponse [ 'body' ][ 'attributes' ]);
2023-03-31 06:36:24 +13:00
$this -> assertEquals ( 2 , $libraryAttributesResponse [ 'body' ][ 'total' ]);
$this -> assertEquals ( 'person_one_to_many' , $libraryAttributesResponse [ 'body' ][ 'attributes' ][ 1 ][ 'key' ]);
2023-03-30 04:34:49 +13:00
$libraryCollectionResponse = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $libraryCollection , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]));
$this -> assertIsArray ( $libraryCollectionResponse [ 'body' ][ 'attributes' ]);
2023-03-30 09:31:49 +13:00
$this -> assertCount ( 2 , $libraryCollectionResponse [ 'body' ][ 'attributes' ]);
2023-03-30 04:34:49 +13:00
2023-03-24 18:56:49 +13:00
$attribute = $this -> client -> call ( Client :: METHOD_GET , " /databases/ { $databaseId } /collections/ { $personCollection } /attributes/libraries " , array_merge ([
2022-08-25 01:32:52 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]));
2022-08-25 01:32:52 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $attribute [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'available' , $attribute [ 'body' ][ 'status' ]);
$this -> assertEquals ( 'libraries' , $attribute [ 'body' ][ 'key' ]);
$this -> assertEquals ( 'relationship' , $attribute [ 'body' ][ 'type' ]);
$this -> assertEquals ( false , $attribute [ 'body' ][ 'required' ]);
$this -> assertEquals ( false , $attribute [ 'body' ][ 'array' ]);
$this -> assertEquals ( 'oneToMany' , $attribute [ 'body' ][ 'relationType' ]);
$this -> assertEquals ( true , $attribute [ 'body' ][ 'twoWay' ]);
2023-03-31 06:36:24 +13:00
$this -> assertEquals ( 'person_one_to_many' , $attribute [ 'body' ][ 'twoWayKey' ]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 'restrict' , $attribute [ 'body' ][ 'onDelete' ]);
$person2 = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $personCollection . '/documents' , array_merge ([
2022-08-25 01:32:52 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
'documentId' => 'person10' ,
'data' => [
'fullName' => 'Stevie Wonder' ,
'libraries' => [
[
'$id' => 'library10' ,
'$permissions' => [
Permission :: read ( Role :: any ()),
Permission :: update ( Role :: any ()),
Permission :: delete ( Role :: any ()),
],
'libraryName' => 'Library 10' ,
],
[
'$id' => 'library11' ,
'$permissions' => [
Permission :: read ( Role :: any ()),
Permission :: update ( Role :: any ()),
Permission :: delete ( Role :: any ()),
],
'libraryName' => 'Library 11' ,
]
],
],
'permissions' => [
Permission :: read ( Role :: any ()),
Permission :: update ( Role :: any ()),
Permission :: delete ( Role :: any ()),
]
2022-08-25 01:32:52 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 201 , $person2 [ 'headers' ][ 'status-code' ]);
$this -> assertArrayHasKey ( 'libraries' , $person2 [ 'body' ]);
$this -> assertEquals ( 2 , count ( $person2 [ 'body' ][ 'libraries' ]));
2022-08-25 01:32:52 +12:00
2023-03-24 18:56:49 +13:00
$response = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $personCollection . '/documents/' . $person2 [ 'body' ][ '$id' ], array_merge ([
2022-08-25 01:32:52 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()));
2022-08-25 01:32:52 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $response [ 'headers' ][ 'status-code' ]);
$this -> assertArrayNotHasKey ( '$collection' , $response [ 'body' ]);
$this -> assertArrayHasKey ( 'libraries' , $response [ 'body' ]);
$this -> assertEquals ( 2 , count ( $response [ 'body' ][ 'libraries' ]));
$response = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $libraryCollection . '/documents/library11' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
2022-08-25 01:32:52 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $response [ 'headers' ][ 'status-code' ]);
2023-03-31 06:36:24 +13:00
$this -> assertArrayHasKey ( 'person_one_to_many' , $response [ 'body' ]);
$this -> assertEquals ( 'person10' , $response [ 'body' ][ 'person_one_to_many' ][ '$id' ]);
2022-08-25 01:32:52 +12:00
2023-03-24 18:56:49 +13:00
$response = $this -> client -> call ( Client :: METHOD_PATCH , '/databases/' . $databaseId . '/collections/' . $personCollection . '/attributes/libraries/relationship' , array_merge ([
2022-08-25 01:32:52 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
2023-03-24 18:56:49 +13:00
]), [
'onDelete' => Database :: RELATION_MUTATE_CASCADE ,
2022-08-25 01:32:52 +12:00
]);
2021-08-14 08:20:54 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $response [ 'headers' ][ 'status-code' ]);
$attribute = $this -> client -> call ( Client :: METHOD_GET , " /databases/ { $databaseId } /collections/ { $personCollection } /attributes/libraries " , array_merge ([
2021-08-14 08:20:54 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]));
2021-08-14 08:20:54 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $attribute [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'available' , $attribute [ 'body' ][ 'status' ]);
$this -> assertEquals ( 'libraries' , $attribute [ 'body' ][ 'key' ]);
$this -> assertEquals ( 'relationship' , $attribute [ 'body' ][ 'type' ]);
$this -> assertEquals ( false , $attribute [ 'body' ][ 'required' ]);
$this -> assertEquals ( false , $attribute [ 'body' ][ 'array' ]);
$this -> assertEquals ( 'oneToMany' , $attribute [ 'body' ][ 'relationType' ]);
2023-03-31 21:29:02 +13:00
$this -> assertEquals ( true , $attribute [ 'body' ][ 'twoWay' ]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( Database :: RELATION_MUTATE_CASCADE , $attribute [ 'body' ][ 'onDelete' ]);
return [ 'databaseId' => $databaseId , 'personCollection' => $personCollection ];
2021-08-12 13:07:46 +12:00
}
2021-09-02 02:52:38 +12:00
2021-08-19 06:49:57 +12:00
/**
2023-03-24 18:56:49 +13:00
* @ depends testCreateDatabase
2021-08-19 06:49:57 +12:00
*/
2023-03-24 18:56:49 +13:00
public function testManyToOneRelationship ( array $data ) : array
2021-08-19 06:49:57 +12:00
{
2022-06-22 22:51:49 +12:00
$databaseId = $data [ 'databaseId' ];
2023-03-24 18:56:49 +13:00
// Create album collection
$albums = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections' , array_merge ([
2021-08-19 06:49:57 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2023-03-24 18:56:49 +13:00
'collectionId' => ID :: unique (),
'name' => 'Albums' ,
'documentSecurity' => true ,
'permissions' => [
Permission :: create ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: read ( Role :: user ( $this -> getUser ()[ '$id' ])),
],
2021-08-19 06:49:57 +12:00
]);
2023-03-24 18:56:49 +13:00
// Create album name attribute
$this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $albums [ 'body' ][ '$id' ] . '/attributes/string' , array_merge ([
2021-08-19 06:49:57 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'name' ,
'size' => 255 ,
'required' => true ,
2021-08-19 06:49:57 +12:00
]);
2023-03-24 18:56:49 +13:00
// Create artist collection
$artists = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections' , array_merge ([
2021-08-19 06:49:57 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'collectionId' => ID :: unique (),
'name' => 'Artists' ,
'documentSecurity' => true ,
2022-08-03 16:17:49 +12:00
'permissions' => [
2023-03-24 18:56:49 +13:00
Permission :: create ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: read ( Role :: user ( $this -> getUser ()[ '$id' ])),
],
2021-08-19 06:49:57 +12:00
]);
2023-03-24 18:56:49 +13:00
// Create artist name attribute
$this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $artists [ 'body' ][ '$id' ] . '/attributes/string' , array_merge ([
2021-08-19 06:49:57 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'name' ,
'size' => 255 ,
'required' => true ,
2021-08-19 06:49:57 +12:00
]);
2023-03-24 18:56:49 +13:00
// Create relationship
$response = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $albums [ 'body' ][ '$id' ] . '/attributes/relationship' , array_merge ([
2022-07-08 21:36:19 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
2023-03-24 18:56:49 +13:00
]), [
'relatedCollectionId' => $artists [ 'body' ][ '$id' ],
'type' => Database :: RELATION_MANY_TO_ONE ,
'twoWay' => true ,
'key' => 'artist' ,
'twoWayKey' => 'albums' ,
2022-07-08 21:36:19 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 202 , $response [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'artist' , $response [ 'body' ][ 'key' ]);
$this -> assertEquals ( 'relationship' , $response [ 'body' ][ 'type' ]);
$this -> assertEquals ( false , $response [ 'body' ][ 'required' ]);
$this -> assertEquals ( false , $response [ 'body' ][ 'array' ]);
$this -> assertEquals ( 'manyToOne' , $response [ 'body' ][ 'relationType' ]);
$this -> assertEquals ( true , $response [ 'body' ][ 'twoWay' ]);
$this -> assertEquals ( 'albums' , $response [ 'body' ][ 'twoWayKey' ]);
$this -> assertEquals ( 'restrict' , $response [ 'body' ][ 'onDelete' ]);
2022-07-08 21:36:19 +12:00
2023-03-24 18:56:49 +13:00
sleep ( 1 ); // Wait for worker
2022-07-08 21:36:19 +12:00
2023-03-24 18:56:49 +13:00
$permissions = [
Permission :: read ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: update ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: delete ( Role :: user ( $this -> getUser ()[ '$id' ])),
];
2022-07-08 21:36:19 +12:00
2023-03-24 18:56:49 +13:00
// Create album
$album = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $albums [ 'body' ][ '$id' ] . '/documents' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'documentId' => 'album1' ,
'permissions' => $permissions ,
2022-07-08 21:36:19 +12:00
'data' => [
2023-03-24 18:56:49 +13:00
'name' => 'Album 1' ,
'artist' => [
'$id' => ID :: unique (),
'name' => 'Artist 1' ,
],
],
2022-07-08 21:36:19 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 201 , $album [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'album1' , $album [ 'body' ][ '$id' ]);
$this -> assertEquals ( 'Album 1' , $album [ 'body' ][ 'name' ]);
$this -> assertEquals ( 'Artist 1' , $album [ 'body' ][ 'artist' ][ 'name' ]);
$this -> assertEquals ( $permissions , $album [ 'body' ][ '$permissions' ]);
$this -> assertEquals ( $permissions , $album [ 'body' ][ 'artist' ][ '$permissions' ]);
2022-07-08 21:36:19 +12:00
2023-03-24 18:56:49 +13:00
$album = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $albums [ 'body' ][ '$id' ] . '/documents/album1' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
2022-07-08 21:36:19 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $album [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'album1' , $album [ 'body' ][ '$id' ]);
$this -> assertEquals ( 'Album 1' , $album [ 'body' ][ 'name' ]);
$this -> assertEquals ( 'Artist 1' , $album [ 'body' ][ 'artist' ][ 'name' ]);
$this -> assertEquals ( $permissions , $album [ 'body' ][ '$permissions' ]);
$this -> assertEquals ( $permissions , $album [ 'body' ][ 'artist' ][ '$permissions' ]);
2022-07-08 21:36:19 +12:00
2023-03-24 18:56:49 +13:00
$artist = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $artists [ 'body' ][ '$id' ] . '/documents/' . $album [ 'body' ][ 'artist' ][ '$id' ], array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
2022-07-08 21:36:19 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $artist [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'Artist 1' , $artist [ 'body' ][ 'name' ]);
$this -> assertEquals ( $permissions , $artist [ 'body' ][ '$permissions' ]);
$this -> assertEquals ( 1 , count ( $artist [ 'body' ][ 'albums' ]));
$this -> assertEquals ( 'album1' , $artist [ 'body' ][ 'albums' ][ 0 ][ '$id' ]);
$this -> assertEquals ( 'Album 1' , $artist [ 'body' ][ 'albums' ][ 0 ][ 'name' ]);
$this -> assertEquals ( $permissions , $artist [ 'body' ][ 'albums' ][ 0 ][ '$permissions' ]);
2022-07-08 21:36:19 +12:00
2023-03-24 18:56:49 +13:00
return [
'databaseId' => $databaseId ,
'albumsCollection' => $albums [ 'body' ][ '$id' ],
'artistsCollection' => $artists [ 'body' ][ '$id' ],
];
2022-07-08 21:36:19 +12:00
}
2023-03-24 18:56:49 +13:00
/**
* @ depends testCreateDatabase
*/
public function testManyToManyRelationship ( array $data ) : array
2022-06-21 10:24:51 +12:00
{
2023-03-24 18:56:49 +13:00
$databaseId = $data [ 'databaseId' ];
// Create sports collection
$sports = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections' , array_merge ([
2022-06-25 00:18:10 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2023-03-24 18:56:49 +13:00
'collectionId' => ID :: unique (),
'name' => 'Sports' ,
'documentSecurity' => true ,
'permissions' => [
Permission :: create ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: read ( Role :: user ( $this -> getUser ()[ '$id' ])),
],
2022-06-25 00:18:10 +12:00
]);
2023-03-24 18:56:49 +13:00
// Create sport name attribute
$this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $sports [ 'body' ][ '$id' ] . '/attributes/string' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'name' ,
'size' => 255 ,
'required' => true ,
]);
2022-06-25 00:18:10 +12:00
2023-03-24 18:56:49 +13:00
// Create player collection
$players = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections' , array_merge ([
2022-06-21 10:33:26 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2022-08-14 22:33:36 +12:00
'collectionId' => ID :: unique (),
2023-03-24 18:56:49 +13:00
'name' => 'Players' ,
'documentSecurity' => true ,
2022-08-14 02:10:28 +12:00
'permissions' => [
2023-03-24 18:56:49 +13:00
Permission :: create ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: read ( Role :: user ( $this -> getUser ()[ '$id' ])),
2022-08-14 02:10:28 +12:00
],
2022-06-21 10:33:26 +12:00
]);
2023-03-24 18:56:49 +13:00
// Create player name attribute
$this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $players [ 'body' ][ '$id' ] . '/attributes/string' , array_merge ([
2022-06-21 10:33:26 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2023-03-24 18:56:49 +13:00
'key' => 'name' ,
'size' => 255 ,
2022-06-21 10:33:26 +12:00
'required' => true ,
]);
2023-03-24 18:56:49 +13:00
// Create relationship
$response = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $sports [ 'body' ][ '$id' ] . '/attributes/relationship' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'relatedCollectionId' => $players [ 'body' ][ '$id' ],
'type' => Database :: RELATION_MANY_TO_MANY ,
'twoWay' => true ,
'key' => 'players' ,
'twoWayKey' => 'sports' ,
'onDelete' => Database :: RELATION_MUTATE_SET_NULL ,
]);
2022-06-21 10:33:26 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 202 , $response [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'players' , $response [ 'body' ][ 'key' ]);
$this -> assertEquals ( 'relationship' , $response [ 'body' ][ 'type' ]);
$this -> assertEquals ( false , $response [ 'body' ][ 'required' ]);
$this -> assertEquals ( false , $response [ 'body' ][ 'array' ]);
$this -> assertEquals ( 'manyToMany' , $response [ 'body' ][ 'relationType' ]);
$this -> assertEquals ( true , $response [ 'body' ][ 'twoWay' ]);
$this -> assertEquals ( 'sports' , $response [ 'body' ][ 'twoWayKey' ]);
$this -> assertEquals ( 'setNull' , $response [ 'body' ][ 'onDelete' ]);
2022-06-21 10:33:26 +12:00
2023-03-24 18:56:49 +13:00
sleep ( 1 ); // Wait for worker
$permissions = [
Permission :: read ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: update ( Role :: user ( $this -> getUser ()[ '$id' ])),
];
// Create sport
$sport = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $sports [ 'body' ][ '$id' ] . '/documents' , array_merge ([
2022-06-21 10:24:51 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-03-24 18:56:49 +13:00
'documentId' => 'sport1' ,
'permissions' => $permissions ,
2022-06-21 10:24:51 +12:00
'data' => [
2023-03-24 18:56:49 +13:00
'name' => 'Sport 1' ,
'players' => [
[
'$id' => 'player1' ,
'name' => 'Player 1' ,
],
[
'$id' => 'player2' ,
'name' => 'Player 2' ,
]
],
2022-08-02 21:18:49 +12:00
],
2022-06-21 10:24:51 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 201 , $sport [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'sport1' , $sport [ 'body' ][ '$id' ]);
$this -> assertEquals ( 'Sport 1' , $sport [ 'body' ][ 'name' ]);
$this -> assertEquals ( 'Player 1' , $sport [ 'body' ][ 'players' ][ 0 ][ 'name' ]);
$this -> assertEquals ( 'Player 2' , $sport [ 'body' ][ 'players' ][ 1 ][ 'name' ]);
$this -> assertEquals ( $permissions , $sport [ 'body' ][ '$permissions' ]);
$this -> assertEquals ( $permissions , $sport [ 'body' ][ 'players' ][ 0 ][ '$permissions' ]);
$this -> assertEquals ( $permissions , $sport [ 'body' ][ 'players' ][ 1 ][ '$permissions' ]);
2022-06-21 10:24:51 +12:00
2023-03-24 18:56:49 +13:00
$sport = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $sports [ 'body' ][ '$id' ] . '/documents/sport1' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
2022-06-21 10:24:51 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $sport [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'sport1' , $sport [ 'body' ][ '$id' ]);
$this -> assertEquals ( 'Sport 1' , $sport [ 'body' ][ 'name' ]);
$this -> assertEquals ( 'Player 1' , $sport [ 'body' ][ 'players' ][ 0 ][ 'name' ]);
$this -> assertEquals ( 'Player 2' , $sport [ 'body' ][ 'players' ][ 1 ][ 'name' ]);
$this -> assertEquals ( $permissions , $sport [ 'body' ][ '$permissions' ]);
$this -> assertEquals ( $permissions , $sport [ 'body' ][ 'players' ][ 0 ][ '$permissions' ]);
$this -> assertEquals ( $permissions , $sport [ 'body' ][ 'players' ][ 1 ][ '$permissions' ]);
$player = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $databaseId . '/collections/' . $players [ 'body' ][ '$id' ] . '/documents/' . $sport [ 'body' ][ 'players' ][ 0 ][ '$id' ], array_merge ([
2022-06-21 10:24:51 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()));
2022-06-21 10:24:51 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $player [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'Player 1' , $player [ 'body' ][ 'name' ]);
$this -> assertEquals ( $permissions , $player [ 'body' ][ '$permissions' ]);
$this -> assertEquals ( 1 , count ( $player [ 'body' ][ 'sports' ]));
$this -> assertEquals ( 'sport1' , $player [ 'body' ][ 'sports' ][ 0 ][ '$id' ]);
$this -> assertEquals ( 'Sport 1' , $player [ 'body' ][ 'sports' ][ 0 ][ 'name' ]);
$this -> assertEquals ( $permissions , $player [ 'body' ][ 'sports' ][ 0 ][ '$permissions' ]);
2022-06-21 10:24:51 +12:00
2023-03-24 18:56:49 +13:00
return [
'databaseId' => $databaseId ,
'sportsCollection' => $sports [ 'body' ][ '$id' ],
'playersCollection' => $players [ 'body' ][ '$id' ],
];
}
/**
* @ depends testOneToManyRelationship
*/
public function testValidateOperators ( array $data ) : void
{
$response = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $data [ 'databaseId' ] . '/collections/' . $data [ 'personCollection' ] . '/documents' , array_merge ([
2022-06-23 11:38:56 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-03-24 18:56:49 +13:00
'queries' => [
2023-12-20 23:55:09 +13:00
Query :: isNotNull ( '$id' ) -> toString (),
Query :: startsWith ( 'fullName' , 'Stevie' ) -> toString (),
Query :: endsWith ( 'fullName' , 'Wonder' ) -> toString (),
Query :: between ( '$createdAt' , '1975-12-06' , '2050-12-0' ) -> toString (),
2022-08-02 21:18:49 +12:00
],
2022-06-23 11:38:56 +12:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $response [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 1 , count ( $response [ 'body' ][ 'documents' ]));
$this -> assertEquals ( 'person10' , $response [ 'body' ][ 'documents' ][ 0 ][ '$id' ]);
$this -> assertEquals ( 'Stevie Wonder' , $response [ 'body' ][ 'documents' ][ 0 ][ 'fullName' ]);
$this -> assertEquals ( 2 , count ( $response [ 'body' ][ 'documents' ][ 0 ][ 'libraries' ]));
2022-06-23 11:38:56 +12:00
2023-03-24 18:56:49 +13:00
$response = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $data [ 'databaseId' ] . '/collections/' . $data [ 'personCollection' ] . '/documents' , array_merge ([
2022-06-23 11:38:56 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
'queries' => [
2023-12-20 23:55:09 +13:00
Query :: isNotNull ( '$id' ) -> toString (),
Query :: isNull ( 'fullName' ) -> toString (),
Query :: select ([ 'fullName' ]) -> toString (),
2023-03-24 18:56:49 +13:00
],
]);
2022-06-23 11:38:56 +12:00
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $response [ 'headers' ][ 'status-code' ]);
2023-04-13 15:59:57 +12:00
$this -> assertEquals ( 2 , count ( $response [ 'body' ][ 'documents' ]));
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( null , $response [ 'body' ][ 'documents' ][ 0 ][ 'fullName' ]);
$this -> assertArrayNotHasKey ( " libraries " , $response [ 'body' ][ 'documents' ][ 0 ]);
2024-02-26 03:22:12 +13:00
$this -> assertArrayNotHasKey ( '$databaseId' , $response [ 'body' ][ 'documents' ][ 0 ]);
$this -> assertArrayNotHasKey ( '$collectionId' , $response [ 'body' ][ 'documents' ][ 0 ]);
2022-06-21 10:24:51 +12:00
}
2022-09-30 22:27:19 +13:00
/**
2023-03-24 18:56:49 +13:00
* @ depends testOneToManyRelationship
2022-09-30 22:27:19 +13:00
*/
2024-04-23 15:20:25 +12:00
public function testSelectQueries ( array $data ) : void
2022-09-30 22:27:19 +13:00
{
2023-03-24 18:56:49 +13:00
$response = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $data [ 'databaseId' ] . '/collections/' . $data [ 'personCollection' ] . '/documents' , array_merge ([
2022-09-30 22:27:19 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
'queries' => [
2023-12-20 23:55:09 +13:00
Query :: equal ( 'fullName' , [ 'Stevie Wonder' ]) -> toString (),
Query :: select ([ 'fullName' ]) -> toString (),
2023-03-24 18:56:49 +13:00
],
2022-09-30 22:27:19 +13:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $response [ 'headers' ][ 'status-code' ]);
$this -> assertArrayNotHasKey ( 'libraries' , $response [ 'body' ][ 'documents' ][ 0 ]);
2024-02-26 03:22:12 +13:00
$this -> assertArrayNotHasKey ( '$databaseId' , $response [ 'body' ][ 'documents' ][ 0 ]);
$this -> assertArrayNotHasKey ( '$collectionId' , $response [ 'body' ][ 'documents' ][ 0 ]);
2022-09-30 22:27:19 +13:00
2023-03-24 18:56:49 +13:00
$response = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $data [ 'databaseId' ] . '/collections/' . $data [ 'personCollection' ] . '/documents' , array_merge ([
2022-09-30 22:27:19 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
'queries' => [
2023-12-20 23:55:09 +13:00
Query :: select ([ 'libraries.*' , '$id' ]) -> toString (),
2023-03-24 18:56:49 +13:00
],
2022-09-30 22:27:19 +13:00
]);
2023-03-24 18:56:49 +13:00
$document = $response [ 'body' ][ 'documents' ][ 0 ];
$this -> assertEquals ( 200 , $response [ 'headers' ][ 'status-code' ]);
$this -> assertArrayHasKey ( 'libraries' , $document );
2024-02-26 03:22:12 +13:00
$this -> assertArrayNotHasKey ( '$databaseId' , $document );
$this -> assertArrayNotHasKey ( '$collectionId' , $document );
2022-09-30 22:27:19 +13:00
2023-03-24 18:56:49 +13:00
$response = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $data [ 'databaseId' ] . '/collections/' . $data [ 'personCollection' ] . '/documents/' . $document [ '$id' ], array_merge ([
2022-09-30 22:27:19 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-03-24 18:56:49 +13:00
], $this -> getHeaders ()), [
'queries' => [
2023-12-20 23:55:09 +13:00
Query :: select ([ 'fullName' , '$id' ]) -> toString (),
2023-03-24 18:56:49 +13:00
],
2022-09-30 22:27:19 +13:00
]);
2023-03-24 18:56:49 +13:00
$this -> assertEquals ( 200 , $response [ 'headers' ][ 'status-code' ]);
$this -> assertArrayHasKey ( 'fullName' , $response [ 'body' ]);
$this -> assertArrayNotHasKey ( 'libraries' , $response [ 'body' ]);
2022-09-30 22:27:19 +13:00
}
2023-05-02 20:27:09 +12:00
2024-04-23 15:20:25 +12:00
/**
* @ throws \Utopia\Database\Exception
* @ throws \Utopia\Database\Exception\Query
*/
public function testOrQueries () : void
{
// Create database
$database = $this -> client -> call ( Client :: METHOD_POST , '/databases' , [
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
], [
'databaseId' => ID :: unique (),
'name' => 'Or queries'
]);
$this -> assertNotEmpty ( $database [ 'body' ][ '$id' ]);
$this -> assertEquals ( 201 , $database [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'Or queries' , $database [ 'body' ][ 'name' ]);
$databaseId = $database [ 'body' ][ '$id' ];
// Create Collection
$presidents = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'collectionId' => ID :: unique (),
'name' => 'USA Presidents' ,
'documentSecurity' => true ,
'permissions' => [
Permission :: create ( Role :: user ( $this -> getUser ()[ '$id' ])),
],
]);
$this -> assertEquals ( 201 , $presidents [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $presidents [ 'body' ][ 'name' ], 'USA Presidents' );
// Create Attributes
$firstName = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $presidents [ 'body' ][ '$id' ] . '/attributes/string' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'first_name' ,
'size' => 256 ,
'required' => true ,
]);
$this -> assertEquals ( 202 , $firstName [ 'headers' ][ 'status-code' ]);
$lastName = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $presidents [ 'body' ][ '$id' ] . '/attributes/string' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'last_name' ,
'size' => 256 ,
'required' => true ,
]);
$this -> assertEquals ( 202 , $lastName [ 'headers' ][ 'status-code' ]);
// Wait for worker
sleep ( 2 );
$document1 = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $presidents [ 'body' ][ '$id' ] . '/documents' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'documentId' => ID :: unique (),
'data' => [
'first_name' => 'Donald' ,
'last_name' => 'Trump' ,
],
'permissions' => [
Permission :: read ( Role :: user ( $this -> getUser ()[ '$id' ])),
]
]);
$this -> assertEquals ( 201 , $document1 [ 'headers' ][ 'status-code' ]);
$document2 = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $presidents [ 'body' ][ '$id' ] . '/documents' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'documentId' => ID :: unique (),
'data' => [
'first_name' => 'George' ,
'last_name' => 'Bush' ,
],
'permissions' => [
Permission :: read ( Role :: user ( $this -> getUser ()[ '$id' ])),
]
]);
$this -> assertEquals ( 201 , $document2 [ 'headers' ][ 'status-code' ]);
$document3 = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $presidents [ 'body' ][ '$id' ] . '/documents' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'documentId' => ID :: unique (),
'data' => [
'first_name' => 'Joe' ,
'last_name' => 'Biden' ,
],
'permissions' => [
Permission :: read ( Role :: user ( $this -> getUser ()[ '$id' ])),
]
]);
$this -> assertEquals ( 201 , $document3 [ 'headers' ][ 'status-code' ]);
$documents = $this -> client -> call (
Client :: METHOD_GET ,
'/databases/' . $databaseId . '/collections/' . $presidents [ 'body' ][ '$id' ] . '/documents' ,
array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()),
[
'queries' => [
Query :: select ([ 'first_name' , 'last_name' ]) -> toString (),
Query :: or ([
Query :: equal ( 'first_name' , [ 'Donald' ]),
Query :: equal ( 'last_name' , [ 'Bush' ])
]) -> toString (),
Query :: limit ( 999 ) -> toString (),
Query :: offset ( 0 ) -> toString ()
],
]
);
$this -> assertEquals ( 200 , $documents [ 'headers' ][ 'status-code' ]);
$this -> assertCount ( 2 , $documents [ 'body' ][ 'documents' ]);
}
2023-05-02 20:27:09 +12:00
/**
* @ depends testCreateDatabase
* @ param array $data
* @ return void
* @ throws \Exception
*/
public function testUpdateWithExistingRelationships ( array $data ) : void
{
$databaseId = $data [ 'databaseId' ];
$collection1 = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'collectionId' => ID :: unique (),
'name' => 'Collection1' ,
'documentSecurity' => true ,
'permissions' => [
Permission :: create ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: read ( Role :: user ( $this -> getUser ()[ '$id' ])),
],
]);
$collection2 = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'collectionId' => ID :: unique (),
'name' => 'Collection2' ,
'documentSecurity' => true ,
'permissions' => [
Permission :: create ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: read ( Role :: user ( $this -> getUser ()[ '$id' ])),
],
]);
$collection1 = $collection1 [ 'body' ][ '$id' ];
$collection2 = $collection2 [ 'body' ][ '$id' ];
$this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collection1 . '/attributes/string' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'name' ,
'size' => '49' ,
'required' => true ,
]);
$this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collection2 . '/attributes/string' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'name' ,
'size' => '49' ,
'required' => true ,
]);
$this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collection1 . '/attributes/relationship' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'relatedCollectionId' => $collection2 ,
'type' => Database :: RELATION_ONE_TO_MANY ,
'twoWay' => true ,
'key' => 'collection2'
]);
sleep ( 1 );
$document = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $collection1 . '/documents' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ]
], $this -> getHeaders ()), [
'documentId' => ID :: unique (),
'data' => [
'name' => 'Document 1' ,
'collection2' => [
[
'name' => 'Document 2' ,
],
],
],
]);
$update = $this -> client -> call ( Client :: METHOD_PATCH , '/databases/' . $databaseId . '/collections/' . $collection1 . '/documents/' . $document [ 'body' ][ '$id' ], array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ]
], $this -> getHeaders ()), [
'data' => [
'name' => 'Document 1 Updated' ,
],
]);
$this -> assertEquals ( 200 , $update [ 'headers' ][ 'status-code' ]);
}
2023-10-19 17:06:57 +13:00
/**
* @ depends testCreateDatabase
*/
public function testTimeout ( array $data ) : void
{
$collection = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $data [ 'databaseId' ] . '/collections' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'collectionId' => ID :: unique (),
'name' => 'Slow Queries' ,
'documentSecurity' => true ,
'permissions' => [
Permission :: create ( Role :: user ( $this -> getUser ()[ '$id' ])),
],
]);
$this -> assertEquals ( 201 , $collection [ 'headers' ][ 'status-code' ]);
$data = [
'$id' => $collection [ 'body' ][ '$id' ],
'databaseId' => $collection [ 'body' ][ 'databaseId' ]
];
$longtext = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $data [ 'databaseId' ] . '/collections/' . $data [ '$id' ] . '/attributes/string' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'longtext' ,
'size' => 100000000 ,
'required' => false ,
'default' => null ,
]);
$this -> assertEquals ( $longtext [ 'headers' ][ 'status-code' ], 202 );
for ( $i = 0 ; $i < 1 ; $i ++ ) {
$this -> client -> call ( Client :: METHOD_POST , '/databases/' . $data [ 'databaseId' ] . '/collections/' . $data [ '$id' ] . '/documents' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'documentId' => ID :: unique (),
'data' => [
'longtext' => file_get_contents ( 'tests/resources/longtext.txt' ),
],
'permissions' => [
Permission :: read ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: update ( Role :: user ( $this -> getUser ()[ '$id' ])),
Permission :: delete ( Role :: user ( $this -> getUser ()[ '$id' ])),
]
]);
}
$response = $this -> client -> call ( Client :: METHOD_GET , '/databases/' . $data [ 'databaseId' ] . '/collections/' . $data [ '$id' ] . '/documents' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-timeout' => 1 ,
], $this -> getHeaders ()), [
2023-12-20 23:55:09 +13:00
'queries' => [
Query :: notEqual ( 'longtext' , 'appwrite' ) -> toString (),
],
2023-10-19 17:06:57 +13:00
]);
$this -> assertEquals ( 408 , $response [ 'headers' ][ 'status-code' ]);
}
2022-05-24 02:54:50 +12:00
}