From 54b11df3d527dc6b020fc776d0c73939788b26c9 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Tue, 13 May 2025 16:37:14 +0530 Subject: [PATCH 1/3] updated index validator length test cases --- src/Database/Validator/Index.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Database/Validator/Index.php b/src/Database/Validator/Index.php index 2270a6e0f..8eaf33a6d 100644 --- a/src/Database/Validator/Index.php +++ b/src/Database/Validator/Index.php @@ -176,8 +176,12 @@ public function checkIndexLength(Document $index): bool $total = 0; $lengths = $index->getAttribute('lengths', []); - - foreach ($index->getAttribute('attributes', []) as $attributePosition => $attributeName) { + $attributes = $index->getAttribute('attributes', []); + if (count($lengths) > count($attributes)) { + $this->message = 'Invalid index lengths. Count of lengths must be equal or less than the number of attributes.'; + return false; + } + foreach ($attributes as $attributePosition => $attributeName) { $attribute = $this->attributes[\strtolower($attributeName)]; switch ($attribute->getAttribute('type')) { @@ -194,6 +198,10 @@ public function checkIndexLength(Document $index): bool $indexLength = 1; break; } + if ($indexLength < 0) { + $this->message = 'Negative index provided for '.$attribute->getAttribute('key'); + return false; + } if ($attribute->getAttribute('array', false)) { $attributeSize = Database::ARRAY_INDEX_LENGTH; From 6a197b503ea56b69911f351a4e4aedbe79c4e965 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Tue, 13 May 2025 16:48:41 +0530 Subject: [PATCH 2/3] added tests --- src/Database/Validator/Index.php | 2 +- tests/e2e/Adapter/Scopes/IndexTests.php | 43 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/Database/Validator/Index.php b/src/Database/Validator/Index.php index 8eaf33a6d..64e9b4032 100644 --- a/src/Database/Validator/Index.php +++ b/src/Database/Validator/Index.php @@ -199,7 +199,7 @@ public function checkIndexLength(Document $index): bool break; } if ($indexLength < 0) { - $this->message = 'Negative index provided for '.$attribute->getAttribute('key'); + $this->message = 'Negative index provided for '.$attributeName; return false; } diff --git a/tests/e2e/Adapter/Scopes/IndexTests.php b/tests/e2e/Adapter/Scopes/IndexTests.php index 6e0069d54..ae178dd42 100644 --- a/tests/e2e/Adapter/Scopes/IndexTests.php +++ b/tests/e2e/Adapter/Scopes/IndexTests.php @@ -243,6 +243,49 @@ public function testIndexValidation(): void } catch (Exception $e) { $this->assertEquals($errorMessage, $e->getMessage()); } + + + $indexes = [ + new Document([ + '$id' => ID::custom('index_negative_length'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['title1'], + 'lengths' => [-1], + 'orders' => [], + ]), + ]; + + $errorMessage = 'Negative index provided for title1'; + $this->assertFalse($validator->isValid($indexes[0])); + $this->assertEquals($errorMessage, $validator->getDescription()); + + try { + static::getDatabase()->createCollection(ID::unique(), $attributes, $indexes); + $this->fail('Failed to throw exception'); + } catch (Exception $e) { + $this->assertEquals($errorMessage, $e->getMessage()); + } + + $indexes = [ + new Document([ + '$id' => ID::custom('index_extra_lengths'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['title1', 'title2'], + 'lengths' => [100, 100, 100], + 'orders' => [], + ]), + ]; + + $errorMessage = 'Invalid index lengths. Count of lengths must be equal or less than the number of attributes.'; + $this->assertFalse($validator->isValid($indexes[0])); + $this->assertEquals($errorMessage, $validator->getDescription()); + + try { + static::getDatabase()->createCollection(ID::unique(), $attributes, $indexes); + $this->fail('Failed to throw exception'); + } catch (Exception $e) { + $this->assertEquals($errorMessage, $e->getMessage()); + } } public function testRenameIndex(): void From 86c8f175112eb7f3f78390102e1dce5ca3421650 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Tue, 13 May 2025 17:08:05 +0530 Subject: [PATCH 3/3] updated message --- src/Database/Validator/Index.php | 2 +- tests/e2e/Adapter/Scopes/IndexTests.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Database/Validator/Index.php b/src/Database/Validator/Index.php index 64e9b4032..5452471cb 100644 --- a/src/Database/Validator/Index.php +++ b/src/Database/Validator/Index.php @@ -199,7 +199,7 @@ public function checkIndexLength(Document $index): bool break; } if ($indexLength < 0) { - $this->message = 'Negative index provided for '.$attributeName; + $this->message = 'Negative index length provided for ' . $attributeName; return false; } diff --git a/tests/e2e/Adapter/Scopes/IndexTests.php b/tests/e2e/Adapter/Scopes/IndexTests.php index ae178dd42..6bb563379 100644 --- a/tests/e2e/Adapter/Scopes/IndexTests.php +++ b/tests/e2e/Adapter/Scopes/IndexTests.php @@ -255,7 +255,7 @@ public function testIndexValidation(): void ]), ]; - $errorMessage = 'Negative index provided for title1'; + $errorMessage = 'Negative index length provided for title1'; $this->assertFalse($validator->isValid($indexes[0])); $this->assertEquals($errorMessage, $validator->getDescription());