In my current project I create relations programmatically. I was able to create relations successfully - relation_save returned a correct rid - without these relations showing up in the relation browser. By looking at the database I figured out relations where created with a wrong type.

To prevent invalid relations to be created, relation_create should check the specified relation type.

Comments

mikran’s picture

Component: Code » API
Category: feature » bug

You can't create nodes of types that don't exist so imo this goes as a bug and should apply to relation_save() as well.

mikran’s picture

Status: Active » Needs review
StatusFileSize
new1.15 KB

Attached patch adds type validation to relation_save() plus relation_type_load() $types param is now marked as optional.

mikran’s picture

StatusFileSize
new1.73 KB
mikran’s picture

Status: Needs review » Needs work

The last submitted patch, type_validation_with_tests-1345648-3.patch, failed testing.

mikran’s picture

StatusFileSize
new1.73 KB

reroll

mikran’s picture

Status: Needs work » Needs review
StatusFileSize
new1.73 KB

reroll

mikran’s picture

Status: Needs review » Needs work

relation_endpoint_field_validate() already does this:

  // Check that relation_type exists.
  if (!$relation_type) {
    $errors[$field['field_name']][$langcode][$delta][] = array(
      'error' => 'relation_nonexistent_type',
      'message' => t("The !relation_type relation type does not exist.", array('!relation_type' => $entity->relation_type)),
    );
  }

So instead of previous patch got to figure out why the code above is not working. For now it looks to be same bug as #1597770: Required fields are not attached on relation_save

naught101’s picture

StatusFileSize
new3.45 KB

Attached patch adds four tests, that try to create invalid relations: a relation with a non-existent relation type, a relation with invalid endpoints, a relation with a required field missing, and a relation with a field with invalid data. (The later two are for #1597770: Required fields are not attached on relation_save).

My understanding is that these should all fail at present, although if the field validation is working, then the latter two will only fail because the expected message is wrong. Let's see how it goes.

naught101’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 1345648-relation_validation_tests-9.patch, failed testing.

naught101’s picture

Status: Needs work » Needs review
StatusFileSize
new3.46 KB

Again!

Status: Needs review » Needs work

The last submitted patch, 1345648-relation_validation_tests-12.patch, failed testing.

naught101’s picture

Status: Needs work » Needs review
StatusFileSize
new3.46 KB

MOAR

Status: Needs review » Needs work

The last submitted patch, 1345648-relation_validation_tests-14.patch, failed testing.

naught101’s picture

Status: Needs work » Needs review
StatusFileSize
new3.55 KB

Ok, this should do it. Fails properly if it succeeds!

Status: Needs review » Needs work

The last submitted patch, 1345648-relation_validation_tests-16.patch, failed testing.

naught101’s picture

Status: Needs work » Needs review
StatusFileSize
new4.21 KB

Ok, notifiees, I'm gonna stop bugging you after this one - I figured out what I was doing wrong with my local tests, and this one is correct.

First 3 tests are failing because the relations are created, the fourth one is failing because the relation is being created, but there's a an SQLSTATE error due to the incorrect field data. So validation is failing in all cases.

Commited to the relation_validation_tests branch, incase anyone wants to work on it while I'm asleep. Good night!

Status: Needs review » Needs work

The last submitted patch, 1345648-relation_validation_tests-18.patch, failed testing.

naught101’s picture

naught101’s picture

There's potentially reasons why it might make sense to create an invalid relation, so relation_create should probably either throw a warning if the relation is invalid, or happily create invalid relations, while relation_save should throw a error.

dpi’s picture

Relation_create shouldnt be so strict on any passed default values, except for relation_type, which I think should throw an exception.

On save, the type can be rechecked to ensure it exists. Or throw invalid type exception.

If the relation does not have the correct number of endpoints, or incorrect type endpoints, then an integrity exception should be thrown.

naught101’s picture

I think create probably shouldn't do any checking. I can imagine a UI where the endpoints are selected first, and then on the next page, all possible matching relation types are given as options. Sounds unlikely, but it may have some kind of use..

mikran’s picture

Title: relation_create should check $type parameter » relation_validate does not properly validate $type parameter

Issue title was bad. There is no reason to validate anything in relation_create, but broken relations shouldn't get stored to database.

dpi’s picture

Right... relation_save

naught101’s picture

Title: relation_validate does not properly validate $type parameter » relation_save does not properly validate $type parameter

There is no relation_validate() :)

mikran’s picture

Issue summary: View changes

#2160525: "Field validation failed." - relations are not saved closed as duplicate of this. But now we have mixed arguments

should relation_save() validate more stuff or less? At the very least we need to add tests that verify what's the intended behaviour.