diff --git a/docroot/modules/custom/va_gov_form_builder/src/Service/FieldValidator/UniquenessValidator.php b/docroot/modules/custom/va_gov_form_builder/src/Service/FieldValidator/UniquenessValidator.php deleted file mode 100644 index 70cc7caa7c..0000000000 --- a/docroot/modules/custom/va_gov_form_builder/src/Service/FieldValidator/UniquenessValidator.php +++ /dev/null @@ -1,64 +0,0 @@ -entityTypeManager = $entityTypeManager; - } - - /** - * The validation function. - * - * @param string $nodeType - * The type of the node (machine name) to check. - * @param string $fieldName - * The field name to validate. - * @param mixed $fieldValue - * The value of the field to check for uniqueness. - * @param int|string|null $nid - * (optional) The node ID to exclude from the validation, typically used - * when validating during a node edit. Defaults to NULL. - * - * @return bool - * TRUE if the field is unique, FALSE otherwise. - */ - public function validate($nodeType, $fieldName, $fieldValue, $nid = NULL) { - $query = $this->entityTypeManager->getStorage('node')->getQuery(); - - // accessCheck FALSE to ensure all nodes of type `$nodeType` are checked, - // even if editor does not have access. - $query->accessCheck(FALSE) - ->condition('type', $nodeType) - ->condition($fieldName, $fieldValue); - - // If there's a current node in question, - // exclude the current node from the query. - if ($nid) { - $query->condition('nid', $nid, '!='); - } - - $results = $query->execute(); - - // If the query returns no results, the field value is unique. - return empty($results); - } - -} diff --git a/tests/phpunit/va_gov_form_builder/functional/Service/FieldValidator/UniquenessValidatorTest.php b/tests/phpunit/va_gov_form_builder/functional/Service/FieldValidator/UniquenessValidatorTest.php deleted file mode 100644 index ed91993c77..0000000000 --- a/tests/phpunit/va_gov_form_builder/functional/Service/FieldValidator/UniquenessValidatorTest.php +++ /dev/null @@ -1,103 +0,0 @@ -validatorService = \Drupal::service('va_gov_form_builder.field_validator.uniqueness'); - } - - /** - * Tests that the service is available. - * - * @covers ::__construct - */ - public function testConstruct() { - $this->assertInstanceOf(UniquenessValidator::class, $this->validatorService); - } - - /** - * Tests the validate() method when $nid of current node IS NOT passed. - * - * @covers ::validate - */ - public function testValidateWithoutCurrentNode() { - // A Digital Form node with our unique VA form number - // should not already exist. - $isUnique = $this->validatorService->validate( - 'digital_form', - 'field_va_form_number', - $this->UNIQUE_VA_FORM_NUMBER, - ); - $this->assertTrue($isUnique); - } - - /** - * Tests the validate() method when $nid of current node IS passed. - * - * @covers ::validate - */ - public function testValidateWithCurrentNode() { - // Create a node with our unique VA form number. - $createdNode = $this->createNode([ - 'type' => 'digital_form', - 'field_va_form_number' => $this->UNIQUE_VA_FORM_NUMBER, - ]); - - // Should be unique if we pass the $nid of the newly created node - // (newly created node is excluded from the check). - $isUnique = $this->validatorService->validate( - 'digital_form', - 'field_va_form_number', - $this->UNIQUE_VA_FORM_NUMBER, - $createdNode->nid->value, - ); - $this->assertTrue($isUnique); - - // Should NOT be unique if we pass the $nid of a different node - // (newly created node is included in the check). - $isUnique = $this->validator_service->validate( - 'digital_form', - 'field_va_form_number', - $this->UNIQUE_VA_FORM_NUMBER, - // $nid = '1' to represent some other node. - '1', - ); - $this->assertFalse($isUnique); - } - -} diff --git a/tests/phpunit/va_gov_form_builder/unit/Service/FieldValidator/UniquenessValidatorTest.php b/tests/phpunit/va_gov_form_builder/unit/Service/FieldValidator/UniquenessValidatorTest.php deleted file mode 100644 index 167756f23e..0000000000 --- a/tests/phpunit/va_gov_form_builder/unit/Service/FieldValidator/UniquenessValidatorTest.php +++ /dev/null @@ -1,113 +0,0 @@ -entityTypeManager = $this->createMock(EntityTypeManagerInterface::class); - - $this->query = $this->createMock(QueryInterface::class); - $this->query->expects($this->once()) - ->method('accessCheck') - ->willReturn($this->query); - $this->query->expects($this->exactly(2)) - ->method('condition') - ->willReturn($this->query); - - $this->validator = new UniquenessValidator($this->entityTypeManager); - } - - /** - * Tests the validate() method when no matching node exists. - * - * @covers ::validate - */ - public function testFieldIsUnique() { - $nodeStorage = $this->createMock(EntityStorageInterface::class); - - // Empty result - no matching node exists. - $this->query->expects($this->once()) - ->method('execute') - ->willReturn([]); - - $nodeStorage->expects($this->once()) - ->method('getQuery') - ->willReturn($this->query); - - $this->entityTypeManager->expects($this->once()) - ->method('getStorage') - ->with('node') - ->willReturn($nodeStorage); - - $result = $this->validator->validate('some_content_type', 'some_field', 'some_value'); - $this->assertTrue($result); - } - - /** - * Tests the validate() method when a matching node exists. - * - * @covers ::validate - */ - public function testFieldIsNotUnique() { - $nodeStorage = $this->createMock(EntityStorageInterface::class); - - // Return something other than empty - matching node exists. - $this->query->expects($this->once()) - ->method('execute') - ->willReturn([1]); - - $nodeStorage->expects($this->once()) - ->method('getQuery') - ->willReturn($this->query); - - $this->entityTypeManager->expects($this->once()) - ->method('getStorage') - ->with('node') - ->willReturn($nodeStorage); - - $result = $this->validator->validate('some_content_type', 'some_field', 'some_value'); - $this->assertFalse($result); - } - -}