From 7d074edf5f82f6e05661a4767e66b65f7c7ca594 Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 4 Oct 2023 17:09:05 +0300 Subject: [PATCH 1/4] fetchAll getDocument --- src/Database/Adapter/SQL.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index 8525d2738..f5e296474 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -121,12 +121,14 @@ public function getDocument(string $collection, string $id, array $queries = []) $stmt->bindValue(':_uid', $id); $stmt->execute(); - $document = $stmt->fetch(); + $document = $stmt->fetchAll(); if (empty($document)) { return new Document([]); } + $document = $document[0]; + if (\array_key_exists('_id', $document)) { $document['$internalId'] = $document['_id']; unset($document['_id']); From d58ab21afa018d36235a87826dd36669fc3b0479 Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 4 Oct 2023 18:59:04 +0300 Subject: [PATCH 2/4] validator test --- src/Database/Validator/Index.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Database/Validator/Index.php b/src/Database/Validator/Index.php index 07c5b3688..a6a3a6082 100644 --- a/src/Database/Validator/Index.php +++ b/src/Database/Validator/Index.php @@ -158,6 +158,10 @@ public function checkIndexLength(Document $collection): bool public function isValid($value): bool { foreach ($value->getAttribute('attributes', []) as $attribute) { + + var_dump($attribute); + + $this->attributes[$attribute->getAttribute('key', $attribute->getAttribute('$id'))] = $attribute; } From 200e5f0bfc77a1df0d40de5e79a6f8d4bbc36dc7 Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 4 Oct 2023 19:50:47 +0300 Subject: [PATCH 3/4] checkAttributesNotFound --- src/Database/Validator/Index.php | 26 +++++++++++++++--- tests/Database/Validator/IndexTest.php | 38 ++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/src/Database/Validator/Index.php b/src/Database/Validator/Index.php index a6a3a6082..4ebc4f98a 100644 --- a/src/Database/Validator/Index.php +++ b/src/Database/Validator/Index.php @@ -34,6 +34,24 @@ public function getDescription(): string return $this->message; } + /** + * @param Document $collection + * @return bool + */ + public function checkAttributesNotFound(Document $collection): bool + { + foreach ($collection->getAttribute('indexes', []) as $index) { + foreach ($index->getAttribute('attributes', []) as $attributeName) { + if (!isset($this->attributes[$attributeName])) { + $this->message = 'Invalid index attribute "' . $attributeName . '" not found'; + return false; + } + } + } + + return true; + } + /** * @param Document $collection * @return bool @@ -158,10 +176,6 @@ public function checkIndexLength(Document $collection): bool public function isValid($value): bool { foreach ($value->getAttribute('attributes', []) as $attribute) { - - var_dump($attribute); - - $this->attributes[$attribute->getAttribute('key', $attribute->getAttribute('$id'))] = $attribute; } @@ -169,6 +183,10 @@ public function isValid($value): bool $this->attributes[$attribute->getAttribute('$id')] = $attribute; } + if (!$this->checkAttributesNotFound($value)) { + return false; + } + if (!$this->checkEmptyIndexAttributes($value)) { return false; } diff --git a/tests/Database/Validator/IndexTest.php b/tests/Database/Validator/IndexTest.php index 7d4d86ee6..4247ecd32 100644 --- a/tests/Database/Validator/IndexTest.php +++ b/tests/Database/Validator/IndexTest.php @@ -19,6 +19,44 @@ public function tearDown(): void { } + /** + * @throws Exception + */ + public function testAttributeNotFound(): void + { + $validator = new Index(768); + + $collection = new Document([ + '$id' => ID::custom('test'), + 'name' => 'test', + 'attributes' => [ + new Document([ + '$id' => ID::custom('title'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 255, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ]) + ], + 'indexes' => [ + new Document([ + '$id' => ID::custom('index1'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['not_exist'], + 'lengths' => [], + 'orders' => [], + ]), + ], + ]); + + $this->assertFalse($validator->isValid($collection)); + $this->assertEquals('Invalid index attribute "not_exist" not found', $validator->getDescription()); + } + /** * @throws Exception */ From 50e716c1522a290c4b2dd45b01aa4a7fcb5481c2 Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 4 Oct 2023 19:53:45 +0300 Subject: [PATCH 4/4] revert org branch changes --- src/Database/Adapter/SQL.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index f5e296474..8525d2738 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -121,14 +121,12 @@ public function getDocument(string $collection, string $id, array $queries = []) $stmt->bindValue(':_uid', $id); $stmt->execute(); - $document = $stmt->fetchAll(); + $document = $stmt->fetch(); if (empty($document)) { return new Document([]); } - $document = $document[0]; - if (\array_key_exists('_id', $document)) { $document['$internalId'] = $document['_id']; unset($document['_id']);