1
0
Fork 0
mirror of synced 2024-06-02 10:54:44 +12:00
appwrite/tests/e2e/Services/Database/DatabaseBase.php

1091 lines
49 KiB
PHP
Raw Normal View History

2020-01-13 21:46:09 +13:00
<?php
namespace Tests\E2E\Services\Database;
use Tests\E2E\Client;
trait DatabaseBase
{
public function testCreateCollection():array
{
/**
* Test for SUCCESS
*/
$movies = $this->client->call(Client::METHOD_POST, '/database/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']
]), [
2021-07-19 21:26:00 +12:00
'collectionId' => 'unique()',
'name' => 'Movies',
'read' => ['role:all'],
'write' => ['role:all'],
2021-08-23 02:06:59 +12:00
'permission' => 'document',
2020-01-13 21:46:09 +13:00
]);
$this->assertEquals($movies['headers']['status-code'], 201);
$this->assertEquals($movies['body']['name'], 'Movies');
2020-01-13 21:46:09 +13:00
return ['moviesId' => $movies['body']['$id']];
}
/**
* @depends testCreateCollection
*/
public function testCreateAttributes(array $data): array
{
2021-07-22 03:05:18 +12:00
$title = $this->client->call(Client::METHOD_POST, '/database/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-07-22 03:05:18 +12:00
'attributeId' => 'title',
'size' => 256,
'required' => true,
2020-01-13 21:46:09 +13:00
]);
2021-07-22 03:05:18 +12:00
$releaseYear = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/attributes/integer', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
2021-07-22 03:05:18 +12:00
'attributeId' => 'releaseYear',
'required' => true,
]);
2020-01-13 21:46:09 +13:00
2021-07-22 03:05:18 +12:00
$actors = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/attributes/string', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
2021-07-22 03:05:18 +12:00
'attributeId' => 'actors',
'size' => 256,
'required' => false,
'array' => true,
]);
2021-06-15 07:54:19 +12:00
$this->assertEquals($title['headers']['status-code'], 201);
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);
$this->assertEquals($releaseYear['headers']['status-code'], 201);
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);
$this->assertEquals($actors['headers']['status-code'], 201);
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);
// wait for database worker to create attributes
2021-08-23 16:06:53 +12:00
sleep(2);
$movies = $this->client->call(Client::METHOD_GET, '/database/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']['attributes']);
$this->assertCount(3, $movies['body']['attributes']);
2021-08-23 02:06:59 +12:00
$this->assertEquals($movies['body']['attributes'][0]['key'], $title['body']['key']);
$this->assertEquals($movies['body']['attributes'][1]['key'], $releaseYear['body']['key']);
$this->assertEquals($movies['body']['attributes'][2]['key'], $actors['body']['key']);
return $data;
2020-01-13 21:46:09 +13:00
}
/**
* @depends testCreateAttributes
2020-01-13 21:46:09 +13:00
*/
2021-06-15 07:54:49 +12:00
public function testCreateIndexes(array $data): array
{
$titleIndex = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/indexes', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
2021-08-09 11:51:18 +12:00
'indexId' => 'titleIndex',
2021-06-15 07:54:49 +12:00
'type' => 'fulltext',
'attributes' => ['title'],
]);
2021-08-23 04:36:26 +12:00
$this->assertEquals($titleIndex['headers']['status-code'], 201);
$this->assertEquals($titleIndex['body']['key'], 'titleIndex');
$this->assertEquals($titleIndex['body']['type'], 'fulltext');
$this->assertCount(1, $titleIndex['body']['attributes']);
$this->assertEquals($titleIndex['body']['attributes'][0], 'title');
// wait for database worker to create index
2021-08-23 16:06:53 +12:00
sleep(2);
2021-08-23 04:36:26 +12:00
$movies = $this->client->call(Client::METHOD_GET, '/database/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(1, $movies['body']['indexes']);
$this->assertEquals($movies['body']['indexes'][0]['key'], $titleIndex['body']['key']);
2021-06-15 07:54:49 +12:00
return $data;
}
/**
* @depends testCreateIndexes
*/
2020-01-13 21:46:09 +13:00
public function testCreateDocument(array $data):array
{
2020-01-31 06:22:58 +13:00
$document1 = $this->client->call(Client::METHOD_POST, '/database/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()), [
2021-07-19 21:26:00 +12:00
'documentId' => 'unique()',
2020-01-13 21:46:09 +13:00
'data' => [
'title' => 'Captain America',
2020-01-13 21:46:09 +13:00
'releaseYear' => 1944,
'actors' => [
'Chris Evans',
'Samuel Jackson',
2020-01-13 21:46:09 +13:00
]
2020-01-14 06:53:22 +13:00
],
2020-02-17 20:16:11 +13:00
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
2020-01-13 21:46:09 +13:00
]);
2020-01-31 06:22:58 +13:00
$document2 = $this->client->call(Client::METHOD_POST, '/database/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()), [
2021-07-19 21:26:00 +12:00
'documentId' => 'unique()',
2020-01-13 21:46:09 +13:00
'data' => [
'title' => 'Spider-Man: Far From Home',
2020-01-13 21:46:09 +13:00
'releaseYear' => 2019,
'actors' => [
'Tom Holland',
'Zendaya Maree Stoermer',
'Samuel Jackson',
2020-01-13 21:46:09 +13:00
]
2020-01-14 06:53:22 +13:00
],
2020-02-17 20:16:11 +13:00
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
2020-01-13 21:46:09 +13:00
]);
2020-01-31 06:22:58 +13:00
$document3 = $this->client->call(Client::METHOD_POST, '/database/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()), [
2021-07-19 21:26:00 +12:00
'documentId' => 'unique()',
2020-01-13 21:46:09 +13:00
'data' => [
'title' => 'Spider-Man: Homecoming',
2020-01-13 21:46:09 +13:00
'releaseYear' => 2017,
'actors' => [
'Tom Holland',
'Zendaya Maree Stoermer',
2020-01-13 21:46:09 +13:00
],
2020-01-14 06:53:22 +13:00
],
2020-02-17 20:16:11 +13:00
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
2020-01-13 21:46:09 +13:00
]);
2020-01-31 06:22:58 +13:00
$document4 = $this->client->call(Client::METHOD_POST, '/database/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()), [
2021-07-19 21:26:00 +12:00
'documentId' => 'unique()',
2020-01-13 21:46:09 +13:00
'data' => [
'releaseYear' => 2020, // Missing title, expect an 400 error
2020-01-14 06:53:22 +13:00
],
2020-02-17 20:16:11 +13:00
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
2020-01-13 21:46:09 +13:00
]);
$this->assertEquals($document1['headers']['status-code'], 201);
$this->assertEquals($document1['body']['title'], 'Captain America');
2020-01-13 21:46:09 +13:00
$this->assertEquals($document1['body']['releaseYear'], 1944);
$this->assertIsArray($document1['body']['$read']);
$this->assertIsArray($document1['body']['$write']);
$this->assertCount(1, $document1['body']['$read']);
$this->assertCount(1, $document1['body']['$write']);
2020-01-13 21:46:09 +13:00
$this->assertCount(2, $document1['body']['actors']);
$this->assertEquals($document1['body']['actors'][0], 'Chris Evans');
$this->assertEquals($document1['body']['actors'][1], 'Samuel Jackson');
2020-01-13 21:46:09 +13:00
$this->assertEquals($document2['headers']['status-code'], 201);
$this->assertEquals($document2['body']['title'], 'Spider-Man: Far From Home');
2020-01-13 21:46:09 +13:00
$this->assertEquals($document2['body']['releaseYear'], 2019);
$this->assertIsArray($document2['body']['$read']);
$this->assertIsArray($document2['body']['$write']);
$this->assertCount(1, $document2['body']['$read']);
$this->assertCount(1, $document2['body']['$write']);
2020-01-13 21:46:09 +13:00
$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');
2020-01-13 21:46:09 +13:00
$this->assertEquals($document3['headers']['status-code'], 201);
$this->assertEquals($document3['body']['title'], 'Spider-Man: Homecoming');
2020-01-13 21:46:09 +13:00
$this->assertEquals($document3['body']['releaseYear'], 2017);
$this->assertIsArray($document3['body']['$read']);
$this->assertIsArray($document3['body']['$write']);
$this->assertCount(1, $document3['body']['$read']);
$this->assertCount(1, $document3['body']['$write']);
2020-01-13 21:46:09 +13:00
$this->assertCount(2, $document3['body']['actors']);
2021-08-23 16:06:53 +12:00
$this->assertEquals($document3['body']['actors'][0], 'Tom Holland');
$this->assertEquals($document3['body']['actors'][1], 'Zendaya Maree Stoermer');
2020-01-13 21:46:09 +13:00
$this->assertEquals($document4['headers']['status-code'], 400);
return $data;
}
/**
* @depends testCreateDocument
*/
public function testListDocuments(array $data):array
{
2020-01-31 06:22:58 +13:00
$documents = $this->client->call(Client::METHOD_GET, '/database/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()), [
2021-06-15 07:55:36 +12:00
'orderAttributes' => ['releaseYear'],
'orderTypes' => ['ASC'],
2020-01-13 21:46:09 +13:00
]);
2021-08-23 16:06:53 +12:00
$this->assertEquals($documents['headers']['status-code'], 200);
2020-01-13 21:46:09 +13:00
$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->assertCount(3, $documents['body']['documents']);
2020-01-31 06:22:58 +13:00
$documents = $this->client->call(Client::METHOD_GET, '/database/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()), [
2021-06-15 07:55:36 +12:00
'orderAttributes' => ['releaseYear'],
'orderTypes' => ['DESC'],
2020-01-13 21:46:09 +13:00
]);
2021-08-23 16:06:53 +12:00
$this->assertEquals($documents['headers']['status-code'], 200);
2020-01-13 21:46:09 +13:00
$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 [];
}
2021-08-06 07:01:00 +12:00
/**
* @depends testCreateDocument
*/
public function testListDocumentsAfterPagination(array $data):array
{
/**
* Test after without order.
*/
$base = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
2021-08-23 16:06:53 +12:00
$this->assertEquals($base['headers']['status-code'], 200);
2021-08-06 07:01:00 +12:00
$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, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'after' => $base['body']['documents'][0]['$id']
2021-08-06 07:01:00 +12:00
]);
2021-08-23 16:06:53 +12:00
$this->assertEquals($documents['headers']['status-code'], 200);
2021-08-06 07:01:00 +12:00
$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, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'after' => $base['body']['documents'][2]['$id']
2021-08-06 07:01:00 +12:00
]);
2021-08-23 16:06:53 +12:00
$this->assertEquals($documents['headers']['status-code'], 200);
2021-08-06 07:01:00 +12:00
$this->assertEmpty($documents['body']['documents']);
/**
* Test with ASC order and after.
*/
$base = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'orderAttributes' => ['releaseYear'],
'orderTypes' => ['ASC'],
]);
2021-08-23 16:06:53 +12:00
$this->assertEquals($base['headers']['status-code'], 200);
2021-08-06 07:01:00 +12:00
$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, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'orderAttributes' => ['releaseYear'],
'orderTypes' => ['ASC'],
'after' => $base['body']['documents'][1]['$id']
2021-08-06 07:01:00 +12:00
]);
2021-08-23 16:06:53 +12:00
$this->assertEquals($documents['headers']['status-code'], 200);
2021-08-06 07:01:00 +12:00
$this->assertEquals($base['body']['documents'][2]['$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, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'orderAttributes' => ['releaseYear'],
'orderTypes' => ['DESC'],
]);
2021-08-23 16:06:53 +12:00
$this->assertEquals($base['headers']['status-code'], 200);
2021-08-06 07:01:00 +12:00
$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, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'orderAttributes' => ['releaseYear'],
'orderTypes' => ['DESC'],
'after' => $base['body']['documents'][1]['$id']
2021-08-06 07:01:00 +12:00
]);
2021-08-23 16:06:53 +12:00
$this->assertEquals($documents['headers']['status-code'], 200);
2021-08-06 07:01:00 +12:00
$this->assertEquals($base['body']['documents'][2]['$id'], $documents['body']['documents'][0]['$id']);
$this->assertCount(1, $documents['body']['documents']);
/**
* Test after with unknown document.
*/
$documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'after' => 'unknown'
2021-08-06 07:01:00 +12:00
]);
$this->assertEquals($documents['headers']['status-code'], 400);
return [];
}
2020-01-13 21:46:09 +13:00
/**
* @depends testCreateDocument
*/
public function testListDocumentsLimitAndOffset(array $data):array
{
2020-01-31 06:22:58 +13:00
$documents = $this->client->call(Client::METHOD_GET, '/database/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()), [
'limit' => 1,
2021-06-15 07:55:36 +12:00
'orderAttributes' => ['releaseYear'],
'orderTypes' => ['ASC'],
2020-01-13 21:46:09 +13:00
]);
2021-08-23 16:06:53 +12:00
$this->assertEquals($documents['headers']['status-code'], 200);
2020-01-13 21:46:09 +13:00
$this->assertEquals(1944, $documents['body']['documents'][0]['releaseYear']);
$this->assertCount(1, $documents['body']['documents']);
2020-01-31 06:22:58 +13:00
$documents = $this->client->call(Client::METHOD_GET, '/database/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()), [
'limit' => 2,
'offset' => 1,
2021-06-15 07:55:36 +12:00
'orderAttributes' => ['releaseYear'],
'orderTypes' => ['ASC'],
2020-01-13 21:46:09 +13:00
]);
2021-08-23 16:06:53 +12:00
$this->assertEquals($documents['headers']['status-code'], 200);
2020-01-13 21:46:09 +13:00
$this->assertEquals(2017, $documents['body']['documents'][0]['releaseYear']);
$this->assertEquals(2019, $documents['body']['documents'][1]['releaseYear']);
$this->assertCount(2, $documents['body']['documents']);
return [];
}
/**
* @depends testCreateDocument
*/
2021-08-23 19:27:09 +12:00
// public function testDocumentsListSuccessSearch(array $data):array
// {
// $documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
// 'content-type' => 'application/json',
// 'x-appwrite-project' => $this->getProject()['$id'],
// ], $this->getHeaders()), [
// 'queries' => ['title.search("Captain America")'],
// ]);
2020-01-13 21:46:09 +13:00
2021-08-23 19:27:09 +12:00
// var_dump($documents);
2020-01-13 21:46:09 +13:00
2021-08-23 19:27:09 +12:00
// $this->assertEquals($documents['headers']['status-code'], 200);
// $this->assertEquals(1944, $documents['body']['documents'][0]['releaseYear']);
// $this->assertCount(1, $documents['body']['documents']);
2020-01-13 21:46:09 +13:00
2021-08-23 19:27:09 +12:00
// $documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
// 'content-type' => 'application/json',
// 'x-appwrite-project' => $this->getProject()['$id'],
// ], $this->getHeaders()), [
// 'queries' => ['title.search("Homecoming")'],
// ]);
2020-01-13 21:46:09 +13:00
2021-08-23 19:27:09 +12:00
// $this->assertEquals($documents['headers']['status-code'], 200);
// $this->assertEquals(2017, $documents['body']['documents'][0]['releaseYear']);
// $this->assertCount(1, $documents['body']['documents']);
2020-01-13 21:46:09 +13:00
2021-08-23 19:27:09 +12:00
// $documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
// 'content-type' => 'application/json',
// 'x-appwrite-project' => $this->getProject()['$id'],
// ], $this->getHeaders()), [
// 'queries' => ['title.search("spider")'],
// ]);
// $this->assertEquals($documents['headers']['status-code'], 200);
// $this->assertEquals(2019, $documents['body']['documents'][0]['releaseYear']);
// $this->assertEquals(2017, $documents['body']['documents'][1]['releaseYear']);
// $this->assertCount(2, $documents['body']['documents']);
// return [];
// }
2021-06-15 07:55:36 +12:00
// TODO@kodumbeats test for empty searches and misformatted queries
2020-01-13 21:46:09 +13:00
/**
* @depends testCreateDocument
*/
2021-06-15 07:55:36 +12:00
// public function testListDocumentsFilters(array $data):array
// {
// $documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
// 'content-type' => 'application/json',
// 'x-appwrite-project' => $this->getProject()['$id'],
// ], $this->getHeaders()), [
// 'filters' => [
// 'actors.firstName=Tom'
// ],
// ]);
// $this->assertCount(2, $documents['body']['documents']);
// $this->assertEquals('Spider-Man: Far From Home', $documents['body']['documents'][0]['name']);
// $this->assertEquals('Spider-Man: Homecoming', $documents['body']['documents'][1]['name']);
// $documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
// 'content-type' => 'application/json',
// 'x-appwrite-project' => $this->getProject()['$id'],
// ], $this->getHeaders()), [
// 'filters' => [
// 'releaseYear=1944'
// ],
// ]);
// $this->assertCount(1, $documents['body']['documents']);
// $this->assertEquals('Captain America', $documents['body']['documents'][0]['name']);
// $documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
// 'content-type' => 'application/json',
// 'x-appwrite-project' => $this->getProject()['$id'],
// ], $this->getHeaders()), [
// 'filters' => [
// 'releaseYear!=1944'
// ],
// ]);
// $this->assertCount(2, $documents['body']['documents']);
// $this->assertEquals('Spider-Man: Far From Home', $documents['body']['documents'][0]['name']);
// $this->assertEquals('Spider-Man: Homecoming', $documents['body']['documents'][1]['name']);
// return [];
// }
2020-01-13 21:46:09 +13:00
/**
* @depends testCreateDocument
*/
public function testUpdateDocument(array $data):array
{
2020-01-31 06:22:58 +13:00
$document = $this->client->call(Client::METHOD_POST, '/database/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()), [
2021-07-19 21:26:00 +12:00
'documentId' => 'unique()',
2020-01-13 21:46:09 +13:00
'data' => [
'title' => 'Thor: Ragnaroc',
2020-01-13 21:46:09 +13:00
'releaseYear' => 2017,
'actors' => [],
2020-01-14 06:53:22 +13:00
],
2021-07-06 08:27:20 +12:00
'read' => ['user:'.$this->getUser()['$id'], 'user:testx'],
'write' => ['user:'.$this->getUser()['$id'], 'user:testy'],
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
$this->assertEquals($document['headers']['status-code'], 201);
$this->assertEquals($document['body']['title'], 'Thor: Ragnaroc');
2020-01-13 21:46:09 +13:00
$this->assertEquals($document['body']['releaseYear'], 2017);
2021-07-06 08:27:20 +12:00
$this->assertEquals($document['body']['$read'][1], 'user:testx');
$this->assertEquals($document['body']['$write'][1], 'user:testy');
2020-01-13 21:46:09 +13:00
$document = $this->client->call(Client::METHOD_PATCH, '/database/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()), [
'data' => [
'title' => 'Thor: Ragnarok',
2021-03-16 00:44:11 +13:00
],
2020-01-13 21:46:09 +13:00
]);
$this->assertEquals($document['headers']['status-code'], 200);
$this->assertEquals($document['body']['title'], 'Thor: Ragnarok');
2020-01-13 21:46:09 +13:00
$this->assertEquals($document['body']['releaseYear'], 2017);
$document = $this->client->call(Client::METHOD_GET, '/database/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()));
2020-02-17 20:16:11 +13:00
$id = $document['body']['$id'];
2020-01-13 21:46:09 +13:00
$this->assertEquals($document['headers']['status-code'], 200);
$this->assertEquals($document['body']['title'], 'Thor: Ragnarok');
2020-01-13 21:46:09 +13:00
$this->assertEquals($document['body']['releaseYear'], 2017);
return [];
}
/**
* @depends testCreateDocument
*/
public function testDeleteDocument(array $data):array
{
2020-01-31 06:22:58 +13:00
$document = $this->client->call(Client::METHOD_POST, '/database/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()), [
2021-07-19 21:26:00 +12:00
'documentId' => 'unique()',
2020-01-13 21:46:09 +13:00
'data' => [
'title' => 'Thor: Ragnarok',
2020-01-13 21:46:09 +13:00
'releaseYear' => 2017,
'actors' => [],
2020-01-14 06:53:22 +13:00
],
2020-02-17 20:16:11 +13:00
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
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
$this->assertEquals($document['headers']['status-code'], 201);
$document = $this->client->call(Client::METHOD_GET, '/database/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()));
$this->assertEquals($document['headers']['status-code'], 200);
$document = $this->client->call(Client::METHOD_DELETE, '/database/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()));
$this->assertEquals($document['headers']['status-code'], 204);
$document = $this->client->call(Client::METHOD_GET, '/database/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()));
$this->assertEquals($document['headers']['status-code'], 404);
2021-03-22 20:34:51 +13:00
return $data;
}
public function testInvalidDocumentStructure()
{
$collection = $this->client->call(Client::METHOD_POST, '/database/collections', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
2021-08-05 16:29:43 +12:00
'collectionId' => 'unique()',
'name' => 'invalidDocumentStructure',
'read' => ['role:all'],
'write' => ['role:all'],
2021-08-23 02:06:59 +12:00
'permission' => 'document',
]);
2021-07-24 03:04:31 +12:00
$this->assertEquals(201, $collection['headers']['status-code']);
$this->assertEquals('invalidDocumentStructure', $collection['body']['name']);
$collectionId = $collection['body']['$id'];
2021-07-27 13:00:36 +12:00
$email = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/email', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => 'email',
'required' => false,
]);
2021-07-27 13:00:36 +12:00
$ip = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/ip', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => 'ip',
'required' => false,
]);
2021-07-27 13:00:36 +12:00
$url = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/url', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => 'url',
'size' => 256,
'required' => false,
]);
$range = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/integer', array_merge([
2021-07-27 13:00:36 +12:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => 'range',
'required' => false,
'min' => 1,
'max' => 10,
]);
2021-07-28 06:19:37 +12:00
// TODO@kodumbeats min and max are rounded in error message
$floatRange = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/float', array_merge([
2021-07-27 13:00:36 +12:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => 'floatRange',
'required' => false,
2021-07-28 07:12:24 +12:00
'min' => 1.1,
'max' => 1.4,
]);
2021-07-28 07:12:24 +12:00
// TODO@kodumbeats float validator rejects 0.0 and 1.0 as floats
// $probability = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/float', array_merge([
// 'content-type' => 'application/json',
// 'x-appwrite-project' => $this->getProject()['$id'],
// 'x-appwrite-key' => $this->getProject()['apiKey']
// ]), [
// 'attributeId' => 'probability',
// 'required' => false,
// 'min' => \floatval(0.0),
// 'max' => \floatval(1.0),
// ]);
$upperBound = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/integer', array_merge([
2021-07-27 13:00:36 +12:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => 'upperBound',
'required' => false,
'max' => 10,
]);
$lowerBound = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/integer', array_merge([
2021-07-27 13:00:36 +12:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => 'lowerBound',
'required' => false,
'min' => 5,
]);
/**
* Test for failure
*/
// TODO@kodumbeats troubleshoot
// $invalidRange = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/integer', array_merge([
// 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'],
// 'x-appwrite-key' => $this->getProject()['apiKey']
// ]), [
// 'attributeId' => 'invalidRange',
// 'required' => false,
// 'min' => 4,
// 'max' => 3,
// ]);
2021-07-24 03:04:31 +12:00
$this->assertEquals(201, $email['headers']['status-code']);
$this->assertEquals(201, $ip['headers']['status-code']);
$this->assertEquals(201, $url['headers']['status-code']);
$this->assertEquals(201, $range['headers']['status-code']);
$this->assertEquals(201, $floatRange['headers']['status-code']);
$this->assertEquals(201, $upperBound['headers']['status-code']);
$this->assertEquals(201, $lowerBound['headers']['status-code']);
// $this->assertEquals(400, $invalidRange['headers']['status-code']);
// $this->assertEquals('Minimum value must be lesser than maximum value', $invalidRange['body']['message']);
// wait for worker to add attributes
2021-08-23 16:06:53 +12:00
sleep(2);
$collection = $this->client->call(Client::METHOD_GET, '/database/collections/' . $collectionId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), []);
$this->assertCount(7, $collection['body']['attributes']);
/**
* Test for successful validation
*/
$goodEmail = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2021-08-05 16:29:43 +12:00
'documentId' => 'unique()',
'data' => [
'email' => 'user@example.com',
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$goodIp = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2021-08-05 21:24:42 +12:00
'documentId' => 'unique()',
'data' => [
'ip' => '1.1.1.1',
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$goodUrl = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2021-08-05 16:29:43 +12:00
'documentId' => 'unique()',
'data' => [
'url' => 'http://www.example.com',
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$goodRange = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2021-08-05 16:29:43 +12:00
'documentId' => 'unique()',
'data' => [
'range' => 3,
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$goodFloatRange = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2021-08-05 21:24:42 +12:00
'documentId' => 'unique()',
'data' => [
2021-07-28 07:12:24 +12:00
'floatRange' => 1.4,
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$notTooHigh = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2021-08-05 16:29:43 +12:00
'documentId' => 'unique()',
'data' => [
'upperBound' => 8,
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$notTooLow = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2021-08-05 20:09:06 +12:00
'documentId' => 'unique()',
'data' => [
'lowerBound' => 8,
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$this->assertEquals(201, $goodEmail['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, $notTooHigh['headers']['status-code']);
$this->assertEquals(201, $notTooLow['headers']['status-code']);
/*
* Test that custom validators reject documents
*/
$badEmail = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2021-08-05 16:29:43 +12:00
'documentId' => 'unique()',
'data' => [
'email' => 'user@@example.com',
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$badIp = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2021-08-05 16:29:43 +12:00
'documentId' => 'unique()',
'data' => [
'ip' => '1.1.1.1.1',
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$badUrl = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2021-08-05 16:29:43 +12:00
'documentId' => 'unique()',
'data' => [
'url' => 'example...com',
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$badRange = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2021-08-05 16:29:43 +12:00
'documentId' => 'unique()',
'data' => [
'range' => 11,
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
2021-07-28 07:12:24 +12:00
$badFloatRange = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2021-08-05 16:29:43 +12:00
'documentId' => 'unique()',
'data' => [
'floatRange' => 2.5,
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$tooHigh = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2021-08-05 16:29:43 +12:00
'documentId' => 'unique()',
'data' => [
'upperBound' => 11,
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$tooLow = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2021-08-05 16:29:43 +12:00
'documentId' => 'unique()',
'data' => [
'lowerBound' => 3,
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$this->assertEquals(400, $badEmail['headers']['status-code']);
$this->assertEquals(400, $badIp['headers']['status-code']);
$this->assertEquals(400, $badUrl['headers']['status-code']);
$this->assertEquals(400, $badRange['headers']['status-code']);
2021-07-28 07:12:24 +12:00
$this->assertEquals(400, $badFloatRange['headers']['status-code']);
$this->assertEquals(400, $tooHigh['headers']['status-code']);
$this->assertEquals(400, $tooLow['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 "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']);
2021-07-28 07:12:24 +12:00
$this->assertEquals('Invalid document structure: Attribute "floatRange" has invalid format. Value must be a valid range between 1 and 1', $badFloatRange['body']['message']);
2021-08-23 02:06:59 +12:00
$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']);
$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,808', $tooLow['body']['message']);
}
2021-03-22 20:34:51 +13:00
/**
* @depends testDeleteDocument
*/
public function testDefaultPermissions(array $data):array
{
$document = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2021-07-19 21:26:00 +12:00
'documentId' => 'unique()',
2021-03-22 20:34:51 +13:00
'data' => [
2021-06-16 02:24:51 +12:00
'title' => 'Captain America',
2021-03-22 20:34:51 +13:00
'releaseYear' => 1944,
'actors' => [],
],
]);
$id = $document['body']['$id'];
$this->assertEquals($document['headers']['status-code'], 201);
2021-06-16 02:24:51 +12:00
$this->assertEquals($document['body']['title'], 'Captain America');
2021-03-22 20:34:51 +13:00
$this->assertEquals($document['body']['releaseYear'], 1944);
2021-06-16 02:24:51 +12:00
$this->assertIsArray($document['body']['$read']);
$this->assertIsArray($document['body']['$write']);
2021-03-22 20:34:51 +13:00
if($this->getSide() == 'client') {
2021-06-16 02:24:51 +12:00
$this->assertCount(1, $document['body']['$read']);
$this->assertCount(1, $document['body']['$write']);
$this->assertEquals(['user:'.$this->getUser()['$id']], $document['body']['$read']);
$this->assertEquals(['user:'.$this->getUser()['$id']], $document['body']['$write']);
2021-03-22 20:34:51 +13:00
}
if($this->getSide() == 'server') {
2021-06-16 02:24:51 +12:00
$this->assertCount(0, $document['body']['$read']);
$this->assertCount(0, $document['body']['$write']);
$this->assertEquals([], $document['body']['$read']);
$this->assertEquals([], $document['body']['$write']);
2021-03-22 20:34:51 +13:00
}
// Updated and Inherit Permissions
$document = $this->client->call(Client::METHOD_PATCH, '/database/collections/' . $data['moviesId'] . '/documents/' . $id, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
2021-06-16 02:24:51 +12:00
'title' => 'Captain America 2',
2021-03-22 20:34:51 +13:00
'releaseYear' => 1945,
'actors' => [],
],
2021-06-12 06:23:16 +12:00
'read' => ['role:all'],
2021-03-22 20:34:51 +13:00
]);
$this->assertEquals($document['headers']['status-code'], 200);
2021-06-16 02:24:51 +12:00
$this->assertEquals($document['body']['title'], 'Captain America 2');
2021-03-22 20:34:51 +13:00
$this->assertEquals($document['body']['releaseYear'], 1945);
if($this->getSide() == 'client') {
2021-06-16 02:24:51 +12:00
$this->assertCount(1, $document['body']['$read']);
$this->assertCount(1, $document['body']['$write']);
$this->assertEquals(['role:all'], $document['body']['$read']);
$this->assertEquals(['user:'.$this->getUser()['$id']], $document['body']['$write']);
2021-03-22 20:34:51 +13:00
}
if($this->getSide() == 'server') {
2021-06-16 02:24:51 +12:00
$this->assertCount(1, $document['body']['$read']);
$this->assertCount(0, $document['body']['$write']);
$this->assertEquals(['role:all'], $document['body']['$read']);
$this->assertEquals([], $document['body']['$write']);
2021-03-22 20:34:51 +13:00
}
$document = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents/' . $id, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals($document['headers']['status-code'], 200);
2021-06-16 02:24:51 +12:00
$this->assertEquals($document['body']['title'], 'Captain America 2');
2021-03-22 20:34:51 +13:00
$this->assertEquals($document['body']['releaseYear'], 1945);
if($this->getSide() == 'client') {
2021-06-16 02:24:51 +12:00
$this->assertCount(1, $document['body']['$read']);
$this->assertCount(1, $document['body']['$write']);
$this->assertEquals(['role:all'], $document['body']['$read']);
$this->assertEquals(['user:'.$this->getUser()['$id']], $document['body']['$write']);
2021-03-22 20:34:51 +13:00
}
if($this->getSide() == 'server') {
2021-06-16 02:24:51 +12:00
$this->assertCount(1, $document['body']['$read']);
$this->assertCount(0, $document['body']['$write']);
$this->assertEquals(['role:all'], $document['body']['$read']);
$this->assertEquals([], $document['body']['$write']);
2021-03-22 20:34:51 +13:00
}
// Reset Permissions
$document = $this->client->call(Client::METHOD_PATCH, '/database/collections/' . $data['moviesId'] . '/documents/' . $id, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
2021-06-16 02:24:51 +12:00
'title' => 'Captain America 3',
2021-03-22 20:34:51 +13:00
'releaseYear' => 1946,
'actors' => [],
],
'read' => [],
'write' => [],
]);
if($this->getSide() == 'client') {
$this->assertEquals($document['headers']['status-code'], 401);
}
if($this->getSide() == 'server') {
$this->assertEquals($document['headers']['status-code'], 200);
2021-06-16 02:24:51 +12:00
$this->assertEquals($document['body']['title'], 'Captain America 3');
2021-03-22 20:34:51 +13:00
$this->assertEquals($document['body']['releaseYear'], 1946);
2021-06-16 02:24:51 +12:00
$this->assertCount(0, $document['body']['$read']);
$this->assertCount(0, $document['body']['$write']);
$this->assertEquals([], $document['body']['$read']);
$this->assertEquals([], $document['body']['$write']);
2021-03-22 20:34:51 +13:00
}
return $data;
2020-01-13 21:46:09 +13:00
}
}